package main

import (
	"time"

	"lc/common/onvif/discovery"
	"lc/common/util"
)

// /usr/local/srs/objs srs的服务器路径
// 球机 一键报警
var pool *util.Pool
var IDGen util.IdWorker

var appName = "camera"
var appVersion = "1.0.1"

func DiscoveryCamera(args ...interface{}) interface{} {
	for {
		devices, err := discovery.StartDiscovery(5 * time.Second)
		if err == nil && len(devices) > 0 {
			for _, d := range devices {
				GetLcDeviceMgr().Update(d.ID, d.XAddr)
			}
		}
		time.Sleep(10 * time.Minute)
	}
}

func GetNextUint64() uint64 {
	u64, err := IDGen.NextId()
	if err != nil {
		u64 = util.MlNow().Unix()
	}
	return uint64(u64)
}

func main() {
	util.InitLogrus("release")
	if err := loadAppConfig(); err != nil {
		return
	}

	IDGen.InitIdWorker(1000, 1)

	GetMQTTMgr()
	if err := GetLcDeviceMgr().initAllLcDevice(); err != nil {
		return
	}
	pool = util.NewPool(5)
	pool.Add(DiscoveryCamera, 1)                //发现摄像头
	pool.Add(GetMQTTMgr().MQTTMessageHandle, 2) //发布进程
	pool.Add(GetMQTTMgr().MQTTConnectMgr, 3)    //保持连接
	pool.Add(GetLcDeviceMgr().Handle, 4)        //mqtt来消息执行
	pool.Run()
	pool.Wait()
}
