cron.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package service
  2. import (
  3. "net"
  4. "server/logger"
  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. logger.Get().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. rcTime, rlTime, err := utils.SunriseSunsetForChina(28.23, 113.05)
  31. if err != nil {
  32. logger.Get().Errorf("SunriseSunsetForChina err = %s", err.Error())
  33. rcTime = "06:00"
  34. rlTime = "18:00"
  35. }
  36. for _, r := range relays {
  37. //日出日落时间
  38. if r.TimeCondition1OnTime == "日出" {
  39. r.TimeCondition1OnTime = rcTime
  40. } else if r.TimeCondition1OnTime == "日落" {
  41. r.TimeCondition1OnTime = rlTime
  42. }
  43. if r.TimeCondition1OffTime == "日出" {
  44. r.TimeCondition1OffTime = rcTime
  45. } else if r.TimeCondition1OffTime == "日落" {
  46. r.TimeCondition1OffTime = rlTime
  47. }
  48. if r.TimeCondition2OnTime == "日出" {
  49. r.TimeCondition2OnTime = rcTime
  50. } else if r.TimeCondition2OnTime == "日落" {
  51. r.TimeCondition2OnTime = rlTime
  52. }
  53. if r.TimeCondition2OffTime == "日出" {
  54. r.TimeCondition2OffTime = rcTime
  55. } else if r.TimeCondition2OffTime == "日落" {
  56. r.TimeCondition2OffTime = rlTime
  57. }
  58. isTiming := 0 // 用来判断该设备的回路 是否定时
  59. //控制回路 时间判断 是否关闭 当前时间是否等与规定时间 规定时间到规定时间+10分钟
  60. //因为1分钟执行一次,所以会多次执行,则新加判断 如果数据库中状态和回路状态相同则不执行下去
  61. layout := "15:04"
  62. rTimeCondition1OnTime, _ := time.Parse(layout, r.TimeCondition1OnTime)
  63. rTimeCondition1OffTime, _ := time.Parse(layout, r.TimeCondition1OffTime)
  64. rTimeCondition2OnTime, _ := time.Parse(layout, r.TimeCondition2OnTime)
  65. rTimeCondition2OffTime, _ := time.Parse(layout, r.TimeCondition2OffTime)
  66. rCurrent, _ := time.Parse(layout, current)
  67. if r.TimeCondition1OnTime != "关闭" &&
  68. (r.TimeCondition1OnTime == current ||
  69. (rCurrent.Before(rTimeCondition1OnTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition1OnTime))) {
  70. tmp[r.ID] = 1
  71. isTiming = 1
  72. }
  73. if r.TimeCondition1OffTime != "关闭" && (r.TimeCondition1OffTime == current ||
  74. (rCurrent.Before(rTimeCondition1OffTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition1OffTime))) {
  75. tmp[r.ID] = 0
  76. isTiming = 1
  77. }
  78. if r.TimeCondition2OnTime != "关闭" && (r.TimeCondition2OnTime == current ||
  79. (rCurrent.Before(rTimeCondition2OnTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition2OnTime))) {
  80. tmp[r.ID] = 1
  81. isTiming = 1
  82. }
  83. if r.TimeCondition2OffTime != "关闭" && (r.TimeCondition2OffTime == current ||
  84. (rCurrent.Before(rTimeCondition2OffTime.Add(11*time.Minute)) && rCurrent.After(rTimeCondition2OffTime))) {
  85. tmp[r.ID] = 0
  86. isTiming = 1
  87. }
  88. if isTiming == 1 {
  89. relayMap[d.Sn] = tmp
  90. }
  91. }
  92. }
  93. for key, value := range relayMap {
  94. reg, dev, err := utils.GetDataByDeviceId(key)
  95. if err != nil {
  96. logger.Get().Errorf("GetDataByDeviceId err = %s\n", err.Error())
  97. }
  98. for i, i2 := range value {
  99. data := modbus.DeviceLoopSwitch(i, i2)
  100. if conn1, ok := model.ConnectionMap1.Load(key); ok {
  101. // 成功找到连接
  102. netConn := conn1.(net.Conn)
  103. err := utils.WriteDevice(data, netConn)
  104. time.Sleep(1 * time.Second)
  105. if err != nil {
  106. logger.Get().Errorf("WriteDevice err = %s\n", err.Error())
  107. }
  108. for j, loop := range dev.DeviceLoops {
  109. if loop.ID == i {
  110. dev.DeviceLoops[j].State = i2
  111. }
  112. }
  113. } else {
  114. // 没有找到对应的连接
  115. logger.Get().Printf("Connection for key %s not found", key)
  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. logger.Get().Errorf("SaveRegionOnData err = %s\n", err.Error())
  126. }
  127. err = SaveData(data)
  128. if err != nil {
  129. logger.Get().Errorf("SaveData err = %s\n", err.Error())
  130. }
  131. }
  132. }