|
@@ -1,51 +1,210 @@
|
|
|
-package com.huimv.guowei.admin.mqtt.back;
|
|
|
-
|
|
|
-import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Date;
|
|
|
-
|
|
|
-@Component
|
|
|
-public
|
|
|
-//接收消息回调
|
|
|
-class PushCallback implements MqttCallback {
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public void connectionLost(Throwable cause) {
|
|
|
-
|
|
|
- // 连接丢失后,一般在这里面进行重连
|
|
|
- System.out.println("连接断开,可以做重连");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void deliveryComplete(IMqttDeliveryToken token) {
|
|
|
- System.out.println("deliveryComplete---------" + token.isComplete());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void messageArrived(String topic, MqttMessage message) throws Exception {
|
|
|
- // subscribe后得到的消息会执行到这里面
|
|
|
- System.out.println("接收消息主题 : " + topic);
|
|
|
- System.out.println("接收消息Qos : " + message.getQos());
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- Date date = new Date();
|
|
|
- System.out.println("接收消息的时间:" + sdf.format(date));
|
|
|
- byte[] payload = message.getPayload();
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- for (byte b : payload) {
|
|
|
- sb.append(String.format("%02x", b));
|
|
|
- }
|
|
|
- String s = sb.toString();
|
|
|
- System.out.println("接收消息内容 : " + s);
|
|
|
- System.out.println("开始处理当前数据...");
|
|
|
- System.out.println("数据处理完成!");
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
+//package com.huimv.guowei.admin.mqtt.back;
|
|
|
+//
|
|
|
+//import cn.hutool.core.util.ObjectUtil;
|
|
|
+//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+//import com.huimv.guowei.admin.entity.*;
|
|
|
+//import com.huimv.guowei.admin.service.*;
|
|
|
+//import com.huimv.guowei.admin.utils.SpringUtil;
|
|
|
+//import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
+//import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
+//import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
+//import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//
|
|
|
+//import java.math.BigDecimal;
|
|
|
+//import java.text.SimpleDateFormat;
|
|
|
+//import java.util.ArrayList;
|
|
|
+//import java.util.Date;
|
|
|
+//import java.util.List;
|
|
|
+//
|
|
|
+//@Component
|
|
|
+//public
|
|
|
+////接收消息回调
|
|
|
+//class PushCallback implements MqttCallback {
|
|
|
+//
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void connectionLost(Throwable cause) {
|
|
|
+//
|
|
|
+// // 连接丢失后,一般在这里面进行重连
|
|
|
+// System.out.println("连接断开,可以做重连");
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void deliveryComplete(IMqttDeliveryToken token) {
|
|
|
+// System.out.println("deliveryComplete---------" + token.isComplete());
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void messageArrived(String topic, MqttMessage message) throws Exception {
|
|
|
+// // subscribe后得到的消息会执行到这里面
|
|
|
+// System.out.println("接收消息主题 : " + topic);
|
|
|
+// System.out.println("接收消息Qos : " + message.getQos());
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+// Date date = new Date();
|
|
|
+// System.out.println("接收消息的时间:" + sdf.format(date));
|
|
|
+// byte[] payload = message.getPayload();
|
|
|
+// StringBuilder sb = new StringBuilder();
|
|
|
+// for (byte b : payload) {
|
|
|
+// sb.append(String.format("%02x", b));
|
|
|
+// }
|
|
|
+// String s = sb.toString();
|
|
|
+// System.out.println("接收消息内容 : " + s);
|
|
|
+// System.out.println("开始处理当前数据...");
|
|
|
+//
|
|
|
+// List list = getListFromContent(s, 2);
|
|
|
+// String id = list.get(5).toString().concat(list.get(4).toString()).concat(list.get(3).toString()).concat(list.get(2).toString());
|
|
|
+// System.out.println(id);
|
|
|
+// int id1 = Integer.parseInt(id, 16);
|
|
|
+// System.out.println("设备id:" + id1);//id
|
|
|
+// String s1 = list.get(8).toString().concat(list.get(7).toString());//电量
|
|
|
+// int anInt = Integer.parseInt(s1, 16);
|
|
|
+// Float f = (float) anInt / 10;
|
|
|
+// System.out.println("电池电量:" + f);
|
|
|
+//
|
|
|
+// IRawDataService rawDataService = SpringUtil.getBean(IRawDataService.class);
|
|
|
+// QueryWrapper<RawData> queryWrapper = new QueryWrapper<>();
|
|
|
+// queryWrapper.eq("device_code", id1).orderByDesc("create_time").last(" limit 1");
|
|
|
+// RawData one = rawDataService.getOne(queryWrapper);//拿到上一次传输时的最后一条数据
|
|
|
+// System.out.println("第一个查询没问题!!");
|
|
|
+//
|
|
|
+// IEnvDeviceService envDeviceService = SpringUtil.getBean(IEnvDeviceService.class);
|
|
|
+// QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
|
|
|
+// queryWrapper1.eq("device_code", id1);
|
|
|
+// EnvDevice device = envDeviceService.getOne(queryWrapper1);
|
|
|
+// System.out.println("第二个查询没问题!!");
|
|
|
+//
|
|
|
+// IBaseDuckInfoService infoService = SpringUtil.getBean(IBaseDuckInfoService.class);
|
|
|
+// QueryWrapper<BaseDuckInfo> queryWrapper2 = new QueryWrapper<>();
|
|
|
+// queryWrapper2.eq("unit_id", device.getUnitId());
|
|
|
+// BaseDuckInfo info = infoService.getOne(queryWrapper2);
|
|
|
+// System.out.println("第三个查询没问题!!");
|
|
|
+//
|
|
|
+// IEnvRegularCallFeedingService feedingService = SpringUtil.getBean(IEnvRegularCallFeedingService.class);
|
|
|
+// IEnvRegularCallEggService eggService = SpringUtil.getBean(IEnvRegularCallEggService.class);
|
|
|
+// System.out.println("设置两个service没问题!!");
|
|
|
+//
|
|
|
+// BigDecimal a1;
|
|
|
+// if (ObjectUtil.isEmpty(one)) {
|
|
|
+// a1 = BigDecimal.valueOf(0);
|
|
|
+// } else {
|
|
|
+// String oneData = one.getData();
|
|
|
+// a1 = new BigDecimal(oneData);
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// for (int i = 10; i < list.size() - 1; i = i + 2) {
|
|
|
+// String data = list.get(i + 1).toString().concat(list.get(i).toString());
|
|
|
+// int parseInt = Integer.parseInt(data, 16);
|
|
|
+// Float d = (float) parseInt / 100;
|
|
|
+// float weight = ((float) Math.round(d * 10)) / 10;//下一次的数据
|
|
|
+// System.out.println("重量:" + weight);
|
|
|
+//
|
|
|
+// BigDecimal a2 = new BigDecimal(Float.toString(weight));
|
|
|
+// System.out.println("---重量转换是正常的:"+a2);
|
|
|
+// System.out.println("---上一次的重量:"+a1);
|
|
|
+//
|
|
|
+// if (a2.compareTo(a1) !=1) { //a1>=a2
|
|
|
+// System.out.println("进入了保存采食环节!!");
|
|
|
+// BigDecimal subtract = a1.subtract(a2);
|
|
|
+// System.out.println("1");
|
|
|
+// EnvRegularCallFeeding feeding = new EnvRegularCallFeeding();
|
|
|
+// feeding.setUnitName(device.getUnitName());
|
|
|
+// System.out.println("2");
|
|
|
+// feeding.setUnitId(device.getUnitId());
|
|
|
+// System.out.println("3");
|
|
|
+// feeding.setDuckNum(info.getDuckNum());
|
|
|
+// System.out.println("4");
|
|
|
+// feeding.setCallName(device.getDeviceName());
|
|
|
+// System.out.println("5");
|
|
|
+// feeding.setCallCode(device.getDeviceCode());
|
|
|
+// System.out.println("6");
|
|
|
+// feeding.setDuckWeight(subtract.doubleValue());
|
|
|
+// System.out.println("7");
|
|
|
+// feeding.setCallDate(new Date());
|
|
|
+// System.out.println("8");
|
|
|
+// feeding.setFarmId(device.getFarmId());
|
|
|
+// System.out.println("9");
|
|
|
+// feeding.setBattery(f.toString());
|
|
|
+// System.out.println("10");
|
|
|
+// feeding.setDuckId(info.getId());
|
|
|
+// System.out.println("值设置完成!!");
|
|
|
+// feedingService.save(feeding);
|
|
|
+// } else {
|
|
|
+// BigDecimal subtract = a2.subtract(a1);
|
|
|
+// BigDecimal a = new BigDecimal("50");
|
|
|
+// BigDecimal b = new BigDecimal("100");
|
|
|
+//
|
|
|
+// System.out.println("进入了彩蛋环节!!!");
|
|
|
+// if (subtract.compareTo(a) != -1 && subtract.compareTo(b) != 1) {
|
|
|
+// EnvRegularCallEgg egg = new EnvRegularCallEgg();
|
|
|
+// egg.setCallName(device.getDeviceName());
|
|
|
+// egg.setCallCode(device.getDeviceCode());
|
|
|
+// egg.setCallDate(new Date());
|
|
|
+// egg.setDuckNum(String.valueOf(id1));
|
|
|
+// egg.setDuckId(info.getId());
|
|
|
+// egg.setBattery(f.toString());
|
|
|
+// egg.setEggNum(1);
|
|
|
+// egg.setDuckWeight(subtract.doubleValue());
|
|
|
+// egg.setFarmId(device.getFarmId());
|
|
|
+// egg.setUnitName(device.getUnitName());
|
|
|
+// egg.setUnitId(device.getUnitId());
|
|
|
+// eggService.save(egg);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// RawData rawData = new RawData();
|
|
|
+// rawData.setDeviceCode(String.valueOf(id1));
|
|
|
+// rawData.setData(String.valueOf(weight));
|
|
|
+// rawData.setCreateTime(new Date());
|
|
|
+// rawDataService.save(rawData);
|
|
|
+// a1 = a2;
|
|
|
+//
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// System.out.println("-------保存出现了异常!!!" );
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// System.out.println("数据处理完成!");
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static List getListFromContent(String content, int count) {
|
|
|
+// List list = new ArrayList();
|
|
|
+// // 获取String的总长度
|
|
|
+// int contentLength = content.length();
|
|
|
+// if (contentLength < count) {
|
|
|
+// list.add(content);
|
|
|
+// } else {
|
|
|
+// int begin = 0;
|
|
|
+// // 获取需要切割多少段
|
|
|
+// int cutCount = contentLength / count;
|
|
|
+// int cutCounts = contentLength % count;
|
|
|
+// // 获取切割段的长度
|
|
|
+// if (cutCounts != 0) {
|
|
|
+// cutCount++;
|
|
|
+// }
|
|
|
+// for (int i = 1; i <= cutCount; i++) {
|
|
|
+// String temp;
|
|
|
+// // 不是最后一段
|
|
|
+// if (i != cutCount) {
|
|
|
+// temp = content.substring(begin, count * i);
|
|
|
+// } else {
|
|
|
+// temp = content.substring(begin, contentLength);
|
|
|
+// }
|
|
|
+// begin = count * i;
|
|
|
+// list.add(temp);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return list;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static void main(String[] args) {
|
|
|
+// BigDecimal a = BigDecimal.valueOf(0);
|
|
|
+// BigDecimal b = new BigDecimal(0.0);
|
|
|
+// BigDecimal subtract = b.subtract(a);
|
|
|
+// System.out.println(subtract);
|
|
|
+//
|
|
|
+// }
|
|
|
+//}
|
|
|
+//
|