package com.zhgd.xmgl.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: wisdomSite
 * @description: 配电箱服务对接
 * @author: Mr.Peng
 * @create: 2021-12-01 09:19
 **/
@Slf4j
public class ElectricBoxUtils {

    public static String getToken(String devCode,String scret,String url){
        String token=null;
        try {
            Map<String,Object> param=new HashMap();
            param.put("devCode",devCode);
            param.put("scret",scret);
            String result= HttpUtil.post(url+"/api/login", JSONUtil.toJsonStr(param));
            log.info(result);
            if(StringUtils.isNotEmpty(result)){
                JSONObject object= JSONObject.parseObject(result);
                String code=String.valueOf(object.get("code").toString());
                if("200".equals(code)){
                    JSONObject data=object.getJSONObject("data");
                    token=object.getString("result");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return token;
    }

    public static void sendData(Map<String,Object> param,String url){
        try {
            log.info("--配电箱上传数据---："+JSONUtil.toJsonStr(param));
            String result= HttpUtil.post(url+"/api/SendRealData", JSONUtil.toJsonStr(param));
            log.info("--配电箱上传结果---："+result);
            if(StringUtils.isNotEmpty(result)){
                JSONObject object= JSONObject.parseObject(result);
                String code=String.valueOf(object.get("code").toString());
                if("200".equals(code)){

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,Object> param=new HashMap<>();
        param.put("token","3x06B4C04qxlwps6S07093Y8qWvCW8cS");
        param.put("modelTag","Data");
        param.put("gTime",sdf.format(new Date()));
        Map<String,Object> data=new HashMap<>();
        data.put("GTime","2021-12-01 13:43:00");
        //data.put("DianD",electricalData)
        data.put("WenD_A",1);
        data.put("WenD_B",1);
        data.put("WenD_C",1);
        ///data.put("WenD_N")
        data.put("LouDL",1);
        data.put("WenD",1);
        data.put("DianL_A",1);
        data.put("DianL_B",1);
        data.put("DianL_C",1);
        data.put("DianY_A",1);
        data.put("DIanY_B",1);
        data.put("DIanY_C",1);
        data.put("A_LouD",0);
        data.put("A_WenD",0);
        data.put("A_DianL",0);
        data.put("A_DianY",0);
        param.put("datas",data);
        //log.info(getToken("21040015","e8908fbf-eade-40e6-a029-b613595baed2","http://124.14.22.135:8084"));
        sendData(param,"http://124.14.22.135:8084");
    }
}
