|
@@ -0,0 +1,110 @@
|
|
|
+package com.xy.alipay;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.xy.consumer.disconnect.DisconnectedMqttConfiguration;
|
|
|
+import com.xy.consumer.disconnect.DisconnectedProducer;
|
|
|
+import com.xy.dto.DeviceErrorsRecordDto;
|
|
|
+import com.xy.dto.spi.DeviceAlarmNotifyDTO;
|
|
|
+import com.xy.dto.spi.DeviceStatusChangeNotifyDTO;
|
|
|
+import com.xy.entity.DeviceCharging;
|
|
|
+import com.xy.entity.DeviceInfo;
|
|
|
+import com.xy.entity.SysDictRedis;
|
|
|
+import com.xy.service.DeviceChargingServiceImpl;
|
|
|
+import com.xy.service.DeviceErrorsRecordServiceImpl;
|
|
|
+import com.xy.service.DeviceInfoServiceImpl;
|
|
|
+import com.xy.utils.SysDictUtils;
|
|
|
+import com.xy.utils.enums.DeviceErrorRecordTypesEnum;
|
|
|
+import com.xy.utils.enums.DictEnum;
|
|
|
+import com.xy.utils.enums.DictSonEnum;
|
|
|
+import com.xy.work.SpiDeviceService;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * spi 设备相关
|
|
|
+ *
|
|
|
+ * @author 谭斌
|
|
|
+ * @date 2023/04/16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+@Slf4j
|
|
|
+@Api(tags = "支付宝设备相关SPI")
|
|
|
+public class SpiDeviceServiceImpl implements SpiDeviceService {
|
|
|
+
|
|
|
+ private DeviceInfoServiceImpl deviceInfoService;
|
|
|
+
|
|
|
+ private DeviceErrorsRecordServiceImpl deviceErrorsRecordService;
|
|
|
+
|
|
|
+ private DisconnectedProducer disconnectedProducer;
|
|
|
+
|
|
|
+ private DeviceChargingServiceImpl deviceChargingService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @ApiOperation("设备告警通知")
|
|
|
+ public void deviceAlarmNotify(Map<String, String> params, DeviceAlarmNotifyDTO deviceAlarmNotifyDTO) {
|
|
|
+ //查询设备信息
|
|
|
+ DeviceInfo deviceInfo = deviceInfoService.getById(Long.valueOf(deviceAlarmNotifyDTO.getTerminalId()));
|
|
|
+ String faultCode = deviceAlarmNotifyDTO.getFaultCode();
|
|
|
+ DeviceErrorRecordTypesEnum deviceErrorRecordTypesEnum = faultCode.equals("DeviceOffline") ? DeviceErrorRecordTypesEnum.NET
|
|
|
+ : faultCode.equals("DoorOpenedOnNoTrade") ? DeviceErrorRecordTypesEnum.DOOR_LOCK
|
|
|
+ : null;
|
|
|
+ if (deviceErrorRecordTypesEnum == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //添加设备异常记录
|
|
|
+ DeviceErrorsRecordDto.Save save = new DeviceErrorsRecordDto.Save()
|
|
|
+ .setDeviceId(deviceInfo.getDeviceId());
|
|
|
+ save.setCode(deviceErrorRecordTypesEnum.getCode());
|
|
|
+ save.setErrorMsg(deviceAlarmNotifyDTO.getFaultMsg());
|
|
|
+ deviceErrorsRecordService.save(save);
|
|
|
+ //修改设备为离线
|
|
|
+ if (deviceErrorRecordTypesEnum.getCode() == DeviceErrorRecordTypesEnum.NET.getCode()) {
|
|
|
+ JSONObject jsonObject = new JSONObject().set("clientid", deviceInfo.getDeviceId());
|
|
|
+ disconnectedProducer.sendToMqtt(jsonObject.toString(), DisconnectedMqttConfiguration.TOPIC, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @ApiOperation("设备状态变更通知")
|
|
|
+ public void deviceStatusChangeNotify(Map<String, String> params, DeviceStatusChangeNotifyDTO deviceStatusChangeNotifyDTO) {
|
|
|
+ //查询设备信息
|
|
|
+ DeviceInfo deviceInfo = deviceInfoService.getById(Long.valueOf(deviceStatusChangeNotifyDTO.getTerminalId()));
|
|
|
+ Integer status = deviceStatusChangeNotifyDTO.getStatus();
|
|
|
+ if (deviceInfo.getFreezeStatus().equals(status)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //修改冻结状态、运营状态
|
|
|
+ DeviceInfo updateDeviceInfo = new DeviceInfo()
|
|
|
+ .setDeviceId(deviceInfo.getDeviceId())
|
|
|
+ .setBusyState(status)
|
|
|
+ .setFreezeStatus(status);
|
|
|
+ //修改激活状态
|
|
|
+ if (deviceStatusChangeNotifyDTO.getActiveStatus() != null && deviceStatusChangeNotifyDTO.getActiveStatus() == 1) {
|
|
|
+ if (deviceInfo == null || deviceInfo.getActiveState() != 1) {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ updateDeviceInfo.setActiveState(deviceStatusChangeNotifyDTO.getActiveStatus());
|
|
|
+ updateDeviceInfo.setActiveTime(now);
|
|
|
+ //首次激活可试用x天
|
|
|
+ DeviceCharging deviceCharging = deviceChargingService.getById(deviceInfo.getDeviceId());
|
|
|
+ if (deviceCharging == null) {
|
|
|
+ SysDictRedis sysDictRedis = SysDictUtils.get(DictEnum.DEVICE_CHARGING.getKey(), DictSonEnum.DEVICE_CHARGING_X.getKey());
|
|
|
+ deviceChargingService.save(new DeviceCharging()
|
|
|
+ .setDeviceId(deviceInfo.getDeviceId())
|
|
|
+ .setChargingX(Integer.valueOf(sysDictRedis.getValue()))
|
|
|
+ .setTimeout(now)
|
|
|
+ .setCreateTime(now)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ deviceInfoService.updateById(updateDeviceInfo);
|
|
|
+ }
|
|
|
+}
|