DeviceParameter.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package Handle
  2. import (
  3. "ColdP_server/controllers/MqttServer"
  4. "ColdP_server/controllers/lib"
  5. "ColdP_server/models/Device"
  6. "ColdP_server/models/System"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. func Go_Read_DeviceParameter() {
  12. for true {
  13. for k, _ := range lib.CountryRead_DeviceParameterSnMap {
  14. fmt.Println("CountryRead_DeviceParameterSnMap => KEY", k)
  15. Read_DeviceParameter(k)
  16. delete(lib.CountryRead_DeviceParameterSnMap, k)
  17. }
  18. time.Sleep(1)
  19. }
  20. }
  21. func init() {
  22. //go Go_Read_DeviceParameter()
  23. }
  24. func Read_DeviceParameter(T_sn string) {
  25. // base 读取基本参数
  26. Msid := lib.Random(1, 9999)
  27. Rt_parameter := MqttServer.Rt_Parameter{
  28. Sn: T_sn,
  29. Type: 3,
  30. Param: "base",
  31. Msid: (Msid * 10) + 1,
  32. }
  33. // offset 温湿度补偿
  34. Msid = lib.Random(1, 9999)
  35. Rt_parameter.Param = "offset"
  36. Rt_parameter.Msid = (Msid * 10) + 3
  37. // 回复
  38. jsonStu, err := json.Marshal(Rt_parameter)
  39. if err != nil {
  40. System.Add_Logs("MqttServer", "参数请求 [Rt_Parameter]", "offset")
  41. fmt.Println("回复失败 [Rt_Parameter],err=", err)
  42. }
  43. fmt.Println(string(jsonStu))
  44. MqttServer.Mqtt_publish(T_sn, string(jsonStu))
  45. //lib.CountryMsidMap[(Msid*10)+3] = T_uuid // Mqtt 消息透传
  46. return
  47. }
  48. func Pu_DeviceParameter(T_uuid string, T_sn string, Rt_parameter_base_ MqttServer.Rt_Parameter_Base_) {
  49. // base 读取基本参数
  50. Msid := lib.Random(1, 9999)
  51. Rt_parameter := MqttServer.Rt_Parameter_Base{
  52. Sn: T_sn,
  53. Type: 4,
  54. Msid: (Msid * 10) + 1,
  55. Base: Rt_parameter_base_,
  56. }
  57. // 回复
  58. jsonStu, err := json.Marshal(Rt_parameter)
  59. if err != nil {
  60. System.Add_Logs("设备配置", "MQTT回复失败 [Pu_DeviceParameter]", string(jsonStu))
  61. fmt.Println("设备配置 MQTT回复失败 [Pu_DeviceParameter],err=", err)
  62. return
  63. }
  64. fmt.Println(string(jsonStu))
  65. MqttServer.Mqtt_publish(T_sn, string(jsonStu))
  66. //lib.CountryMsidMap[(Msid*10)+1] = T_uuid // Mqtt 消息透传
  67. Deviceparameter := Device.DeviceParameter{
  68. T_sn: T_sn,
  69. T_devName: Rt_parameter_base_.T_devName,
  70. T_uploadTime: Rt_parameter_base_.T_uploadTime,
  71. T_saveTime: Rt_parameter_base_.T_saveTime,
  72. T_overrunSave: Rt_parameter_base_.T_overrunSave,
  73. T_overrunAlarm: Rt_parameter_base_.T_overrunAlarm,
  74. T_outageAlarm: Rt_parameter_base_.T_outageAlarm,
  75. T_lostAlarm: Rt_parameter_base_.T_lostAlarm,
  76. T_warningTime: Rt_parameter_base_.T_warningTime,
  77. T_warningDelay: Rt_parameter_base_.T_warningDelay,
  78. T_batteryLimit: Rt_parameter_base_.T_batteryLimit,
  79. T_tempPre: Rt_parameter_base_.T_tempPre,
  80. T_humPre: Rt_parameter_base_.T_humPre,
  81. T_enwarning: Rt_parameter_base_.T_enwarning,
  82. T_uuid: T_uuid,
  83. T_Msid: (Msid * 10) + 1,
  84. T_SendState: 0,
  85. T_State: 2,
  86. }
  87. if !Device.Add_DeviceParameter(Deviceparameter) {
  88. System.Add_Logs("设备配置", "插入失败 [Pu_DeviceParameter]", string(jsonStu))
  89. fmt.Println("设备配置 [Pu_DeviceParameter],err=", err)
  90. }
  91. return
  92. }
  93. func Pu_DeviceParameter_Sensor(T_uuid string, T_sn string, Rt_parameter_sensor_ []MqttServer.Rt_Parameter_Sensor_) {
  94. // base 读取基本参数
  95. Msid := lib.Random(1, 9999)
  96. Rt_parameter := MqttServer.Rt_Parameter_Sensor{
  97. Sn: T_sn,
  98. Type: 4,
  99. Msid: (Msid * 10) + 2,
  100. Sensor: Rt_parameter_sensor_,
  101. }
  102. // 回复
  103. jsonStu, err := json.Marshal(Rt_parameter)
  104. if err != nil {
  105. System.Add_Logs("设备配置", "MQTT回复失败 [Pu_DeviceParameter_Sensor]", string(jsonStu))
  106. fmt.Println("设备配置 MQTT回复失败 [Pu_DeviceParameter_Sensor],err=", err)
  107. }
  108. fmt.Println(string(jsonStu))
  109. MqttServer.Mqtt_publish(T_sn, string(jsonStu))
  110. //lib.CountryMsidMap[(Msid*10)+2] = T_uuid // Mqtt 消息透传
  111. //
  112. ///
  113. for _, v := range Rt_parameter_sensor_ {
  114. // 记录传感器
  115. Devicesensorparameter := Device.DeviceSensorParameter{
  116. T_sn: T_sn,
  117. T_id: v.T_id,
  118. T_name: v.T_name,
  119. T_Tlower: v.T_Tlower,
  120. T_Tupper: v.T_Tupper,
  121. T_RHlower: v.T_RHlower,
  122. T_RHupper: v.T_RHupper,
  123. T_en: v.T_en,
  124. T_free: v.T_free,
  125. T_uuid: T_uuid,
  126. T_Msid: (Msid * 10) + 2,
  127. T_SendState: 0,
  128. T_State: 2,
  129. }
  130. if !Device.Add_DeviceSensorParameter(Devicesensorparameter) {
  131. System.Add_Logs("设备配置", "插入失败 [Pu_DeviceParameter_Sensor]", string(jsonStu))
  132. fmt.Println("设备配置 [Pu_DeviceParameter_Sensor],err=", err)
  133. }
  134. }
  135. return
  136. }
  137. func Pu_DeviceParameter_Compensate(T_uuid string, T_sn string, Rt_parameter_compensate_ []MqttServer.Rt_Parameter_Compensate_) {
  138. // base 读取基本参数
  139. Msid := lib.Random(1, 9999)
  140. Rt_parameter_offset := MqttServer.Rt_Parameter_Offset{
  141. Sn: T_sn,
  142. Type: 4,
  143. Msid: (Msid * 10) + 3,
  144. Offset: Rt_parameter_compensate_,
  145. }
  146. // 回复
  147. jsonStu, err := json.Marshal(Rt_parameter_offset)
  148. if err != nil {
  149. System.Add_Logs("MqttServer", "参数请求 [Pu_DeviceParameter_Compensate]", "Offset")
  150. fmt.Println("回复失败 [Pu_DeviceParameter_Compensate],err=", err)
  151. }
  152. fmt.Println(string(jsonStu))
  153. MqttServer.Mqtt_publish(T_sn, string(jsonStu))
  154. // lib.CountryMsidMap[(Msid*10)+3] = T_uuid // Mqtt 消息透传
  155. ///
  156. return
  157. }