device.go 2.5 KB

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