package com.yiwei.demo.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yiwei.Response.Response;
import com.yiwei.http.HttpRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: apiRequestDemo
 * @description: api示例
 * @author: zhy
 * @create: 2021-11-18 19:08
 */
public class ApiDemo {
    /**
     * 实时数据查询
     *
     * @param httpRequest
     * @return
     * @throws IOException
     */
    public static void getDevInfo(HttpRequest httpRequest) throws IOException {
        //实时数据
        Map<String, Object> paraMap = new HashMap<String, Object>(3);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("devState", "-1");
        //请求
        String respJson = httpRequest.getDevInfo(paraMap);
        analysisResp(respJson);

    }


    /**
     * 获取实时数据配置
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevRTDConfig(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(3);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("snCode", "100021");
        //请求
        String respJson = httpRequest.getDevRTDConfig(paraMap);
        analysisResp(respJson);
    }

    /**
     * 获取实时数据
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevRTDData(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(2);
        //设置必填参数
        paraMap.put("dataCode", "1");
        paraMap.put("snCode", "10");
        String respJson = httpRequest.getDevRTDData(paraMap);
        analysisResp(respJson);

    }

    /**
     * 写实时数据
     * @param httpRequest
     * @throws IOException
     */
    public static void setDeviceRTD(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(3);
        //设置必填参数
        paraMap.put("dataCode", "1");
        paraMap.put("value", "10");
        paraMap.put("snCode", "-1");
        String respJson = httpRequest.setDeviceRTD(paraMap);
        analysisResp(respJson);
    }

    /**
     * 获取报警配置
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevAlarmConfig(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(3);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("snCode", "-1");
        String respJson = httpRequest.getDevAlarmConfig(paraMap);
        analysisResp(respJson);
    }

    /**
     * 获取实时数据
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevAlarmData(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(3);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("snCode", "-1");
        String respJson = httpRequest.getDevAlarmData(paraMap);
        analysisResp(respJson);
    }
    /**
     * 获取历史报警数据
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevAlarmHistoryData(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(5);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("snCode", "-1");
        paraMap.put("startTime", "-1");
        paraMap.put("endTime", "-1");
        String respJson = httpRequest.getDevAlarmHistoryData(paraMap);
        analysisResp(respJson);
    }

    /**
     * 获取历史数据配置
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevHistoryConfig(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(3);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("snCode", "-1");
        String respJson = httpRequest.getDevHistoryConfig(paraMap);
        analysisResp(respJson);
    }

    /**
     * 获取历史数据
     * @param httpRequest
     * @throws IOException
     */
    public static void getDevHistoryData(HttpRequest httpRequest) throws IOException {
        Map<String, Object> paraMap = new HashMap<String, Object>(6);
        //设置必填参数
        paraMap.put("currPage", "1");
        paraMap.put("perPage", "10");
        paraMap.put("snCode", "-1");
        paraMap.put("startTime", "-1");
        paraMap.put("endTime", "-1");
        paraMap.put("groupId", "-1");
        String respJson = httpRequest.getDevHistoryData(paraMap);
        analysisResp(respJson);
    }


    /**
     * 解析返回值
     *
     * @param respJson
     * @throws com.fasterxml.jackson.core.JsonProcessingException
     */
    private static void analysisResp(String respJson) throws com.fasterxml.jackson.core.JsonProcessingException {
        //解析response Json
        if (null != respJson && respJson.length() > 0) {
            ObjectMapper mapper = new ObjectMapper();
            Response response = mapper.readValue(respJson, Response.class);
            if (response.getMeta().isSuccess()) {
                //处理业务
                Object data = response.getData();
                System.out.println(data.toString());
            } else {
                //处理错误代码
                String errorCode = response.getMeta().getMessage();
                System.out.println(errorCode);
            }

        }
    }

    public static void main(String[] args) {
        //创建请求对象，这里用定时器刷新，不需要请求一次new一次（ttl 过期时间 秒）
//        HttpRequest httpRequest = new HttpRequest("填写公网域名(https://www.gongyeyuniot.com)或者私有化部署域名ip",
//                "设备管理平台查看的pubKey(公钥)",
//                "设备管理平台查看的priKey(私钥)", 10);
                HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080",
                "1117586036fb4b138b9571abb4216185",
                "2ecf6ed31ea14e25909175638923bc38", 10);
        try {
            getDevInfo(httpRequest);
            getDevRTDConfig(httpRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}