package com.pwt.netty.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pwt.common.constant.CommandConstants;
import com.pwt.common.constant.WebMessageConstants;
import com.pwt.common.utils.StringUtils;
import com.pwt.netty.common.chargerold.client.BatteryStatusData;
import com.pwt.netty.common.chargerold.client.ChargerChargeStatusData;
import com.pwt.netty.common.chargerold.client.ChargerStatusData;
import com.pwt.netty.message.WebSocketMessage;
import lombok.extern.slf4j.Slf4j;

/**
 * @author linyun
 * @version V1.0
 * @Title: PutshDataUtil
 * @Description: 用于向站控web端推送数据
 * @date 2022/11/18
 **/
@Slf4j
public class PushDataUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * 向websocket推送业务数据
     */
    public static void pushData(int cmd, Object obj)
    {
        if (StringUtils.isNull(obj))
        {
            return;
        }
        switch (cmd)
        {
            // 充电机充电状态数据
            case CommandConstants.CHARGER_CHARGE_STATUS:
                pushChargerChargeStatusData(obj);
                break;
            // 电池状态数据
            case CommandConstants.BATTERY_STATUS:
                pushBatteryStatusData(obj);
                break;
            // 充电机状态数据
            case CommandConstants.CHARGER_STATUS:
                pushChargerStatusData(obj);
                break;
            default:
                break;
        }
    }

    /*充电机状态数据*/
    private static void pushChargerStatusData(Object obj)
    {
        ChargerStatusData chargerStatus = mapper.convertValue(obj, ChargerStatusData.class);
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build(WebMessageConstants.CHARGER_STATUS, chargerStatus));
    }

    /**
     * 电池状态数据
     */
    private static void pushBatteryStatusData(Object obj)
    {
        BatteryStatusData batteryStatus = mapper.convertValue(obj, BatteryStatusData.class);
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build(WebMessageConstants.BATTERY_STATUS, batteryStatus));
    }

    /**
     * 充电机充电状态数据
     */
    private static void pushChargerChargeStatusData(Object obj)
    {
        ChargerChargeStatusData chargeStatus = mapper.convertValue(obj, ChargerChargeStatusData.class);
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build(WebMessageConstants.CHARGER_CHARGE_STATUS, chargeStatus));
    }
}
