|
@@ -0,0 +1,95 @@
|
|
|
|
+package com.huimv.testpublic;
|
|
|
|
+
|
|
|
|
+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 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.Date;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+//接收消息回调
|
|
|
|
+class PushCallbackYv implements MqttCallback {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IEnvElectricityService envElectricityService;
|
|
|
|
+
|
|
|
|
+ private MqttClient client;
|
|
|
|
+ @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");
|
|
|
|
+ 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("开始处理当前数据...");
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ 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("数据处理完成!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|