package com.yuchen.saas.park.service.iot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuchen.saas.park.api.dto.park.IoTEvent;
import com.yuchen.saas.park.service.iot.NetParkProductEnum;
import com.yuchen.saas.park.service.iot.service.IotDeviceMqttService;
import com.yuchen.saas.park.service.service.security.ISaasParkElectronicFenceAlarmEventService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.redis.cache.NestRedis;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:
 * @author: 洪铭铭
 * @time: 2024/9/20 17:30
 */
@Service
@Slf4j
public class ElectronicFenceDeviceServiceImpl implements IotDeviceMqttService {

        private final String alarmEvent = "alarmEvent";

        private final String deviceStatusEvent = "deviceStatusEvent";

        private final String defenseAreaStatusEvent = "defenseAreaStatusEvent";

        @Resource
        ISaasParkElectronicFenceAlarmEventService saasParkElectronicFenceAlarmEventService;

        @Override
        public String type() {
            return NetParkProductEnum.ELECTRONIC_FENCE.getKey();
        }

        @Resource
        private NestRedis nestRedis;


        @Override
        public void receiveIotEvent(String json) {
                // 处理接收到的iot事件
                log.info("====== 接收到的iot上报设备事件:{}", json);
                if(Func.isEmpty(json)){
                        log.error("====== 接收到的iot上报事件为空");
                }
                IoTEvent ioTEvent = JSON.parseObject(json, IoTEvent.class);

//                if(Func.isNotEmpty(ioTEvent) || Func.isNotEmpty(ioTEvent.getIdentifier())){
//                        throw new RuntimeException("====== 接收到的iot上报事件为空");
//                }
                String deviceName = ioTEvent.getDeviceName();
                String productKey = ioTEvent.getProductKey();
                log.info("设备信息:productKey=="+productKey+",deviceName=="+deviceName+":该电子围栏设备上报信息"+ioTEvent.getName());
                if(ioTEvent.getProductKey().equals(NetParkProductEnum.ELECTRONIC_FENCE.getKey())) {

                        String identifier = ioTEvent.getIdentifier();
                        if (identifier.equals(alarmEvent)) {
                                //报警事件
                                saasParkElectronicFenceAlarmEventService.saveAlarmEvent(ioTEvent);
                        } else if (identifier.equals(deviceStatusEvent)) {
                                nestRedis.set("deviceName:identify:" + ioTEvent.getDeviceName() + ":" + deviceStatusEvent, JSONObject.toJSONString(ioTEvent));
                                log.info("deviceName:identify:" + ioTEvent.getDeviceName() + ":" + deviceStatusEvent + ":{}", (Object) nestRedis.get("deviceName:identify:" + ioTEvent.getDeviceName() + ":" + deviceStatusEvent));
                        } else if (identifier.equals(defenseAreaStatusEvent)) {
                                nestRedis.set("deviceName:identify:" + ioTEvent.getDeviceName() + ":" + defenseAreaStatusEvent, JSONObject.toJSONString(ioTEvent));
                                log.info("deviceName:identify:" + ioTEvent.getDeviceName() + ":" + defenseAreaStatusEvent + ":{}", (Object) nestRedis.get("deviceName:identify:" + ioTEvent.getDeviceName() + ":" + defenseAreaStatusEvent));

                        }
                }


        }


}
