device.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "fmt"
  4. "net"
  5. "server/dao"
  6. "server/logger"
  7. "server/modbus"
  8. "server/model"
  9. "server/utils"
  10. )
  11. func SaveData(data []dao.Region) error {
  12. if len(data) == 0 {
  13. return fmt.Errorf("数据不能为空")
  14. }
  15. utils.SaveRegions(data)
  16. return nil
  17. }
  18. func DeviceSave(device dao.Device) error {
  19. regions, _ := utils.LoadData()
  20. for i, region := range regions {
  21. for j, d := range region.Devices {
  22. if device.Sn == d.Sn {
  23. regions[i].Devices[j] = device
  24. }
  25. }
  26. }
  27. err := SaveData(regions)
  28. if err != nil {
  29. return err
  30. }
  31. go utils.SaveData("./static/data.json", regions)
  32. return nil
  33. }
  34. func DevicesSave(devices []dao.Device) error {
  35. regions, _ := utils.LoadData()
  36. for i, rd := range regions[0].Devices {
  37. for _, d := range devices {
  38. if rd.Sn == d.Sn {
  39. regions[0].Devices[i] = d
  40. }
  41. }
  42. }
  43. err := SaveData(regions)
  44. if err != nil {
  45. return err
  46. }
  47. go utils.SaveData("./static/data.json", regions)
  48. // Remove the redundant return err statement.
  49. return nil // Return nil to indicate success
  50. }
  51. func DeviceLoopSwitch(deviceLoop dao.DeviceLoop) error {
  52. data := modbus.DeviceLoopSwitch(deviceLoop.ID, deviceLoop.State)
  53. _, dev1, _ := utils.GetDataByDeviceId(deviceLoop.DeviceId)
  54. if conn1, ok := model.ConnectionMap1.Load(dev1.Ip); ok {
  55. // 成功找到连接
  56. netConn := conn1.(net.Conn)
  57. _, dev, _ := utils.GetDataByDeviceIP(dev1.Ip)
  58. if dev.State == 0 {
  59. return fmt.Errorf("设备离线无法操作")
  60. }
  61. err := utils.WriteDevice(data, netConn)
  62. if err != nil {
  63. logger.Get().Errorf("设备回路开关 写命令错误: %s -- conn: %v", err, netConn.RemoteAddr().String())
  64. return err
  65. }
  66. } else {
  67. // 没有找到对应的连接
  68. logger.Get().Printf("设备回路开关Connection for key %s not found", dev1.Ip)
  69. }
  70. return nil
  71. }
  72. func DeviceSwitch(req model.DeviceRequest) error {
  73. data := modbus.DeviceSwitch(req.Device.LoopNumber, req.State)
  74. if conn1, ok := model.ConnectionMap1.Load(req.Device.Ip); ok {
  75. // 成功找到连接
  76. netConn := conn1.(net.Conn)
  77. modbus.OperationCommand = data
  78. err := utils.WriteDevice(data, netConn)
  79. if err != nil {
  80. logger.Get().Errorf("设备开关 写命令错误: %s -- conn: %v", err, netConn.RemoteAddr().String())
  81. return err
  82. }
  83. } else {
  84. // 没有找到对应的连接
  85. logger.Get().Printf("设备开关Connection for key %s not found", req.Device.Ip)
  86. }
  87. return nil
  88. }
  89. func DeviceBatchSwitch(req model.DevicesRequest) error {
  90. for _, device := range req.Devices {
  91. if device.State == 0 {
  92. continue
  93. }
  94. data := modbus.DeviceSwitch(device.LoopNumber, req.State)
  95. modbus.OperationCommand = data
  96. if conn1, ok := model.ConnectionMap1.Load(device.Ip); ok {
  97. // 成功找到连接
  98. netConn := conn1.(net.Conn)
  99. err := utils.WriteDevice(data, netConn)
  100. if err != nil {
  101. logger.Get().Errorf("设备批量控制 写命令错误: %s -- conn: %v", err, netConn.RemoteAddr().String())
  102. }
  103. } else {
  104. // 没有找到对应的连接
  105. logger.Get().Printf("设备批量控制Connection for key %s not found", device.Ip)
  106. }
  107. }
  108. return nil
  109. }
  110. // GetOnlineDevice 获取在线设备 总共设备 在线率
  111. func GetOnlineDevice() (total, online int) {
  112. devices, err := utils.GetOnlineDevices()
  113. if err != nil {
  114. logger.Get().Errorf("GetOnlineDevices err = %s\n", err.Error())
  115. return
  116. }
  117. allDevices, err := utils.GetAllDevices()
  118. if err != nil {
  119. logger.Get().Errorf("GetAllDevices err = %s\n", err.Error())
  120. return
  121. }
  122. return len(allDevices), len(devices)
  123. }
  124. func GetSunDevices() (devices []dao.Device, err error) {
  125. allDevices, err := utils.GetAllDevices()
  126. if err != nil {
  127. return nil, err
  128. }
  129. for _, device := range allDevices {
  130. if device.IsSun {
  131. devices = append(devices, device)
  132. }
  133. }
  134. return
  135. }