device.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "fmt"
  4. "server/dao"
  5. "server/logger"
  6. "server/modbus"
  7. "server/model"
  8. "server/utils"
  9. )
  10. func SaveData(data []dao.Region) error {
  11. if len(data) == 0 {
  12. return fmt.Errorf("数据不能为空")
  13. }
  14. err := utils.SaveData("static/data.json", data)
  15. if err != nil {
  16. return err
  17. }
  18. // Remove the redundant return err statement.
  19. return nil // Return nil to indicate success
  20. }
  21. func DeviceLoopSwitch(deviceLoop dao.DeviceLoop) error {
  22. data := modbus.DeviceLoopSwitch(deviceLoop.ID, deviceLoop.State)
  23. if model.ConnectionMap[deviceLoop.DeviceId] == nil {
  24. return fmt.Errorf("设备连接丢失")
  25. }
  26. _, dev, _ := utils.GetDataByDeviceId(deviceLoop.DeviceId)
  27. if dev.State == 0 {
  28. return fmt.Errorf("设备离线无法操作")
  29. }
  30. err := utils.WriteDevice(dev.Sn, data, model.ConnectionMap[deviceLoop.DeviceId])
  31. if err != nil {
  32. fmt.Printf("Write device error: %v\n", err)
  33. return err
  34. }
  35. return nil
  36. }
  37. func DeviceSwitch(req model.DeviceRequest) error {
  38. data := modbus.DeviceSwitch(req.Device.LoopNumber, req.State)
  39. if model.ConnectionMap[req.Device.Sn] == nil {
  40. return fmt.Errorf("设备连接丢失")
  41. }
  42. err := utils.WriteDevice(req.Device.Sn, data, model.ConnectionMap[req.Device.Sn])
  43. if err != nil {
  44. logger.Get().Errorf("Write device error: %v\n", err)
  45. return err
  46. }
  47. return nil
  48. }
  49. func DeviceBatchSwitch(req model.DevicesRequest) error {
  50. for _, device := range req.Devices {
  51. if device.State == 0 {
  52. continue
  53. }
  54. data := modbus.DeviceSwitch(device.LoopNumber, req.State)
  55. if model.ConnectionMap[device.Sn] == nil {
  56. return fmt.Errorf("设备连接丢失")
  57. }
  58. err := utils.WriteDevice(device.Sn, data, model.ConnectionMap[device.Sn])
  59. if err != nil {
  60. fmt.Printf("Write device error: %v\n", err)
  61. return err
  62. }
  63. }
  64. return nil
  65. }
  66. // GetOnlineDevice 获取在线设备 总共设备 在线率
  67. func GetOnlineDevice() (total, online int) {
  68. devices, err := utils.GetOnlineDevices()
  69. if err != nil {
  70. logger.Get().Errorf("GetOnlineDevices err = %s\n", err.Error())
  71. return
  72. }
  73. allDevices, err := utils.GetAllDevices()
  74. if err != nil {
  75. logger.Get().Errorf("GetAllDevices err = %s\n", err.Error())
  76. return
  77. }
  78. return len(allDevices), len(devices)
  79. }
  80. func GetSunDevices() (devices []dao.Device, err error) {
  81. allDevices, err := utils.GetAllDevices()
  82. if err != nil {
  83. return nil, err
  84. }
  85. for _, device := range allDevices {
  86. if device.IsSun {
  87. devices = append(devices, device)
  88. }
  89. }
  90. return
  91. }
  92. func UpdateRegisterAddress(deviceId string) error {
  93. err := utils.WriteDevice(deviceId, modbus.UpdateRegisterAddress(), model.ConnectionMap[deviceId])
  94. if err != nil {
  95. logger.Get().Errorf("UpdateRegisterAddress error: %v\n", err)
  96. return err
  97. }
  98. return nil
  99. }