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 com.fastbee.deviceData.api.renke.constant.RenKeDeviceTypeConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RenKeUserDeviceOverview {
    @Autowired
    private RenKeAuthorizationService authorizationService;
    public Map getsysAllUserDevice() {
        HttpResponse response = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/entrance/device/getsysUserDevice")
                .header("token", authorizationService.getToken())
                .execute();
        String resultObjectStr = response.body();
        if (StringUtils.isBlank(response.toString())) {
            throw new RuntimeException("获取token失败!");
        }
        JSONObject respBody = JSONUtil.parseObj(resultObjectStr);
        JSONArray realtimeDataList = JSONUtil.parseArray(respBody.get("data"));
        Map<String,Integer> map = new HashMap<>();
        Integer InsectsLights = 0;
        Integer WORM = 0;
        for (Object jsonObject : realtimeDataList) {
            JSONObject josnConversion = (JSONObject) jsonObject;
            if (RenKeDeviceTypeConstant.WORM.equals(josnConversion.get("deviceType"))) {
                WORM=WORM+1;
            } else if (RenKeDeviceTypeConstant.InsectsLights.equals(josnConversion.get("deviceType"))) {
                InsectsLights=InsectsLights+1;
            }
        }
        map.put("InsectsLights",InsectsLights);
        map.put("wormFlagship",WORM);
        return map;
    }



}
