|
@@ -0,0 +1,70 @@
|
|
|
|
|
+package com.yunfei.adapter.http.uniagro.runner;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.lang.Pair;
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.yunfei.adapter.forword.MqttPublisher;
|
|
|
|
|
+import com.yunfei.adapter.http.uniagro.UniagroHttp;
|
|
|
|
|
+import jakarta.annotation.Resource;
|
|
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@Component
|
|
|
|
|
+public class ScheduledTasks {
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UniagroHttp uniagroHttp;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MqttPublisher mqttPublisher;
|
|
|
|
|
+ static List<Pair<String, String>> deviceIds = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ static {
|
|
|
|
|
+ deviceIds.add(new Pair<>("9001", "ZZ"));
|
|
|
|
|
+ deviceIds.add(new Pair<>("9100", "PY"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(fixedRate = 20000)
|
|
|
|
|
+ public void performTask() {
|
|
|
|
|
+ for (Pair<String, String> device : deviceIds) {
|
|
|
|
|
+ String msg = uniagroHttp.readReport(device.getKey());
|
|
|
|
|
+ try {
|
|
|
|
|
+ mqttPublisher.publish("/" + device.getValue() + "/" + device.getKey() + "/properties/report", this.decode(msg));
|
|
|
|
|
+ } catch (MqttException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String decode(String msg) {
|
|
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
|
|
+ JSONObject obj = JSON.parseObject(msg);
|
|
|
|
|
+ if (obj.containsKey("data")) {
|
|
|
|
|
+ JSONObject data = obj.getJSONObject("data");
|
|
|
|
|
+ if(data.containsKey("angle")){
|
|
|
|
|
+ this.putInputs(result,data.getJSONObject("angle"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (obj.containsKey("timeStamp")) {
|
|
|
|
|
+ result.put("timeStamp", obj.get("timeStamp"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (obj.containsKey("deviceType")) {
|
|
|
|
|
+ result.put("deviceType", obj.get("deviceType"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (obj.getJSONObject("data").containsKey("detail")) {
|
|
|
|
|
+ this.putInputs(result,obj.getJSONObject("data").getJSONObject("detail"));
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject property = new JSONObject();
|
|
|
|
|
+ property.put("property",result);
|
|
|
|
|
+ return property.toJSONString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void putInputs(JSONObject result,JSONObject data){
|
|
|
|
|
+ for (String key : data.keySet()) {
|
|
|
|
|
+ result.put(key, data.get(key));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|