|
|
|
|
@ -0,0 +1,150 @@
|
|
|
|
|
package cc.iotkit.openapi.sync;
|
|
|
|
|
|
|
|
|
|
import cc.iotkit.manager.config.DeviceSyncConfig;
|
|
|
|
|
import cc.iotkit.manager.event.DeviceSyncEvent;
|
|
|
|
|
import cc.iotkit.system.dto.vo.SysTenantVo;
|
|
|
|
|
import cc.iotkit.system.service.ISysTenantService;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
|
import javax.crypto.SecretKeyFactory;
|
|
|
|
|
import javax.crypto.spec.DESKeySpec;
|
|
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class DeviceSyncService {
|
|
|
|
|
|
|
|
|
|
private final DeviceSyncConfig config;
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
private final ISysTenantService sysTenantService;
|
|
|
|
|
private final OkHttpClient httpClient = new OkHttpClient.Builder().build();
|
|
|
|
|
|
|
|
|
|
public void sync(DeviceSyncEvent event) {
|
|
|
|
|
if (!config.isEnable()) {
|
|
|
|
|
log.info("设备同步功能未启用,跳过同步:operation={}, deviceId={}",
|
|
|
|
|
event.getOperation(), event.getDevice().getDeviceId());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxRetry = config.getRetry();
|
|
|
|
|
for (int i = 0; i < maxRetry; i++) {
|
|
|
|
|
try {
|
|
|
|
|
doSync(event);
|
|
|
|
|
log.info("同步设备信息成功:operation={}, deviceId={}",
|
|
|
|
|
event.getOperation(), event.getDevice().getDeviceId());
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.warn("同步设备信息失败,第{}次:{}", (i + 1), e.getMessage());
|
|
|
|
|
if (i == maxRetry - 1) {
|
|
|
|
|
log.error("同步设备信息失败,已达重试上限:operation={}, deviceId={}",
|
|
|
|
|
event.getOperation(), event.getDevice().getDeviceId(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doSync(DeviceSyncEvent event) throws Exception {
|
|
|
|
|
String deviceId = event.getDevice().getDeviceId();
|
|
|
|
|
String token = encrypt(deviceId, config.getEncryptKey());
|
|
|
|
|
|
|
|
|
|
if ("delete".equals(event.getOperation())) {
|
|
|
|
|
doDelete(deviceId, token);
|
|
|
|
|
} else {
|
|
|
|
|
doAddOrEdit(event, deviceId, token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doDelete(String deviceId, String token) throws Exception {
|
|
|
|
|
String url = config.getDeleteUrl() + "?deviceCode=" + deviceId + "&token=" + token;
|
|
|
|
|
log.info("删除设备请求:url={}", url);
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new RuntimeException("删除设备失败,HTTP " + response.code());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doAddOrEdit(DeviceSyncEvent event, String deviceId, String token) throws Exception {
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
body.put("deviceCode", deviceId);
|
|
|
|
|
body.put("token", token);
|
|
|
|
|
body.put("deviceModel", event.getDevice().getModel());
|
|
|
|
|
body.put("longitude", event.getDevice().getLongitude());
|
|
|
|
|
body.put("latitude", event.getDevice().getLatitude());
|
|
|
|
|
body.put("districtName", "");
|
|
|
|
|
body.put("dockingCompany", config.getDockingCompany());
|
|
|
|
|
body.put("deviceAddress", event.getDevice().getSite());
|
|
|
|
|
|
|
|
|
|
Long tenantId = event.getDevice().getTenantId();
|
|
|
|
|
if (tenantId != null) {
|
|
|
|
|
try {
|
|
|
|
|
SysTenantVo tenant = sysTenantService.queryByTenantId(tenantId);
|
|
|
|
|
if (tenant != null) {
|
|
|
|
|
body.put("enterpriseName", tenant.getCompanyName());
|
|
|
|
|
body.put("enterpriseUser", tenant.getContactUserName());
|
|
|
|
|
body.put("phone", tenant.getContactPhone());
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("租户信息为空:tenantId={}", tenantId);
|
|
|
|
|
body.put("enterpriseName", "");
|
|
|
|
|
body.put("enterpriseUser", "");
|
|
|
|
|
body.put("phone", "");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.warn("查询租户信息异常:tenantId={}, error={}", tenantId, e.getMessage());
|
|
|
|
|
body.put("enterpriseName", "");
|
|
|
|
|
body.put("enterpriseUser", "");
|
|
|
|
|
body.put("phone", "");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
body.put("enterpriseName", "");
|
|
|
|
|
body.put("enterpriseUser", "");
|
|
|
|
|
body.put("phone", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body.put("enterpriseUser1", "");
|
|
|
|
|
body.put("phone1", "");
|
|
|
|
|
body.put("enterpriseUser2", "");
|
|
|
|
|
body.put("phone2", "");
|
|
|
|
|
|
|
|
|
|
log.info("同步设备请求:operation={}, url={}, body={}",
|
|
|
|
|
event.getOperation(), config.getDeviceUrl(), body);
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(config.getDeviceUrl())
|
|
|
|
|
.post(RequestBody.create(objectMapper.writeValueAsString(body),
|
|
|
|
|
MediaType.parse("application/json")))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new RuntimeException("同步设备失败,HTTP " + response.code());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String encrypt(String message, String key) throws Exception {
|
|
|
|
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
|
|
|
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
|
|
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
|
|
|
|
IvParameterSpec iv = new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
|
|
|
|
|
return java.util.Base64.getEncoder().encodeToString(cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
}
|
|
|
|
|
}
|