XingheDeviceService.java
4.39 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
package com.viontech.integration.jiaoguansuo.service;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.integration.jiaoguansuo.entity.VideoEntity;
import com.viontech.integration.jiaoguansuo.model.DeviceDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
/**
* msl
* 星河平台:设备服务
*/
@Service
@Slf4j
public class XingheDeviceService {
@Value("${xinghe.device.syncSwitch:0}")
private String deviceSyncSwitch;
@Value("${xinghe.device.addUrl:}")
private String deviceAddUrl;
@Value("${xinghe.device.delUrl:}")
private String deviceDelUrl;
@Value("${xinghe.device.queryUrl:}")
private String deviceQueryUrl;
@Resource
private RestTemplate restTemplate;
@Autowired
private ObjectMapper objectMapper;
/**
* 添加设备
*
* @param addVideo
*/
public DeviceDO addDevice(VideoEntity addVideo) {
if (!"1".equals(deviceSyncSwitch)) {
return null;
}
if (StringUtils.isBlank(deviceAddUrl)) {
log.warn("deviceAddUrl is null");
return null;
}
DeviceDO deviceDO = new DeviceDO();
try {
deviceDO.setSbcj("02");
deviceDO.setJkdId(0);
deviceDO.setSbmc("无锡所ADD");
deviceDO.setSbip("0.0.0.0");
deviceDO.setSbbh(addVideo.getSxjbh());
deviceDO.setSbldz(addVideo.getJkdz());
log.info("addDevice.data={}", JSON.toJSONString(deviceDO));
//保存设备信息
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
HttpEntity<DeviceDO> httpEntity = new HttpEntity<>(deviceDO, headers);
ResponseEntity<Map> resEntity = restTemplate.exchange(deviceAddUrl, HttpMethod.PUT, httpEntity, Map.class);
log.info("addDevice.result={}", JSON.toJSONString(resEntity.getBody()));
} catch (Exception e) {
log.error("addDevice.Exception", e);
}
return deviceDO;
}
/**
* 删除设备
*
* @param delVideo
*/
public void deleteDevice(VideoEntity delVideo) {
if (!"1".equals(deviceSyncSwitch)) {
return;
}
if (StringUtils.isBlank(delVideo.getSxjbh())) {
log.warn("sxjbh is null");
return;
}
if (StringUtils.isBlank(deviceQueryUrl) || StringUtils.isBlank(deviceDelUrl)) {
log.warn("deviceQueryUrl,deviceDelUrl is null");
return;
}
//查询出设备
Integer deviceId = null;
try {
String requestUrl = deviceQueryUrl + "?pageSize=50&pageNum=1&sbbh=" + delVideo.getSxjbh();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(URLDecoder.decode(requestUrl, "UTF-8"), String.class);
Map resultMap = objectMapper.readValue(responseEntity.getBody(), Map.class);
if (resultMap.containsKey("data") && resultMap.get("data") != null) {
Map map = (Map) resultMap.get("data");
if (map.get("list") != null) {
List<Map> mapList = (List<Map>) map.get("list");
for (Map deviceMap : mapList) {
if (delVideo.getSxjbh().equals(deviceMap.get("sbbh"))) {
deviceId = (Integer) deviceMap.get("id");
break;
}
}
}
}
log.info("deleteDevice.id={}", deviceId);
if (deviceId == null || deviceId <= 0) {
return;
}
//删除设备信息
String delUrl = String.format(deviceDelUrl, deviceId + "");
restTemplate.delete(delUrl);
log.info("deleteDevice.id={},success", deviceId);
} catch (Exception e) {
log.error("deleteDevice.Exception", e);
}
return;
}
}