TenantDeviceSyncComponent.java
3.16 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
package cn.com.glsx.admin.modules.component;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.context.utils.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.text.MessageFormat;
import java.util.List;
/**
* @author wangxw
* @version 1.0.0
* @ClassName TenantDeviceSyncComponent.java
* @description: 租户设备数据同步到数据平台 - 用于数据平台数据分发
* @createTime 2022年03月07日 19:47:00
*/
@Slf4j
@Component
public class TenantDeviceSyncComponent {
// 0关,1开
private static int valid_on = 1;
private static int valid_off = 0;
/**
* 添加同步设备
* @param sn
* @param tenantDataCode 数据平台编码
*/
@Async("threadPoolTaskExecutor")
public void addDevice(String sn, String tenantDataCode) {
if (StringUtils.isBlank(sn) || StringUtils.isBlank(tenantDataCode)) {
return;
}
invokeDataAPI(sn, tenantDataCode, valid_on);
}
/**
* 删除同步设备
* @param sn
* @param tenantDataCode 数据平台编码
*/
@Async("threadPoolTaskExecutor")
public void deleteDevice(String sn, String tenantDataCode) {
if (StringUtils.isBlank(sn) || StringUtils.isBlank(tenantDataCode)) {
return;
}
invokeDataAPI(sn, tenantDataCode, valid_off);
}
/**
* 批量添加设备
* @param snList
* @param tenantDataCode 数据平台编码
*/
@Async("threadPoolTaskExecutor")
public void addDeviceList(List<String> snList, String tenantDataCode) {
if (CollectionUtil.isEmpty(snList) || StringUtils.isBlank(tenantDataCode)) {
return;
}
for (String sn: snList) {
invokeDataAPI(sn, tenantDataCode, valid_on);
}
}
/**
* 批量删除设备
* @param snList
* @param tenantDataCode 数据平台编码
*/
@Async("threadPoolTaskExecutor")
public void deleteDeviceList(List<String> snList, String tenantDataCode) {
if (CollectionUtil.isEmpty(snList) || StringUtils.isBlank(tenantDataCode)) {
return;
}
for (String sn: snList) {
invokeDataAPI(sn, tenantDataCode, valid_off);
}
}
private void invokeDataAPI(String sn, String tenantDataCode, int valid) {
String modul = PropertiesUtils.getProperty("tenant.deivce.sync.datahourse.url");
String timeOutConfig = PropertiesUtils.getProperty("tenant.deivce.sync.datahourse.timeout");
int timeOut = (StringUtils.isNotBlank(timeOutConfig))?Integer.parseInt(timeOutConfig):3000;
try {
String url = MessageFormat.format(modul, sn, tenantDataCode, valid);
log.info("tenant device data sync to data url:{}", url);
String result = HttpUtil.get(url, timeOut);
log.info("tenant device data sync result:{}", result);
} catch (Exception e) {
log.error("tenant device data sync exception:{}", e.getMessage(), e);
}
}
}