|
@@ -1,20 +1,32 @@
|
|
package com.huimv.subscribe;
|
|
package com.huimv.subscribe;
|
|
|
|
|
|
-import com.huimv.entity.People;
|
|
|
|
-import com.huimv.service.IPeopleService;
|
|
|
|
-import com.huimv.service.impl.PeopleServiceImpl;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.huimv.entity.EnvElectricity;
|
|
|
|
+import com.huimv.entity.EnvWater;
|
|
|
|
+import com.huimv.service.IEnvElectricityService;
|
|
|
|
+import com.huimv.service.IEnvWaterService;
|
|
import com.huimv.untils.SpringUtil;
|
|
import com.huimv.untils.SpringUtil;
|
|
-import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
|
+import org.eclipse.paho.client.mqttv3.*;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.sql.SQLOutput;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.time.ZonedDateTime;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
@Component
|
|
@Component
|
|
//接收消息回调
|
|
//接收消息回调
|
|
class PushCallback implements MqttCallback {
|
|
class PushCallback implements MqttCallback {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private IEnvElectricityService envElectricityService;
|
|
|
|
|
|
|
|
+ private MqttClient client;
|
|
@Override
|
|
@Override
|
|
public void connectionLost(Throwable cause) {
|
|
public void connectionLost(Throwable cause) {
|
|
|
|
|
|
@@ -32,19 +44,73 @@ class PushCallback implements MqttCallback {
|
|
// subscribe后得到的消息会执行到这里面
|
|
// subscribe后得到的消息会执行到这里面
|
|
System.out.println("接收消息主题 : " + topic);
|
|
System.out.println("接收消息主题 : " + topic);
|
|
System.out.println("接收消息Qos : " + message.getQos());
|
|
System.out.println("接收消息Qos : " + message.getQos());
|
|
- System.out.println("接收消息内容 : " + new String(message.getPayload()));
|
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ System.out.println("接收消息的时间 :" + sdf.format(new Date()));
|
|
|
|
+// System.out.println("siejgfskjgfsldjfhg"+message);
|
|
|
|
+// System.out.println(message.getPayload());
|
|
|
|
+ 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("开始处理当前数据...");
|
|
|
|
+ if (s.startsWith("1c04")){
|
|
|
|
+ try {
|
|
|
|
+ System.out.println("开始处理电表数据!");
|
|
|
|
+ String substring = s.substring(6, 14);
|
|
|
|
+ System.out.println("截取字符串:"+substring);
|
|
|
|
+// IEnvElectricityService electricityService = SpringUtil.getBean(IEnvElectricityService.class);
|
|
|
|
+ System.out.println("-------->1");
|
|
|
|
+
|
|
|
|
+ EnvElectricity envElectricity = new EnvElectricity();
|
|
|
|
+ int eleOrigValue = Integer.parseInt(substring, 16);
|
|
|
|
+ System.out.println("整形:"+eleOrigValue);
|
|
|
|
+ Float f = Float.intBitsToFloat(eleOrigValue);
|
|
|
|
+ System.out.println("浮点:"+f);
|
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(f);
|
|
|
|
+ envElectricity.setOriginalValue(bigDecimal);
|
|
|
|
+ envElectricity.setUpdateTime(new Date());
|
|
|
|
+ envElectricity.setFarmId(27);
|
|
|
|
+ envElectricityService.save(envElectricity);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.out.println("sssssssss"+ e);
|
|
|
|
+ }
|
|
|
|
|
|
- String messages = new String(message.getPayload());
|
|
|
|
- String[] split = messages.split(",");
|
|
|
|
-// PeopleServiceImpl peopleService = SpringUtil.getBean(PeopleServiceImpl.class);//创建impl实例
|
|
|
|
-// People people = new People();
|
|
|
|
-// people.setName(split[0]);
|
|
|
|
-// people.setAge(Integer.parseInt(split[1]));
|
|
|
|
-// people.setSex(split[2]);
|
|
|
|
-// peopleService.save(people);
|
|
|
|
-// peopleService.addPeople(split);
|
|
|
|
|
|
+ }
|
|
|
|
+ if (s.startsWith("3303")) {
|
|
|
|
+ System.out.println("开始处理水表数据!");
|
|
|
|
+ String substring = s.substring(6, 14);
|
|
|
|
+ IEnvWaterService waterService = SpringUtil.getBean(IEnvWaterService.class);
|
|
|
|
+ EnvWater water = new EnvWater();
|
|
|
|
+ int eleOrigValue = Integer.parseInt(substring, 16);
|
|
|
|
+ BigDecimal value = BigDecimal.valueOf(eleOrigValue * 0.01);
|
|
|
|
+ water.setOriginalValue(value);
|
|
|
|
+ water.setUpdateTime(new Date());
|
|
|
|
+ water.setFarmId(27);
|
|
|
|
+ waterService.save(water);
|
|
|
|
+ }
|
|
System.out.println("数据处理完成!");
|
|
System.out.println("数据处理完成!");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String s = "1c0404405c0a3d2426";
|
|
|
|
+ String substring = s.substring(6, 14);
|
|
|
|
+ System.out.println(substring);
|
|
|
|
+
|
|
|
|
+// int eleOrigValue = Integer.parseInt(substring, 16);
|
|
|
|
+// BigDecimal value = BigDecimal.valueOf(eleOrigValue * 0.01);
|
|
|
|
+// System.out.println(value);
|
|
|
|
+//
|
|
|
|
+ int eleOrigValue = Integer.parseInt(substring, 16);
|
|
|
|
+ Float f = Float.intBitsToFloat(eleOrigValue);
|
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(f);
|
|
|
|
+ System.out.println(bigDecimal);
|
|
|
|
+// Float f = Float.intBitsToFloat(1079773757);
|
|
|
|
+// System.out.println(f);
|
|
|
|
+//// BigDecimal bigDecimal = new BigDecimal(f);
|
|
|
|
+// // 1079773757
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|