Browse Source

20210525

logistics
zy_Java 4 years ago
parent
commit
acfa15e044
  1. 1
      logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java
  2. 2
      logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java
  3. 99
      logistics/src/main/java/com/ccsens/logistics/service/HttpService.java

1
logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java

@ -73,6 +73,7 @@ public class NettyClient {
.newSingleThreadScheduledExecutor(); .newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间 // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, 2, 5, TimeUnit.SECONDS); service.scheduleAtFixedRate(runnable, 2, 5, TimeUnit.SECONDS);
// 创建并执行在给定延迟后启用的 ScheduledFuture。 // 创建并执行在给定延迟后启用的 ScheduledFuture。
// 参数: // 参数:
// callable - 要执行的功能 // callable - 要执行的功能

2
logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java

@ -24,7 +24,7 @@ public class SimpleClientHandler extends ChannelInboundHandlerAdapter{
public void channelRead(ChannelHandlerContext ctx, Object msg) { public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBuf) { if (msg instanceof ByteBuf) {
String value = ((ByteBuf) msg).toString(Charset.defaultCharset()); String value = ((ByteBuf) msg).toString(Charset.defaultCharset());
System.out.println("服务器端返回的数据:" + value); System.out.println("服务器端返回的数据:" + value.substring(value.length()-3,value.length()));
if(StrUtil.isNotEmpty(value) && value.lastIndexOf("not online") == -1) { if(StrUtil.isNotEmpty(value) && value.lastIndexOf("not online") == -1) {
thermalImageryService.disposeMessage(value); thermalImageryService.disposeMessage(value);
} }

99
logistics/src/main/java/com/ccsens/logistics/service/HttpService.java

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

Loading…
Cancel
Save