package com.ruoyi.bst.device.utils;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.orderDevice.domain.enums.OrderDeviceStatus;
import com.ruoyi.common.utils.map.GpsCoordinateUtils;
import com.ruoyi.iot.domain.IotDeviceInfo;
import com.ruoyi.iot.domain.IotDeviceSysInfo;
import com.ruoyi.iot.util.BatUtil;

public class DeviceUtil {

    /**
     * 设置设备系统信息
     * @param device 设备
     * @param sys 设备信息
     */
    public static void setIotSysInfo(DeviceVO device, IotDeviceSysInfo sys) {
        if (device == null || sys == null) {
            return;
        }

        // 转换经纬度坐标系并赋值
        if (sys.getLon() != null && sys.getLat() != null) {
            List<BigDecimal> coordinates = GpsCoordinateUtils.coordinateConvert(sys.getLon(), sys.getLat());
            if (coordinates != null && coordinates.size() >= 2) {
                device.setLongitude(coordinates.get(1));
                device.setLatitude(coordinates.get(0));
            }
        }

        device.setVoltage(sys.getBat());
        device.setSignalStrength(sys.getCsq());
        device.setQuality(sys.getQ());
        device.setSatellites(sys.getS());
        device.setIotStatus(sys.getStatus());
        device.setRemainingPower(BatUtil.calcPowerPoint(device.getVoltage(), device.getModelFullVoltage(), device.getModelLowVoltage()));
    }

    /**
     * 设置设备信息
     * @param device 设备
     * @param iot 设备信息
     */
    public static void setIotInfo(DeviceVO device, IotDeviceInfo iot) {
        if (device == null || iot == null) {
            return;
        }

        IotDeviceSysInfo sys = iot.getSys();
        if (sys != null) {
            setIotSysInfo(device, sys);
        }
        device.setLastTime(iot.getAt());
        device.setLastLocationTime(iot.getAt());
    }

    /**
     * 判断设备是否有订单正在使用
     * @param device 设备
     * @return 是否正在使用
     */
    public static boolean isOrderUsing(DeviceVO device) {
        return device != null && device.getOrderDeviceId() != null && OrderDeviceStatus.inUse().contains(device.getOrderDeviceStatus());
    }
}