devicemgr.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package camera
  2. import (
  3. "github.com/sirupsen/logrus"
  4. goonvif "github.com/use-go/onvif"
  5. "server/utils/mqtt"
  6. "sync"
  7. )
  8. var CameraDevices map[string]*goonvif.Device
  9. var _once sync.Once
  10. var _single *CameraDeviceMgr
  11. type CameraDeviceMgr struct {
  12. downQueue mqtt.MlQueue
  13. mapTopicHandle map[string]func(m mqtt.Message)
  14. mapDevice map[string]*CameraDev
  15. }
  16. // CameraDeviceMgr 单例
  17. func GetCameraDeviceMgr() *CameraDeviceMgr {
  18. _once.Do(func() {
  19. _single = &CameraDeviceMgr{
  20. downQueue: *mqtt.NewQueue(100),
  21. mapTopicHandle: make(map[string]func(m mqtt.Message)),
  22. mapDevice: make(map[string]*CameraDev),
  23. }
  24. })
  25. return _single
  26. }
  27. // 初始化
  28. func (c CameraDeviceMgr) InitAllCameraDevice() error {
  29. if err := LoadCameraDevConfig(); err != nil {
  30. logrus.Errorf("加载配置文件失败:%s", err.Error())
  31. return err
  32. }
  33. CameraDevices = make(map[string]*goonvif.Device)
  34. for _, v := range cameraDevConfig.Rtu {
  35. c.mapDevice[v.IP] = &CameraDev{
  36. Code: v.Code,
  37. IP: v.IP,
  38. Name: v.Name,
  39. Brand: v.Brand,
  40. Model: v.Model,
  41. DevType: v.DevType,
  42. User: v.User,
  43. Password: v.Password,
  44. RtmpServer: v.RtmpServer,
  45. WebServer: v.WebServer,
  46. Event: v.Event,
  47. Gb28181: v.Gb28181,
  48. }
  49. newDevice, err := NewDevice(c.mapDevice[v.IP])
  50. if err != nil {
  51. return err
  52. }
  53. if newDevice != nil {
  54. CameraDevices[v.Code] = newDevice
  55. }
  56. }
  57. return nil
  58. }