package main

//屏幕管理
import (
	"context"
	"lc/common/mqtt"
	"lc/common/util"
	"sync"
)

var ledMgr *LedMgr
var once sync.Once

type LedMgr struct {
	ctx            context.Context
	cancel         context.CancelFunc
	downQueue      *util.MlQueue
	mapTopicHandle map[string]func(m mqtt.Message)
	rwMutex        sync.RWMutex
	mapLedDevice   map[string]*LedDevice
}

func GetLedMgr() *LedMgr {
	once.Do(func() {
		ctx, cancel := context.WithCancel(context.Background())
		ledMgr = &LedMgr{
			ctx:            ctx,
			cancel:         cancel,
			mapTopicHandle: make(map[string]func(m mqtt.Message)),
			mapLedDevice:   make(map[string]*LedDevice),
		}
	})
	return ledMgr
}

func (l *LedMgr) initAll() error {
	loadDevInfos()
	CreateLedDevs()
	return nil
}

func CreateLedDevs() {
	for _, info := range ledInfos {
		device := NewLedDevice(info)
		ledMgr.mapLedDevice[device.IP] = device
	}
}

// todo
func (l *LedMgr) Update() error {
	return nil
}

func (l *LedMgr) HandleMessage(m mqtt.Message) {
	l.downQueue.Put(m)
}
