|
@@ -0,0 +1,168 @@
|
|
|
+package com.fjhx.equipment;
|
|
|
+
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.cron.CronUtil;
|
|
|
+import cn.hutool.cron.task.Task;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fjhx.MyMain;
|
|
|
+import com.fjhx.entity.ConfigEntity;
|
|
|
+import com.fjhx.entity.UploadEntity;
|
|
|
+import com.fjhx.entity.agreement.data.ConfigurationData;
|
|
|
+import com.fjhx.utils.MyUtil;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class ConfigurationUpload {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组态软件url
|
|
|
+ */
|
|
|
+ private final String configurationUrl;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组态软件账号
|
|
|
+ */
|
|
|
+ private final String configurationUsername;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组态软件密码
|
|
|
+ */
|
|
|
+ private final String configurationPassword;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组态软件工程名称
|
|
|
+ */
|
|
|
+ private final String configurationProjectInstanceName;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组态软件设备名称列表
|
|
|
+ */
|
|
|
+ private final List<String> configurationDeviceNameList;
|
|
|
+
|
|
|
+
|
|
|
+ private String ipAddress;
|
|
|
+
|
|
|
+ private Integer port;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * token
|
|
|
+ */
|
|
|
+ private String token;
|
|
|
+
|
|
|
+ public static void execute(ConfigEntity config) {
|
|
|
+ new ConfigurationUpload(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ConfigurationUpload(ConfigEntity config) {
|
|
|
+ configurationUrl = config.getConfigurationUrl();
|
|
|
+ configurationUsername = config.getConfigurationUsername();
|
|
|
+ configurationPassword = config.getConfigurationPassword();
|
|
|
+ configurationProjectInstanceName = config.getConfigurationProjectInstanceName();
|
|
|
+ configurationDeviceNameList = config.getConfigurationDeviceNameList();
|
|
|
+
|
|
|
+ if (ObjectUtil.isAllNotEmpty(configurationUrl, configurationUsername, configurationPassword,
|
|
|
+ configurationProjectInstanceName, configurationDeviceNameList)) {
|
|
|
+
|
|
|
+ String[] split = configurationUrl
|
|
|
+ .replace("http://", "")
|
|
|
+ .replace("https://", "")
|
|
|
+ .replace("/", "")
|
|
|
+ .split(":");
|
|
|
+
|
|
|
+ ipAddress = split[0];
|
|
|
+ port = Convert.toInt(split[1]);
|
|
|
+
|
|
|
+ this.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void start() {
|
|
|
+ refreshToken();
|
|
|
+ for (String device : configurationDeviceNameList) {
|
|
|
+ CronUtil.schedule("0/3 * * * * *", (Task) () -> {
|
|
|
+ List<ConfigurationData> dataList = getDataList(device);
|
|
|
+ MyUtil.infoLog(JSONObject.toJSONString(dataList));
|
|
|
+
|
|
|
+ uploadIot(dataList, device);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新token
|
|
|
+ */
|
|
|
+ private void refreshToken() {
|
|
|
+ Map<String, Object> map = new HashMap<>(2);
|
|
|
+ map.put("username", configurationUsername);
|
|
|
+ map.put("password", configurationPassword);
|
|
|
+
|
|
|
+ String dataStr = HttpRequest.post(configurationUrl + "api/v1/login")
|
|
|
+ .body(JSONObject.toJSONString(map))
|
|
|
+ .execute()
|
|
|
+ .body();
|
|
|
+ JSONObject data = JSONObject.parseObject(dataStr);
|
|
|
+
|
|
|
+ Integer code = data.getInteger("code");
|
|
|
+ if (code != 0) {
|
|
|
+ throw new IllegalArgumentException("获取token失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ token = data.getJSONObject("data").getString("Authorization");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据list
|
|
|
+ */
|
|
|
+ private List<ConfigurationData> getDataList(String deviceName) {
|
|
|
+ String dataStr = HttpRequest.get(configurationUrl + "api/v1/devicerealvalue?projectInstanceName="
|
|
|
+ + configurationProjectInstanceName + "&DeviceName=" + deviceName)
|
|
|
+ .header("Authorization", token)
|
|
|
+ .execute()
|
|
|
+ .body();
|
|
|
+
|
|
|
+ JSONObject data = JSONObject.parseObject(dataStr);
|
|
|
+
|
|
|
+ Integer code = data.getInteger("code");
|
|
|
+ if (code != 0) {
|
|
|
+ this.refreshToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONArray dataList = data.getJSONArray("data");
|
|
|
+
|
|
|
+ return dataList.toJavaList(ConfigurationData.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把数据推送到华为云Iot
|
|
|
+ */
|
|
|
+ public void uploadIot(List<ConfigurationData> configurationDataList, String equipmentNo) {
|
|
|
+
|
|
|
+
|
|
|
+ UploadEntity uploadEntity = new UploadEntity();
|
|
|
+ uploadEntity.setEquipmentNo(equipmentNo);
|
|
|
+ uploadEntity.setIpAddress(ipAddress);
|
|
|
+ uploadEntity.setPort(port);
|
|
|
+ uploadEntity.setType(99);
|
|
|
+ uploadEntity.setCreateTime(new Date());
|
|
|
+ uploadEntity.setData(configurationDataList);
|
|
|
+
|
|
|
+ Map<String, Object> properties = new HashMap<>();
|
|
|
+ properties.put("DeviceData", JSON.toJSONString(uploadEntity));
|
|
|
+
|
|
|
+ Map<String, Object> service = new HashMap<>();
|
|
|
+ service.put("service_id", "Data");
|
|
|
+ service.put("properties", properties);
|
|
|
+
|
|
|
+ Map<Object, Object> services = new HashMap<>();
|
|
|
+ services.put("services", Collections.singletonList(service));
|
|
|
+
|
|
|
+ // 上传统计数据到华为IoTDA
|
|
|
+ MyMain.mqttClient.publishMessage(JSON.toJSONString(services));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|