|
|
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 XmlDevicePullService {
|
|
|
35
|
+
|
|
|
36
|
+ @Value("${xml.iot.organizeId:}")
|
|
|
37
|
+ private String iotOrganizeId;
|
|
|
38
|
+ @Value("${xml.iot.profileId}")
|
|
|
39
|
+ private String iotProfileId;
|
|
|
40
|
+ @Value("${xml.iot.deviceProfileId}")
|
|
|
41
|
+ private String iotDeviceProfileId;
|
|
|
42
|
+ @Value("${xml.iot.userName:}")
|
|
|
43
|
+ private String iotUserName;
|
|
|
44
|
+ @Value("${xml.iot.password:}")
|
|
|
45
|
+ private String iotPassword;
|
|
|
46
|
+ @Value("${xml.iot.tokenUrl}")
|
|
|
47
|
+ private String iotTokenUrl;
|
|
|
48
|
+ @Value("${xml.iot.infoUrl}")
|
|
|
49
|
+ private String iotDeviceInfoUrl;
|
|
|
50
|
+ @Value("${xml.iot.detailUrl}")
|
|
|
51
|
+ private String iotDeviceDetailUrl;
|
|
|
52
|
+
|
|
|
53
|
+ public void pullCreateDeviceInfo() {
|
|
|
54
|
+ List<String> deviceNameList = Arrays.asList(
|
|
|
55
|
+ "空压机1",
|
|
|
56
|
+ "空压机2", "空压机3", "空压机4",
|
|
|
57
|
+ "热水系统1",
|
|
|
58
|
+ "地磅1",
|
|
|
59
|
+ "充电桩1",
|
|
|
60
|
+ "污水在线监测系统1",
|
|
|
61
|
+ "压泥机1",
|
|
|
62
|
+ "磅房系统1",
|
|
|
63
|
+ "消防控制系统1",
|
|
|
64
|
+ "监控系统1",
|
|
|
65
|
+ "收放板机1", "收放板机2", "收放板机3", "收放板机4", "收放板机5", "收放板机6", "收放板机7", "收放板机8", "收放板机9", "收放板机10", "收放板机11", "收放板机12", "收放板机13", "收放板机14", "收放板机15", "收放板机16", "收放板机17", "收放板机18",
|
|
|
66
|
+ "提铜线1",
|
|
|
67
|
+ "纯水机1",
|
|
|
68
|
+ "环境抽风系统1", "环境抽风系统2", "环境抽风系统3", "环境抽风系统4", "环境抽风系统5", "环境抽风系统6", "环境抽风系统7",
|
|
|
69
|
+ "成品清洗线1",
|
|
|
70
|
+ "冰水机1", "冰水机2", "冰水机3",
|
|
|
71
|
+ "提锡线1",
|
|
|
72
|
+ "沉铜自动药水添加1",
|
|
|
73
|
+ "披峰机1",
|
|
|
74
|
+ "脱水机1",
|
|
|
75
|
+ "喷锡前处理线1",
|
|
|
76
|
+ "喷锡后处理线1",
|
|
|
77
|
+ "喷锡机1",
|
|
|
78
|
+ "智能电表1",
|
|
|
79
|
+ "气体报警仪1", "气体报警仪2", "气体报警仪3",
|
|
|
80
|
+ "阻焊LDI1",
|
|
|
81
|
+ "移载机1",
|
|
|
82
|
+ "烘箱1", "烘箱2", "烘箱3", "烘箱4", "烘箱5", "烘箱6", "烘箱7",
|
|
|
83
|
+ "薄膜压缩机1",
|
|
|
84
|
+ "手动压膜机1",
|
|
|
85
|
+ "中央空调主机1",
|
|
|
86
|
+ "丝印机1", "丝印机2", "丝印机3", "丝印机4", "丝印机5", "丝印机6", "丝印机7",
|
|
|
87
|
+ "光绘机1", "光绘机2",
|
|
|
88
|
+ "覆膜机1",
|
|
|
89
|
+ "打孔机1",
|
|
|
90
|
+ "压板翘机1",
|
|
|
91
|
+ "打包机1",
|
|
|
92
|
+ "真空包装机1",
|
|
|
93
|
+ "补线机1",
|
|
|
94
|
+ "数控钻机1",
|
|
|
95
|
+ "数控锣机1",
|
|
|
96
|
+ "销钉机1",
|
|
|
97
|
+ "除尘器1", "除尘器2",
|
|
|
98
|
+ "烘箱8", "烘箱9", "烘箱10", "烘箱11", "烘箱12",
|
|
|
99
|
+ "丝印机8", "丝印机9", "丝印机10", "丝印机11",
|
|
|
100
|
+ "收放板机19", "收放板机20", "收放板机21", "收放板机22", "收放板机23",
|
|
|
101
|
+ "环境抽风系统8", "环境抽风系统9", "环境抽风系统10",
|
|
|
102
|
+ "空压机5", "空压机6",
|
|
|
103
|
+ "冰水机4",
|
|
|
104
|
+ "气体报警仪4",
|
|
|
105
|
+ "光绘机3",
|
|
|
106
|
+ "打包机2",
|
|
|
107
|
+ "真空包装机2",
|
|
|
108
|
+ "补线机2",
|
|
|
109
|
+ "数控钻机2",
|
|
|
110
|
+ "数控锣机2",
|
|
|
111
|
+ "销钉机2",
|
|
|
112
|
+ "除尘器3",
|
|
|
113
|
+ "纯水机2",
|
|
|
114
|
+ "成品清洗线2",
|
|
|
115
|
+ "空压机8", "空压机9", "空压机10",
|
|
|
116
|
+ "冰水机7", "冰水机8",
|
|
|
117
|
+ "烘箱16", "烘箱17", "烘箱18", "烘箱19", "烘箱20",
|
|
|
118
|
+ "丝印机15", "丝印机16", "丝印机17", "丝印机18",
|
|
|
119
|
+ "收放板机27", "收放板机28", "收放板机29", "收放板机30",
|
|
|
120
|
+ "气体报警仪7", "气体报警仪8", "气体报警仪9",
|
|
|
121
|
+ "除尘器6", "除尘器7",
|
|
|
122
|
+ "数控钻机4", "数控钻机5",
|
|
|
123
|
+ "数控锣机4", "数控锣机5"
|
|
|
124
|
+ );
|
|
|
125
|
+
|
|
|
126
|
+ List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
|
|
|
127
|
+ List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
|
|
|
128
|
+ Random random = new Random();
|
|
|
129
|
+ for (int index = 0; index < deviceNameList.size(); index++) {
|
|
|
130
|
+ QxDeviceInfo qxDeviceInfo = new QxDeviceInfo();
|
|
|
131
|
+ qxDeviceInfo.setDeviceType("DIRECT_CONNECTION");
|
|
|
132
|
+ qxDeviceInfo.setTransportType("DEFAULT");
|
|
|
133
|
+ qxDeviceInfo.setOrganizationId(iotOrganizeId);
|
|
|
134
|
+ qxDeviceInfo.setDeviceProfileId(iotDeviceProfileId);
|
|
|
135
|
+ qxDeviceInfo.setProfileId(iotProfileId);
|
|
|
136
|
+ qxDeviceInfo.setLabel("生产设备");
|
|
|
137
|
+ String deviceName = deviceNameList.get(index);
|
|
|
138
|
+ qxDeviceInfo.setName(deviceName);
|
|
|
139
|
+ qxDeviceInfo.setBrand(deviceName);
|
|
|
140
|
+ String dtuSn = "AXM26518123" + String.format("%03d", index);
|
|
|
141
|
+ qxDeviceInfo.setSn(dtuSn);
|
|
|
142
|
+ DeviceToken deviceToken = new DeviceToken();
|
|
|
143
|
+ deviceToken.setCredentialsType("ACCESS_TOKEN");
|
|
|
144
|
+ deviceToken.setCredentialsId(dtuSn);
|
|
|
145
|
+ deviceToken.setCredentialsValue(dtuSn);
|
|
|
146
|
+ qxDeviceInfo.setDeviceToken(deviceToken);
|
|
|
147
|
+ qxDeviceInfos.add(qxDeviceInfo);
|
|
|
148
|
+ //有序列号直接获取灯信息
|
|
|
149
|
+ QxDeviceInfoDetail qxDeviceInfoDetail = new QxDeviceInfoDetail();
|
|
|
150
|
+ qxDeviceInfoDetail.setAlarm(false);
|
|
|
151
|
+ //灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
|
|
|
152
|
+// Integer lampState = isWithinRange ? 3 : 0;
|
|
|
153
|
+ double r = random.nextDouble();
|
|
|
154
|
+ if (r < 0.05) {
|
|
|
155
|
+ qxDeviceInfoDetail.setStatus("OFF"); // 5% 状态0
|
|
|
156
|
+ } else if (r < 0.10) {
|
|
|
157
|
+ qxDeviceInfoDetail.setStatus("STAND"); // 5% 状态2(跳过状态1)
|
|
|
158
|
+ } else {
|
|
|
159
|
+ qxDeviceInfoDetail.setStatus("RUN"); // 90% 状态3
|
|
|
160
|
+ }
|
|
|
161
|
+// switch (lampState) {
|
|
|
162
|
+// case 0:
|
|
|
163
|
+// qxDeviceInfoDetail.setStatus("OFF");
|
|
|
164
|
+// break;
|
|
|
165
|
+//// case 1:
|
|
|
166
|
+//// qxDeviceInfoDetail.setStatus("ERROR");
|
|
|
167
|
+//// qxDeviceInfoDetail.setAlarm(true);
|
|
|
168
|
+// break;
|
|
|
169
|
+// case 2:
|
|
|
170
|
+// qxDeviceInfoDetail.setStatus("STAND");
|
|
|
171
|
+// break;
|
|
|
172
|
+// case 3:
|
|
|
173
|
+// qxDeviceInfoDetail.setStatus("RUN");
|
|
|
174
|
+// break;
|
|
|
175
|
+// default:
|
|
|
176
|
+// continue;
|
|
|
177
|
+// }
|
|
|
178
|
+
|
|
|
179
|
+ qxDeviceInfoDetail.setStartTime(new Date());
|
|
|
180
|
+ qxDeviceInfoDetail.setDtuSn(dtuSn);
|
|
|
181
|
+ qxAddDeviceInfoDetails.add(qxDeviceInfoDetail);
|
|
|
182
|
+ }
|
|
|
183
|
+
|
|
|
184
|
+ //将数据同步到IOT平台
|
|
|
185
|
+ Map<String, String> qxParam = new HashMap<>(2);
|
|
|
186
|
+ qxParam.put("username", iotUserName);
|
|
|
187
|
+ qxParam.put("password", iotPassword);
|
|
|
188
|
+
|
|
|
189
|
+ HttpPost qxHttpPost = new HttpPost(iotTokenUrl);
|
|
|
190
|
+ String qxResult = sendPost(qxHttpPost, JSON.toJSONString(qxParam));
|
|
|
191
|
+ if (StringUtils.isBlank(qxResult)) {
|
|
|
192
|
+ return;
|
|
|
193
|
+ }
|
|
|
194
|
+ Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<Map<String, Object>>() {
|
|
|
195
|
+ });
|
|
|
196
|
+
|
|
|
197
|
+ String qxAccessToken = (String) qxRes.get("token");
|
|
|
198
|
+ if (StringUtils.isBlank(qxAccessToken)) {
|
|
|
199
|
+ return;
|
|
|
200
|
+ }
|
|
|
201
|
+
|
|
|
202
|
+ BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
|
|
|
203
|
+ if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
|
|
|
204
|
+ HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
|
|
|
205
|
+ qxDeviceInfoPost.addHeader(qxAuthorization);
|
|
|
206
|
+ for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
|
|
|
207
|
+ // todo
|
|
|
208
|
+ String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
|
|
|
209
|
+ //log.info("同步设备信息 syncDeviceInfo:{}", syncDeviceInfo);
|
|
|
210
|
+ }
|
|
|
211
|
+ }
|
|
|
212
|
+
|
|
|
213
|
+ if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
|
|
|
214
|
+ for (QxDeviceInfoDetail qxDeviceInfoDetail : qxAddDeviceInfoDetails) {
|
|
|
215
|
+ String qxDeviceInfoDetailUrlStr = iotDeviceDetailUrl + qxDeviceInfoDetail.getDtuSn() + "/telemetry";
|
|
|
216
|
+ HttpPost qxDeviceInfoDetailPost = new HttpPost(qxDeviceInfoDetailUrlStr);
|
|
|
217
|
+ qxDeviceInfoDetailPost.addHeader(qxAuthorization);
|
|
|
218
|
+ String syncDeviceInfoDetail = sendPost(qxDeviceInfoDetailPost, JSON.toJSONString(qxDeviceInfoDetail));
|
|
|
219
|
+ }
|
|
|
220
|
+ }
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ private String sendPost(HttpPost httpPost, String jsonData) {
|
|
|
224
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
225
|
+ StringEntity entity = new StringEntity(jsonData, ContentType.create("application/json", Consts.UTF_8));
|
|
|
226
|
+ httpPost.setEntity(entity);
|
|
|
227
|
+ String result = null;
|
|
|
228
|
+ try {
|
|
|
229
|
+ CloseableHttpResponse execute = httpClient.execute(httpPost);
|
|
|
230
|
+ HttpEntity res = execute.getEntity();
|
|
|
231
|
+ InputStream is = res.getContent();
|
|
|
232
|
+ int len;
|
|
|
233
|
+ byte[] buf = new byte[128];
|
|
|
234
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
235
|
+ while ((len = is.read(buf)) != -1) {
|
|
|
236
|
+ byteArrayOutputStream.write(buf, 0, len);
|
|
|
237
|
+ }
|
|
|
238
|
+ result = byteArrayOutputStream.toString();
|
|
|
239
|
+ } catch (IOException e) {
|
|
|
240
|
+ e.printStackTrace();
|
|
|
241
|
+ }
|
|
|
242
|
+ return result;
|
|
|
243
|
+ }
|
|
|
244
|
+} |
...
|
...
|
|