|
@@ -5,13 +5,14 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yunfei.adapter.forword.MqttPublisher;
|
|
import com.yunfei.adapter.forword.MqttPublisher;
|
|
|
import com.yunfei.adapter.http.uniagro.UniagroHttp;
|
|
import com.yunfei.adapter.http.uniagro.UniagroHttp;
|
|
|
-import jakarta.annotation.Resource;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
@@ -22,20 +23,43 @@ public class ScheduledTasks {
|
|
|
private MqttPublisher mqttPublisher;
|
|
private MqttPublisher mqttPublisher;
|
|
|
static List<Pair<String, String>> deviceIds = new ArrayList<>();
|
|
static List<Pair<String, String>> deviceIds = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
|
|
+
|
|
|
static {
|
|
static {
|
|
|
deviceIds.add(new Pair<>("9001", "ZZ"));
|
|
deviceIds.add(new Pair<>("9001", "ZZ"));
|
|
|
deviceIds.add(new Pair<>("9100", "PY"));
|
|
deviceIds.add(new Pair<>("9100", "PY"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ int i = 0;
|
|
|
|
|
+
|
|
|
@Scheduled(fixedRate = 20000)
|
|
@Scheduled(fixedRate = 20000)
|
|
|
public void performTask() {
|
|
public void performTask() {
|
|
|
|
|
+ i++;
|
|
|
for (Pair<String, String> device : deviceIds) {
|
|
for (Pair<String, String> device : deviceIds) {
|
|
|
- String msg = uniagroHttp.readReport(device.getKey());
|
|
|
|
|
|
|
+ String msg = null;
|
|
|
try {
|
|
try {
|
|
|
- mqttPublisher.publish("/" + device.getValue() + "/" + device.getKey() + "/properties/report", this.decode(msg));
|
|
|
|
|
- } catch (MqttException e) {
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
|
|
+ msg = uniagroHttp.readReport(device.getKey());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (msg != null) {
|
|
|
|
|
+ String decode = this.decode(msg);
|
|
|
|
|
+ if (decode == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ mqttPublisher.publish("/" + device.getValue() + "/" + device.getKey() + "/properties/report", decode);
|
|
|
|
|
+ map.put(device.getKey(), decode);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (map.get(device.getKey()) != null) {
|
|
|
|
|
+ mqttPublisher.publish("/" + device.getValue() + "/" + device.getKey() + "/properties/report", map.get(device.getKey()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (MqttException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -44,11 +68,11 @@ public class ScheduledTasks {
|
|
|
JSONObject obj = JSON.parseObject(msg);
|
|
JSONObject obj = JSON.parseObject(msg);
|
|
|
if (obj.containsKey("data")) {
|
|
if (obj.containsKey("data")) {
|
|
|
JSONObject data = obj.getJSONObject("data");
|
|
JSONObject data = obj.getJSONObject("data");
|
|
|
- if(data.containsKey("angle")){
|
|
|
|
|
- this.putInputs(result,data.getJSONObject("angle"));
|
|
|
|
|
|
|
+ if (data.containsKey("angle")) {
|
|
|
|
|
+ this.putInputs(result, data.getJSONObject("angle"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(data.containsKey("connected")){
|
|
|
|
|
|
|
+ if (data.containsKey("connected")) {
|
|
|
result.put("connected", data.get("connected"));
|
|
result.put("connected", data.get("connected"));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -59,17 +83,26 @@ public class ScheduledTasks {
|
|
|
if (obj.containsKey("deviceType")) {
|
|
if (obj.containsKey("deviceType")) {
|
|
|
result.put("deviceType", obj.get("deviceType"));
|
|
result.put("deviceType", obj.get("deviceType"));
|
|
|
}
|
|
}
|
|
|
|
|
+ if (!obj.containsKey("data")) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
if (obj.getJSONObject("data").containsKey("detail")) {
|
|
if (obj.getJSONObject("data").containsKey("detail")) {
|
|
|
- this.putInputs(result,obj.getJSONObject("data").getJSONObject("detail"));
|
|
|
|
|
|
|
+ this.putInputs(result, obj.getJSONObject("data").getJSONObject("detail"));
|
|
|
}
|
|
}
|
|
|
JSONObject property = new JSONObject();
|
|
JSONObject property = new JSONObject();
|
|
|
- property.put("properties",result);
|
|
|
|
|
|
|
+ property.put("properties", result);
|
|
|
|
|
+ property.put("reportTime", new Date());
|
|
|
return property.toJSONString();
|
|
return property.toJSONString();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void putInputs(JSONObject result,JSONObject data){
|
|
|
|
|
|
|
+ public void putInputs(JSONObject result, JSONObject data) {
|
|
|
for (String key : data.keySet()) {
|
|
for (String key : data.keySet()) {
|
|
|
- result.put(key, data.get(key));
|
|
|
|
|
|
|
+ Object integer = data.get(key);
|
|
|
|
|
+ if (integer == null) {
|
|
|
|
|
+ result.put(key, 0);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result.put(key, integer);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|