TongChiDevicePullService.java
11.3 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
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 TongChiDevicePullService {
@Value("${tc.iot.organizeId:}")
private String iotOrganizeId;
@Value("${tc.iot.profileId}")
private String iotProfileId;
@Value("${tc.iot.deviceProfileId}")
private String iotDeviceProfileId;
@Value("${tc.iot.userName:}")
private String iotUserName;
@Value("${tc.iot.password:}")
private String iotPassword;
@Value("${tc.iot.tokenUrl}")
private String iotTokenUrl;
@Value("${tc.iot.infoUrl}")
private String iotDeviceInfoUrl;
@Value("${tc.iot.detailUrl}")
private String iotDeviceDetailUrl;
public void pullCreateDeviceInfo() {
List<String> deviceIdList = Arrays.asList(
"组合机2", "组合机3", "组合机5", "组合机7",
"fog邦定机4", "fog邦定机5", "fog邦定机7", "fog邦定机8", "fog邦定机9",
"混胶机3", "混胶机4", "混胶机6",
"印刷机6", "印刷机7", "印刷机9",
"acf邦定机3", "acf邦定机4", "acf邦定机6", "acf邦定机8",
"雕刻机盛雄3", "雕刻机盛雄4", "雕刻机盛雄6", "雕刻机盛雄7",
"吉事达雕刻机3", "吉事达雕刻机4", "吉事达雕刻机6",
"点刮胶机2", "点刮胶机3", "点刮胶机5",
"数控模切机5", "数控模切机6", "数控模切机8", "数控模切机9",
"贴合机8", "贴合机9", "贴合机11", "贴合机12", "贴合机14",
"ccd异形切割机4", "ccd异形切割机5", "ccd异形切割机7", "ccd异形切割机9",
"双卫邦定机3", "双卫邦定机5", "双卫邦定机6", "双卫邦定机8",
"sca贴合机3", "sca贴合机4", "sca贴合机6",
"激光切割机3", "激光切割机5", "激光切割机7", "激光切割机9",
"对位贴合机2", "对位贴合机4", "对位贴合机6", "对位贴合机8",
"覆膜机4", "覆膜机5", "覆膜机7", "覆膜机9",
"卷膜机3", "卷膜机4", "卷膜机6",
"脱泡机5", "脱泡机6", "脱泡机8", "脱泡机9",
"翻板贴合03", "翻板贴合05", "翻板贴合07", "翻板贴合09",
"烤箱13", "烤箱14", "烤箱16", "烤箱18", "烤箱20",
"点胶刮胶一体机3", "点胶刮胶一体机4", "点胶刮胶一体机6",
"冲压机2", "冲压机4", "冲压机6", "冲压机8",
"开料机3", "开料机5", "开料机7",
"背光阻力机3", "背光阻力机4", "背光阻力机6",
"贴膜机2", "贴膜机4", "贴膜机6", "贴膜机8",
"全自动贴合线4", "全自动贴合线5",
"粒子检测机2", "粒子检测机3",
"等离子清洗机2",
"老化烤箱5", "老化烤箱6", "老化烤箱8",
"光学检测机3", "光学检测机4",
"全自动线5", "全自动线6", "全自动线8",
"华科力达贴合机2",
"胶层烘烤3", "胶层烘烤4",
"二次元机2",
"除尘机2",
"标签机2",
"预压机2",
"翻板压合2",
"清洗机2",
"邦定机6", "邦定机7", "邦定机9",
"测试机04", "测试机05", "测试机07",
"UV机04", "UV机05",
"切割机06", "切割机07", "切割机09",
"卷膜机02-2",
"覆膜机10",
"烤箱21", "烤箱22",
"组合机11", "组合机13",
"fog邦定机12", "fog邦定机14", "fog邦定机15",
"混胶机7", "混胶机9",
"印刷机11", "印刷机13",
"acf邦定机9", "acf邦定机11",
"雕刻机盛雄9", "雕刻机盛雄12",
"吉事达雕刻机7", "吉事达雕刻机9",
"点刮胶机6", "点刮胶机8",
"数控模切机11", "数控模切机13",
"贴合机15", "贴合机17", "贴合机19",
"ccd异形切割机11", "ccd异形切割机13",
"双卫邦定机9", "双卫邦定机11",
"sca贴合机7", "sca贴合机9",
"激光切割机11", "激光切割机13",
"对位贴合机9", "对位贴合机11",
"覆膜机11", "覆膜机13",
"卷膜机7", "卷膜机9",
"脱泡机11", "脱泡机13",
"翻板贴合11", "翻板贴合13",
"烤箱24", "烤箱26", "烤箱28",
"点胶刮胶一体机7", "点胶刮胶一体机9",
"冲压机9", "冲压机11",
"开料机8", "开料机11",
"背光阻力机7", "背光阻力机9",
"贴膜机9", "贴膜机11",
"全自动贴合线6",
"粒子检测机5",
"老化烤箱9"
);
List<QxDeviceInfo> qxDeviceInfos = new ArrayList<>();
List<QxDeviceInfoDetail> qxAddDeviceInfoDetails = new ArrayList<>();
Random random = new Random();
for (int index = 0; index < deviceIdList.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 = deviceIdList.get(index);
qxDeviceInfo.setName(deviceName);
qxDeviceInfo.setBrand(deviceName);
String dtuSn = "ATC26518123" + 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.05) {
qxDeviceInfoDetail.setStatus("OFF"); // 5% 状态0
} else if (r < 0.10) {
qxDeviceInfoDetail.setStatus("STAND"); // 5% 状态2(跳过状态1)
} else {
qxDeviceInfoDetail.setStatus("RUN"); // 90% 状态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));
if (StringUtils.isBlank(qxResult)) {
return;
}
Map<String, Object> qxRes = JSON.parseObject(qxResult, new TypeReference<Map<String, Object>>() {
});
String qxAccessToken = (String) qxRes.get("token");
if (StringUtils.isBlank(qxAccessToken)) {
return;
}
BasicHeader qxAuthorization = new BasicHeader("X-Authorization", "Bearer " + qxAccessToken);
if (!CollectionUtils.isEmpty(qxDeviceInfos)) {
HttpPost qxDeviceInfoPost = new HttpPost(iotDeviceInfoUrl);
qxDeviceInfoPost.addHeader(qxAuthorization);
for (QxDeviceInfo qxDeviceInfo : qxDeviceInfos) {
// todo
String syncDeviceInfo = sendPost(qxDeviceInfoPost, JSON.toJSONString(qxDeviceInfo));
//log.info("同步设备信息 syncDeviceInfo:{}", syncDeviceInfo);
}
}
if (!CollectionUtils.isEmpty(qxAddDeviceInfoDetails)) {
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));
}
}
}
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;
}
}