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.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
import com.fastbee.deviceInfo.domain.DeviceInformationTargetpests;
import com.fastbee.deviceInfo.mapper.DeviceInformationTargetpestsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class RenkeTargetpestsDeviceDataService {

    @Autowired
    private RenKeAuthorizationService authenticationService;

    @Autowired
    private DeviceInformationTargetpestsMapper targetpestsMapper;

    /**
     * 获取设备实时数据
     * @param deviceAddrs 设备地址，支持多个用英文逗号分隔
     */
    public void setData(String deviceAddrs) {
        DeviceRealtimedataWorms deviceRealtimedataWorms = new DeviceRealtimedataWorms();
        //处理鉴权
        String token = authenticationService.getToken();
//        String token = "34491732858666778";

        //获取设备基础实时数据
        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();

        JSONObject respBody = JSONUtil.parseObj(respBodyStr);
//        System.err.println(respBodyStr);
        if(!respBody.get("code") .toString().equals("1000")){
            System.err.println(respBodyStr);
            throw new ServiceException("获取设备实时数据失败!");
        }
        JSONArray realtimeDataList = JSONUtil.parseArray(respBody.get("data"));
        realtimeDataList.forEach(json -> {
            //设备编码
            JSONObject jsonObject = JSONUtil.parseObj(json);
            String deviceAddr = jsonObject.getStr("deviceAddr");
            String data = jsonObject.getStr("data");
            JSONArray jsonArray = JSONUtil.parseArray(data);
            jsonArray.forEach(json1 -> {
                JSONObject obj = JSONUtil.parseObj(json1);
                String nodeName = obj.getStr("nodeName");
                if(nodeName.equals("引虫灯状态")){
                    //引虫灯状态
                    String temValueStr = obj.getStr("temValueStr");
//                    System.err.println(deviceAddr+"引虫灯状态:"+temValueStr);
                    //如果引虫灯设备关闭判定为离线
                    if(temValueStr.equals("关闭")){
                        //更新设备状态为离线
                         new LambdaUpdateChainWrapper<>(targetpestsMapper).eq(DeviceInformationTargetpests::getDeviceEncoding,deviceAddr).set(DeviceInformationTargetpests::getStatus, 0).update();
                    } else {
                        new LambdaUpdateChainWrapper<>(targetpestsMapper).eq(DeviceInformationTargetpests::getDeviceEncoding,deviceAddr).set(DeviceInformationTargetpests::getStatus, 1).update();
                    }
                }
            });
        });
    }


    public static void main(String[] args) {
        RenkeTargetpestsDeviceDataService renkeDeviceDataService = new RenkeTargetpestsDeviceDataService();
        renkeDeviceDataService.setData("21094868");
//        HttpResponse response1 = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/entrance/device/getRealTimeData?deviceAddrs="+"21094868")
//                .header("token", "34491732858666778")
//                .execute();
//        String respBodyStr1 = response1.body();
//
//        JSONObject respBody1 = JSONUtil.parseObj(respBodyStr1);
//        System.err.println(respBodyStr1);
    }

}
