|
@@ -0,0 +1,103 @@
|
|
|
+package com.huimv.subscribe;
|
|
|
+
|
|
|
+import com.huimv.service.IEnvElectricityService;
|
|
|
+import com.huimv.untils.MySQLDemo;
|
|
|
+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 PushCallback2 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);
|
|
|
+//// IEnvElectricityService electricityService = SpringUtil.getBean(IEnvElectricityService.class);
|
|
|
+// MySQLDemo demo = new MySQLDemo();
|
|
|
+// int eleOrigValue = Integer.parseInt(substring, 16);
|
|
|
+// Float f = Float.intBitsToFloat(eleOrigValue);
|
|
|
+// BigDecimal bigDecimal = new BigDecimal(f);
|
|
|
+// java.util.Date date = new java.util.Date();
|
|
|
+// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+// String format = simpleDateFormat.format(date);
|
|
|
+// demo.add(format, bigDecimal);
|
|
|
+// } catch (Exception e) {
|
|
|
+// System.out.println("sssssssss"+ e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (s.startsWith("3303")) {
|
|
|
+// System.out.println("开始处理水表数据!");
|
|
|
+// String substring = s.substring(6, 14);
|
|
|
+// int eleOrigValue = Integer.parseInt(substring, 16);
|
|
|
+// BigDecimal value = BigDecimal.valueOf(eleOrigValue * 0.01);
|
|
|
+// MySQLDemo demo = new MySQLDemo();
|
|
|
+// java.util.Date date = new java.util.Date();
|
|
|
+// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+// String format = simpleDateFormat.format(date);
|
|
|
+// demo.add2(format, value);
|
|
|
+// }
|
|
|
+ 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
|
|
|
+// }
|
|
|
+}
|
|
|
+
|