package com.yuchen.saas.park.service.pull.property.device;

import cn.hutool.json.JSONUtil;
import com.yuchen.saas.message.api.feign.ws.WebSocketFeignServer;
import com.yuchen.saas.message.api.vo.NoticeVo;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.vo.fault.DeviceProperty;
import com.yuchen.saas.park.api.vo.park.devicepro.ValveControllerDevicePro;
import com.yuchen.saas.park.service.pull.property.template.DeviceHandleBasePropertyValue;
import com.yuchen.saas.park.service.service.IrrigationService;
import org.nest.springwrap.core.tool.utils.SpringUtil;

import java.util.Map;

/**
 * @Author: 肖润飞
 * @CreateTime: 2022/10/01 10:25
 * @Description:  阀门控制器 ,基于设备的属性值进行处理
 * @Version: 1.0
 */
public class ValveControllerHandleImpl implements DeviceHandleBasePropertyValue {

    /**
     * 注入灌溉相关接口
     */
    private static IrrigationService irrigationService;

    /**
     * 引入ws推送模块接口
     */
    private static WebSocketFeignServer pushService;

    static {
        //从 spring容器中获得指定的bean数据
        irrigationService = SpringUtil.getBean(IrrigationService.class);
        pushService = SpringUtil.getBean(WebSocketFeignServer.class);
    }

    @Override
    public boolean doHandle(DeviceProperty deviceProperty, PmgBatSmartEquipment deviceInfo) {
        if (ValveControllerDevicePro.CHECK_ATTRIBUTE_VALVE_STATE.equals(deviceProperty.getIdentify())){
            // 需要往前端推送整体数据
            Map<String, Object> deviceCount = irrigationService.getDeviceCount(deviceInfo.getParkId());
            // 组织WS推送的数据
            NoticeVo noticeVo = new NoticeVo();
            noticeVo.setParkId(deviceInfo.getParkId().toString());
            noticeVo.setDestination("/valveState");
            noticeVo.setInfoJson(JSONUtil.toJsonStr(deviceCount));
            pushService.sendNotice(noticeVo);
        }
        return true;
    }
}