package com.huimv.env.input.timer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.huimv.env.input.entity.BasePigpen;
import com.huimv.env.input.entity.Gateway;
import com.huimv.env.input.entity.vo.AlarmInfoVO;
import com.huimv.env.input.entity.vo.DataInfoVO;
import com.huimv.env.input.entity.vo.HeartInfoVo;
import com.huimv.env.input.service.IBaseFarmService;
import com.huimv.env.input.service.IBasePigpenService;
import com.huimv.env.input.service.IGatewayService;
import com.huimv.env.input.service.IHeartInfoService;
import com.huimv.env.input.utils.ElePostUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.env.input
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2024/6/19 0019 14:49
 **/
//@Configuration
//@EnableScheduling
@RestController
@RequestMapping("/test")
public class ElePostTimer {

    public static  final  String  BASE_URL = "https://south.bowaniot.com";
    //网关信息
    public static  final  String  GATEWAY_INFO_URL = "/airswitch/message/elec/gateway/info/import";
    //心跳信息
    public static  final  String  TRACK_URL = "/airswitch/message/elec/track/import";
    //流水信息
    public static  final  String  DATA_URL = "/airswitch/message/elec/data/import";
    //事件数据
    public static  final  String  ALARM_URL = "/airswitch/message/elec/alarm/import";

    @Autowired
    private IBaseFarmService baseFarmService;
    @Autowired
    private IBasePigpenService basePigpenService;

//    @Scheduled(cron = "0 0 0/1 * * ? ")
//    @Scheduled(cron = "0 * * * * ? ")
    @GetMapping("/test")
    public void  gateWayImport() throws IOException {
        System.out.println("----网关传输开始----");
        List<Gateway> gateways = baseFarmService.getUploadFarm();
        for (Gateway gateway : gateways) {
            BasePigpen byId = basePigpenService.getById(gateway.getLocationId());
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("mode",null);
            dataMap.put("version","v1.0");
            dataMap.put("np",null);
            dataMap.put("heartbeat",null);
            dataMap.put("longitude",byId.getLng());
            dataMap.put("latitude",byId.getLat());
            dataMap.put("imsi",null);
            dataMap.put("iccid",null);
            System.out.println("result--->"+ElePostUtils.basePost(BASE_URL + GATEWAY_INFO_URL, gateway.getDeviceCode(), dataMap));
        }
        System.out.println("----网关传输结束----");
    }


//    @Scheduled(cron = "5 * * * * ? ")
    @GetMapping("/test1")
    public void  trackImport() throws IOException {
        System.out.println("----心跳传输开始----");
        List<Gateway> gateways = baseFarmService.getUploadFarm();
        for (Gateway gateway : gateways) {
            DateTime dateTime = DateUtil.offsetMinute(new Date(), -1);
            List<HeartInfoVo> heartInfoVos = baseFarmService.trackImport(dateTime,gateway.getDeviceCode());
            System.out.println("result--->"+ElePostUtils.heartPost(BASE_URL + TRACK_URL, gateway.getDeviceCode(), heartInfoVos));
        }
        System.out.println("----心跳传输结束----");

    }

//    @Scheduled(cron = "10 * * * * ? ")
    @GetMapping("/test2")
    public void  dataImport() throws IOException {
        System.out.println("----流水传输开始----");
        List<Gateway> gateways = baseFarmService.getUploadFarm();
        for (Gateway gateway : gateways) {
            DateTime dateTime = DateUtil.offsetMinute(new Date(), -1);
            List<DataInfoVO> dataInfoVOS = baseFarmService.dataImport(dateTime,gateway.getDeviceCode());
            System.out.println("result--->"+ElePostUtils.heartPost(BASE_URL + DATA_URL, gateway.getDeviceCode(), dataInfoVOS));
        }
        System.out.println("----流水传输结束----");

    }

//    @Scheduled(cron = "1 * * * * ? ")
//    public void  alarmImport() throws IOException {
//        System.out.println("----事件传输开始----");
//        List<Gateway> gateways = baseFarmService.getUploadFarm();
//        for (Gateway gateway : gateways) {
//            DateTime dateTime = DateUtil.offsetMinute(new Date(), -1);
//            List<AlarmInfoVO> dataInfoVOS = baseFarmService.alarmImport(dateTime,gateway.getDeviceCode());
//            System.out.println("data--->"+dataInfoVOS.toString());
//            System.out.println("result--->"+ElePostUtils.heartPost(BASE_URL + ALARM_URL, gateway.getDeviceCode(), dataInfoVOS));
//        }
//        System.out.println("----事件传输结束----");
//
//    }


}
