devicemgr.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package camera
  2. import (
  3. goonvif "github.com/use-go/onvif"
  4. "gopkg.in/yaml.v2"
  5. "os"
  6. "server/edge/util/mqtt"
  7. "sync"
  8. )
  9. var CameraDevices map[string]*goonvif.Device
  10. var _once sync.Once
  11. var _single *CameraDeviceMgr
  12. type CameraDeviceMgr struct {
  13. downQueue mqtt.MlQueue
  14. mapTopicHandle map[string]func(m mqtt.Message)
  15. mapDevice map[string]*CameraDev
  16. }
  17. // CameraDeviceMgr 单例
  18. func GetCameraDeviceMgr() *CameraDeviceMgr {
  19. _once.Do(func() {
  20. _single = &CameraDeviceMgr{
  21. downQueue: *mqtt.NewQueue(100),
  22. mapTopicHandle: make(map[string]func(m mqtt.Message)),
  23. mapDevice: make(map[string]*CameraDev),
  24. }
  25. })
  26. return _single
  27. }
  28. // 初始化
  29. func (c CameraDeviceMgr) InitAllCameraDevice() error {
  30. var conf DeviceConfig
  31. filePath := "./edge/dev/dev.yaml"
  32. if f, err := os.Open(filePath); err != nil {
  33. panic(err)
  34. } else {
  35. err := yaml.NewDecoder(f).Decode(&conf)
  36. if err != nil {
  37. panic(err)
  38. }
  39. }
  40. CameraDevices = make(map[string]*goonvif.Device)
  41. for _, v := range conf.CameraDevs {
  42. newDevice, err := NewDevice(v)
  43. if err != nil {
  44. return err
  45. }
  46. if newDevice != nil {
  47. CameraDevices[v.Code] = newDevice
  48. }
  49. }
  50. return nil
  51. }