package com.zhgd.xmgl.util;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 雾炮喷淋设备控制
 */
@Slf4j
public class DustUtils {

    /*private static final String authurl=PropertiesUtils.getValue("dust.authUrl");
    private static final String operationUrl=PropertiesUtils.getValue("dust.operationUrl");*/
    private static final String authurl = "http://www.lmcraft.com:8382/getonenetctrlinfo.asp";
    private static final String operationUrl = "http://www.lmcraft.com:4321/";

    public static String[] getDevAuth(String devNo) {
        String[] str = null;
        try {
            String result = HttpUtil.get(authurl + "?auth_info=" + devNo + "&password=");
            log.info(result);
            if (result != null && result.length() > 0) {
                String[] data = result.split(",");
                if ("ok".equals(data[0])) {
                    str = data;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * @param @param  devId   设备的编号
     * @param @param  type    0表示关闭,-1表示一直开启
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: sendDevOperation
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    public static String sendDevOperation(String devId, String type) {
        String flag = "error";
        try {
            String[] authStr = getDevAuth(devId);
            if (authStr != null) {
                String opType = "";
                if ("-1".equals(type)) {
                    opType = "enable_d";
                } else if ("0".equals(type)) {
                    opType = "disable_d";
                }
                String result = HttpUtil.get(operationUrl + opType + "?dev_id=" + authStr[1] + "&time=" + type + "&api_key=" + authStr[2]);
                log.info(result);
                if (result != null && result.length() > 0) {
                    if ("success".equals(result)) {
                        flag = "success";
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 单通道4G控制板
     *
     * @param @param  devId   设备的编号
     * @param @param  type    0表示关闭,-1表示一直开启
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: sendDevOperation
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    public static String sendSingleChannelDevOperation(String devId, String type) {
        String flag = "error";
        try {
            String[] authStr = getDevAuth(devId);
            if (authStr != null) {
                String result = HttpUtil.get(operationUrl + "ctrl_sac07csa?api_key=" + authStr[2] + "&dev_id=" + authStr[1] + "&time=" + type);
                log.info(result);
                if (result != null && result.length() > 0) {
                    if ("success".equals(result)) {
                        flag = "success";
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 双通道GPRS控制
     *
     * @param @param  devId   设备的编号
     * @param @param  type    0表示关闭,-1表示一直开启
     * @param @param  channelNo    通道号
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: sendDevOperation
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    public static String sendDualChannelDevOperation(String devId, String type, Integer openchannelNo, Integer closechannelNo) {
        String flag = "error";
        try {
            String[] authStr = getDevAuth(devId);
            if (authStr != null) {
                Integer channelNo = null;
                if ("-1".equals(type)) {
                    channelNo = openchannelNo;
                } else if ("0".equals(type)) {
                    channelNo = closechannelNo;
                }
                String result = HttpUtil.get(operationUrl + "ctrl_sdc00gdb?api_key=" + authStr[2] + "&dev_id=" + authStr[1] + "&time=" + 3 + "&ch=" + channelNo);
                log.info(result);
                if (result != null && result.length() > 0) {
                    if ("success".equals(result)) {
                        flag = "success";
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static void main(String[] args) {
        log.info("{}", getDevAuth("1010018192"));
    }

}
