|
|
|
|
@ -2,8 +2,10 @@ package cc.iotkit.openapi.sync;
|
|
|
|
|
|
|
|
|
|
import cc.iotkit.manager.config.DeviceSyncConfig;
|
|
|
|
|
import cc.iotkit.manager.event.DeviceSyncEvent;
|
|
|
|
|
import cc.iotkit.model.device.DeviceInfo;
|
|
|
|
|
import cc.iotkit.system.dto.vo.SysTenantVo;
|
|
|
|
|
import cc.iotkit.system.service.ISysTenantService;
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
@ -30,12 +32,26 @@ public class DeviceSyncService {
|
|
|
|
|
private final OkHttpClient httpClient = new OkHttpClient.Builder().build();
|
|
|
|
|
|
|
|
|
|
public void sync(DeviceSyncEvent event) {
|
|
|
|
|
if (event == null || event.getDevice() == null) {
|
|
|
|
|
log.warn("设备同步事件缺少设备信息,跳过同步");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!config.isEnable()) {
|
|
|
|
|
log.info("设备同步功能未启用,跳过同步:operation={}, deviceId={}",
|
|
|
|
|
event.getOperation(), event.getDevice().getDeviceId());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 产品白名单过滤,只同步指定产品下的设备
|
|
|
|
|
if (ObjectUtil.isNotEmpty(config.getSyncProductKeys())) {
|
|
|
|
|
String productKey = event.getDevice().getProductKey();
|
|
|
|
|
if (!config.getSyncProductKeys().contains(productKey)) {
|
|
|
|
|
log.info("设备产品不在同步白名单内,跳过同步:operation={}, deviceId={}, productKey={}",
|
|
|
|
|
event.getOperation(), event.getDevice().getDeviceId(), productKey);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxRetry = config.getRetry();
|
|
|
|
|
for (int i = 0; i < maxRetry; i++) {
|
|
|
|
|
try {
|
|
|
|
|
@ -54,13 +70,17 @@ public class DeviceSyncService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doSync(DeviceSyncEvent event) throws Exception {
|
|
|
|
|
String deviceId = event.getDevice().getDeviceId();
|
|
|
|
|
String token = encrypt(deviceId, config.getEncryptKey());
|
|
|
|
|
DeviceInfo device = event.getDevice();
|
|
|
|
|
// 盐城要求 deviceCode 为设备 IMEI;FIGARO 设备 deviceName 即 IMEI
|
|
|
|
|
String deviceCode = config.isUseDeviceNameAsCode()
|
|
|
|
|
? device.getDeviceName()
|
|
|
|
|
: device.getDeviceId();
|
|
|
|
|
String token = encrypt(deviceCode, config.getEncryptKey());
|
|
|
|
|
|
|
|
|
|
if ("delete".equals(event.getOperation())) {
|
|
|
|
|
doDelete(deviceId, token);
|
|
|
|
|
doDelete(deviceCode, token);
|
|
|
|
|
} else {
|
|
|
|
|
doAddOrEdit(event, deviceId, token);
|
|
|
|
|
doAddOrEdit(event, deviceCode, token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -84,7 +104,8 @@ public class DeviceSyncService {
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
body.put("deviceCode", deviceId);
|
|
|
|
|
body.put("token", token);
|
|
|
|
|
body.put("deviceModel", event.getDevice().getModel());
|
|
|
|
|
// 已与甲方确认:deviceModel 传产品 key
|
|
|
|
|
body.put("deviceModel", event.getDevice().getProductKey());
|
|
|
|
|
body.put("longitude", event.getDevice().getLongitude());
|
|
|
|
|
body.put("latitude", event.getDevice().getLatitude());
|
|
|
|
|
body.put("districtName", "");
|
|
|
|
|
@ -139,6 +160,7 @@ public class DeviceSyncService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String encrypt(String message, String key) throws Exception {
|
|
|
|
|
// 加密方式与甲方提供的 DesUtil.java 一致:DES/CBC/PKCS5Padding,key 字节同时作为 IV,Base64 输出
|
|
|
|
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
|
|
|
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
|
|
|