myTool.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "os"
  7. "server/dao"
  8. "time"
  9. )
  10. const connDataFilePath = "static/devicePath.json"
  11. const dataFilePath = "static/data.json"
  12. // LoadData 从文件读取数据
  13. func LoadData() (regions []dao.Region, err error) {
  14. data, err := os.ReadFile(dataFilePath)
  15. if err != nil {
  16. if os.IsNotExist(err) {
  17. return nil, nil
  18. }
  19. return nil, err
  20. }
  21. err = json.Unmarshal(data, &regions)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return regions, nil
  26. }
  27. func GetOnlineDevices() (devices []dao.Device, err error) {
  28. regions, err := LoadData()
  29. if err != nil {
  30. return nil, err
  31. }
  32. for _, region := range regions {
  33. for _, device := range region.Devices {
  34. if device.State == 0 {
  35. continue
  36. }
  37. devices = append(devices, device)
  38. }
  39. }
  40. return devices, err
  41. }
  42. func GetAllDevices() (devices []dao.Device, err error) {
  43. regions, err := LoadData()
  44. if err != nil {
  45. return nil, err
  46. }
  47. for _, region := range regions {
  48. for _, device := range region.Devices {
  49. devices = append(devices, device)
  50. }
  51. }
  52. return devices, err
  53. }
  54. // GetDataByDeviceId 按设备id获取信息 地区+设备
  55. func GetDataByDeviceId(deviceId string) (reg dao.Region, dev dao.Device, err error) {
  56. regions, err := LoadData()
  57. if err != nil {
  58. return reg, dev, err
  59. }
  60. for _, region := range regions {
  61. for _, device := range region.Devices {
  62. if device.Sn == deviceId {
  63. return region, device, nil
  64. }
  65. }
  66. }
  67. return reg, dev, fmt.Errorf("设备%s未找到", deviceId)
  68. }
  69. func SaveRegionOnData(data dao.Region) ([]dao.Region, error) {
  70. regions, err := LoadData()
  71. if err != nil {
  72. return regions, err
  73. }
  74. for i, region := range regions {
  75. if region.Name == data.Name {
  76. regions[i] = data
  77. }
  78. }
  79. return regions, nil
  80. }
  81. // SaveData 保存数据到文件
  82. func SaveData(path string, parameter interface{}) error {
  83. data, err := json.MarshalIndent(parameter, "", " ")
  84. if err != nil {
  85. return err
  86. }
  87. return os.WriteFile(path, data, 0644)
  88. }
  89. // CalculateCircuitSum 计算给定回路数量的总和
  90. func CalculateCircuitSum(circuits int) (int, error) {
  91. if circuits <= 0 {
  92. return 0, fmt.Errorf("回路数量必须大于0")
  93. }
  94. // 计算结果
  95. result := (1 << circuits) - 1
  96. return result, nil
  97. }
  98. // WriteDevice 纯写
  99. func WriteDevice(frame []byte, conn net.Conn) error {
  100. _, err := conn.Write(frame)
  101. if err != nil {
  102. fmt.Println("Error writing to connection:", err)
  103. return err
  104. }
  105. return nil
  106. }
  107. // ReadDevice 纯读
  108. func ReadDevice(conn net.Conn) (data []byte) {
  109. buffer := make([]byte, 1024)
  110. n, err := conn.Read(buffer)
  111. if err != nil {
  112. fmt.Println("Error reading from connection:", err)
  113. return
  114. }
  115. fmt.Printf("Received %d bytes: %X\n", n, buffer[6:n])
  116. return buffer
  117. }
  118. // WriteAndReadDevice 又写又读
  119. func WriteAndReadDevice(frame []byte, conn net.Conn, former, after int) (data []byte, err error) {
  120. // 发送 Modbus RTU 帧
  121. //FE 04 03 EE 00 08 85 B2
  122. n, err := conn.Write(frame)
  123. if err != nil {
  124. fmt.Println("Error writing to connection:", err)
  125. return
  126. }
  127. time.Sleep(1000 * time.Millisecond)
  128. buffer := make([]byte, 1024)
  129. // 读取响应
  130. n, err = conn.Read(buffer)
  131. if err != nil {
  132. fmt.Println("Error reading from connection:", err)
  133. return
  134. }
  135. return buffer[former : n-after], err
  136. }