device.go 2.6 KB

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