vince
2024-03-01 f7178996ca77a1bccc941c0e7a73b36803e508cb
src/main/java/com/fzzy/gateway/GatewayUtils.java
@@ -1,12 +1,15 @@
package com.fzzy.gateway;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.data.DepotType;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.gateway.data.GrainCableData;
import com.fzzy.gateway.entity.GatewayDevice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 常量
@@ -14,36 +17,78 @@
@Component
public class GatewayUtils {
    /**
     * 设备在线标记,针对WEBSocket请求,控制设备是否在线,key = deviceId,Value=Y/N
     */
    public static Map<String, String> contextOnlineMap = new HashMap<>();
    /**
     * 设备缓存
     */
    public static Map<String, GatewayDevice> cacheMapDeviceId = new HashMap<>();
    /**
     * 设备缓存
     * 设备缓存-只针对
     */
    public static Map<String, GatewayDevice> cacheMapDeviceSn = new HashMap<>();
    public static Map<String, GatewayDevice> cacheMapDeviceWeight = new HashMap<>();
    public static void add2Cache(GatewayDevice device) {
        cacheMapDeviceId.put(device.getDeviceId(), device);
        cacheMapDeviceSn.put(device.getDeviceSn(), device);
        //保存地磅
        if (GatewayDeviceType.TYPE_01.getCode().equals(device.getType())) {
            cacheMapDeviceWeight.put(device.getDeviceId(), device);
        }
    }
    public static Collection<GatewayDevice> allCacheDevice() {
        return cacheMapDeviceId.values();
    }
    public static GatewayDevice getCacheByDeviceId(String deviceId) {
        return cacheMapDeviceId.get(deviceId);
    }
    /**
     * 针对一个分机对廒间的情况
     *
     * @param deviceSn
     * @return
     */
    public static GatewayDevice getCacheByDeviceSn(String deviceSn) {
        return cacheMapDeviceSn.get(deviceSn);
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
        for (GatewayDevice device : list) {
            if (deviceSn.equals(device.getDeviceSn())) return device;
        }
        return null;
    }
    public static GatewayDevice getCacheByDeviceSIp(String ip) {
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
        for (GatewayDevice device : list) {
            if (ip.equals(device.getIp())) return device;
        }
        return null;
    }
    /**
     * 针对一个通讯分机对多个仓情况
     *
     * @param deviceSn
     * @return
     */
    public static List<GatewayDevice> getCacheByDeviceSn2(String deviceSn) {
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
        List<GatewayDevice> result = new ArrayList<>();
        for (GatewayDevice device : list) {
            if (deviceSn.equals(device.getDeviceSn())) result.add(device);
        }
        return result;
    }
    public static List<GatewayDevice> getCacheByDeviceType(String deviceType) {
        List<GatewayDevice> result = new ArrayList<>();
@@ -63,20 +108,74 @@
    public static void removeCache(GatewayDevice data) {
        cacheMapDeviceId.remove(data.getDeviceId());
        cacheMapDeviceSn.remove(data.getDeviceSn());
        //cacheMapDeviceSn.remove(data.getDeviceSn());
    }
    public static void updateOnline(String deviceId) {
        contextOnlineMap.put(deviceId, "Y");
    public static String getStatus(String sn) {
        ApiCommonDevice device = Constant.getCommonDeviceCache(sn);
        if (null == device) return Constant.YN_N;
        return device.getStatus();
    }
    public static void updateOffOnline(String deviceId) {
        contextOnlineMap.put(deviceId, "N");
    }
    public static boolean isOnline(String deviceId) {
        String value = contextOnlineMap.get(deviceId);
        if (null == value || "N".equals(value)) return false;
        GatewayDevice device = getCacheByDeviceId(deviceId);
        if (null == device.getStatus() || Constant.YN_N.equals(device.getStatus())) return false;
        return true;
    }
    public static Collection<GatewayDevice> listDeviceWeight() {
        return cacheMapDeviceWeight.values();
    }
    public static GatewayDevice getCacheByDepotSysId(String depotSysId) {
        if (null == depotSysId) return null;
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
        for (GatewayDevice device : list) {
            if (null == device.getDepotIdSys()) continue;
            if (depotSysId.equals(device.getDepotIdSys())) return device;
        }
        return null;
    }
    public static GrainCableData getCableData(GatewayDevice gatewayDevice) {
        String cableRule = gatewayDevice.getCableRule();
        String cableCir = gatewayDevice.getCableCir();
        GrainCableData result = new GrainCableData();
        result.setCableRule(cableRule);
        result.setCableCir(cableCir);
        int cableY, cableX;
        String[] attCable = cableRule.split("-");
        int cableZ = Integer.valueOf(attCable[0]);
        if (StringUtils.isEmpty(cableCir)) {
            cableY = Integer.valueOf(attCable[1]);
            cableX = Integer.valueOf(attCable[2]);
        } else {
            String[] attCir = cableCir.split("-");
            cableZ = Integer.valueOf(attCir[0]);
            cableY = 1;
            cableX = 0;
            //针对多圈计算总过多少根电缆
            for (int i = 0; i < cableCir.length(); i++) {
                cableX += Integer.valueOf(attCable[i]);
            }
        }
        result.setCableY(cableY);
        result.setCableZ(cableZ);
        result.setCableX(cableX);
        result.setSumNum(cableZ * cableY * cableX);
        result.setTotalCircle(attCable.length);
        return result;
    }
}