package com.fastbee.mq.redischannel.consumer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastbee.common.constant.FastBeeConstant;
import com.fastbee.common.core.mq.DeviceReportBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class HydropowerDeviceReportMsgConsumer {

    @Async(FastBeeConstant.TASK.DEVICE_WATER_ELECTRIC_TASK)
    public void consume(DeviceReportBo bo){
        try {
            //处理emq订阅的非 property/post 属性上报的消息 ，因为其他消息量小，放在一起处理
            String serialNumber;//设备序列号
            Long packetId;//包号
            byte[] data = bo.getData();//数据
            String topic;//主题
            System.err.println("mqtt接收到数据："+ new String(data));
            //解析
            String jsonString = "{\"type\": \"waterEleData\", \"pakSn\": 123, \"data\": {\"workState\": 1, \"action\": \"startPump\", \"mcuSn\": \"MCU123456\", \"sumEle\": 5000, \"sumFlow\": 3000, \"insFlow\": 2.5, \"userSumFlow\": 1500, \"areaCode\": \"010\", \"cardId\": \"CARD12345678\", \"userBalance\": 100.0, \"userSumEle\": 3000, \"curEle\": 50, \"curFlow\": 200, \"insPower\": 2300}}";
            JSONObject entries = JSONUtil.parseObj(jsonString);
            System.err.println("解析后："+entries);
            //获取帧标识
            String type = entries.getStr("type");
            //获取帧号
            Long pakSn = entries.getLong("pakSn");
            //获取数据
            JSONObject dataObj = entries.getJSONObject("data");
            //获取水泵状态0=关泵,1=开泵
            Integer workState = dataObj.getInt("workState");
            //获取动作
            String action = dataObj.getStr("action");
            //获取单片机编码
            String mcuSn = dataObj.getStr("mcuSn");
            //获取累计用电量
            Integer sumEle = dataObj.getInt("sumEle");
            //获取累计用水量
            Integer sumFlow = dataObj.getInt("sumFlow");
            //获取瞬时流量
            Double insFlow = dataObj.getDouble("insFlow");
            //当前用户累计用水量
            Integer userSumFlow = dataObj.getInt("userSumFlow");
            //获取区域号
            String areaCode = dataObj.getStr("areaCode");
            //获取卡号
            String cardId = dataObj.getStr("cardId");
            //获取用户余额
            Double userBalance = dataObj.getDouble("userBalance");
            //获取用户累计用电量
            Integer userSumEle = dataObj.getInt("userSumEle");
            //获取本次用电量
            Integer curEle = dataObj.getInt("curEle");
            //获取本次用水量
            Integer curFlow = dataObj.getInt("curFlow");
            //获取瞬时功率
            Integer insPower = dataObj.getInt("insPower");

//            otherMsgHandler.messageHandler(bo);
        }catch (Exception e){
            log.error("=>设备其他消息处理出错",e);
        }
    }

}
