package com.usky.transfer.service.mqtt.control;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.usky.common.core.util.JsonUtils;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.transfer.domain.DeviceDataWriteVO;
import com.usky.transfer.domain.DmpDeviceCommand;
import com.usky.transfer.service.DmpDeviceCommandService;
import com.usky.transfer.service.QueryInfluxdbDataService;
import com.usky.transfer.service.mqtt.MqttStrategy;
import com.usky.transfer.service.rocketmq.MyProducer;
import com.usky.transfer.service.vo.MqttBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

@Service("controlResponse")
public class control implements MqttStrategy {
    @Autowired
    private DmpDeviceCommandService dmpDeviceCommandService;

    //处理下发命令响应消息
    public String disposeMessage(MqttBaseVO mqttBaseVO) {

        //存储下发设备控制命令到数据库表中
        DmpDeviceCommand command = new DmpDeviceCommand();

        String recData = mqttBaseVO.getData().toString();
        JSONObject dataJson = JSONObject.parseObject(recData);
        Integer commandId = Integer.valueOf(dataJson.get("id").toString());
        command.setId(commandId);
        command.setCommandResponse(recData);
        if(recData.contains("error")){
            command.setCommandStatus(2);
        }else{
            command.setCommandStatus(1);
        }

        command.setUpdatedTime(LocalDateTime.now());

        dmpDeviceCommandService.updateById(command);

        return null;
    }


}
