XmlDevicePullService.java
10.4 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
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 XmlDevicePullService {
@Value("${xml.iot.organizeId:}")
private String iotOrganizeId;
@Value("${xml.iot.profileId}")
private String iotProfileId;
@Value("${xml.iot.deviceProfileId}")
private String iotDeviceProfileId;
@Value("${xml.iot.userName:}")
private String iotUserName;
@Value("${xml.iot.password:}")
private String iotPassword;
@Value("${xml.iot.tokenUrl}")
private String iotTokenUrl;
@Value("${xml.iot.infoUrl}")
private String iotDeviceInfoUrl;
@Value("${xml.iot.detailUrl}")
private String iotDeviceDetailUrl;
public void pullCreateDeviceInfo() {
List<String> deviceNameList = Arrays.asList(
"空压机1",
"空压机2", "空压机3", "空压机4",
"热水系统1",
"地磅1",
"充电桩1",
"污水在线监测系统1",
"压泥机1",
"磅房系统1",
"消防控制系统1",
"监控系统1",
"收放板机1", "收放板机2", "收放板机3", "收放板机4", "收放板机5", "收放板机6", "收放板机7", "收放板机8", "收放板机9", "收放板机10", "收放板机11", "收放板机12", "收放板机13", "收放板机14", "收放板机15", "收放板机16", "收放板机17", "收放板机18",
"提铜线1",
"纯水机1",
"环境抽风系统1", "环境抽风系统2", "环境抽风系统3", "环境抽风系统4", "环境抽风系统5", "环境抽风系统6", "环境抽风系统7",
"成品清洗线1",
"冰水机1", "冰水机2", "冰水机3",
"提锡线1",
"沉铜自动药水添加1",
"披峰机1",
"脱水机1",
"喷锡前处理线1",
"喷锡后处理线1",
"喷锡机1",
"智能电表1",
"气体报警仪1", "气体报警仪2", "气体报警仪3",
"阻焊LDI1",
"移载机1",
"烘箱1", "烘箱2", "烘箱3", "烘箱4", "烘箱5", "烘箱6", "烘箱7",
"薄膜压缩机1",
"手动压膜机1",
"中央空调主机1",
"丝印机1", "丝印机2", "丝印机3", "丝印机4", "丝印机5", "丝印机6", "丝印机7",
"光绘机1", "光绘机2",
"覆膜机1",
"打孔机1",
"压板翘机1",
"打包机1",
"真空包装机1",
"补线机1",
"数控钻机1",
"数控锣机1",
"销钉机1",
"除尘器1", "除尘器2",
"烘箱8", "烘箱9", "烘箱10", "烘箱11", "烘箱12",
"丝印机8", "丝印机9", "丝印机10", "丝印机11",
"收放板机19", "收放板机20", "收放板机21", "收放板机22", "收放板机23",
"环境抽风系统8", "环境抽风系统9", "环境抽风系统10",
"空压机5", "空压机6",
"冰水机4",
"气体报警仪4",
"光绘机3",
"打包机2",
"真空包装机2",
"补线机2",
"数控钻机2",
"数控锣机2",
"销钉机2",
"除尘器3",
"纯水机2",
"成品清洗线2",
"空压机8", "空压机9", "空压机10",
"冰水机7", "冰水机8",
"烘箱16", "烘箱17", "烘箱18", "烘箱19", "烘箱20",
"丝印机15", "丝印机16", "丝印机17", "丝印机18",
"收放板机27", "收放板机28", "收放板机29", "收放板机30",
"气体报警仪7", "气体报警仪8", "气体报警仪9",
"除尘器6", "除尘器7",
"数控钻机4", "数控钻机5",
"数控锣机4", "数控锣机5"
);
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 = "AXM26518123" + 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;
}
}