package com.dcs.hnyz.webSocket.handler;

import com.dcs.hnyz.cache.CacheCenter;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * WebSocketSessionManager
 * 管理页面订阅关系
 * @author: hmy
 * @date: 2025/5/30 16:04
 */
public class WebSocketSessionManager {
    // pageCode -> 用户名 -> 大设备 -> 小设备列表
    private static final Map<String, Map<String, Map<String, List<String>>>> sessionMap = new ConcurrentHashMap<>();

    public static void register(String pageCode, String username, Map<String, List<String>> deviceGroupMap) {
        sessionMap
                .computeIfAbsent(pageCode, k -> new ConcurrentHashMap<>())
                .put(username, deviceGroupMap);
        // 同步更新全局缓存
        CacheCenter.pageDeviceGroupMap.put(pageCode, deviceGroupMap);
    }

    public static Map<String, Map<String, List<String>>> getPageSessions(String pageCode) {
        return sessionMap.getOrDefault(pageCode, Collections.emptyMap());
    }
}

