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

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RenkeDeviceDataService {

    @Autowired
    private RenKeAuthorizationService authenticationService;

    /**
     * 获取设备实时数据
     * @param deviceAddrs 设备地址，支持多个用英文逗号分隔
     */
    public void getDeviceRealtimeData(String deviceAddrs) {
        //处理鉴权
//        String token = authenticationService.getToken();
        String token = new RenKeAuthorizationService().getToken();
        //获取设备实时数据
        HttpResponse response = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/entrance/device/getRealTimeData")
                .header("token", token)
                .form("deviceAddrs", deviceAddrs)
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取设备实时数据失败!");
        }
        JSONObject respBody = JSONUtil.parseObj(respBodyStr);
        JSONArray realtimeDataList = JSONUtil.parseArray(respBody.get("data"));
        System.err.println("设备实时数据："+realtimeDataList);
    }

    public static void main(String[] args) {
        RenkeDeviceDataService renkeDeviceDataService = new RenkeDeviceDataService();
        renkeDeviceDataService.getDeviceRealtimeData("1017240042");
    }

}
