device_status.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package manager
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/gogf/gf/container/gmap"
  7. "github.com/gogf/gf/encoding/gjson"
  8. "sparrow/pkg/rpcs"
  9. "sparrow/pkg/server"
  10. "sparrow/pkg/utils"
  11. "time"
  12. )
  13. type DeviceSceneConfig struct {
  14. SceneId string `json:"scene_id"`
  15. DecisionExpr string `json:"decision_expr"` // 条件表达式 and or
  16. Conditions []*DeviceCondition `json:"conditions"` // 条件
  17. Actions []*Action `json:"actions"` // 执行动作
  18. Interval int `json:"interval"` // 检查间隔(秒)
  19. ticker *time.Ticker `json:"-"` // 定时器
  20. stopChan chan struct{} `json:"-"` // 停止信号通道
  21. }
  22. // DeviceCondition 设备场景配置
  23. type DeviceCondition struct {
  24. Key string `json:"key"` // redis key
  25. DeviceType string `json:"device_type"` // 设备类型
  26. DeviceId string `json:"device_id"` // 设备id
  27. SubDeviceId string `json:"sub_device_id"` // 子设备id
  28. FieldType int `json:"field_type"` // 字段类型 1字符串 2数值
  29. Field string `json:"field"` // 字段名
  30. TargetValue string `json:"target_value"` // 值
  31. Operator int `json:"operator"` // 比较类型 数值比较 1 > 2 >= 3 = 4 <= 5 < 6 !=
  32. }
  33. type DeviceSceneService struct {
  34. tasks *gmap.HashMap
  35. }
  36. func NewDeviceSceneService() *DeviceSceneService {
  37. return &DeviceSceneService{
  38. tasks: gmap.New(true),
  39. }
  40. }
  41. func (d *DeviceSceneService) Add(config string) error {
  42. var c DeviceSceneConfig
  43. err := json.Unmarshal([]byte(config), &c)
  44. if err != nil {
  45. }
  46. if len(c.Conditions) == 0 {
  47. return errors.New("设备状态监控任务配置错误:判断条件不能为空")
  48. }
  49. // 初始化Ticker和停止通道
  50. c.ticker = time.NewTicker(20 * time.Second)
  51. c.stopChan = make(chan struct{})
  52. // 启动监控协程
  53. go d.monitorTask(c)
  54. fmt.Printf("保存设备状态监控任务成功:%s\n", gjson.New(c).MustToJsonString())
  55. d.tasks.Set(c.SceneId, c)
  56. p := d.tasks.Get(c.SceneId)
  57. fmt.Printf("pppppppppppppp:%s\n", gjson.New(p).MustToJsonString())
  58. return nil
  59. }
  60. func (d *DeviceSceneService) Update(config string) error {
  61. var c DeviceSceneConfig
  62. err := json.Unmarshal([]byte(config), &c)
  63. if err != nil {
  64. server.Log.Errorf("config to timerConfig error :%s", err.Error())
  65. }
  66. _ = d.Stop(c.SceneId)
  67. // 初始化Ticker和停止通道
  68. c.ticker = time.NewTicker(time.Duration(c.Interval) * time.Minute)
  69. c.stopChan = make(chan struct{})
  70. // 启动监控协程
  71. go d.monitorTask(c)
  72. d.tasks.Set(c.SceneId, c)
  73. server.Log.Debugf("UpdateWeatherScene :%s", config)
  74. return nil
  75. }
  76. func (d *DeviceSceneService) Remove(id string) error {
  77. fmt.Printf("task count--------------:%d", d.tasks.Size())
  78. scene := d.tasks.Get(id)
  79. fmt.Printf("scene-------------:%s", gjson.New(scene).MustToJsonString())
  80. c := scene.(DeviceSceneConfig)
  81. c.stopChan <- struct{}{}
  82. d.tasks.Remove(c.SceneId)
  83. server.Log.Debugf("RemoveTimeScene :%s", c.SceneId)
  84. return nil
  85. }
  86. // Start 停止任务
  87. func (d *DeviceSceneService) Start(id string) error {
  88. if !d.tasks.Contains(id) {
  89. return errors.New("任务不存在")
  90. }
  91. task := d.tasks.Get(id)
  92. c := task.(DeviceSceneConfig)
  93. go d.monitorTask(c)
  94. return nil
  95. }
  96. // Stop 停止任务
  97. func (d *DeviceSceneService) Stop(id string) error {
  98. if !d.tasks.Contains(id) {
  99. return errors.New("任务不存在")
  100. }
  101. task := d.tasks.Get(id)
  102. c := task.(DeviceSceneConfig)
  103. c.stopChan <- struct{}{}
  104. return nil
  105. }
  106. // monitorTask 监控任务:使用select监听Ticker和停止信号
  107. func (d *DeviceSceneService) monitorTask(config DeviceSceneConfig) {
  108. for {
  109. select {
  110. case <-config.ticker.C: // 定时触发
  111. result, err := d.checkDeviceCondition(config)
  112. if err != nil {
  113. server.Log.Errorf("compare weather condition error :%s", err.Error())
  114. }
  115. if result {
  116. if err = NewTaskExecutor(config.Actions).Do(); err != nil {
  117. server.Log.Errorf("weather do taskid :%s error:%s", config.SceneId, err.Error())
  118. }
  119. }
  120. case <-config.stopChan: // 收到停止信号
  121. config.ticker.Stop()
  122. return
  123. }
  124. }
  125. }
  126. func (d *DeviceSceneService) checkDeviceCondition(config DeviceSceneConfig) (bool, error) {
  127. var results []bool
  128. for _, v := range config.Conditions {
  129. var args rpcs.ArgsGetStatus
  130. args.Key = v.Key
  131. var reply rpcs.ReplyStatus
  132. err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatusByKey", args, &reply)
  133. if err != nil {
  134. server.Log.Errorf("设备状态数据获取失败:%v", err)
  135. return false, err
  136. }
  137. j := gjson.New(reply.Status)
  138. // 判断是否满足条件并填入到result
  139. fmt.Printf("判断条件:target_value:%s,value:%s,type:%d,operator:%d\n", v.TargetValue, j.Get(v.Field), v.FieldType, v.Operator)
  140. result := utils.CheckValue(v.TargetValue, j.Get(v.Field), v.FieldType, v.Operator)
  141. results = append(results, result)
  142. fmt.Printf("判断结果:%v\n", result)
  143. }
  144. switch config.DecisionExpr {
  145. case "and":
  146. for _, v := range results {
  147. if !v {
  148. return false, nil
  149. }
  150. }
  151. return true, nil
  152. case "or":
  153. for _, v := range results {
  154. if v {
  155. return true, nil
  156. }
  157. }
  158. }
  159. return false, nil
  160. }