Commit ccffaab8da841b4a8027ffb1f886bbebe276f81a

Authored by 胡翰林
1 parent f170d8fd

鑫米兰设备属性上报

@@ -38,7 +38,7 @@ public class KSDeviceReportService { @@ -38,7 +38,7 @@ public class KSDeviceReportService {
38 @Value("${ks.third.selectSql1:SELECT * FROM ts_kv_dictionary;}") 38 @Value("${ks.third.selectSql1:SELECT * FROM ts_kv_dictionary;}")
39 private String selectSql1; 39 private String selectSql1;
40 40
41 - @Value("${ks.third.broker:192.168.0.249:1884}") 41 + @Value("${ks.third.broker:117.71.99.102:21883}")
42 private String broker; 42 private String broker;
43 @Value("${ks.third.username:yunpower}") 43 @Value("${ks.third.username:yunpower}")
44 private String username; 44 private String username;
@@ -46,9 +46,9 @@ public class KSDeviceReportService { @@ -46,9 +46,9 @@ public class KSDeviceReportService {
46 private String password; 46 private String password;
47 @Value("${ks.third.topic:storage0}") 47 @Value("${ks.third.topic:storage0}")
48 private String topic; 48 private String topic;
49 - @Value("${ks.third.clientId:2020672015207559170}") 49 + @Value("${ks.third.clientId:2020672015207559171}")
50 private String clientId; 50 private String clientId;
51 - @Value("${ks.third.channelSn:8VAUR9H3}") 51 + @Value("${ks.third.channelSn:B049Z974}")
52 private String channelSn; 52 private String channelSn;
53 53
54 public void deviceElectricity() { 54 public void deviceElectricity() {
@@ -66,21 +66,8 @@ public class KSDeviceReportService { @@ -66,21 +66,8 @@ public class KSDeviceReportService {
66 List<String> failedDevices = new ArrayList<>(); 66 List<String> failedDevices = new ArrayList<>();
67 Map<Object, String> map = new HashMap<>(); 67 Map<Object, String> map = new HashMap<>();
68 map.put(1,"Ep"); 68 map.put(1,"Ep");
69 - map.put(2,"Ubc");  
70 - map.put(3,"Uc");  
71 - map.put(4,"P");  
72 - map.put(5,"Uab");  
73 - map.put(6,"Ib");  
74 - map.put(7,"Eq");  
75 - map.put(8,"F");  
76 - map.put(9,"Q");  
77 - map.put(10,"Ua");  
78 - map.put(11,"S");  
79 - map.put(12,"Ic");  
80 - map.put(13,"Ub");  
81 - map.put(14,"Uca");  
82 - map.put(15,"PSum");  
83 - map.put(16,"Ia"); 69 + map.put(2,"Ua");
  70 + map.put(3,"Ia");
84 71
85 for (int i = 0; i < needSyncDataList.size(); i++) { 72 for (int i = 0; i < needSyncDataList.size(); i++) {
86 Object needSyncData = needSyncDataList.get(i); 73 Object needSyncData = needSyncDataList.get(i);
@@ -108,7 +95,7 @@ public class KSDeviceReportService { @@ -108,7 +95,7 @@ public class KSDeviceReportService {
108 if (o == null) { 95 if (o == null) {
109 continue; 96 continue;
110 } 97 }
111 - String deviceVarSn = "ks_" + sn + map.get(j); 98 + String deviceVarSn = "xml_" + sn + map.get(j);
112 log.info("===deviceVarSn==" + deviceVarSn); 99 log.info("===deviceVarSn==" + deviceVarSn);
113 properties.put("channelSn", channelSn); 100 properties.put("channelSn", channelSn);
114 properties.put("deviceVarSn", deviceVarSn); 101 properties.put("deviceVarSn", deviceVarSn);
@@ -196,7 +183,7 @@ public class KSDeviceReportService { @@ -196,7 +183,7 @@ public class KSDeviceReportService {
196 if (o == null) { 183 if (o == null) {
197 continue; 184 continue;
198 } 185 }
199 - String deviceVarSn = "ks_" + sn + map.get(j); 186 + String deviceVarSn = "xml_" + sn + map.get(j);
200 log.info("===deviceVarSn==" + deviceVarSn); 187 log.info("===deviceVarSn==" + deviceVarSn);
201 properties.put("channelSn", channelSn); 188 properties.put("channelSn", channelSn);
202 properties.put("deviceVarSn", deviceVarSn); 189 properties.put("deviceVarSn", deviceVarSn);
1 -package com.iot.scheduler.service.chizhou;  
2 -  
3 -import com.alibaba.fastjson.JSON;  
4 -import com.iot.scheduler.utils.MqttUtils;  
5 -import com.iot.scheduler.utils.SqlTypedValueUtils;  
6 -import com.zaxxer.hikari.HikariConfig;  
7 -import com.zaxxer.hikari.HikariDataSource;  
8 -import lombok.extern.slf4j.Slf4j;  
9 -import org.springframework.stereotype.Service;  
10 -  
11 -import java.sql.*;  
12 -import java.text.MessageFormat;  
13 -import java.text.SimpleDateFormat;  
14 -import java.util.*;  
15 -import java.util.Date;  
16 -  
17 -/**  
18 - * 池州经开区数据上传  
19 - */  
20 -@Slf4j  
21 -@Service  
22 -public class CzDeviceReportService {  
23 -  
24 - String broker = "112.30.143.137:5007";  
25 - String username = "admin";  
26 - String password = "ly@666yc";  
27 -  
28 - String jdbcUrl = "jdbc:postgresql://192.168.0.249:5432/iot";  
29 - String jdbcUserName = "postgres";  
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'";  
114 -  
115 - public void deviceReport() {  
116 -// List<String> deviceIdList = Arrays.asList("TCKJ-001", "TCKJ-002", "TCKJ-003", "TCKJ-004", "TCKJ-005", "TCKJ-006",  
117 -// "TCKJ-007", "TCKJ-008", "TCKJ-009", "TCKJ-010", "TCKJ-011", "TCKJ-012", "TCKJ-013", "TCKJ-014", "TCKJ-015",  
118 -// "TCKJ-016", "TCKJ-017", "TCKJ-018", "TCKJ-019", "TCKJ-020", "TCKJ-021", "TCKJ-022", "TCKJ-023", "TCKJ-024",  
119 -// "TCKJ-025", "TCKJ-026", "TCKJ-027", "TCKJ-028", "TCKJ-029", "TCKJ-030", "TCKJ-031", "TCKJ-032", "TCKJ-033",  
120 -// "TCKJ-034", "TCKJ-035", "TCKJ-036", "TCKJ-037", "TCKJ-038", "TCKJ-039", "TCKJ-040", "TCKJ-041",  
121 -// "vJAzZWk1Q2I8cdq7kx3z", "LmiW3ljI4KP487CeMmol", "ceK7YSNLrTjVSSPaFmn3", "6KePBYyCiEgo10iJXIrF",  
122 -// "9vrxIQhbaZ542DvCVne0", "XMLN-001", "XMLN-002", "XMLN-003", "XMLN-004", "XMLN-005", "XMLN-006", "XMLN-007",  
123 -// "XMLN-008", "XMLN-009", "XMLN-010", "XMLN-011", "XMLN-012", "XMLN-013", "XMLN-014", "XMLN-015", "XMLN-016",  
124 -// "XMLN-017", "XMLN-018", "XMLN-019", "XMLN-020", "XMLN-021", "XMLN-022", "XMLN-023", "XMLN-024", "XMLN-025",  
125 -// "XMLN-026", "XMLN-027", "XMLN-028", "XMLN-029", "XMLN-030", "XMLN-031", "XMLN-032", "XMLN-033", "XMLN-034",  
126 -// "XMLN-035", "XMLN-036", "XMLN-037", "XMLN-038", "XMLN-039", "XMLN-040", "XMLN-041", "XMLN-042", "XMLN-043",  
127 -// "XMLN-044", "XMLN-045", "XMLN-046", "XMLN-047", "XMLN-048", "XMLN-049", "XMLN-050", "XMLN-051", "XMLN-052",  
128 -// "GYDPF-014", "GYDPF-006", "KBS-1-1", "KBS-1-2", "KBS-3-1", "KBS-3-5", "KBS-3-3", "CS-001", "uzGf4mjzkOPwGO4aCOf3",  
129 -// "GYDPF-001", "GYDPF-010", "KBS-1-5", "KBS-2-2", "KBS-3-7", "CS-003", "LBKXrYxLQN9W7qmmqzp5", "rba7VggnqGdNyPvVNs1n",  
130 -// "GYDPF-002", "GYDPF-016", "GYDPF-017", "KBS-1-6", "EqFMlJarb2aDwpwvUi6J", "x2173A2HKOiPqeRsKtWY", "GYDPF-005",  
131 -// "KBS-1-3", "KBS-2-1", "KBS-3-4", "1fzR6X57k2aafbbkH4po", "lbqMr5pmkNJLWEPSa0yp", "mYz1kUajPOROg0Ewcfhr",  
132 -// "6TLlfKBbTMaUZGgHtvuH", "XTE8PKwLSYp29gE1umfW", "MqYu4y0OycHQkzZhothe", "IFeN0lAXlAVQ1fD7gwk1", "hZb9NsRCqtVBSagFqnPS",  
133 -// "r22r53PNgY7TeqCK5y14", "KCGSwfTvGCtBfd0qsH63", "4KzFQCncmwFh7UT4vbHp", "GYDPF-007", "KBS-3-2", "t2kkUTZYqxmKLN4X4VPB",  
134 -// "9vWKALGoat3XalEtQaRB", "vYtwLMkppPbM3CtB3U9U", "4vwTfMqi8oEtWxdEUrE8", "vVvK2cTLvjYtZX1dksyd", "GYDPF-003",  
135 -// "KBS-3-8", "2E6eUu66qxl5Llr6ib5v", "iWbeBfwg36lIpBRMDnvO", "GYDPF-015", "KBS-2-4", "OSbA7X7FGETF0TXub4OS",  
136 -// "GYDPF-008", "KBS-2-3", "KBS-2-7", "pRhGGECbXu6FBshyg2yg", "zrVVNRo8QIbjeYjdoCUq", "fjPj3oHW7cL5m0AE1t3y",  
137 -// "8dJsgqjmxIIduP4Sr2rL", "GYDPF-012", "KBS-1-4", "JZ8Tbzye8rKotYTbHUJw", "dwToSYEWr04sPtBUxaOH", "GYDPF-009",  
138 -// "oe3OmagW5Wi9yw9LfUr3", "2mDiH94D7uSh4CtD2bi0", "ceGyDxlK7xzMoUacIwCv", "gxECiKjMEsVkYyowKIMd", "irAqF3fmzaM5GIo8ZLAx",  
139 -// "GYDPF-013", "KBS-2-5", "KBS-3-6", "dxPdbg2UqqYADPMy9TVe", "cjFUpeVPTOh4Uf6wnVH1", "VR5XgpqhFWGMcwdy8yPQ",  
140 -// "GYDPF-004", "KBS-1-7", "KBS-2-8", "HEXmiSz0sXsahRHV2bQm", "0lIIGWP49Xuo91TnTvVx", "C99WsNGFSGQSV4n3YmrW",  
141 -// "3paJSiHK32wvVM6f1tO8", "GYDPF-011", "KBS-1-8", "KBS-2-6", "yrk5Cxa6EJncWX2kTywa", "xOxHHscmrEBhLTlmyRrF",  
142 -// "e3HzLoIl0p3iEM860EdK");  
143 -  
144 - List<Object> needSyncDataList = initConnectAndSelectData();  
145 - log.info("总设备数量: {}台", needSyncDataList.size());  
146 -  
147 - Map<String, String> organizeIdAndClientIdMap = new HashMap<>(3);  
148 - organizeIdAndClientIdMap.put("63934b6f-1e02-4d29-ac14-1a64649e2231", "2020672119054331904"); // 安徽鑫米兰电子科技有限公司  
149 - organizeIdAndClientIdMap.put("35bcdb94-31ec-4750-9ee9-cc855aa66e17", "2020672015207559169"); // 安徽同池科技有限公司  
150 - organizeIdAndClientIdMap.put("1697500a-dc11-45cc-88f5-2ad47472a9bb", "2020672228886376448"); // 凯盛信息显示材料(池州)有限公司  
151 -  
152 - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
153 - String formattedDate = sdf.format(new Date());  
154 - log.info("上报时间统一为: {}", formattedDate);  
155 -  
156 - // 用于统计上报结果  
157 - int successCount = 0;  
158 - int failCount = 0;  
159 - List<String> failedDevices = new ArrayList<>();  
160 - for (int index = 0; index < needSyncDataList.size(); index++) {  
161 - Object needSyncData = needSyncDataList.get(index);  
162 - List<Object> dataList = (ArrayList) needSyncData;  
163 - String deviceId = dataList.get(2).toString();  
164 - String status = dataList.get(1).toString();  
165 - String organizeId = dataList.get(3).toString();  
166 - String deviceProfileId = dataList.get(4).toString();  
167 - String id = dataList.get(5).toString();  
168 -  
169 - // 根据index确定clientId  
170 - String clientId = organizeIdAndClientIdMap.get(organizeId);  
171 - String topic = MessageFormat.format("/{0}/{1}/properties/report", clientId, deviceId);  
172 -  
173 - // 获取设备状态  
174 - int deviceState = getDeviceState(status);  
175 -  
176 - // 计算当日用电量和用水量  
177 - double electricity = calculateDailyElectricity(id, deviceProfileId);  
178 - double water = calculateDailyWater(id, deviceProfileId);  
179 -  
180 - Map<String, Object> properties = new HashMap<>(5);  
181 - properties.put("type", 1);  
182 - properties.put("state", deviceState);  
183 - properties.put("time", formattedDate);  
184 - properties.put("output", 0); // 当日产量,还未确定,暂定为0  
185 - properties.put("energy", 0); // 当日能耗,还未确定,暂定为0  
186 - properties.put("electricity", electricity); // 当日用电量  
187 - properties.put("water", water); // 当日用水量  
188 -  
189 - Map<String, Object> deviceMap = new HashMap<>(2);  
190 - deviceMap.put("deviceId", deviceId);  
191 - deviceMap.put("properties", properties);  
192 -  
193 - // 记录调试信息  
194 -// if (log.isDebugEnabled()) {  
195 -// log.debug("准备上报设备信息: deviceId={}, clientId={}, state={}, topic={}",  
196 -// deviceId, clientId, deviceState, topic);  
197 -// }  
198 -  
199 - try {  
200 - log.debug("开始MQTT发布: deviceId={}", deviceId);  
201 - MqttUtils.publish(broker, topic, username, password, clientId, JSON.toJSONString(deviceMap));  
202 - successCount++;  
203 -  
204 - // 每10个设备记录一次进度  
205 -// if ((index + 1) % 10 == 0) {  
206 -// log.info("已处理 {} 个设备,当前处理: {}", index + 1, deviceId);  
207 -// }  
208 -  
209 - } catch (Exception e) {  
210 - failCount++;  
211 - failedDevices.add(deviceId);  
212 - log.error("设备上报失败: deviceId={}, clientId={}, topic={}, 错误信息: {}",  
213 - deviceId, clientId, topic, e.getMessage());  
214 - log.error("详细异常信息:", e);  
215 - }  
216 -  
217 - // 可选:添加延迟,避免发送过快  
218 - try {  
219 - Thread.sleep(10); // 10毫秒延迟  
220 - } catch (InterruptedException e) {  
221 - log.warn("线程休眠被中断", e);  
222 - Thread.currentThread().interrupt();  
223 - }  
224 - }  
225 -  
226 - // 任务完成,输出统计信息  
227 - log.info("========== 设备状态上报任务完成 ==========");  
228 - log.info("成功上报: {}台", successCount);  
229 - log.info("失败上报: {}台", failCount);  
230 -  
231 - if (failCount > 0) {  
232 - log.warn("失败设备列表: {}", failedDevices);  
233 - }  
234 -  
235 - // 任务执行耗时  
236 - log.info("========== 任务执行结束 ==========");  
237 - }  
238 -  
239 - private int getDeviceState(String status) {  
240 - int deviceState;  
241 - if ("RUN".equals(status)) {  
242 - deviceState = 1;  
243 - } else if ("STAND".equals(status)) {  
244 - deviceState = 2;  
245 - } else if ("OFF".equals(status)) {  
246 - deviceState = 3;  
247 - } else {  
248 - deviceState = 4;  
249 - }  
250 -  
251 - return deviceState;  
252 - }  
253 -  
254 - /**  
255 - * 计算当日用电量  
256 - * deviceProfileId = c2401630-ffec-11f0-926f-2f3182abc65f,key=64,读取dbl_v  
257 - * @param deviceId 设备ID(entity_id)  
258 - * @param deviceProfileId 设备配置ID  
259 - * @return 当日用电量  
260 - */  
261 - private double calculateDailyElectricity(String deviceId, String deviceProfileId) {  
262 - // 非用电设备类型,返回0  
263 - if (!"c2401630-ffec-11f0-926f-2f3182abc65f".equals(deviceProfileId)) {  
264 - return 0;  
265 - }  
266 -  
267 - String sql = "SELECT " +  
268 - " (SELECT COALESCE(dbl_v, 0) FROM ts_kv " +  
269 - " WHERE entity_id = ? AND key = 64 AND ts >= ? AND ts <= ? AND dbl_v IS NOT NULL " +  
270 - " ORDER BY ts ASC LIMIT 1) AS first_value, " +  
271 - " (SELECT COALESCE(dbl_v, 0) FROM ts_kv " +  
272 - " WHERE entity_id = ? AND key = 64 AND ts >= ? AND ts <= ? AND dbl_v IS NOT NULL " +  
273 - " ORDER BY ts DESC LIMIT 1) AS last_value";  
274 -  
275 - return calculateDailyConsumption(deviceId, sql, "用电量");  
276 - }  
277 -  
278 - /**  
279 - * 计算当日用水量  
280 - * deviceProfileId = 4e404b10-ffe7-11f0-926f-2f3182abc65f,key=83,读取dbl_v或long_v  
281 - * @param deviceId 设备ID(entity_id)  
282 - * @param deviceProfileId 设备配置ID  
283 - * @return 当日用水量  
284 - */  
285 - private double calculateDailyWater(String deviceId, String deviceProfileId) {  
286 - // 非用水设备类型,返回0  
287 - if (!"4e404b10-ffe7-11f0-926f-2f3182abc65f".equals(deviceProfileId)) {  
288 - return 0;  
289 - }  
290 -  
291 - String sql = "SELECT " +  
292 - " (SELECT COALESCE(dbl_v, long_v, 0) FROM ts_kv " +  
293 - " WHERE entity_id = ? AND key = 83 AND ts >= ? AND ts <= ? " +  
294 - " AND (dbl_v IS NOT NULL OR long_v IS NOT NULL) " +  
295 - " ORDER BY ts ASC LIMIT 1) AS first_value, " +  
296 - " (SELECT COALESCE(dbl_v, long_v, 0) FROM ts_kv " +  
297 - " WHERE entity_id = ? AND key = 83 AND ts >= ? AND ts <= ? " +  
298 - " AND (dbl_v IS NOT NULL OR long_v IS NOT NULL) " +  
299 - " ORDER BY ts DESC LIMIT 1) AS last_value";  
300 -  
301 - return calculateDailyConsumption(deviceId, sql, "用水量");  
302 - }  
303 -  
304 - /**  
305 - * 执行能耗计算查询  
306 - * @param deviceId 设备ID  
307 - * @param sql 查询SQL  
308 - * @param logName 日志名称  
309 - * @return 当日消耗量  
310 - */  
311 - private double calculateDailyConsumption(String deviceId, String sql, String logName) {  
312 - Connection connection = null;  
313 - PreparedStatement statement = null;  
314 - ResultSet resultSet = null;  
315 - HikariDataSource dataSource = null;  
316 -  
317 - try {  
318 - HikariConfig config = new HikariConfig();  
319 - config.setJdbcUrl(jdbcUrl);  
320 - config.setUsername(jdbcUserName);  
321 - config.setPassword(jdbcPassword);  
322 - config.setDriverClassName("org.postgresql.Driver");  
323 - config.setMaximumPoolSize(5);  
324 - config.setMinimumIdle(5);  
325 - config.setConnectionTimeout(60000);  
326 - config.setConnectionTestQuery("SELECT 1");  
327 -  
328 - dataSource = new HikariDataSource(config);  
329 - connection = dataSource.getConnection();  
330 -  
331 - // 获取当天开始和结束时间戳(毫秒)  
332 - Calendar calendar = Calendar.getInstance();  
333 - calendar.set(Calendar.HOUR_OF_DAY, 0);  
334 - calendar.set(Calendar.MINUTE, 0);  
335 - calendar.set(Calendar.SECOND, 0);  
336 - calendar.set(Calendar.MILLISECOND, 0);  
337 - long startOfDay = calendar.getTimeInMillis();  
338 - calendar.set(Calendar.HOUR_OF_DAY, 23);  
339 - calendar.set(Calendar.MINUTE, 59);  
340 - calendar.set(Calendar.SECOND, 59);  
341 - calendar.set(Calendar.MILLISECOND, 999);  
342 - long endOfDay = calendar.getTimeInMillis();  
343 -  
344 - statement = connection.prepareStatement(sql);  
345 - statement.setString(1, deviceId);  
346 - statement.setLong(2, startOfDay);  
347 - statement.setLong(3, endOfDay);  
348 - statement.setString(4, deviceId);  
349 - statement.setLong(5, startOfDay);  
350 - statement.setLong(6, endOfDay);  
351 -  
352 - resultSet = statement.executeQuery();  
353 - if (resultSet.next()) {  
354 - double firstValue = resultSet.getDouble("first_value");  
355 - double lastValue = resultSet.getDouble("last_value");  
356 - double consumption = lastValue - firstValue;  
357 - log.debug("设备 {} 当日{}计算: 首值={}, 末值={}, 消耗={}", deviceId, logName, firstValue, lastValue, consumption);  
358 - return Math.max(consumption, 0); // 确保非负  
359 - }  
360 - } catch (SQLException e) {  
361 - log.error("查询设备 {} 当日{}失败: {}", deviceId, logName, e.getMessage(), e);  
362 - } catch (Exception e) {  
363 - log.error("计算设备 {} 当日{}时发生异常", deviceId, logName, e);  
364 - } finally {  
365 - try {  
366 - if (resultSet != null) resultSet.close();  
367 - if (statement != null) statement.close();  
368 - if (connection != null) connection.close();  
369 - } catch (SQLException e) {  
370 - log.error("关闭数据库资源时发生异常", e);  
371 - }  
372 - if (dataSource != null) {  
373 - try {  
374 - dataSource.close();  
375 - } catch (Exception e) {  
376 - log.error("关闭HikariDataSource连接池时发生异常", e);  
377 - }  
378 - }  
379 - }  
380 - return 0;  
381 - }  
382 -  
383 - private List<Object> initConnectAndSelectData() {  
384 - Connection connection = null;  
385 - PreparedStatement statement = null;  
386 - ResultSet resultSet = null;  
387 - HikariDataSource dataSource = null;  
388 - List<Object> resultList = new ArrayList<>();  
389 -  
390 - log.info("开始连接数据库,URL: {}", jdbcUrl);  
391 -  
392 - try {  
393 - HikariConfig config = new HikariConfig();  
394 - config.setJdbcUrl(jdbcUrl);  
395 - config.setUsername(jdbcUserName);  
396 - config.setPassword(jdbcPassword);  
397 - config.setDriverClassName("org.postgresql.Driver");  
398 - config.setMaximumPoolSize(5);  
399 - config.setMinimumIdle(5);  
400 - config.setConnectionTimeout(60000);  
401 - config.setConnectionTestQuery("SELECT 1");  
402 -  
403 - dataSource = new HikariDataSource(config);  
404 - log.info("Hikari连接池配置完成");  
405 -  
406 - connection = dataSource.getConnection();  
407 - log.info("数据库连接成功");  
408 -  
409 - statement = connection.prepareStatement(selectSql);  
410 - log.info("执行SQL查询: {}", selectSql);  
411 -  
412 - resultSet = statement.executeQuery();  
413 - ResultSetMetaData metaData = resultSet.getMetaData();  
414 - int columnCount = metaData.getColumnCount();  
415 - log.info("查询结果集元数据获取成功,共{}列", columnCount);  
416 -  
417 - int rowCount = 0;  
418 - while (resultSet.next()) {  
419 - List<Object> result = new ArrayList<>(columnCount);  
420 - for (int index = 1; index <= columnCount; index++) {  
421 - int columnType = metaData.getColumnType(index);  
422 - Object value = SqlTypedValueUtils.getTypedValue(resultSet, index, columnType);  
423 - result.add(value);  
424 - }  
425 - resultList.add(result);  
426 - rowCount++;  
427 -  
428 - // 每处理1000行记录一次日志  
429 - if (rowCount % 1000 == 0) {  
430 - log.info("已处理{}行数据", rowCount);  
431 - }  
432 - }  
433 -  
434 - log.info("数据查询完成,共获取{}行数据", rowCount);  
435 -  
436 - } catch (SQLException e) {  
437 - log.error("数据库操作异常,URL: {}, 用户名: {}", jdbcUrl, jdbcUserName, e);  
438 - } catch (Exception e) {  
439 - log.error("初始化数据库连接或查询数据时发生异常", e);  
440 - } finally {  
441 - // 释放资源  
442 - try {  
443 - if (resultSet != null) resultSet.close();  
444 - if (statement != null) statement.close();  
445 - if (connection != null) connection.close();  
446 - log.info("数据库连接资源已释放");  
447 - } catch (SQLException e) {  
448 - log.error("关闭数据库资源时发生异常", e);  
449 - }  
450 -  
451 - if (dataSource != null) {  
452 - try {  
453 - dataSource.close();  
454 - log.info("HikariDataSource连接池已关闭");  
455 - } catch (Exception e) {  
456 - log.error("关闭HikariDataSource连接池时发生异常", e);  
457 - }  
458 - }  
459 - }  
460 -  
461 - log.info("数据库操作完成,返回{}条记录", resultList.size());  
462 - return resultList;  
463 - }  
464 -}  
1 -package com.iot.scheduler.zone;  
2 -  
3 -import com.iot.scheduler.service.chizhou.CzDeviceReportService;  
4 -import com.iot.scheduler.task.AbstractZoneScheduler;  
5 -import jakarta.annotation.Resource;  
6 -import lombok.extern.slf4j.Slf4j;  
7 -import org.springframework.scheduling.annotation.Scheduled;  
8 -import org.springframework.stereotype.Component;  
9 -  
10 -@Slf4j  
11 -@Component  
12 -public class ChizhouZoneScheduler extends AbstractZoneScheduler {  
13 -  
14 - @Resource  
15 - private CzDeviceReportService czDeviceReportService;  
16 -  
17 - @Override  
18 - protected String getZoneName() {  
19 - return "Chizhou (池州经开区)";  
20 - }  
21 -  
22 - @Scheduled(cron = "${scheduler.chizhou.push:0 0/15 * * * ?}")  
23 - public void pushDevicesToThirdParty() {  
24 - String taskName = "Push Devices (IoT -> 3rd Party)";  
25 - logStart(taskName);  
26 - try {  
27 - // TODO: Implement actual logic  
28 - log.info("[{}] Simulating pushing devices...", getZoneName());  
29 - czDeviceReportService.deviceReport();  
30 - Thread.sleep(1000);  
31 - } catch (Exception e) {  
32 - logError(taskName, e);  
33 - } finally {  
34 - logEnd(taskName);  
35 - }  
36 - }  
37 -}  
@@ -5,7 +5,7 @@ spring: @@ -5,7 +5,7 @@ spring:
5 banner-mode: off 5 banner-mode: off
6 6
7 server: 7 server:
8 - port: 32211 8 + port: 32212
9 9
10 logging: 10 logging:
11 level: 11 level:
@@ -33,63 +33,60 @@ ks: @@ -33,63 +33,60 @@ ks:
33 jdbcUserName: "postgres" 33 jdbcUserName: "postgres"
34 jdbcPassword: "1qaz@WSX" 34 jdbcPassword: "1qaz@WSX"
35 selectSql: "SELECT 35 selectSql: "SELECT
36 - de.sn AS sn,  
37 - tkl1.dbl_v AS Ep,  
38 - tkl2.dbl_v AS Ubc,  
39 - tkl3.dbl_v AS Uc,  
40 - tkl4.dbl_v AS P,  
41 - tkl5.dbl_v AS Uab,  
42 - tkl6.dbl_v AS Ib,  
43 - tkl7.dbl_v AS Eq,  
44 - tkl8.dbl_v AS F,  
45 - tkl9.dbl_v AS Q,  
46 - tkl10.dbl_v AS Ua,  
47 - tkl11.dbl_v AS S,  
48 - tkl12.dbl_v AS Ic,  
49 - tkl13.dbl_v AS Ub,  
50 - tkl14.dbl_v AS Uca,  
51 - tkl15.dbl_v AS PSum,  
52 - tkl16.dbl_v AS Ia  
53 - FROM  
54 - device de  
55 - LEFT JOIN ts_kv_latest tkl1 ON de.id = tkl1.entity_id  
56 - AND tkl1.key = 64  
57 - LEFT JOIN ts_kv_latest tkl2 ON de.id = tkl2.entity_id  
58 - AND tkl2.key = 65  
59 - LEFT JOIN ts_kv_latest tkl3 ON de.id = tkl3.entity_id  
60 - AND tkl3.key = 66  
61 - LEFT JOIN ts_kv_latest tkl4 ON de.id = tkl4.entity_id  
62 - AND tkl4.key = 67  
63 - LEFT JOIN ts_kv_latest tkl5 ON de.id = tkl5.entity_id  
64 - AND tkl5.key = 68  
65 - LEFT JOIN ts_kv_latest tkl6 ON de.id = tkl6.entity_id  
66 - AND tkl6.key = 69  
67 - LEFT JOIN ts_kv_latest tkl7 ON de.id = tkl7.entity_id  
68 - AND tkl7.key = 70  
69 - LEFT JOIN ts_kv_latest tkl8 ON de.id = tkl8.entity_id  
70 - AND tkl8.key = 71  
71 - LEFT JOIN ts_kv_latest tkl9 ON de.id = tkl9.entity_id  
72 - AND tkl9.key = 72  
73 - LEFT JOIN ts_kv_latest tkl10 ON de.id = tkl10.entity_id  
74 - AND tkl10.key = 73  
75 - LEFT JOIN ts_kv_latest tkl11 ON de.id = tkl11.entity_id  
76 - AND tkl11.key = 74  
77 - LEFT JOIN ts_kv_latest tkl12 ON de.id = tkl12.entity_id  
78 - AND tkl12.key = 75  
79 - LEFT JOIN ts_kv_latest tkl13 ON de.id = tkl13.entity_id  
80 - AND tkl13.key = 76  
81 - LEFT JOIN ts_kv_latest tkl14 ON de.id = tkl14.entity_id  
82 - AND tkl14.key = 77  
83 - LEFT JOIN ts_kv_latest tkl15 ON de.id = tkl15.entity_id  
84 - AND tkl15.key = 78  
85 - LEFT JOIN ts_kv_latest tkl16 ON de.id = tkl16.entity_id  
86 - AND tkl16.key = 79  
87 - WHERE  
88 - de.organization_id='1697500a-dc11-45cc-88f5-2ad47472a9bb'  
89 - AND de.device_profile_id = 'c2401630-ffec-11f0-926f-2f3182abc65f'" 36 + CASE
  37 + WHEN de.id = '9d8539a0-2f27-11f1-926f-2f3182abc65f' THEN '数控车间'
  38 + WHEN de.id = 'bb1cfba0-2be5-11f1-926f-2f3182abc65f' THEN '无尘车间'
  39 + WHEN de.id = 'bb1cd490-2be5-11f1-926f-2f3182abc65f' THEN '化金线'
  40 + WHEN de.id = '77c0bf22-29c6-11f1-926f-2f3182abc65f' THEN '飞针机车间'
  41 + WHEN de.id = '77c0bf23-29c6-11f1-926f-2f3182abc65f' THEN '蚀刻生产线'
  42 + ELSE '导电膜生产线' END AS sn,
  43 + tkl1.dbl_v AS Ep,
  44 + tkl10.dbl_v AS Ua,
  45 + tkl16.dbl_v AS Ia
  46 +FROM
  47 + device de
  48 + LEFT JOIN ts_kv_latest tkl1 ON de.id = tkl1.entity_id
  49 + AND tkl1.KEY = 64
  50 + LEFT JOIN ts_kv_latest tkl2 ON de.id = tkl2.entity_id
  51 + AND tkl2.KEY = 65
  52 + LEFT JOIN ts_kv_latest tkl3 ON de.id = tkl3.entity_id
  53 + AND tkl3.KEY = 66
  54 + LEFT JOIN ts_kv_latest tkl4 ON de.id = tkl4.entity_id
  55 + AND tkl4.KEY = 67
  56 + LEFT JOIN ts_kv_latest tkl5 ON de.id = tkl5.entity_id
  57 + AND tkl5.KEY = 68
  58 + LEFT JOIN ts_kv_latest tkl6 ON de.id = tkl6.entity_id
  59 + AND tkl6.KEY = 69
  60 + LEFT JOIN ts_kv_latest tkl7 ON de.id = tkl7.entity_id
  61 + AND tkl7.KEY = 70
  62 + LEFT JOIN ts_kv_latest tkl8 ON de.id = tkl8.entity_id
  63 + AND tkl8.KEY = 71
  64 + LEFT JOIN ts_kv_latest tkl9 ON de.id = tkl9.entity_id
  65 + AND tkl9.KEY = 72
  66 + LEFT JOIN ts_kv_latest tkl10 ON de.id = tkl10.entity_id
  67 + AND tkl10.KEY = 73
  68 + LEFT JOIN ts_kv_latest tkl11 ON de.id = tkl11.entity_id
  69 + AND tkl11.KEY = 74
  70 + LEFT JOIN ts_kv_latest tkl12 ON de.id = tkl12.entity_id
  71 + AND tkl12.KEY = 75
  72 + LEFT JOIN ts_kv_latest tkl13 ON de.id = tkl13.entity_id
  73 + AND tkl13.KEY = 76
  74 + LEFT JOIN ts_kv_latest tkl14 ON de.id = tkl14.entity_id
  75 + AND tkl14.KEY = 77
  76 + LEFT JOIN ts_kv_latest tkl15 ON de.id = tkl15.entity_id
  77 + AND tkl15.KEY = 78
  78 + LEFT JOIN ts_kv_latest tkl16 ON de.id = tkl16.entity_id
  79 + AND tkl16.KEY = 79
  80 +WHERE
  81 + de.organization_id = '1697500a-dc11-45cc-88f5-2ad47472a9bb'
  82 + AND de.device_profile_id = 'c2401630-ffec-11f0-926f-2f3182abc65f'
  83 + AND de.id IN ('9d8539a0-2f27-11f1-926f-2f3182abc65f', 'bb1cfba0-2be5-11f1-926f-2f3182abc65f', 'bb1cd491-2be5-11f1-926f-2f3182abc65f', 'bb1cd490-2be5-11f1-926f-2f3182abc65f', '77c0bf22-29c6-11f1-926f-2f3182abc65f', '77c0bf23-29c6-11f1-926f-2f3182abc65f')"
90 84
91 selectSql1: "SELECT 85 selectSql1: "SELECT
92 - de.sn AS sn, 86 + CASE
  87 + WHEN de.id = '6775ad30-ffee-11f0-926f-2f3182abc65f' THEN '1号水表'
  88 + WHEN de.id = '6775d440-ffee-11f0-926f-2f3182abc65f' THEN '2号水表'
  89 + ELSE '其他' END AS sn,
93 tkl1.dbl_v AS zll, 90 tkl1.dbl_v AS zll,
94 tkl2.dbl_v AS ssll 91 tkl2.dbl_v AS ssll
95 FROM 92 FROM
@@ -100,4 +97,5 @@ ks: @@ -100,4 +97,5 @@ ks:
100 AND tkl2.key = 84 97 AND tkl2.key = 84
101 WHERE 98 WHERE
102 de.organization_id='1697500a-dc11-45cc-88f5-2ad47472a9bb' 99 de.organization_id='1697500a-dc11-45cc-88f5-2ad47472a9bb'
103 - AND de.device_profile_id = '4e404b10-ffe7-11f0-926f-2f3182abc65f'" 100 + AND de.device_profile_id = '4e404b10-ffe7-11f0-926f-2f3182abc65f'
  101 + AND de.id in('6775ad30-ffee-11f0-926f-2f3182abc65f','6775d440-ffee-11f0-926f-2f3182abc65f')"