package com.fastbee.deviceData.api.devlink.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.deviceData.domain.DeviceRealtimedataMeteorology;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class DevLinkRealTimeDataService {
    @Autowired
    private DevLinkAuthorizationService authorizationService;


    /**
     * 获取气象设备实时数据
     * @param deviceId 设备id
     * @return 实时数据
     */
    public Map<String,String> getMetDeviceRealData(String deviceId){
        String result = requestDeviceData(deviceId);
        //解析返回数据
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Object data = jsonObject.get("data");
        //获取设备状态
        JSONObject jsonObjectData = JSONUtil.parseObj(data);
        Object status = jsonObjectData.get("status");
//        System.err.println("status:"+status);
        //获取设备属性以及实时数据值
        Object properties = jsonObjectData.get("properties");
        JSONArray propertiesArray = JSONUtil.parseArray(properties);
        DeviceRealtimedataMeteorology realData = new DeviceRealtimedataMeteorology();
        //获取设备各项属性实时数据
        //设备属性与值建立map
        Map<String, String> propertiesMap = new HashMap<>();
        propertiesArray.forEach(p -> {
            Object property = JSONUtil.parseObj(p).get("property");
            JSONObject propertyMap = JSONUtil.parseObj(property);
//            System.err.println("propertyMap:"+propertyMap);
            //属性名称
            String name = propertyMap.get("name").toString();
            //属性key
            String key = propertyMap.get("key").toString();
            //属性值
            Object value1 = JSONUtil.parseObj(p).get("value");
            String value="" ;
            if(value1!=null){
                value = value1.toString();
            }

            //TODO 属性值历史数据，是否需要
            Object list = JSONUtil.parseObj(p).get("list");
//            System.err.print("name:"+name);
            System.err.print("key:"+key);
            System.err.println("------------value:"+value);
            //建立属性-值映射关系
            propertiesMap.put(key,value);
//            saveRealData(key,value,realData);
        });
        propertiesMap.put("status",status.toString());
        propertiesMap.put("deviceId",deviceId);

        return propertiesMap;
    }

    /**
     * 请求气象设备实时数据
     * @param deviceId 设备id
     * @return 实时数据
     */
    private String requestDeviceData(String deviceId) {
        DevLinkAuthorizationService authorization = new DevLinkAuthorizationService();
        //构建请求体
        HashMap<String, Object> body = new HashMap<>();
        body.put("method","get");
        body.put("path","api/v1/product/device/run_status");
        Map<String,Object> params=new HashMap<>();
        params.put("id", deviceId);
        body.put("params",params);
        String jsonStr = JSONUtil.toJsonStr(body);
        System.err.println(authorization.getBaseUrl() + authorization.getAuth());
        return HttpUtil.post(authorization.getBaseUrl() + authorization.getAuth(), jsonStr);
    }

    public BigDecimal toBigDecimalValue(String str) {
        if (str == null||"".equals(str)) {
            return null;
        }
        return new BigDecimal(str);
    }
    public Long toLongValue(String str) {
        if (str == null||"".equals(str)) {
            return null;
        }
        return Long.parseLong(str);
    }

    public static void main(String[] args) {
        DevLinkRealTimeDataService devLinkRealTimeDataService = new DevLinkRealTimeDataService();
        Map<String, String> metDeviceRealData = devLinkRealTimeDataService.getMetDeviceRealData("3270");
//        System.err.println(metDeviceRealData);
    }
}
