package com.ruoyi.iot.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.iot.domain.TChargeRecord;
import com.ruoyi.iot.entity.param.DeviceParam;
import com.ruoyi.iot.openfeign.MsgService;
import com.ruoyi.iot.transdata.DataParam;
import com.ruoyi.iot.transdata.RpcResult;
import com.ruoyi.iot.transdata.entity.MainBoard;
import com.ruoyi.iot.transdata.entity.PlanCharge;
import com.ruoyi.iot.transdata.entity.opertype.OperEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class DeviceControlerService {
    @Autowired
    private MsgService msgService;

    /**
     * 获取主板参数
     * @param deviceId
     * @param ccid
     */
    public void getMainBoard(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.GetMainBord.getType());
        msgService.sendMsg(dataParam);
        log.info("发送获取主板的消息，{}",deviceId);
    }

    /**
     * 设置主板参数
     * @param deviceId
     * @param ccid
     * @param mainBoard
     */
    public void setMainBoard(String deviceId,String ccid,MainBoard mainBoard){
        DataParam dataParam = new DataParam();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.SetMainBord.getType());
        msgService.sendMsg(dataParam);
        log.info("发送设置主板的消息，{}",deviceId);
    }

    /**
     * 开始充电
     * @param deviceId
     */
    public void startCharge(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.StartCharge.getType());
        msgService.sendMsg(dataParam);
        log.info("发送开始充电的消息，{}",deviceId);
    }

    /**
     * 结束充电
     * @param deviceId
     * @param ccid
     */
    public void stopCharge(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.EndCharge.getType());
        msgService.sendMsg(dataParam);
        log.info("发送停止充电的消息，{}",deviceId);
    }

    /**
     * 重启设备
     * @param deviceId
     * @param ccid
     */
    public void restart(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.RestartDevice.getType());
        msgService.sendMsg(dataParam);
        log.info("发送重启设备的消息，{}",deviceId);
    }

    /**
     * 重置设备
     * @param deviceId
     * @param ccid
     */
    public void reset(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.ResetDevice.getType());
        msgService.sendMsg(dataParam);

        log.info("发送重置设备的消息，{}",deviceId);
    }


    /**
     * 发送获取端口状态指令
     * @param deviceParam
     */
    public void sendPortDetailCmd(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String ccid = deviceParam.getCcid();
        DataParam dataParam = new DataParam();
        JSONObject object1 = new JSONObject();
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId.toString());
        dataParam.setType(OperEnum.PortDetail.getType());
        RpcResult rpcResult = msgService.sendMsg(dataParam);
        log.info("发送获取端口详情的消息，{}",deviceId);
    }

    /**
     * 预约充电
     */
    public void planCharge(String deviceId,String ccid,PlanCharge planCharge) {
        DataParam dataParam = new DataParam();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(planCharge));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId.toString());
        dataParam.setType(OperEnum.PlanCHarge.getType());
        RpcResult rpcResult = msgService.sendMsg(dataParam);
        log.info("发送预约充电的消息，{}",deviceId);
    }


}
