cron.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "server/modbus"
  6. "server/model"
  7. "server/utils"
  8. "time"
  9. )
  10. type Cron struct {
  11. }
  12. // RelayOnOffTimeTask 定时开关 前提是 设备在线 否则失效
  13. func (c Cron) RelayOnOffTimeTask() {
  14. current := time.Now().Format("15:04")
  15. devices, err := utils.GetOnlineDevices()
  16. if err != nil {
  17. _ = fmt.Errorf("LoadData err = %s", err.Error())
  18. return
  19. }
  20. relayMap := make(map[string]map[int]int)
  21. for _, d := range devices {
  22. //if d.State == 0 {
  23. // continue
  24. //}
  25. tmp, isExist := relayMap[d.Sn]
  26. if !isExist { // 回路id 回路状态
  27. tmp = make(map[int]int)
  28. }
  29. relays := d.DeviceLoops
  30. if err != nil {
  31. fmt.Errorf(err.Error())
  32. continue
  33. }
  34. rcTime, rlTime, err := utils.SunriseSunsetForChina(28.23, 113.05)
  35. if err != nil {
  36. log.Printf("SunriseSunsetForChina err = %s", err.Error())
  37. rcTime = "06:00"
  38. rlTime = "18:00"
  39. }
  40. for _, r := range relays {
  41. //日出日落时间
  42. if r.TimeCondition1OnTime == "日出" {
  43. r.TimeCondition1OnTime = rcTime
  44. } else if r.TimeCondition1OnTime == "日落" {
  45. r.TimeCondition1OnTime = rlTime
  46. }
  47. if r.TimeCondition1OffTime == "日出" {
  48. r.TimeCondition1OffTime = rcTime
  49. } else if r.TimeCondition1OffTime == "日落" {
  50. r.TimeCondition1OffTime = rlTime
  51. }
  52. if r.TimeCondition2OnTime == "日出" {
  53. r.TimeCondition2OnTime = rcTime
  54. } else if r.TimeCondition2OnTime == "日落" {
  55. r.TimeCondition2OnTime = rlTime
  56. }
  57. if r.TimeCondition2OffTime == "日出" {
  58. r.TimeCondition2OffTime = rcTime
  59. } else if r.TimeCondition2OffTime == "日落" {
  60. r.TimeCondition2OffTime = rlTime
  61. }
  62. isTiming := 0 // 用来判断该设备的回路 是否定时
  63. //控制回路 时间判断 是否关闭 当前时间是否等与规定时间 规定时间到规定时间+10分钟
  64. //因为1分钟执行一次,所以会多次执行,则新加判断 如果数据库中状态和回路状态相同则不执行下去
  65. layout := "15:04"
  66. rTimeCondition1OnTime, _ := time.Parse(layout, r.TimeCondition1OnTime)
  67. rTimeCondition1OffTime, _ := time.Parse(layout, r.TimeCondition1OffTime)
  68. rTimeCondition2OnTime, _ := time.Parse(layout, r.TimeCondition2OnTime)
  69. rTimeCondition2OffTime, _ := time.Parse(layout, r.TimeCondition2OffTime)
  70. rCurrent, _ := time.Parse(layout, current)
  71. if r.TimeCondition1OnTime != "关闭" &&
  72. (r.TimeCondition1OnTime == current ||
  73. (rCurrent.Before(rTimeCondition1OnTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition1OnTime))) {
  74. tmp[r.ID] = 1
  75. isTiming = 1
  76. }
  77. if r.TimeCondition1OffTime != "关闭" && (r.TimeCondition1OffTime == current ||
  78. (rCurrent.Before(rTimeCondition1OffTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition1OffTime))) {
  79. tmp[r.ID] = 0
  80. isTiming = 1
  81. }
  82. if r.TimeCondition2OnTime != "关闭" && (r.TimeCondition2OnTime == current ||
  83. (rCurrent.Before(rTimeCondition2OnTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition2OnTime))) {
  84. tmp[r.ID] = 1
  85. isTiming = 1
  86. }
  87. if r.TimeCondition2OffTime != "关闭" && (r.TimeCondition2OffTime == current ||
  88. (rCurrent.Before(rTimeCondition2OffTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition2OffTime))) {
  89. tmp[r.ID] = 0
  90. isTiming = 1
  91. }
  92. if isTiming == 1 {
  93. relayMap[d.Sn] = tmp
  94. }
  95. }
  96. }
  97. for key, value := range relayMap {
  98. reg, dev, err := utils.GetDataByDeviceId(key)
  99. if err != nil {
  100. fmt.Printf("GetDataByDeviceId err = %s\n", err.Error())
  101. }
  102. for i, i2 := range value {
  103. data := modbus.DeviceLoopSwitch(i, i2)
  104. if model.ConnectionMap[key] == nil {
  105. fmt.Errorf("设备连接丢失")
  106. }
  107. err := utils.WriteDevice(data, model.ConnectionMap[key])
  108. time.Sleep(100 * time.Millisecond)
  109. if err != nil {
  110. fmt.Printf("WriteDevice err = %s\n", err.Error())
  111. }
  112. for j, loop := range dev.DeviceLoops {
  113. if loop.ID == i {
  114. dev.DeviceLoops[j].State = i2
  115. }
  116. }
  117. }
  118. for i, device := range reg.Devices {
  119. if device.Sn == dev.Sn {
  120. reg.Devices[i] = dev
  121. }
  122. }
  123. data, err := utils.SaveRegionOnData(reg)
  124. if err != nil {
  125. fmt.Printf("SaveRegionOnData err = %s\n", err.Error())
  126. }
  127. err = SaveData(data)
  128. if err != nil {
  129. fmt.Printf("SaveData err = %s\n", err.Error())
  130. }
  131. }
  132. }
  133. // UpdateDeviceRelayStatus 更新设备状态
  134. //func (c Cron) UpdateDeviceRelayStatus() {
  135. // service := DeviceService{}
  136. // devices := service.GetDevicesByModule1AndModule3()
  137. // for _, d := range devices {
  138. // //新2路开关,因固件与8路相同 这里要特殊处理下这里
  139. // if *d.Module == 3 {
  140. // d.CircuitNum = 8
  141. // }
  142. // Query(d.DeviceSn, d.CircuitNum)
  143. // }
  144. //}
  145. //
  146. //// StatisticsDevice 设备统计
  147. //func (c Cron) StatisticsDevice() {
  148. // statisticsService := DeviceOnlineDailyStatisticsService{}
  149. // onlines := statisticsService.GetTotalDeviceOnline()
  150. // for _, online := range onlines {
  151. // err := statisticsService.UpdateStatisticsDevice(online)
  152. // if err != nil {
  153. // logger.Logger.Errorf("StatisticsDevice err = %s", err.Error())
  154. // }
  155. // }
  156. //}
  157. //