package com.fzzy.gateway; import com.fzzy.gateway.entity.GatewayDevice; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 常量 */ @Component public class GatewayUtils { /** * 设备在线标记,针对WEBSocket请求,控制设备是否在线,key = deviceId,Value=Y/N */ public static Map contextOnlineMap = new HashMap<>(); /** * 设备缓存 */ public static Map cacheMapDeviceId = new HashMap<>(); /** * 设备缓存 */ public static Map cacheMapDeviceSn = new HashMap<>(); public static void add2Cache(GatewayDevice device) { cacheMapDeviceId.put(device.getDeviceId(), device); cacheMapDeviceSn.put(device.getDeviceSn(), device); } public static GatewayDevice getCacheByDeviceId(String deviceId) { return cacheMapDeviceId.get(deviceId); } public static GatewayDevice getCacheByDeviceSn(String deviceSn) { return cacheMapDeviceSn.get(deviceSn); } public static List getCacheByDeviceType(String deviceType) { List result = new ArrayList<>(); for (GatewayDevice device : cacheMapDeviceId.values()) { if (deviceType.equals(device.getType())) result.add(device); } return result; } public static GatewayDevice getCacheByDeviceTypeOne(String deviceType) { for (GatewayDevice device : cacheMapDeviceId.values()) { if (deviceType.equals(device.getType())) return device; } return null; } public static void removeCache(GatewayDevice data) { cacheMapDeviceId.remove(data.getDeviceId()); cacheMapDeviceSn.remove(data.getDeviceSn()); } public static void updateOnline(String deviceId) { contextOnlineMap.put(deviceId, "Y"); } 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; return true; } }