KaiShengDevicePullService.java
13 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.iot.scheduler.service.chizhou;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.iot.scheduler.model.DeviceToken;
import com.iot.scheduler.model.QxDeviceInfo;
import com.iot.scheduler.model.QxDeviceInfoDetail;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* 凯盛设备同步
*/
@Slf4j
@Service
public class KaiShengDevicePullService {
@Value("${ks.iot.organizeId:}")
private String iotOrganizeId;
@Value("${ks.iot.profileId}")
private String iotProfileId;
@Value("${ks.iot.deviceProfileId}")
private String iotDeviceProfileId;
@Value("${ks.iot.userName:}")
private String iotUserName;
@Value("${ks.iot.password:}")
private String iotPassword;
@Value("${ks.iot.tokenUrl}")
private String iotTokenUrl;
@Value("${ks.iot.infoUrl}")
private String iotDeviceInfoUrl;
@Value("${ks.iot.detailUrl}")
private String iotDeviceDetailUrl;
public void pullCreateDeviceInfo() {
List<String> deviceNameList = Arrays.asList(
"输送手臂1", "输送手臂2", "输送手臂3", "输送手臂4", "输送手臂5",
"55寸G+F+F贴合机1", "55寸G+F+F贴合机2",
"平台烤箱5", "平台烤箱6", "平台烤箱7", "平台烤箱8",
"110寸丝印机3", "110寸丝印机4", "110寸丝印机5",
"磁控溅射真空镀膜线3", "磁控溅射真空镀膜线4",
"ITO转向机3", "ITO转向机4", "ITO转向机5",
"ITO磨边机5", "ITO磨边机6", "ITO磨边机7", "ITO磨边机8",
"110邦定机3", "110邦定机4",
"传送带流水线1", "传送带流水线2", "传送带流水线3",
"水洗机1", "水洗机2",
"下料覆膜机3", "下料覆膜机4", "下料覆膜机5",
"55寸网箱贴合机1", "55寸网箱贴合机2",
"点胶机1", "点胶机2", "点胶机3",
"110寸网箱贴合机1", "110寸网箱贴合机2",
"刮胶机1", "刮胶机2",
"ITO切割台3", "ITO切割台4",
"LCD前段清洗机5", "LCD前段清洗机6", "LCD前段清洗机7",
"自动检测机3", "自动检测机4", "自动检测机5",
"110寸贴合机3", "110寸贴合机4",
"平台烤箱9", "平台烤箱10", "平台烤箱11",
"镀膜前清洗机1", "镀膜前清洗机2",
"银浆烘烤IR炉1", "银浆烘烤IR炉2",
"ACF贴付机(FPC)1", "ACF贴付机(FPC)2",
"自动上片机械手1", "自动上片机械手2", "自动上片机械手3",
"PCB清洗线机1", "PCB清洗线机2",
"上料裁切机1", "上料裁切机2",
"老化烘烤IR炉1", "老化烘烤IR炉2",
"ITO切割台5",
"自动分纸机械手1", "自动分纸机械手2",
"自动分片系统1", "自动分片系统2",
"LCD前段清洗机8",
"输送手臂6", "输送手臂7", "输送手臂8", "输送手臂9", "输送手臂10",
"55寸G+F+F贴合机3", "55寸G+F+F贴合机4",
"平台烤箱12", "平台烤箱13", "平台烤箱14", "平台烤箱15",
"110寸丝印机6", "110寸丝印机7",
"磁控溅射真空镀膜线5", "磁控溅射真空镀膜线6",
"ITO转向机6", "ITO转向机7", "ITO转向机8",
"ITO磨边机9", "ITO磨边机10", "ITO磨边机11", "ITO磨边机12",
"110邦定机5", "110邦定机6",
"传送带流水线4", "传送带流水线5",
"水洗机3", "水洗机4",
"下料覆膜机6", "下料覆膜机7",
"55寸网箱贴合机3",
"点胶机4", "点胶机5",
"110寸网箱贴合机3",
"刮胶机3", "刮胶机4",
"ITO切割台6", "ITO切割台7",
"LCD前段清洗机9", "LCD前段清洗机10",
"自动检测机6", "自动检测机7",
"110寸贴合机5", "110寸贴合机6",
"平台烤箱16", "平台烤箱17",
"镀膜前清洗机3",
"银浆烘烤IR炉3",
"ACF贴付机(FPC)3",
"自动上片机械手4", "自动上片机械手5",
"PCB清洗线机3",
"上料裁切机3",
"老化烘烤IR炉3",
"自动分纸机械手3",
"自动分片系统3",
"输送手臂11", "输送手臂12", "输送手臂13", "输送手臂14", "输送手臂15",
"55寸G+F+F贴合机5", "55寸G+F+F贴合机6",
"平台烤箱18", "平台烤箱19", "平台烤箱20",
"110寸丝印机8", "110寸丝印机9",
"磁控溅射真空镀膜线7",
"ITO转向机9", "ITO转向机10",
"ITO磨边机13", "ITO磨边机14", "ITO磨边机15", "ITO磨边机16",
"110邦定机7", "110邦定机8",
"传送带流水线6", "传送带流水线7",
"水洗机5",
"下料覆膜机8", "下料覆膜机9",
"点胶机6", "点胶机7",
"刮胶机5",
"ITO切割台8", "ITO切割台9",
"LCD前段清洗机11",
"自动检测机8", "自动检测机9",
"110寸贴合机7", "110寸贴合机8",
"镀膜前清洗机4",
"银浆烘烤IR炉4",
"ACF贴付机(FPC)4",
"自动上片机械手6",
"老化烘烤IR炉4",
"自动分纸机械手4",
"自动分片系统4",
"输送手臂16", "输送手臂17",
"平台烤箱21", "平台烤箱22",
"点胶机8"
);
log.info("【凯盛设备同步】开始同步设备信息, 设备数量: {}", deviceNameList.size());
List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
Random random = new Random();
for (int index = 0; index < deviceNameList.size(); index++) {
QxDeviceInfo qxDeviceInfo = new QxDeviceInfo();
qxDeviceInfo.setDeviceType("DIRECT_CONNECTION");
qxDeviceInfo.setTransportType("DEFAULT");
qxDeviceInfo.setOrganizationId(iotOrganizeId);
qxDeviceInfo.setDeviceProfileId(iotDeviceProfileId);
qxDeviceInfo.setProfileId(iotProfileId);
qxDeviceInfo.setLabel("生产设备");
String deviceName = deviceNameList.get(index);
qxDeviceInfo.setName(deviceName);
qxDeviceInfo.setBrand(deviceName);
String dtuSn = "AKS26518123" + String.format("%03d", index);
qxDeviceInfo.setSn(dtuSn);
DeviceToken deviceToken = new DeviceToken();
deviceToken.setCredentialsType("ACCESS_TOKEN");
deviceToken.setCredentialsId(dtuSn);
deviceToken.setCredentialsValue(dtuSn);
qxDeviceInfo.setDeviceToken(deviceToken);
qxDeviceInfos.add(qxDeviceInfo);
//有序列号直接获取灯信息
QxDeviceInfoDetail qxDeviceInfoDetail = new QxDeviceInfoDetail();
qxDeviceInfoDetail.setAlarm(false);
//灯状态(0:灭灯,1:红,2:黄,3:绿,4:蓝)
// Integer lampState = isWithinRange ? 3 : 0;
double r = random.nextDouble();
if (r < 0.08) {
qxDeviceInfoDetail.setStatus("OFF"); // 8% 状态0
} else {
qxDeviceInfoDetail.setStatus("RUN"); // 92% 状态3
}
// switch (lampState) {
// case 0:
// qxDeviceInfoDetail.setStatus("OFF");
// break;
//// case 1:
//// qxDeviceInfoDetail.setStatus("ERROR");
//// qxDeviceInfoDetail.setAlarm(true);
// break;
// case 2:
// qxDeviceInfoDetail.setStatus("STAND");
// break;
// case 3:
// qxDeviceInfoDetail.setStatus("RUN");
// break;
// default:
// continue;
// }
qxDeviceInfoDetail.setStartTime(new Date());
qxDeviceInfoDetail.setDtuSn(dtuSn);
qxAddDeviceInfoDetails.add(qxDeviceInfoDetail);
}
//将数据同步到IOT平台
Map<String, String> qxParam = new HashMap<>(2);
qxParam.put("username", iotUserName);
qxParam.put("password", iotPassword);
HttpPost qxHttpPost = new HttpPost(iotTokenUrl);
String qxResult = sendPost(qxHttpPost, JSON.toJSONString(qxParam));
log.info("【凯盛设备同步】获取token结果: {}", qxResult);
if (StringUtils.isBlank(qxResult)) {
log.error("【凯盛设备同步】获取token失败, 响应为空");
return;
}
Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<Map<String, Object>>() {
});
String qxAccessToken = (String) qxRes.get("token");
if (StringUtils.isBlank(qxAccessToken)) {
log.error("【凯盛设备同步】获取token失败, token为空, 响应内容: {}", qxResult);
return;
}
log.info("【凯盛设备同步】获取token成功");
BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
log.info("【凯盛设备同步】开始同步设备信息, 设备数: {}", qxDeviceInfos.size());
HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
qxDeviceInfoPost.addHeader(qxAuthorization);
for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
// todo
String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
log.info("【凯盛设备同步】同步设备信息, 设备名: {}, SN: {}, 结果: {}", qxDeviceInfo.getName(), qxDeviceInfo.getSn(), syncDeviceInfo);
}
log.info("【凯盛设备同步】同步设备信息完成");
}
if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
log.info("【凯盛设备同步】开始同步设备详情, 详情数: {}", qxAddDeviceInfoDetails.size());
for (QxDeviceInfoDetail qxDeviceInfoDetail : qxAddDeviceInfoDetails) {
String qxDeviceInfoDetailUrlStr = iotDeviceDetailUrl + qxDeviceInfoDetail.getDtuSn() + "/telemetry";
HttpPost qxDeviceInfoDetailPost = new HttpPost(qxDeviceInfoDetailUrlStr);
qxDeviceInfoDetailPost.addHeader(qxAuthorization);
String syncDeviceInfoDetail = sendPost(qxDeviceInfoDetailPost, JSON.toJSONString(qxDeviceInfoDetail));
log.info("【凯盛设备同步】同步设备详情, SN: {}, 状态: {}, 结果: {}", qxDeviceInfoDetail.getDtuSn(), qxDeviceInfoDetail.getStatus(), syncDeviceInfoDetail);
}
log.info("【凯盛设备同步】同步设备详情完成");
}
log.info("【凯盛设备同步】设备同步任务执行完毕");
}
private String sendPost(HttpPost httpPost, String jsonData) {
CloseableHttpClient httpClient = HttpClients.createDefault();
StringEntity entity = new StringEntity(jsonData, ContentType.create("application/json", Consts.UTF_8));
httpPost.setEntity(entity);
String result = null;
try {
CloseableHttpResponse execute = httpClient.execute(httpPost);
HttpEntity res = execute.getEntity();
InputStream is = res.getContent();
int len;
byte[] buf = new byte[128];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = is.read(buf)) != -1) {
byteArrayOutputStream.write(buf, 0, len);
}
result = byteArrayOutputStream.toString();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}