CzDeviceReportService.java
9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.iot.scheduler.service.chizhou;
import com.alibaba.fastjson.JSON;
import com.iot.scheduler.utils.MqttUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 池州经开区数据上传
*/
@Slf4j
@Service
public class CzDeviceReportService {
String broker = "112.30.143.137:5007";
String username = "admin";
String password = "ly@666yc";
public void deviceReport() {
List<String> deviceIdList = Arrays.asList("TCKJ-001", "TCKJ-002", "TCKJ-003", "TCKJ-004", "TCKJ-005", "TCKJ-006",
"TCKJ-007", "TCKJ-008", "TCKJ-009", "TCKJ-010", "TCKJ-011", "TCKJ-012", "TCKJ-013", "TCKJ-014", "TCKJ-015",
"TCKJ-016", "TCKJ-017", "TCKJ-018", "TCKJ-019", "TCKJ-020", "TCKJ-021", "TCKJ-022", "TCKJ-023", "TCKJ-024",
"TCKJ-025", "TCKJ-026", "TCKJ-027", "TCKJ-028", "TCKJ-029", "TCKJ-030", "TCKJ-031", "TCKJ-032", "TCKJ-033",
"TCKJ-034", "TCKJ-035", "TCKJ-036", "TCKJ-037", "TCKJ-038", "TCKJ-039", "TCKJ-040", "TCKJ-041",
"vJAzZWk1Q2I8cdq7kx3z", "LmiW3ljI4KP487CeMmol", "ceK7YSNLrTjVSSPaFmn3", "6KePBYyCiEgo10iJXIrF",
"9vrxIQhbaZ542DvCVne0", "XMLN-001", "XMLN-002", "XMLN-003", "XMLN-004", "XMLN-005", "XMLN-006", "XMLN-007",
"XMLN-008", "XMLN-009", "XMLN-010", "XMLN-011", "XMLN-012", "XMLN-013", "XMLN-014", "XMLN-015", "XMLN-016",
"XMLN-017", "XMLN-018", "XMLN-019", "XMLN-020", "XMLN-021", "XMLN-022", "XMLN-023", "XMLN-024", "XMLN-025",
"XMLN-026", "XMLN-027", "XMLN-028", "XMLN-029", "XMLN-030", "XMLN-031", "XMLN-032", "XMLN-033", "XMLN-034",
"XMLN-035", "XMLN-036", "XMLN-037", "XMLN-038", "XMLN-039", "XMLN-040", "XMLN-041", "XMLN-042", "XMLN-043",
"XMLN-044", "XMLN-045", "XMLN-046", "XMLN-047", "XMLN-048", "XMLN-049", "XMLN-050", "XMLN-051", "XMLN-052",
"GYDPF-014", "GYDPF-006", "KBS-1-1", "KBS-1-2", "KBS-3-1", "KBS-3-5", "KBS-3-3", "CS-001", "uzGf4mjzkOPwGO4aCOf3",
"GYDPF-001", "GYDPF-010", "KBS-1-5", "KBS-2-2", "KBS-3-7", "CS-003", "LBKXrYxLQN9W7qmmqzp5", "rba7VggnqGdNyPvVNs1n",
"GYDPF-002", "GYDPF-016", "GYDPF-017", "KBS-1-6", "EqFMlJarb2aDwpwvUi6J", "x2173A2HKOiPqeRsKtWY", "GYDPF-005",
"KBS-1-3", "KBS-2-1", "KBS-3-4", "1fzR6X57k2aafbbkH4po", "lbqMr5pmkNJLWEPSa0yp", "mYz1kUajPOROg0Ewcfhr",
"6TLlfKBbTMaUZGgHtvuH", "XTE8PKwLSYp29gE1umfW", "MqYu4y0OycHQkzZhothe", "IFeN0lAXlAVQ1fD7gwk1", "hZb9NsRCqtVBSagFqnPS",
"r22r53PNgY7TeqCK5y14", "KCGSwfTvGCtBfd0qsH63", "4KzFQCncmwFh7UT4vbHp", "GYDPF-007", "KBS-3-2", "t2kkUTZYqxmKLN4X4VPB",
"9vWKALGoat3XalEtQaRB", "vYtwLMkppPbM3CtB3U9U", "4vwTfMqi8oEtWxdEUrE8", "vVvK2cTLvjYtZX1dksyd", "GYDPF-003",
"KBS-3-8", "2E6eUu66qxl5Llr6ib5v", "iWbeBfwg36lIpBRMDnvO", "GYDPF-015", "KBS-2-4", "OSbA7X7FGETF0TXub4OS",
"GYDPF-008", "KBS-2-3", "KBS-2-7", "pRhGGECbXu6FBshyg2yg", "zrVVNRo8QIbjeYjdoCUq", "fjPj3oHW7cL5m0AE1t3y",
"8dJsgqjmxIIduP4Sr2rL", "GYDPF-012", "KBS-1-4", "JZ8Tbzye8rKotYTbHUJw", "dwToSYEWr04sPtBUxaOH", "GYDPF-009",
"oe3OmagW5Wi9yw9LfUr3", "2mDiH94D7uSh4CtD2bi0", "ceGyDxlK7xzMoUacIwCv", "gxECiKjMEsVkYyowKIMd", "irAqF3fmzaM5GIo8ZLAx",
"GYDPF-013", "KBS-2-5", "KBS-3-6", "dxPdbg2UqqYADPMy9TVe", "cjFUpeVPTOh4Uf6wnVH1", "VR5XgpqhFWGMcwdy8yPQ",
"GYDPF-004", "KBS-1-7", "KBS-2-8", "HEXmiSz0sXsahRHV2bQm", "0lIIGWP49Xuo91TnTvVx", "C99WsNGFSGQSV4n3YmrW",
"3paJSiHK32wvVM6f1tO8", "GYDPF-011", "KBS-1-8", "KBS-2-6", "yrk5Cxa6EJncWX2kTywa", "xOxHHscmrEBhLTlmyRrF",
"e3HzLoIl0p3iEM860EdK");
log.info("总设备数量: {}台", deviceIdList.size());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(new Date());
log.info("上报时间统一为: {}", formattedDate);
// 用于统计状态分布
Map<Integer, Integer> statusCountMap = new HashMap<>();
statusCountMap.put(1, 0);
statusCountMap.put(2, 0);
statusCountMap.put(3, 0);
statusCountMap.put(4, 0);
// 用于统计上报结果
int successCount = 0;
int failCount = 0;
List<String> failedDevices = new ArrayList<>();
// 用于记录不同clientId的设备数量
int clientId1Count = 0;
int clientId2Count = 0;
int clientId3Count = 0;
for (int index = 0; index < deviceIdList.size(); index++) {
String deviceId = deviceIdList.get(index);
// 根据index确定clientId
String clientId = "2020672015207559169";
if (index > 45 && index <= 97) {
clientId = "2020672119054331904";
clientId2Count++;
} else if (index > 97) {
clientId = "2020672228886376448";
clientId3Count++;
} else {
clientId1Count++;
}
String topic = MessageFormat.format("/{0}/{1}/properties/report", clientId, deviceId);
// 获取设备状态
int deviceState = getDeviceState();
// 统计状态分布
statusCountMap.put(deviceState, statusCountMap.get(deviceState) + 1);
Map<String, Object> properties = new HashMap<>(5);
properties.put("type", 1);
properties.put("state", deviceState);
properties.put("time", formattedDate);
properties.put("output", 0); // 当日产量,还未确定,暂定为0
properties.put("energy", 0); // 当日能耗,还未确定,暂定为0
Map<String, Object> deviceMap = new HashMap<>(2);
deviceMap.put("deviceId", deviceId);
deviceMap.put("properties", properties);
// 记录调试信息
// if (log.isDebugEnabled()) {
// log.debug("准备上报设备信息: deviceId={}, clientId={}, state={}, topic={}",
// deviceId, clientId, deviceState, topic);
// }
try {
log.debug("开始MQTT发布: deviceId={}", deviceId);
MqttUtils.quickPublish(broker, topic, username, password, clientId, JSON.toJSONString(deviceMap));
successCount++;
// 每10个设备记录一次进度
// if ((index + 1) % 10 == 0) {
// log.info("已处理 {} 个设备,当前处理: {}", index + 1, deviceId);
// }
} catch (Exception e) {
failCount++;
failedDevices.add(deviceId);
log.error("设备上报失败: deviceId={}, clientId={}, topic={}, 错误信息: {}",
deviceId, clientId, topic, e.getMessage());
log.error("详细异常信息:", e);
}
// 可选:添加延迟,避免发送过快
try {
Thread.sleep(10); // 10毫秒延迟
} catch (InterruptedException e) {
log.warn("线程休眠被中断", e);
Thread.currentThread().interrupt();
}
}
// 任务完成,输出统计信息
log.info("========== 设备状态上报任务完成 ==========");
log.info("任务完成时间: {}", new Date());
log.info("设备总数: {}台", deviceIdList.size());
log.info("成功上报: {}台", successCount);
log.info("失败上报: {}台", failCount);
if (failCount > 0) {
log.warn("失败设备列表: {}", failedDevices);
}
// 输出clientId分布
log.info("clientId分布统计:");
log.info(" clientId-2020672015207559169: {}台", clientId1Count);
log.info(" clientId-2020672119054331904: {}台", clientId2Count);
log.info(" clientId-2020672228886376448: {}台", clientId3Count);
// 输出状态分布统计
log.info("设备状态分布统计 (按8:1:0.5:0.5比例):");
log.info(" 状态1(80%): {}台 (占比: {}%)",
statusCountMap.get(1),
String.format("%.1f", statusCountMap.get(1) * 100.0 / deviceIdList.size()));
log.info(" 状态2(10%): {}台 (占比: {}%)",
statusCountMap.get(2),
String.format("%.1f", statusCountMap.get(2) * 100.0 / deviceIdList.size()));
log.info(" 状态3(5%): {}台 (占比: {}%)",
statusCountMap.get(3),
String.format("%.1f", statusCountMap.get(3) * 100.0 / deviceIdList.size()));
log.info(" 状态4(5%): {}台 (占比: {}%)",
statusCountMap.get(4),
String.format("%.1f", statusCountMap.get(4) * 100.0 / deviceIdList.size()));
// 计算实际比例
double ratio1 = statusCountMap.get(1) / (double) statusCountMap.get(2);
double ratio3 = statusCountMap.get(3) / (double) statusCountMap.get(2);
double ratio4 = statusCountMap.get(4) / (double) statusCountMap.get(2);
log.info("实际比例: {} : 1 : {} : {}",
String.format("%.1f", ratio1),
String.format("%.1f", ratio3),
String.format("%.1f", ratio4));
// 任务执行耗时
log.info("========== 任务执行结束 ==========");
}
private int getDeviceState() {
Random random = new Random();
double rand = random.nextDouble();
int status;
if (rand < 0.8) {
status = 1; // 80%概率
} else if (rand < 0.9) {
status = 2; // 10%概率
} else if (rand < 0.95) {
status = 3; // 5%概率
} else {
status = 4; // 5%概率
}
// 调试日志
if (log.isTraceEnabled()) {
log.trace("生成设备状态: random={}, status={}", rand, status);
}
return status;
}
}