package org.nl.quartz.task;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nl.acs.agv.server.ZheDaAgvService;
import org.nl.acs.device.domain.Device;
import org.nl.acs.device_driver.basedriver.agv.ndcone.AgvNdcOneDeviceDriver;
import org.nl.acs.device_driver.basedriver.agv.utils.AgvActionEnum;
import org.nl.acs.ext.wms.service.AcsToWmsService;
import org.nl.acs.opc.DeviceAppService;
import org.nl.common.utils.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自动同步设备状态
 */
@Slf4j
@Component
public class SyncDeviceStatus {


    @Resource
    ZheDaAgvService agvService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private DeviceAppService deviceAppService;

    @Resource
    AcsToWmsService acsToWmsService;


    public void run() throws Exception {
        //rgv();
        device();
    }

    private void rgv() {
        try {
            log.info("自动线程开始查询AGV设备状态");
            HttpResponse response = agvService.queryAgvDeviceStatus("1");
            //查询AGV指令列表
            JSONArray jsonArray = JSONArray.parseArray(response.body());
            redisUtils.set("RGV", jsonArray);
        } catch (Exception e) {
            log.error("自动线程查询AGV设备状态失败{}{}", e, e.getMessage());
        }
    }

    private void device() {
        long startTime = System.currentTimeMillis();
        try {
            JSONArray AGV = new JSONArray();
            log.info("自动线程开始查询AGV设备状态");
            List<Device> deviceList = deviceAppService.findAllDevice();
            for (Device device : deviceList) {
                if (device.getDeviceDriver() instanceof AgvNdcOneDeviceDriver) {
                    AgvNdcOneDeviceDriver agvNdcOneDeviceDriver = (AgvNdcOneDeviceDriver) device.getDeviceDriver();
                    if (StringUtils.isNotBlank(agvNdcOneDeviceDriver.getDevice_code())) {
                        JSONObject json = new JSONObject();
                        json.put("device_code", agvNdcOneDeviceDriver.getDevice_code());
                        json.put("device_name", agvNdcOneDeviceDriver.getDevice_code());
                        json.put("device_status", agvNdcOneDeviceDriver.getStatus());
                        json.put("agv_status", AgvActionEnum.ACTION_STATUS.check(String.valueOf(agvNdcOneDeviceDriver.getAgv_status())));
                        json.put("energyLevel", agvNdcOneDeviceDriver.getElectric_qty());
                        json.put("transportOrder", agvNdcOneDeviceDriver.getTransportOrder());
                        json.put("positionX", agvNdcOneDeviceDriver.getX());
                        json.put("positionY", agvNdcOneDeviceDriver.getY());
                        json.put("positionAngle", agvNdcOneDeviceDriver.getAngle());
                        json.put("message", agvNdcOneDeviceDriver.getAgv_message());
                        json.put("isLaden", agvNdcOneDeviceDriver.getIs_have());
                        AGV.add(json);
                    }
                }
            }
            //agv状态信息反馈
            if (AGV.size() > 0) {
                JSONObject param = new JSONObject();
                param.put("AGV", AGV);
                redisUtils.set("AGV", AGV);
                //agv状态信息反馈
                //acsToWmsService.shipAgvUpdate(AGV);
                log.trace("自动上报agv状态，完毕耗时{}", System.currentTimeMillis() - startTime);
                Integer sd =0;
            }
        } catch (Exception e) {
            log.error("自动线程开始查询AGV设备状态失败{}{}", e, e.getMessage());
        }
    }
}
