|
@@ -1,5 +1,6 @@
|
|
|
package com.xy.service;
|
|
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -7,14 +8,9 @@ import com.xy.config.DeviceThreadPoolConfig;
|
|
|
import com.xy.dbmapper.SyncUpdate;
|
|
|
import com.xy.device.EnumDevcieDoorStatus;
|
|
|
import com.xy.device.EnumDeviceTempConfig;
|
|
|
-import com.xy.dto.ActivityInfoDto;
|
|
|
-import com.xy.dto.DeviceEventMsgDto;
|
|
|
-import com.xy.dto.DeviceStatusDto;
|
|
|
-import com.xy.dto.DeviceTempRecordsDto;
|
|
|
-import com.xy.entity.DeviceConfig;
|
|
|
-import com.xy.entity.DeviceStatus;
|
|
|
-import com.xy.entity.DeviceSysinfo;
|
|
|
-import com.xy.entity.SysDictRedis;
|
|
|
+import com.xy.device.EnumMqttCmdTempletTask;
|
|
|
+import com.xy.dto.*;
|
|
|
+import com.xy.entity.*;
|
|
|
import com.xy.mapper.DeviceStatusMapper;
|
|
|
import com.xy.mapper.entity.DeviceStatusCount;
|
|
|
import com.xy.utils.*;
|
|
@@ -23,12 +19,14 @@ import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -42,9 +40,9 @@ import static com.xy.utils.Beans.copy;
|
|
|
* @author lijin
|
|
|
* @since 2022-12-23
|
|
|
*/
|
|
|
-@Service
|
|
|
-@AllArgsConstructor
|
|
|
@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor(onConstructor_ = @Lazy)
|
|
|
@Api(tags = "设备-状态")
|
|
|
public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, DeviceStatus> implements DeviceStatusService {
|
|
|
|
|
@@ -60,91 +58,124 @@ public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, Dev
|
|
|
|
|
|
private DeviceSysinfoServiceImpl deviceSysinfoService;
|
|
|
|
|
|
+ private DeviceVersionUpServiceImpl deviceVersionUpService;
|
|
|
+
|
|
|
+ private RedisService<DeviceVersionUpServiceImpl.UpInfo> upInfoRedisService;
|
|
|
+
|
|
|
+ private MqttServiceImpl mqttService;
|
|
|
+
|
|
|
@Override
|
|
|
@ApiOperation("上报信息")
|
|
|
public R up(DeviceStatusDto.Up up) {
|
|
|
log.info("设备信息上报:{}", JSONUtil.toJsonStr(up));
|
|
|
- DeviceStatus deviceStatusInfo = getById(up.getDeviceId());
|
|
|
- if (deviceStatusInfo == null) {
|
|
|
+ DeviceStatus deviceStatus = getById(up.getDeviceId());
|
|
|
+ if (deviceStatus == null) {
|
|
|
return R.ok();
|
|
|
}
|
|
|
- DeviceStatus deviceStatus = copy(DeviceStatus.class, up).setUpdateTime(LocalDateTime.now());
|
|
|
- updateById(deviceStatus);
|
|
|
- Integer tempState = up.getTempState();
|
|
|
- //故障
|
|
|
- boolean isRisk = false;
|
|
|
- //温控仪异常
|
|
|
- if (tempState != null && tempState.intValue() != 0) {
|
|
|
- isRisk = true;
|
|
|
- }
|
|
|
- //温度上报
|
|
|
- if (Emptys.check(deviceStatus.getTempValue())) {
|
|
|
- DeviceSysinfo deviceSysinfo = deviceSysinfoService.getById(up.getDeviceId());
|
|
|
- //添加设备温度日志
|
|
|
- if (deviceSysinfo.getIsHaveTemp()) {
|
|
|
- DeviceTempRecordsDto.Save save = new DeviceTempRecordsDto.Save()
|
|
|
- .setDeviceId(up.getDeviceId());
|
|
|
- save.setTempValue(isRisk ? null : deviceStatus.getTempValue());
|
|
|
- deviceTempRecordsService.save(save);
|
|
|
- //查询设备配置
|
|
|
- DeviceConfig deviceConfig = deviceConfigService.getById(deviceStatus.getDeviceId());
|
|
|
- int tempMax, tempMin;
|
|
|
- if (deviceConfig == null) {
|
|
|
- Map<String, SysDictRedis> stringSysDictRedisMap = SysDictUtils.get(EnumDeviceTempConfig.Code.CODE.getCode());
|
|
|
- tempMax = Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceTempConfig.MAX.getCode()).getValue());
|
|
|
- tempMin = Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceTempConfig.MIN.getCode()).getValue());
|
|
|
- } else {
|
|
|
- tempMax = deviceConfig.getTempMax();
|
|
|
- tempMin = deviceConfig.getTempMin();
|
|
|
- }
|
|
|
- //温度有异常
|
|
|
- String key = String.format("device_temp_error:%s:%s", deviceStatus.getDeviceId(), deviceStatus.getTempValue() >= tempMax ? "max" : "min");
|
|
|
- if (deviceStatus.getTempValue() > tempMax || deviceStatus.getTempValue() < tempMin) {
|
|
|
- //校验异常次数
|
|
|
- Integer errorSize = SysDictUtils.getValue(EnumDeviceTempConfig.Code.CODE.getCode(), EnumDeviceTempConfig.ERROR_SIZE.getCode(), Integer.class);
|
|
|
- boolean fal = false;
|
|
|
- Integer maxSize = redisService.get(key);
|
|
|
- if (maxSize == null) {
|
|
|
- redisService.set(key, 1, 60 * 60);
|
|
|
+ ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_STATUS_UP, 4)
|
|
|
+ .execute(() -> {
|
|
|
+ //修改状态信息
|
|
|
+ updateById(copy(DeviceStatus.class, up).setUpdateTime(LocalDateTime.now()));
|
|
|
+ })
|
|
|
+ .execute(() -> {
|
|
|
+ //上报关门
|
|
|
+ if (!Emptys.check(deviceStatus.getDoorStateL())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Integer value = SysDictUtils.getValue(EnumDevcieDoorStatus.Code.CODE.getCode(), EnumDevcieDoorStatus.LOCKED.getCode(), Integer.class);
|
|
|
+ if (!deviceStatus.getDoorStateL().equals(value)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ activityInfoService.abort(new ActivityInfoDto.Abort()
|
|
|
+ .setDeviceId(deviceStatus.getDeviceId())
|
|
|
+ .setWorkType(2)
|
|
|
+ .setTimeout(60 * 1)
|
|
|
+ );
|
|
|
+ })
|
|
|
+ .execute(() -> {
|
|
|
+ //温度上报
|
|
|
+ if (!Emptys.check(up.getTempValue())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DeviceSysinfo deviceSysinfo = deviceSysinfoService.getById(deviceStatus.getDeviceId());
|
|
|
+ //添加设备温度日志
|
|
|
+ if (!deviceSysinfo.getIsHaveTemp()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DeviceTempRecordsDto.Save save = new DeviceTempRecordsDto.Save()
|
|
|
+ .setDeviceId(deviceStatus.getDeviceId());
|
|
|
+ save.setTempValue(up.getTempValue());
|
|
|
+ deviceTempRecordsService.save(save);
|
|
|
+ //查询设备配置
|
|
|
+ DeviceConfig deviceConfig = deviceConfigService.getById(deviceStatus.getDeviceId());
|
|
|
+ int tempMax, tempMin;
|
|
|
+ if (deviceConfig == null) {
|
|
|
+ Map<String, SysDictRedis> stringSysDictRedisMap = SysDictUtils.get(EnumDeviceTempConfig.Code.CODE.getCode());
|
|
|
+ tempMax = Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceTempConfig.MAX.getCode()).getValue());
|
|
|
+ tempMin = Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceTempConfig.MIN.getCode()).getValue());
|
|
|
} else {
|
|
|
+ tempMax = deviceConfig.getTempMax();
|
|
|
+ tempMin = deviceConfig.getTempMin();
|
|
|
+ }
|
|
|
+ //温度有异常
|
|
|
+ String key = String.format("device_temp_error:%d", deviceStatus.getDeviceId());
|
|
|
+ if (up.getTempValue() > tempMax || up.getTempValue() < tempMin) {
|
|
|
+ //校验异常次数
|
|
|
+ Integer errorSize = SysDictUtils.getValue(EnumDeviceTempConfig.Code.CODE.getCode(), EnumDeviceTempConfig.ERROR_SIZE.getCode(), Integer.class);
|
|
|
+ Integer maxSize = redisService.get(key);
|
|
|
+ maxSize = maxSize == null ? 1 : maxSize;
|
|
|
if (maxSize >= errorSize) {
|
|
|
- fal = true;
|
|
|
+ //添加事件
|
|
|
+ String msg = "温度异常-温度阈值%d - %d,当前温度%d";
|
|
|
+ DeviceEventMsgDto.Save deviceEventMsg = new DeviceEventMsgDto.Save()
|
|
|
+ .setDeviceId(deviceStatus.getDeviceId());
|
|
|
+ deviceEventMsg.setCode(DeviceErrorRecordTypesEnum.T.getCode());
|
|
|
+ deviceEventMsg.setMsg(String.format(msg, tempMin, tempMax, up.getTempValue()));
|
|
|
+ deviceEventMsgService.save(deviceEventMsg);
|
|
|
redisService.remove(key);
|
|
|
- } else {
|
|
|
- redisService.set(key, maxSize + 1, 60 * 60);
|
|
|
- }
|
|
|
- }
|
|
|
- //添加事件
|
|
|
- if (fal) {
|
|
|
- DeviceEventMsgDto.Save deviceEventMsg = new DeviceEventMsgDto.Save()
|
|
|
- .setDeviceId(deviceStatus.getDeviceId());
|
|
|
- deviceEventMsg.setCode(DeviceErrorRecordTypesEnum.T.getCode());
|
|
|
- String msg = "温度异常-温度阈值%d,当前温度%d";
|
|
|
- if (deviceStatus.getTempValue() >= tempMax) {
|
|
|
- deviceEventMsg.setMsg(String.format(msg, tempMax, deviceStatus.getTempValue()));
|
|
|
- }
|
|
|
- if (deviceStatus.getTempValue() <= tempMin) {
|
|
|
- deviceEventMsg.setMsg(String.format(msg, tempMin, deviceStatus.getTempValue()));
|
|
|
+ return;
|
|
|
}
|
|
|
- deviceEventMsgService.save(deviceEventMsg);
|
|
|
+ redisService.set(key, maxSize + 1, 60 * 60);
|
|
|
+ return;
|
|
|
}
|
|
|
- } else {
|
|
|
redisService.remove(key);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //上报关门
|
|
|
- if (Emptys.check(deviceStatus.getDoorStateL())) {
|
|
|
- Integer value = SysDictUtils.getValue(EnumDevcieDoorStatus.Code.CODE.getCode(), EnumDevcieDoorStatus.LOCKED.getCode(), Integer.class);
|
|
|
- if (deviceStatus.getDoorStateL().equals(value)) {
|
|
|
- ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, 1)
|
|
|
- .execute(() -> activityInfoService.abort(new ActivityInfoDto.Abort()
|
|
|
- .setDeviceId(up.getDeviceId())
|
|
|
- .setWorkType(2)
|
|
|
- .setTimeout(60 * 1)
|
|
|
- ));
|
|
|
- }
|
|
|
- }
|
|
|
+ })
|
|
|
+ .execute(() -> {
|
|
|
+ //检查是否可升级
|
|
|
+ if (!Emptys.check(up.getIsUpVersion()) || !up.getIsUpVersion()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DeviceVersionUpServiceImpl.UpInfo upInfo = upInfoRedisService.get(DeviceVersionUpServiceImpl.upInfoKey);
|
|
|
+ if (upInfo == null || !upInfo.getEnable()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<DeviceVersionUp> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceVersionUp>()
|
|
|
+ .eq(DeviceVersionUp::getDeviceId, deviceStatus.getDeviceId())
|
|
|
+ .ge(DeviceVersionUp::getDeviceVersion, upInfo.getVersion())
|
|
|
+ .eq(DeviceVersionUp::getStatus, 4)
|
|
|
+ .eq(DeviceVersionUp::getProgress, 100);
|
|
|
+ long count = deviceVersionUpService.count(lambdaQueryWrapper);
|
|
|
+ if (count > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //下发升级指令
|
|
|
+ String templetStr = SysDictUtils.getValue(EnumMqttCmdTempletTask.Code.CODE.getCode(), EnumMqttCmdTempletTask.APPUPDATE.getCode(), String.class);
|
|
|
+ String appId = LambdaUtils.getProperty(DeviceVersionUpServiceImpl.UpInfo::getAppId);
|
|
|
+ String appKey = LambdaUtils.getProperty(DeviceVersionUpServiceImpl.UpInfo::getAppKey);
|
|
|
+ String version = LambdaUtils.getProperty(DeviceVersionUpServiceImpl.UpInfo::getVersion);
|
|
|
+ String src = LambdaUtils.getProperty(DeviceVersionUpServiceImpl.UpInfo::getSrc);
|
|
|
+ JSONObject templet = JSONUtil.parseObj(templetStr);
|
|
|
+ JSONObject data = templet.getJSONObject("data")
|
|
|
+ .set(appId, upInfo.getAppId())
|
|
|
+ .set(appKey, upInfo.getAppKey())
|
|
|
+ .set(version, upInfo.getVersion())
|
|
|
+ .set(src, upInfo.getSrc());
|
|
|
+ templet.set("data", data);
|
|
|
+ CommandMqtt commandMqtt = new CommandMqtt()
|
|
|
+ .setDeviceId(deviceStatus.getDeviceId())
|
|
|
+ .setTemplet(templet);
|
|
|
+ mqttService.senCommand(Arrays.asList(commandMqtt));
|
|
|
+ });
|
|
|
return R.ok();
|
|
|
}
|
|
|
|