|
|
|
|
@ -1,46 +1,35 @@ |
|
|
|
|
/** |
|
|
|
|
* BladeX Commercial License Agreement |
|
|
|
|
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
|
|
|
|
* <p> |
|
|
|
|
* Use of this software is governed by the Commercial License Agreement |
|
|
|
|
* obtained after purchasing a license from BladeX. |
|
|
|
|
* <p> |
|
|
|
|
* 1. This software is for development use only under a valid license |
|
|
|
|
* from BladeX. |
|
|
|
|
* <p> |
|
|
|
|
* 2. Redistribution of this software's source code to any third party |
|
|
|
|
* without a commercial license is strictly prohibited. |
|
|
|
|
* <p> |
|
|
|
|
* 3. Licensees may copyright their own code but cannot use segments |
|
|
|
|
* from this software for such purposes. Copyright of this software |
|
|
|
|
* remains with BladeX. |
|
|
|
|
* <p> |
|
|
|
|
* Using this software signifies agreement to this License, and the software |
|
|
|
|
* must not be used for illegal purposes. |
|
|
|
|
* <p> |
|
|
|
|
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is |
|
|
|
|
* not liable for any claims arising from secondary or illegal development. |
|
|
|
|
* <p> |
|
|
|
|
* Author: Chill Zhuang (bladejava@qq.com) |
|
|
|
|
*/ |
|
|
|
|
package org.springblade.desk.device.service.impl; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.alibaba.nacos.common.utils.CollectionUtils; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import jodd.util.StringUtil; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.springblade.desk.device.pojo.entity.EquipmentEntity; |
|
|
|
|
import org.springblade.desk.device.pojo.excel.EquipmentExcel; |
|
|
|
|
import org.springblade.desk.device.pojo.request.MeasuringUsageQuery; |
|
|
|
|
import org.springblade.desk.device.pojo.vo.EquipmentStatusStatisticsVO; |
|
|
|
|
import org.springblade.desk.device.pojo.vo.EquipmentVO; |
|
|
|
|
import org.springblade.desk.device.mapper.EquipmentMapper; |
|
|
|
|
import org.springblade.desk.device.service.IEquipmentService; |
|
|
|
|
import org.springblade.desk.jobtransfer.pojo.entity.CertificateTypeEntity; |
|
|
|
|
import org.springblade.system.cache.DictCache; |
|
|
|
|
import org.springblade.system.pojo.enums.DictEnum; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
|
import org.springblade.core.mp.base.BaseServiceImpl; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.net.http.HttpClient; |
|
|
|
|
import java.net.http.HttpRequest; |
|
|
|
|
import java.net.http.HttpResponse; |
|
|
|
|
import java.time.Duration; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 设备信息表 服务实现类 |
|
|
|
|
@ -48,9 +37,25 @@ import java.util.Objects; |
|
|
|
|
* @author qyl |
|
|
|
|
* @since 2025-12-26 |
|
|
|
|
*/ |
|
|
|
|
@Slf4j |
|
|
|
|
@Service |
|
|
|
|
public class EquipmentServiceImpl extends BaseServiceImpl<EquipmentMapper, EquipmentEntity> implements IEquipmentService { |
|
|
|
|
@Value("${request.iot.orgId}") |
|
|
|
|
private String orgId; |
|
|
|
|
|
|
|
|
|
@Value("${request.iot.systemId}") |
|
|
|
|
private String systemId; |
|
|
|
|
|
|
|
|
|
@Value("${request.iot.url}") |
|
|
|
|
private String iotUrl; |
|
|
|
|
|
|
|
|
|
@Value("${request.iotNew.url}") |
|
|
|
|
private String iotNewUrl; |
|
|
|
|
// 使用JDK 17内置的HttpClient(线程安全,可复用)
|
|
|
|
|
private final HttpClient httpClient = HttpClient.newBuilder() |
|
|
|
|
.connectTimeout(Duration.ofSeconds(30)) |
|
|
|
|
.version(HttpClient.Version.HTTP_2) |
|
|
|
|
.build(); |
|
|
|
|
@Override |
|
|
|
|
public IPage<EquipmentEntity> selectEquipmentPage(IPage<EquipmentEntity> page, EquipmentEntity equipment) { |
|
|
|
|
// return page.setRecords(baseMapper.selectEquipmentPage(page, equipment));
|
|
|
|
|
@ -95,4 +100,84 @@ public class EquipmentServiceImpl extends BaseServiceImpl<EquipmentMapper, Equip |
|
|
|
|
return this.updateById(equipment); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void regularlyUpdated() { |
|
|
|
|
log.debug("开始同步IOT设备数据"); |
|
|
|
|
// 构建请求体JSON
|
|
|
|
|
JSONObject requestBody = buildRequestBody(); |
|
|
|
|
// 创建HTTP请求
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder() |
|
|
|
|
.uri(URI.create(iotNewUrl + "/deviceForZhgd/deviceListQuery")) |
|
|
|
|
.header("Content-Type", "application/json") |
|
|
|
|
.header("Accept", "application/json") |
|
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toJSONString())) |
|
|
|
|
.timeout(Duration.ofSeconds(60)) |
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// 发送同步请求
|
|
|
|
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
|
|
|
|
// 处理响应
|
|
|
|
|
String responseBody = response.body(); |
|
|
|
|
JSONObject result = JSONObject.parseObject(responseBody); |
|
|
|
|
log.debug("同步IOT设备数据: {}", result.toJSONString()); |
|
|
|
|
if (result != null && result.getInteger("code").equals(0)) { |
|
|
|
|
List<JSONObject> jsonList = JSONArray.parseArray(result.getJSONArray("result").toJSONString(), JSONObject.class); |
|
|
|
|
if (jsonList != null && jsonList.size() > 0) { |
|
|
|
|
//接口返回设备id即为本系统code
|
|
|
|
|
Map<String, JSONObject> iotIdObjectMap = jsonList.stream().collect(Collectors.toMap( |
|
|
|
|
e -> e.getString("id"), |
|
|
|
|
e -> e)); |
|
|
|
|
Set<String> iotIds = iotIdObjectMap.keySet(); |
|
|
|
|
if (iotIds == null || iotIds.size() < 1) { |
|
|
|
|
log.error("同步IOT设备数据接口返回数据为空"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
LambdaQueryWrapper<EquipmentEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(iotIds), EquipmentEntity::getDeviceCode, iotIds); |
|
|
|
|
List<EquipmentEntity> equipmentEntities = this.list(lambdaQueryWrapper); |
|
|
|
|
Map<String, EquipmentEntity> equipmentEntityMap = new HashMap<>(); |
|
|
|
|
if (null != equipmentEntities && equipmentEntities.size() > 0) { |
|
|
|
|
equipmentEntities.stream().collect(Collectors.toMap( |
|
|
|
|
e -> e.getDeviceCode(), |
|
|
|
|
e -> e)); |
|
|
|
|
} |
|
|
|
|
List<EquipmentEntity> saves = new ArrayList<>(); |
|
|
|
|
for (JSONObject jsonObject : jsonList) { |
|
|
|
|
EquipmentEntity equipment = equipmentEntityMap.get(jsonObject.getString("id")); |
|
|
|
|
equipment = Optional.ofNullable(equipment).orElse(new EquipmentEntity()); |
|
|
|
|
equipment.setDeviceCode(jsonObject.getString("id")); |
|
|
|
|
equipment.setDeviceName(jsonObject.getString("name")); |
|
|
|
|
equipment.setTypeName(jsonObject.getString("productName")); |
|
|
|
|
JSONObject state = jsonObject.getJSONObject("state"); |
|
|
|
|
equipment.setUsed("在线".equals(state.getString("text")) ? EquipmentEntity.USED_OK : EquipmentEntity.USED_PAUSE); |
|
|
|
|
equipment.setDocking("1"); |
|
|
|
|
equipment.setMemo(jsonObject.getString("describe")); |
|
|
|
|
equipment.setCategorys(jsonObject.getString("category")); |
|
|
|
|
saves.add(equipment); |
|
|
|
|
} |
|
|
|
|
boolean saved = this.saveOrUpdateBatch(saves); |
|
|
|
|
if (saved) { |
|
|
|
|
log.debug("同步IOT设备数据成功"); |
|
|
|
|
} else { |
|
|
|
|
log.error("同步IOT设备数据失败"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("同步IOT设备数据接口调用失败: {}", e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 构建请求体 |
|
|
|
|
*/ |
|
|
|
|
private JSONObject buildRequestBody() { |
|
|
|
|
JSONObject requestBody = new JSONObject(); |
|
|
|
|
requestBody.put("orgId", orgId); |
|
|
|
|
requestBody.put("systemId", systemId); |
|
|
|
|
log.debug("同步IOT设备数据请求参数: {}", requestBody.toJSONString()); |
|
|
|
|
return requestBody; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|