|
|
@ -1,5 +1,6 @@ |
|
|
|
package com.ccsens.logistics.service; |
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.lang.Snowflake; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
@ -35,8 +36,8 @@ import java.util.List; |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) |
|
|
|
public class HttpService implements IHttpService{ |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) |
|
|
|
public class HttpService implements IHttpService { |
|
|
|
|
|
|
|
@Resource |
|
|
|
private LogisticsEnvironmentRecordDao environmentRecordDao; |
|
|
@ -68,41 +69,42 @@ public class HttpService implements IHttpService{ |
|
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(environmentOfData); |
|
|
|
JSONArray dataArray = jsonObject.getJSONArray("data"); |
|
|
|
if (CollectionUtil.isNotEmpty(dataArray)) { |
|
|
|
for (int i = 0; i < dataArray.size(); i++) { |
|
|
|
LogisticsEnvironmentRecord environmentRecord = new LogisticsEnvironmentRecord(); |
|
|
|
JSONObject oneObject = dataArray.getJSONObject(i); |
|
|
|
//获取设备运行状态 0未运行,1离线,2在线
|
|
|
|
Integer deviceStatus = oneObject.getInteger("deviceStatus"); |
|
|
|
//如果设备为在线状态 将数据存入到数据库
|
|
|
|
if (2 == deviceStatus){ |
|
|
|
if (2 == deviceStatus) { |
|
|
|
environmentRecord.setEquipmentNum(oneObject.getString("deviceAddr")); |
|
|
|
environmentRecord.setRecordTime(System.currentTimeMillis()); |
|
|
|
|
|
|
|
JSONArray realTimeData = oneObject.getJSONArray("realTimeData"); |
|
|
|
for (int j = 0; j < realTimeData.size(); j++) { |
|
|
|
JSONObject realData = realTimeData.getJSONObject(j); |
|
|
|
switch (realData.getString("dataName")){ |
|
|
|
case Constant.TEMP : |
|
|
|
switch (realData.getString("dataName")) { |
|
|
|
case Constant.TEMP: |
|
|
|
environmentRecord.setId(snowflake.nextId()); |
|
|
|
environmentRecord.setNumType((byte)1); |
|
|
|
environmentRecord.setNumType((byte) 1); |
|
|
|
environmentRecord.setNumValue(new BigDecimal(realData.getString("dataValue"))); |
|
|
|
environmentRecord.setIsAlarm(realData.getBoolean("isAlarm") ? (byte) 1 : (byte) 0); |
|
|
|
break; |
|
|
|
case Constant.HUMIDITY: |
|
|
|
environmentRecord.setId(snowflake.nextId()); |
|
|
|
environmentRecord.setNumType((byte)0); |
|
|
|
environmentRecord.setNumType((byte) 0); |
|
|
|
environmentRecord.setNumValue(new BigDecimal(realData.getString("dataValue"))); |
|
|
|
environmentRecord.setIsAlarm(realData.getBoolean("isAlarm") ? (byte) 1 : (byte) 0); |
|
|
|
break; |
|
|
|
case Constant.WATER_OUT: |
|
|
|
environmentRecord.setId(snowflake.nextId()); |
|
|
|
environmentRecord.setNumType((byte)2); |
|
|
|
if(Constant.HAVE_WATER.equals(realData.getString("dataValue"))){ |
|
|
|
environmentRecord.setNumType((byte) 2); |
|
|
|
if (Constant.HAVE_WATER.equals(realData.getString("dataValue"))) { |
|
|
|
environmentRecord.setNumValue(new BigDecimal(100)); |
|
|
|
environmentRecord.setIsAlarm((byte)1); |
|
|
|
}else{ |
|
|
|
environmentRecord.setIsAlarm((byte) 1); |
|
|
|
} else { |
|
|
|
environmentRecord.setNumValue(new BigDecimal(0)); |
|
|
|
environmentRecord.setIsAlarm((byte)0); |
|
|
|
environmentRecord.setIsAlarm((byte) 0); |
|
|
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
@ -111,10 +113,9 @@ public class HttpService implements IHttpService{ |
|
|
|
environmentRecordDao.insertSelective(environmentRecord); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
@ -122,10 +123,11 @@ public class HttpService implements IHttpService{ |
|
|
|
|
|
|
|
/** |
|
|
|
* 温湿度登录接口 获取设备的userId 调用其他接口 |
|
|
|
* |
|
|
|
* @param login 账号密码 |
|
|
|
* @return userId |
|
|
|
*/ |
|
|
|
public String getUserId(HttpDto.Login login){ |
|
|
|
public String getUserId(HttpDto.Login login) { |
|
|
|
String requestUrl = Constant.LOGIN_URL; |
|
|
|
String s = RestTemplateUtil.postBody(requestUrl, login); |
|
|
|
JsonResponse<JSONObject> a = JSONObject.parseObject(s, JsonResponse.class); |
|
|
@ -135,10 +137,10 @@ public class HttpService implements IHttpService{ |
|
|
|
|
|
|
|
|
|
|
|
//get请求
|
|
|
|
public static String getBody(String url,String userId){ |
|
|
|
public static String getBody(String url, String userId) { |
|
|
|
try { |
|
|
|
HttpGet get = new HttpGet(url); |
|
|
|
get.setHeader("userId",userId); |
|
|
|
get.setHeader("userId", userId); |
|
|
|
HttpClient httpClient = HttpClients.createDefault(); |
|
|
|
HttpResponse response = httpClient.execute(get); |
|
|
|
return getResult(response); |
|
|
@ -166,5 +168,4 @@ public class HttpService implements IHttpService{ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|