device.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //err := utils.SaveData("static/data.json", data)
  17. //if err != nil {
  18. // return err
  19. //}
  20. // Remove the redundant return err statement.
  21. return nil // Return nil to indicate success
  22. }
  23. func DeviceSave(device dao.Device) error {
  24. regions, _ := utils.LoadData()
  25. for i, region := range regions {
  26. for j, d := range region.Devices {
  27. if device.Sn == d.Sn {
  28. regions[i].Devices[j] = device
  29. }
  30. }
  31. }
  32. err := SaveData(regions)
  33. if err != nil {
  34. return err
  35. }
  36. // Remove the redundant return err statement.
  37. return nil // Return nil to indicate success
  38. }
  39. func DevicesSave(device []dao.Device) error {
  40. regions, _ := utils.LoadData()
  41. regions[0].Devices = device
  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. if conn1, ok := model.ConnectionMap1.Load(deviceLoop.DeviceId); ok {
  52. // 成功找到连接
  53. netConn := conn1.(net.Conn)
  54. _, dev, _ := utils.GetDataByDeviceId(deviceLoop.DeviceId)
  55. if dev.State == 0 {
  56. return fmt.Errorf("设备离线无法操作")
  57. }
  58. err := utils.WriteDevice(data, netConn)
  59. if err != nil {
  60. fmt.Printf("Write device error: %v\n", err)
  61. return err
  62. }
  63. } else {
  64. // 没有找到对应的连接
  65. logger.Get().Printf("Connection for key %s not found", deviceLoop.DeviceId)
  66. }
  67. return nil
  68. }
  69. func DeviceSwitch(req model.DeviceRequest) error {
  70. data := modbus.DeviceSwitch(req.Device.LoopNumber, req.State)
  71. if conn1, ok := model.ConnectionMap1.Load(req.Device.Sn); ok {
  72. // 成功找到连接
  73. netConn := conn1.(net.Conn)
  74. modbus.OperationCommand = data
  75. err := utils.WriteDevice(data, netConn)
  76. if err != nil {
  77. logger.Get().Errorf("Write device error: %v\n", err)
  78. return err
  79. }
  80. } else {
  81. // 没有找到对应的连接
  82. logger.Get().Printf("Connection for key %s not found", req.Device.Sn)
  83. }
  84. return nil
  85. }
  86. func DeviceBatchSwitch(req model.DevicesRequest) error {
  87. for _, device := range req.Devices {
  88. if device.State == 0 {
  89. continue
  90. }
  91. data := modbus.DeviceSwitch(device.LoopNumber, req.State)
  92. modbus.OperationCommand = data
  93. if conn1, ok := model.ConnectionMap1.Load(device.Sn); ok {
  94. // 成功找到连接
  95. netConn := conn1.(net.Conn)
  96. err := utils.WriteDevice(data, netConn)
  97. if err != nil {
  98. fmt.Printf("Write device error: %v\n", err)
  99. return err
  100. }
  101. } else {
  102. // 没有找到对应的连接
  103. logger.Get().Printf("Connection for key %s not found", device.Sn)
  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. }
  134. func UpdateRegisterAddress(deviceId string) error {
  135. if conn1, ok := model.ConnectionMap1.Load(deviceId); ok {
  136. // 成功找到连接
  137. netConn := conn1.(net.Conn)
  138. err := utils.WriteDevice(modbus.UpdateRegisterAddress(), netConn)
  139. if err != nil {
  140. logger.Get().Errorf("UpdateRegisterAddress error: %v\n", err)
  141. return err
  142. }
  143. } else {
  144. // 没有找到对应的连接
  145. logger.Get().Printf("Connection for key %s not found", deviceId)
  146. }
  147. return nil
  148. }