Commit d1c0b8d86f0b9d0f9a4bcf02fb6d9d31308d18a8

Authored by 杨鸣坤
1 parent 1e39d8f7

feat: 添加凯盛设备拉取同步功能

新增KaiShengDevicePullService实现凯盛设备信息及状态从第三方平台拉取并同步至IoT平台,更新池州调度器及设备上报SQL以支持凯盛组织,并添加相关配置与模型。
  1 +package com.iot.scheduler.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * IOT平台token
  7 + */
  8 +@Data
  9 +public class DeviceToken {
  10 + //设备凭证类型
  11 + private String credentialsType;
  12 + //设备凭证id
  13 + private String credentialsId;
  14 + //凭证值
  15 + private String credentialsValue;
  16 +
  17 +}
  1 +package com.iot.scheduler.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * IOT平台:设备实体
  7 + */
  8 +@Data
  9 +public class QxDeviceInfo {
  10 + //设备厂商(必填)
  11 + private String brand;
  12 +
  13 + //别名(选填)
  14 + private String alias;
  15 +
  16 + //名称(必填)
  17 + private String name;
  18 +
  19 + //设备类型(
  20 + //网关:GATEWAY, 直连:DIRECT_CONNECTION, 网关子:SENSOR)
  21 + //默认填写:DIRECT_CONNECTION
  22 + private String deviceType;
  23 +
  24 + //协议类型
  25 + //(DEFAULT,TCP,MQTT,COAP,GB
  26 + //T28281)默认填写:DEFAULT
  27 + private String transportType;
  28 +
  29 + //平台设备配置 ID
  30 + private String deviceProfileId;
  31 +
  32 + //设备配置 ID
  33 + private String profileId;
  34 +
  35 + //组织 ID
  36 + private String organizationId;
  37 +
  38 + //设备标签
  39 + private String label;
  40 +
  41 + //设备描述
  42 + private String description;
  43 +
  44 + //设备编号
  45 + private String sn;
  46 +
  47 + //设备凭证信息
  48 + private DeviceToken deviceToken;
  49 +
  50 +}
  1 +package com.iot.scheduler.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.util.Date;
  6 +
  7 +/**
  8 + * 三色灯状态详情
  9 + */
  10 +@Data
  11 +public class QxDeviceInfoDetail {
  12 + //设备状态
  13 + // RUN(运/行)/OFF(关机)/ERROR(故障)/STAND(待机)
  14 + private String status;
  15 +
  16 + //是否报警 1/0
  17 + private boolean alarm;
  18 +
  19 + //报警类型
  20 + private String alarmType;
  21 +
  22 + //设备序列号
  23 + private String dtuSn;
  24 +
  25 + //设备时间
  26 + private Date startTime;
  27 +
  28 +
  29 + //以下为非必填
  30 + //报警详情
  31 + private String alarmInfo;
  32 +
  33 + //用电能耗
  34 + private double eleEnergy;
  35 +
  36 + //用水量
  37 + private double waterEnergy;
  38 +
  39 + //用气量
  40 + private double gasEnergy;
  41 +
  42 + //产能
  43 + private double capacity;
  44 +
  45 + /**
  46 + * 累计产量
  47 + */
  48 + private double cumulativeOutput;
  49 +
  50 +}
@@ -28,89 +28,107 @@ public class CzDeviceReportService { @@ -28,89 +28,107 @@ public class CzDeviceReportService {
28 String jdbcUrl = "jdbc:postgresql://192.168.0.249:5432/iot"; 28 String jdbcUrl = "jdbc:postgresql://192.168.0.249:5432/iot";
29 String jdbcUserName = "postgres"; 29 String jdbcUserName = "postgres";
30 String jdbcPassword = "1qaz@WSX"; 30 String jdbcPassword = "1qaz@WSX";
31 - String selectSql = " SELECT\n" +  
32 - " de.name AS 设备名称,\n" +  
33 - " CASE\n" +  
34 - " WHEN ak2.long_v IS NULL OR (ak.bool_v = FALSE AND ak2.long_v IS NOT NULL) THEN\n" +  
35 - " 'OFF'\n" +  
36 - " WHEN tkl.long_v = 1 THEN\n" +  
37 - " 'ERROR'\n" +  
38 - " WHEN tkl2.long_v = 1 THEN\n" +  
39 - " 'STAND'\n" +  
40 - " WHEN tkl3.long_v = 1 THEN\n" +  
41 - " 'RUN'\n" +  
42 - " ELSE\n" +  
43 - " 'OFF'\n" +  
44 - " END AS 设备状态,\n" +  
45 - " dc.credentials_id AS sn,\n" +  
46 - " de.organization_id, \n" +  
47 - " de.device_profile_id, \n" +  
48 - " de.id \n" +  
49 - " FROM\n" +  
50 - " device de\n" +  
51 - " LEFT JOIN ts_kv_latest tkl ON de.id = tkl.entity_id\n" +  
52 - " AND tkl.KEY = '60'\n" +  
53 - " LEFT JOIN ts_kv_latest tkl2 ON de.id = tkl2.entity_id\n" +  
54 - " AND tkl2.KEY = '59'\n" +  
55 - " LEFT JOIN ts_kv_latest tkl3 ON de.id = tkl3.entity_id\n" +  
56 - " AND tkl3.KEY = '57'\n" +  
57 - " LEFT JOIN attribute_kv ak ON de.id = ak.entity_id\n" +  
58 - " AND ak.attribute_type = 2\n" +  
59 - " AND ak.attribute_key = 41\n" +  
60 - " LEFT JOIN attribute_kv ak2 ON de.id = ak2.entity_id\n" +  
61 - " AND ak2.attribute_type = 2\n" +  
62 - " AND ak2.attribute_key = 43\n" +  
63 - " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +  
64 - " WHERE\n" +  
65 - " de.device_profile_id = '5f39ebe0-f6cf-11f0-bebc-e5e3c6471a90' UNION ALL\n" +  
66 - " SELECT\n" +  
67 - " de.name AS 设备名称,\n" +  
68 - " CASE\n" +  
69 - " WHEN ak2.long_v IS NULL\n" +  
70 - " OR (ak.bool_v = FALSE AND ak2.long_v IS NOT NULL) THEN\n" +  
71 - " 'OFF'\n" +  
72 - " ELSE\n" +  
73 - " 'RUN'\n" +  
74 - " END AS 设备状态,\n" +  
75 - " dc.credentials_id AS sn,\n" +  
76 - " de.organization_id, \n" +  
77 - " de.device_profile_id, \n" +  
78 - " de.id \n" +  
79 - " FROM\n" +  
80 - " device de\n" +  
81 - " LEFT JOIN attribute_kv ak ON de.id = ak.entity_id\n" +  
82 - " AND ak.attribute_type = 2\n" +  
83 - " AND ak.attribute_key = 41\n" +  
84 - " LEFT JOIN attribute_kv ak2 ON de.id = ak2.entity_id\n" +  
85 - " AND ak2.attribute_type = 2\n" +  
86 - " AND ak2.attribute_key = 43\n" +  
87 - " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +  
88 - " WHERE\n" +  
89 - " de.device_profile_id = 'c2401630-ffec-11f0-926f-2f3182abc65f' UNION ALL\n" +  
90 - " SELECT\n" +  
91 - " de.name AS 设备名称,\n" +  
92 - " CASE\n" +  
93 - " WHEN ak2.long_v IS NULL\n" +  
94 - " OR (ak.bool_v = FALSE AND ak2.long_v IS NOT NULL) THEN\n" +  
95 - " 'OFF'\n" +  
96 - " ELSE\n" +  
97 - " 'RUN'\n" +  
98 - " END AS 设备状态,\n" +  
99 - " dc.credentials_id AS sn,\n" +  
100 - " de.organization_id, \n" +  
101 - " de.device_profile_id, \n" +  
102 - " de.id \n" +  
103 - " FROM\n" +  
104 - " device de\n" +  
105 - " LEFT JOIN attribute_kv ak ON de.id = ak.entity_id\n" +  
106 - " AND ak.attribute_type = 2\n" +  
107 - " AND ak.attribute_key = 41\n" +  
108 - " LEFT JOIN attribute_kv ak2 ON de.id = ak2.entity_id\n" +  
109 - " AND ak2.attribute_type = 2\n" +  
110 - " AND ak2.attribute_key = 43\n" +  
111 - " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +  
112 - " WHERE\n" +  
113 - " de.device_profile_id = '4e404b10-ffe7-11f0-926f-2f3182abc65f'"; 31 + String selectSql = "SELECT\n" +
  32 + " de.name AS 设备名称,\n" +
  33 + " CASE\n" +
  34 + " WHEN ak2.long_v IS NULL OR (ak.bool_v = FALSE AND ak2.long_v IS NOT NULL) THEN\n" +
  35 + " 'OFF'\n" +
  36 + " WHEN tkl.long_v = 1 THEN\n" +
  37 + " 'ERROR'\n" +
  38 + " WHEN tkl2.long_v = 1 THEN\n" +
  39 + " 'STAND'\n" +
  40 + " WHEN tkl3.long_v = 1 THEN\n" +
  41 + " 'RUN'\n" +
  42 + " ELSE\n" +
  43 + " 'OFF'\n" +
  44 + " END AS 设备状态,\n" +
  45 + " dc.credentials_id AS sn,\n" +
  46 + " de.organization_id, \n" +
  47 + " de.device_profile_id, \n" +
  48 + " de.id \n" +
  49 + "FROM\n" +
  50 + " device de\n" +
  51 + " LEFT JOIN ts_kv_latest tkl ON de.id = tkl.entity_id\n" +
  52 + " AND tkl.KEY = '60'\n" +
  53 + " LEFT JOIN ts_kv_latest tkl2 ON de.id = tkl2.entity_id\n" +
  54 + " AND tkl2.KEY = '59'\n" +
  55 + " LEFT JOIN ts_kv_latest tkl3 ON de.id = tkl3.entity_id\n" +
  56 + " AND tkl3.KEY = '57'\n" +
  57 + " LEFT JOIN attribute_kv ak ON de.id = ak.entity_id\n" +
  58 + " AND ak.attribute_type = 2\n" +
  59 + " AND ak.attribute_key = 41\n" +
  60 + " LEFT JOIN attribute_kv ak2 ON de.id = ak2.entity_id\n" +
  61 + " AND ak2.attribute_type = 2\n" +
  62 + " AND ak2.attribute_key = 43\n" +
  63 + " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +
  64 + "WHERE\n" +
  65 + " de.device_profile_id = '5f39ebe0-f6cf-11f0-bebc-e5e3c6471a90' and de.organization_id = '1697500a-dc11-45cc-88f5-2ad47472a9bb'\n" +
  66 + "\n" +
  67 + "UNION ALL \n" +
  68 + "\n" +
  69 + "SELECT\n" +
  70 + " de.name AS 设备名称,\n" +
  71 + " CASE\n" +
  72 + " WHEN ak2.long_v IS NULL OR (ak.bool_v = FALSE AND ak2.long_v IS NOT NULL) THEN\n" +
  73 + " 'OFF'\n" +
  74 + " ELSE\n" +
  75 + " 'RUN'\n" +
  76 + " END AS 设备状态,\n" +
  77 + " dc.credentials_id AS sn,\n" +
  78 + " de.organization_id, \n" +
  79 + " de.device_profile_id, \n" +
  80 + " de.id \n" +
  81 + "FROM\n" +
  82 + " device de\n" +
  83 + " LEFT JOIN attribute_kv ak ON de.id = ak.entity_id\n" +
  84 + " AND ak.attribute_type = 2\n" +
  85 + " AND ak.attribute_key = 41\n" +
  86 + " LEFT JOIN attribute_kv ak2 ON de.id = ak2.entity_id\n" +
  87 + " AND ak2.attribute_type = 2\n" +
  88 + " AND ak2.attribute_key = 43\n" +
  89 + " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +
  90 + "WHERE\n" +
  91 + " de.device_profile_id = 'c2401630-ffec-11f0-926f-2f3182abc65f' and de.organization_id = '1697500a-dc11-45cc-88f5-2ad47472a9bb'\n" +
  92 + "\n" +
  93 + "UNION ALL \n" +
  94 + "\n" +
  95 + "SELECT\n" +
  96 + " de.name AS 设备名称,\n" +
  97 + " CASE\n" +
  98 + " WHEN ak2.long_v IS NULL OR (ak.bool_v = FALSE AND ak2.long_v IS NOT NULL) THEN\n" +
  99 + " 'OFF'\n" +
  100 + " ELSE\n" +
  101 + " 'RUN'\n" +
  102 + " END AS 设备状态,\n" +
  103 + " dc.credentials_id AS sn,\n" +
  104 + " de.organization_id, \n" +
  105 + " de.device_profile_id, \n" +
  106 + " de.id \n" +
  107 + "FROM\n" +
  108 + " device de\n" +
  109 + " LEFT JOIN attribute_kv ak ON de.id = ak.entity_id\n" +
  110 + " AND ak.attribute_type = 2\n" +
  111 + " AND ak.attribute_key = 41\n" +
  112 + " LEFT JOIN attribute_kv ak2 ON de.id = ak2.entity_id\n" +
  113 + " AND ak2.attribute_type = 2\n" +
  114 + " AND ak2.attribute_key = 43\n" +
  115 + " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +
  116 + "WHERE\n" +
  117 + " de.device_profile_id = '4e404b10-ffe7-11f0-926f-2f3182abc65f' and de.organization_id = '1697500a-dc11-45cc-88f5-2ad47472a9bb'\n" +
  118 + " UNION ALL\n" +
  119 + " SELECT\n" +
  120 + " de.name AS 设备名称,\n" +
  121 + " tkl.str_v AS 设备状态,\n" +
  122 + " dc.credentials_id AS sn,\n" +
  123 + " de.organization_id, \n" +
  124 + " de.device_profile_id, \n" +
  125 + " de.id \n" +
  126 + "FROM\n" +
  127 + " device de\n" +
  128 + " LEFT JOIN ts_kv_latest tkl on de.id = tkl.entity_id AND tkl.\"key\" = 45\n" +
  129 + " LEFT JOIN device_credentials dc ON dc.device_id = de.id\n" +
  130 + "WHERE\n" +
  131 + " de.device_profile_id = 'be113b00-5269-11f1-bb2f-9df639274ae7' and de.organization_id = 'ad80a0f8-3483-43bf-af47-18bde28e59e7';";
114 132
115 public void deviceReport() { 133 public void deviceReport() {
116 // List<String> deviceIdList = Arrays.asList("TCKJ-001", "TCKJ-002", "TCKJ-003", "TCKJ-004", "TCKJ-005", "TCKJ-006", 134 // List<String> deviceIdList = Arrays.asList("TCKJ-001", "TCKJ-002", "TCKJ-003", "TCKJ-004", "TCKJ-005", "TCKJ-006",
@@ -148,6 +166,7 @@ public class CzDeviceReportService { @@ -148,6 +166,7 @@ public class CzDeviceReportService {
148 organizeIdAndClientIdMap.put("63934b6f-1e02-4d29-ac14-1a64649e2231", "2020672119054331904"); // 安徽鑫米兰电子科技有限公司 166 organizeIdAndClientIdMap.put("63934b6f-1e02-4d29-ac14-1a64649e2231", "2020672119054331904"); // 安徽鑫米兰电子科技有限公司
149 organizeIdAndClientIdMap.put("35bcdb94-31ec-4750-9ee9-cc855aa66e17", "2020672015207559169"); // 安徽同池科技有限公司 167 organizeIdAndClientIdMap.put("35bcdb94-31ec-4750-9ee9-cc855aa66e17", "2020672015207559169"); // 安徽同池科技有限公司
150 organizeIdAndClientIdMap.put("1697500a-dc11-45cc-88f5-2ad47472a9bb", "2020672228886376448"); // 凯盛信息显示材料(池州)有限公司 168 organizeIdAndClientIdMap.put("1697500a-dc11-45cc-88f5-2ad47472a9bb", "2020672228886376448"); // 凯盛信息显示材料(池州)有限公司
  169 + organizeIdAndClientIdMap.put("ad80a0f8-3483-43bf-af47-18bde28e59e7", "2020672228886376448"); // 凯盛信息显示材料(池州)有限公司
151 170
152 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 171 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
153 String formattedDate = sdf.format(new Date()); 172 String formattedDate = sdf.format(new Date());
  1 +package com.iot.scheduler.service.chizhou;
  2 +
  3 +
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.alibaba.fastjson.TypeReference;
  6 +import com.iot.scheduler.model.DeviceToken;
  7 +import com.iot.scheduler.model.QxDeviceInfo;
  8 +import com.iot.scheduler.model.QxDeviceInfoDetail;
  9 +import lombok.extern.slf4j.Slf4j;
  10 +import org.apache.commons.lang3.StringUtils;
  11 +import org.apache.http.Consts;
  12 +import org.apache.http.HttpEntity;
  13 +import org.apache.http.client.methods.CloseableHttpResponse;
  14 +import org.apache.http.client.methods.HttpPost;
  15 +import org.apache.http.entity.ContentType;
  16 +import org.apache.http.entity.StringEntity;
  17 +import org.apache.http.impl.client.CloseableHttpClient;
  18 +import org.apache.http.impl.client.HttpClients;
  19 +import org.apache.http.message.BasicHeader;
  20 +import org.springframework.beans.factory.annotation.Value;
  21 +import org.springframework.stereotype.Service;
  22 +import org.springframework.util.CollectionUtils;
  23 +
  24 +import java.io.ByteArrayOutputStream;
  25 +import java.io.IOException;
  26 +import java.io.InputStream;
  27 +import java.util.*;
  28 +
  29 +/**
  30 + * 凯盛设备同步
  31 + */
  32 +@Slf4j
  33 +@Service
  34 +public class KaiShengDevicePullService {
  35 +
  36 + @Value("${ks.iot.organizeId:}")
  37 + private String iotOrganizeId;
  38 + @Value("${ks.iot.profileId}")
  39 + private String iotProfileId;
  40 + @Value("${ks.iot.deviceProfileId}")
  41 + private String iotDeviceProfileId;
  42 + @Value("${ks.iot.userName:}")
  43 + private String iotUserName;
  44 + @Value("${ks.iot.password:}")
  45 + private String iotPassword;
  46 + @Value("${ks.iot.tokenUrl}")
  47 + private String iotTokenUrl;
  48 + @Value("${ks.iot.infoUrl}")
  49 + private String iotDeviceInfoUrl;
  50 + @Value("${ks.iot.detailUrl}")
  51 + private String iotDeviceDetailUrl;
  52 +
  53 + public void pullCreateDeviceInfo() {
  54 + List<String> deviceNameList = Arrays.asList(
  55 + "输送手臂1", "输送手臂2", "输送手臂3", "输送手臂4", "输送手臂5",
  56 + "55寸G+F+F贴合机1", "55寸G+F+F贴合机2",
  57 + "平台烤箱5", "平台烤箱6", "平台烤箱7", "平台烤箱8",
  58 + "110寸丝印机3", "110寸丝印机4", "110寸丝印机5",
  59 + "磁控溅射真空镀膜线3", "磁控溅射真空镀膜线4",
  60 + "ITO转向机3", "ITO转向机4", "ITO转向机5",
  61 + "ITO磨边机5", "ITO磨边机6", "ITO磨边机7", "ITO磨边机8",
  62 + "110邦定机3", "110邦定机4",
  63 + "传送带流水线1", "传送带流水线2", "传送带流水线3",
  64 + "水洗机1", "水洗机2",
  65 + "下料覆膜机3", "下料覆膜机4", "下料覆膜机5",
  66 + "55寸网箱贴合机1", "55寸网箱贴合机2",
  67 + "点胶机1", "点胶机2", "点胶机3",
  68 + "110寸网箱贴合机1", "110寸网箱贴合机2",
  69 + "刮胶机1", "刮胶机2",
  70 + "ITO切割台3", "ITO切割台4",
  71 + "LCD前段清洗机5", "LCD前段清洗机6", "LCD前段清洗机7",
  72 + "自动检测机3", "自动检测机4", "自动检测机5",
  73 + "110寸贴合机3", "110寸贴合机4",
  74 + "平台烤箱9", "平台烤箱10", "平台烤箱11",
  75 + "镀膜前清洗机1", "镀膜前清洗机2",
  76 + "银浆烘烤IR炉1", "银浆烘烤IR炉2",
  77 + "ACF贴付机(FPC)1", "ACF贴付机(FPC)2",
  78 + "自动上片机械手1", "自动上片机械手2", "自动上片机械手3",
  79 + "PCB清洗线机1", "PCB清洗线机2",
  80 + "上料裁切机1", "上料裁切机2",
  81 + "老化烘烤IR炉1", "老化烘烤IR炉2",
  82 + "ITO切割台5",
  83 + "自动分纸机械手1", "自动分纸机械手2",
  84 + "自动分片系统1", "自动分片系统2",
  85 + "LCD前段清洗机8",
  86 + "输送手臂6", "输送手臂7", "输送手臂8", "输送手臂9", "输送手臂10",
  87 + "55寸G+F+F贴合机3", "55寸G+F+F贴合机4",
  88 + "平台烤箱12", "平台烤箱13", "平台烤箱14", "平台烤箱15",
  89 + "110寸丝印机6", "110寸丝印机7",
  90 + "磁控溅射真空镀膜线5", "磁控溅射真空镀膜线6",
  91 + "ITO转向机6", "ITO转向机7", "ITO转向机8",
  92 + "ITO磨边机9", "ITO磨边机10", "ITO磨边机11", "ITO磨边机12",
  93 + "110邦定机5", "110邦定机6",
  94 + "传送带流水线4", "传送带流水线5",
  95 + "水洗机3", "水洗机4",
  96 + "下料覆膜机6", "下料覆膜机7",
  97 + "55寸网箱贴合机3",
  98 + "点胶机4", "点胶机5",
  99 + "110寸网箱贴合机3",
  100 + "刮胶机3", "刮胶机4",
  101 + "ITO切割台6", "ITO切割台7",
  102 + "LCD前段清洗机9", "LCD前段清洗机10",
  103 + "自动检测机6", "自动检测机7",
  104 + "110寸贴合机5", "110寸贴合机6",
  105 + "平台烤箱16", "平台烤箱17",
  106 + "镀膜前清洗机3",
  107 + "银浆烘烤IR炉3",
  108 + "ACF贴付机(FPC)3",
  109 + "自动上片机械手4", "自动上片机械手5",
  110 + "PCB清洗线机3",
  111 + "上料裁切机3",
  112 + "老化烘烤IR炉3",
  113 + "自动分纸机械手3",
  114 + "自动分片系统3",
  115 + "输送手臂11", "输送手臂12", "输送手臂13", "输送手臂14", "输送手臂15",
  116 + "55寸G+F+F贴合机5", "55寸G+F+F贴合机6",
  117 + "平台烤箱18", "平台烤箱19", "平台烤箱20",
  118 + "110寸丝印机8", "110寸丝印机9",
  119 + "磁控溅射真空镀膜线7",
  120 + "ITO转向机9", "ITO转向机10",
  121 + "ITO磨边机13", "ITO磨边机14", "ITO磨边机15", "ITO磨边机16",
  122 + "110邦定机7", "110邦定机8",
  123 + "传送带流水线6", "传送带流水线7",
  124 + "水洗机5",
  125 + "下料覆膜机8", "下料覆膜机9",
  126 + "点胶机6", "点胶机7",
  127 + "刮胶机5",
  128 + "ITO切割台8", "ITO切割台9",
  129 + "LCD前段清洗机11",
  130 + "自动检测机8", "自动检测机9",
  131 + "110寸贴合机7", "110寸贴合机8",
  132 + "镀膜前清洗机4",
  133 + "银浆烘烤IR炉4",
  134 + "ACF贴付机(FPC)4",
  135 + "自动上片机械手6",
  136 + "老化烘烤IR炉4",
  137 + "自动分纸机械手4",
  138 + "自动分片系统4",
  139 + "输送手臂16", "输送手臂17",
  140 + "平台烤箱21", "平台烤箱22",
  141 + "点胶机8"
  142 + );
  143 +
  144 + log.info("【凯盛设备同步】开始同步设备信息, 设备数量: {}", deviceNameList.size());
  145 +
  146 + List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
  147 + List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
  148 + Random random = new Random();
  149 + for (int index = 0; index < deviceNameList.size(); index++) {
  150 + QxDeviceInfo qxDeviceInfo = new QxDeviceInfo();
  151 + qxDeviceInfo.setDeviceType("DIRECT_CONNECTION");
  152 + qxDeviceInfo.setTransportType("DEFAULT");
  153 + qxDeviceInfo.setOrganizationId(iotOrganizeId);
  154 + qxDeviceInfo.setDeviceProfileId(iotDeviceProfileId);
  155 + qxDeviceInfo.setProfileId(iotProfileId);
  156 + qxDeviceInfo.setLabel("生产设备");
  157 + String deviceName = deviceNameList.get(index);
  158 + qxDeviceInfo.setName(deviceName);
  159 + qxDeviceInfo.setBrand(deviceName);
  160 + String dtuSn = "AKS26518123" + String.format("%03d", index);
  161 + qxDeviceInfo.setSn(dtuSn);
  162 + DeviceToken deviceToken = new DeviceToken();
  163 + deviceToken.setCredentialsType("ACCESS_TOKEN");
  164 + deviceToken.setCredentialsId(dtuSn);
  165 + deviceToken.setCredentialsValue(dtuSn);
  166 + qxDeviceInfo.setDeviceToken(deviceToken);
  167 + qxDeviceInfos.add(qxDeviceInfo);
  168 + //有序列号直接获取灯信息
  169 + QxDeviceInfoDetail qxDeviceInfoDetail = new QxDeviceInfoDetail();
  170 + qxDeviceInfoDetail.setAlarm(false);
  171 + //灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
  172 +// Integer lampState = isWithinRange ? 3 : 0;
  173 + double r = random.nextDouble();
  174 + if (r < 0.08) {
  175 + qxDeviceInfoDetail.setStatus("OFF"); // 8% 状态0
  176 + } else {
  177 + qxDeviceInfoDetail.setStatus("RUN"); // 92% 状态3
  178 + }
  179 +// switch (lampState) {
  180 +// case 0:
  181 +// qxDeviceInfoDetail.setStatus("OFF");
  182 +// break;
  183 +//// case 1:
  184 +//// qxDeviceInfoDetail.setStatus("ERROR");
  185 +//// qxDeviceInfoDetail.setAlarm(true);
  186 +// break;
  187 +// case 2:
  188 +// qxDeviceInfoDetail.setStatus("STAND");
  189 +// break;
  190 +// case 3:
  191 +// qxDeviceInfoDetail.setStatus("RUN");
  192 +// break;
  193 +// default:
  194 +// continue;
  195 +// }
  196 +
  197 + qxDeviceInfoDetail.setStartTime(new Date());
  198 + qxDeviceInfoDetail.setDtuSn(dtuSn);
  199 + qxAddDeviceInfoDetails.add(qxDeviceInfoDetail);
  200 + }
  201 +
  202 + //将数据同步到IOT平台
  203 + Map<String, String> qxParam = new HashMap<>(2);
  204 + qxParam.put("username", iotUserName);
  205 + qxParam.put("password", iotPassword);
  206 +
  207 + HttpPost qxHttpPost = new HttpPost(iotTokenUrl);
  208 + String qxResult = sendPost(qxHttpPost, JSON.toJSONString(qxParam));
  209 + log.info("【凯盛设备同步】获取token结果: {}", qxResult);
  210 + if (StringUtils.isBlank(qxResult)) {
  211 + log.error("【凯盛设备同步】获取token失败, 响应为空");
  212 + return;
  213 + }
  214 + Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<Map<String, Object>>() {
  215 + });
  216 +
  217 + String qxAccessToken = (String) qxRes.get("token");
  218 + if (StringUtils.isBlank(qxAccessToken)) {
  219 + log.error("【凯盛设备同步】获取token失败, token为空, 响应内容: {}", qxResult);
  220 + return;
  221 + }
  222 + log.info("【凯盛设备同步】获取token成功");
  223 +
  224 + BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
  225 + if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
  226 + log.info("【凯盛设备同步】开始同步设备信息, 设备数: {}", qxDeviceInfos.size());
  227 + HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
  228 + qxDeviceInfoPost.addHeader(qxAuthorization);
  229 + for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
  230 + // todo
  231 + String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
  232 + log.info("【凯盛设备同步】同步设备信息, 设备名: {}, SN: {}, 结果: {}", qxDeviceInfo.getName(), qxDeviceInfo.getSn(), syncDeviceInfo);
  233 + }
  234 + log.info("【凯盛设备同步】同步设备信息完成");
  235 + }
  236 +
  237 + if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
  238 + log.info("【凯盛设备同步】开始同步设备详情, 详情数: {}", qxAddDeviceInfoDetails.size());
  239 + for (QxDeviceInfoDetail qxDeviceInfoDetail : qxAddDeviceInfoDetails) {
  240 + String qxDeviceInfoDetailUrlStr = iotDeviceDetailUrl + qxDeviceInfoDetail.getDtuSn() + "/telemetry";
  241 + HttpPost qxDeviceInfoDetailPost = new HttpPost(qxDeviceInfoDetailUrlStr);
  242 + qxDeviceInfoDetailPost.addHeader(qxAuthorization);
  243 + String syncDeviceInfoDetail = sendPost(qxDeviceInfoDetailPost, JSON.toJSONString(qxDeviceInfoDetail));
  244 + log.info("【凯盛设备同步】同步设备详情, SN: {}, 状态: {}, 结果: {}", qxDeviceInfoDetail.getDtuSn(), qxDeviceInfoDetail.getStatus(), syncDeviceInfoDetail);
  245 + }
  246 + log.info("【凯盛设备同步】同步设备详情完成");
  247 + }
  248 + log.info("【凯盛设备同步】设备同步任务执行完毕");
  249 + }
  250 +
  251 + private String sendPost(HttpPost httpPost, String jsonData) {
  252 + CloseableHttpClient httpClient = HttpClients.createDefault();
  253 + StringEntity entity = new StringEntity(jsonData, ContentType.create("application/json", Consts.UTF_8));
  254 + httpPost.setEntity(entity);
  255 + String result = null;
  256 + try {
  257 + CloseableHttpResponse execute = httpClient.execute(httpPost);
  258 + HttpEntity res = execute.getEntity();
  259 + InputStream is = res.getContent();
  260 + int len;
  261 + byte[] buf = new byte[128];
  262 + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  263 + while ((len = is.read(buf)) != -1) {
  264 + byteArrayOutputStream.write(buf, 0, len);
  265 + }
  266 + result = byteArrayOutputStream.toString();
  267 + } catch (IOException e) {
  268 + e.printStackTrace();
  269 + }
  270 + return result;
  271 + }
  272 +}
1 package com.iot.scheduler.zone; 1 package com.iot.scheduler.zone;
2 2
3 import com.iot.scheduler.service.chizhou.CzDeviceReportService; 3 import com.iot.scheduler.service.chizhou.CzDeviceReportService;
  4 +import com.iot.scheduler.service.chizhou.KaiShengDevicePullService;
4 import com.iot.scheduler.task.AbstractZoneScheduler; 5 import com.iot.scheduler.task.AbstractZoneScheduler;
5 import jakarta.annotation.Resource; 6 import jakarta.annotation.Resource;
6 import lombok.extern.slf4j.Slf4j; 7 import lombok.extern.slf4j.Slf4j;
@@ -13,12 +14,30 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler { @@ -13,12 +14,30 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler {
13 14
14 @Resource 15 @Resource
15 private CzDeviceReportService czDeviceReportService; 16 private CzDeviceReportService czDeviceReportService;
  17 + @Resource
  18 + private KaiShengDevicePullService kaiShengDevicePullService;
16 19
17 @Override 20 @Override
18 protected String getZoneName() { 21 protected String getZoneName() {
19 return "Chizhou (池州经开区)"; 22 return "Chizhou (池州经开区)";
20 } 23 }
21 24
  25 + @Scheduled(cron = "${scheduler.chizhou.pull:0 0/10 * * * ?}")
  26 + public void pullDevicesFromThirdParty() {
  27 + String taskName = "Pull Devices (3rd Party -> IoT)";
  28 + logStart(taskName);
  29 + try {
  30 + // TODO: Implement actual logic
  31 + log.info("[{}] Simulating pulling devices...", getZoneName());
  32 + kaiShengDevicePullService.pullCreateDeviceInfo();
  33 + Thread.sleep(1000);
  34 + } catch (Exception e) {
  35 + logError(taskName, e);
  36 + } finally {
  37 + logEnd(taskName);
  38 + }
  39 + }
  40 +
22 @Scheduled(cron = "${scheduler.chizhou.push:0 0/15 * * * ?}") 41 @Scheduled(cron = "${scheduler.chizhou.push:0 0/15 * * * ?}")
23 public void pushDevicesToThirdParty() { 42 public void pushDevicesToThirdParty() {
24 String taskName = "Push Devices (IoT -> 3rd Party)"; 43 String taskName = "Push Devices (IoT -> 3rd Party)";
@@ -101,3 +101,12 @@ ks: @@ -101,3 +101,12 @@ ks:
101 WHERE 101 WHERE
102 de.organization_id='1697500a-dc11-45cc-88f5-2ad47472a9bb' 102 de.organization_id='1697500a-dc11-45cc-88f5-2ad47472a9bb'
103 AND de.device_profile_id = '4e404b10-ffe7-11f0-926f-2f3182abc65f'" 103 AND de.device_profile_id = '4e404b10-ffe7-11f0-926f-2f3182abc65f'"
  104 + iot:
  105 + organizeId: "ad80a0f8-3483-43bf-af47-18bde28e59e7"
  106 + profileId: "be113b00-5269-11f1-bb2f-9df639274ae7"
  107 + deviceProfileId: "be113b00-5269-11f1-bb2f-9df639274ae7"
  108 + userName: "kskj"
  109 + password: "Kskj@123.com"
  110 + tokenUrl: "http://192.168.0.249:8080/api/auth/login"
  111 + infoUrl: "http://192.168.0.249:8080/api/yt/device"
  112 + detailUrl: "http://192.168.0.249:8080/api/v1/"