device.go 3.5 KB

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