V3MqttControllers.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package MqttServer
  2. import (
  3. "Cold_mqtt/conf"
  4. "Cold_mqtt/lib"
  5. "Cold_mqtt/logs"
  6. "Cold_mqtt/models/Account"
  7. "Cold_mqtt/models/Device"
  8. "Cold_mqtt/models/Product"
  9. "encoding/hex"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/astaxie/beego/cache"
  13. _ "github.com/astaxie/beego/cache/redis"
  14. "math/rand"
  15. "strconv"
  16. "time"
  17. )
  18. var redis_V3MqttControllers cache.Cache
  19. func init() {
  20. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  21. "redis_V3MqttControllers", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  22. logs.Println(config)
  23. var err error
  24. redis_V3MqttControllers, err = cache.NewCache("redis", config)
  25. if err != nil || redis_V3MqttControllers == nil {
  26. errMsg := "failed to init redis"
  27. logs.Println(errMsg, err)
  28. }
  29. }
  30. func Read_DeviceParameter3(T_sn string) {
  31. logs.Println("Read_DeviceParameter:", T_sn)
  32. Rt_parameter := Ms2_Project{
  33. Type: 3,
  34. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  35. }
  36. // 回复
  37. jsonStu, err := json.Marshal(Rt_parameter)
  38. if err != nil {
  39. logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
  40. logs.Println("回复失败 [Rt_Parameter],err=", err)
  41. }
  42. logs.Println(string(jsonStu))
  43. // 缓存数据
  44. DeviceMqttMapAdd(T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: T_sn, T_data: string(jsonStu), T_num: 10})
  45. Mqtt_publish(T_sn, string(jsonStu))
  46. //lib.CountryMsidMap[(Msid*10)+1] = T_uuid // Mqtt 消息透传
  47. //go func() {
  48. // time.Sleep(3 * time.Second)
  49. // Read_DeviceSensorParameter3(T_sn)
  50. //}()
  51. return
  52. }
  53. func Read_DeviceSensorParameter3(T_sn string) {
  54. time.Sleep(1 * time.Second)
  55. logs.Println("Read_DeviceSensorParameter:", T_sn)
  56. // base 读取基本参数
  57. Rt_parameter := Ms_Read_DeviceSensorParameter{
  58. Type: 5,
  59. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  60. Data: []int{},
  61. }
  62. // 回复
  63. jsonStu, err := json.Marshal(Rt_parameter)
  64. if err != nil {
  65. logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "sensor")
  66. logs.Println("回复失败 [Rt_Parameter],err=", err)
  67. }
  68. logs.Println(string(jsonStu))
  69. // 缓存数据
  70. DeviceMqttMapAdd(T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: T_sn, T_data: string(jsonStu), T_num: 10})
  71. Mqtt_publish(T_sn, string(jsonStu))
  72. //lib.CountryMsidMap[(Msid*10)+2] = T_uuid // Mqtt 消息透传
  73. return
  74. }
  75. // 修改设备参数
  76. func Pu_DeviceParameter3(Rt_DeviceParameter Device.DeviceParameter) {
  77. Rt_parameter := Ms_Project2_Device_Parameter{
  78. //Sn: Rt_DeviceParameter.T_sn,
  79. Type: 4,
  80. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  81. }
  82. T_prt := "未知公司"
  83. r_Device, err := Device.Read_Device_ByT_sn(Rt_DeviceParameter.T_sn)
  84. if err == nil {
  85. _, Company_r := Account.Read_Company_id(r_Device.T_pid)
  86. T_prt = Company_r.T_name
  87. }
  88. //T_prt, err = iconvx.ConvertString(T_prt, "utf-8", "GBK")
  89. //T_prt = hex.EncodeToString( []byte(T_prt))
  90. //
  91. T_prt_b, err := lib.Utf8ToGbk([]byte(T_prt))
  92. //T_prt = string(T_prt_b)
  93. T_prt = hex.EncodeToString(T_prt_b)
  94. Rt_parameter.Data = Ms_Project_Device_Parameter_Data{
  95. T_name: Rt_DeviceParameter.T_name,
  96. T_saveT: Rt_DeviceParameter.T_saveT,
  97. T_overA: Rt_DeviceParameter.T_overA,
  98. T_outA: Rt_DeviceParameter.T_outA,
  99. T_lostA: Rt_DeviceParameter.T_lostA,
  100. T_scan: Rt_DeviceParameter.T_scan,
  101. T_bat: Rt_DeviceParameter.T_bat,
  102. T_warn: Rt_DeviceParameter.T_warn,
  103. T_warnD: Rt_DeviceParameter.T_warnD,
  104. T_dormancy: Rt_DeviceParameter.T_dormancy,
  105. T_snum: Rt_DeviceParameter.T_snum,
  106. T_prt: T_prt,
  107. T_btname: Rt_DeviceParameter.T_btname,
  108. T_btsid: Rt_DeviceParameter.T_btsid,
  109. T_btchar: Rt_DeviceParameter.T_btchar,
  110. }
  111. // 更新 消息ID
  112. Rt_DeviceParameter.T_Msid = Rt_parameter.Msid
  113. Device.Update_DeviceParameter(Rt_DeviceParameter, "T_Msid")
  114. // 发布数据
  115. jsonStu, err := json.Marshal(Rt_parameter)
  116. if err != nil {
  117. logs.Println("设备配置", "MQTT回复失败 [Pu_DeviceParameter]", string(jsonStu))
  118. logs.Println("设备配置 MQTT回复失败 [Pu_DeviceParameter],err=", err)
  119. return
  120. }
  121. logs.Println(string(jsonStu))
  122. // 缓存数据
  123. DeviceMqttMapAdd(Rt_DeviceParameter.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: Rt_DeviceParameter.T_sn, T_data: string(jsonStu), T_num: 10})
  124. Mqtt_publish(Rt_DeviceParameter.T_sn, string(jsonStu))
  125. return
  126. }
  127. // 修改传感器参数
  128. func Pu_DeviceParameter_Sensor3(Rt_parameter_sensor Device.DeviceSensorParameter) {
  129. Rt_parameter := Ms_Project2_Sensor_Parameter{
  130. //Sn: Rt_parameter_sensor.T_sn,
  131. Type: 6,
  132. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  133. }
  134. // 强制修改 是否启用预警记录数据 1:启用 0:关闭 默认:1
  135. if Rt_parameter_sensor.T_enprelnote != 0 {
  136. Rt_parameter_sensor.T_enprelnote = 1
  137. }
  138. Rt_parameter.Data = append(Rt_parameter.Data, Ms_Project_Sensor_Parameter_Data{
  139. T_id: Rt_parameter_sensor.T_id,
  140. T_name: Rt_parameter_sensor.T_name,
  141. T_Tlower: Rt_parameter_sensor.T_Tlower,
  142. T_Tupper: Rt_parameter_sensor.T_Tupper,
  143. T_RHlower: Rt_parameter_sensor.T_RHlower,
  144. T_RHupper: Rt_parameter_sensor.T_RHupper,
  145. T_en: Rt_parameter_sensor.T_en,
  146. T_free: Rt_parameter_sensor.T_free,
  147. T_enprel: Rt_parameter_sensor.T_enprel,
  148. T_tprel: Rt_parameter_sensor.T_tprel,
  149. T_tpreu: Rt_parameter_sensor.T_tpreu,
  150. T_hprel: Rt_parameter_sensor.T_hprel,
  151. T_hpreu: Rt_parameter_sensor.T_hpreu,
  152. T_enprelnote: Rt_parameter_sensor.T_enprelnote,
  153. //T_speed: Rt_parameter_sensor.T_speed,
  154. //T_sense: Rt_parameter_sensor.T_sense,
  155. })
  156. // 回复
  157. jsonStu, err := json.Marshal(Rt_parameter)
  158. if err != nil {
  159. logs.Println("设备配置", "MQTT回复失败 [Pu_DeviceParameter_Sensor]", string(jsonStu))
  160. logs.Println("设备配置 MQTT回复失败 [Pu_DeviceParameter_Sensor2],err=", err)
  161. }
  162. // 更新 消息ID
  163. Rt_parameter_sensor.T_Msid = Rt_parameter.Msid
  164. // 缓存数据
  165. DeviceMqttMapAdd(Rt_parameter_sensor.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: Rt_parameter_sensor.T_sn, T_data: string(jsonStu), T_num: 10})
  166. Device.Update_DeviceSensorParameter(Rt_parameter_sensor, "T_Msid")
  167. logs.Println(string(jsonStu))
  168. Mqtt_publish(Rt_parameter_sensor.T_sn, string(jsonStu))
  169. return
  170. }
  171. func Set_DeviceTask3(v Device.Device_task) {
  172. Rt_parameter := Ms2_Project{
  173. Type: 11,
  174. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  175. Data: map[string]interface{}{},
  176. }
  177. if v.T_task == "start" {
  178. Rt_parameter.Data["task"] = 1
  179. } else {
  180. Rt_parameter.Data["task"] = 0
  181. }
  182. // 回复
  183. jsonStu, err := json.Marshal(Rt_parameter)
  184. if err != nil {
  185. logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
  186. logs.Println("回复失败 [Rt_Parameter],err=", err)
  187. }
  188. logs.Println(v.T_sn, "==>", string(jsonStu))
  189. // 缓存数据
  190. DeviceMqttMapAdd(v.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: v.T_sn, T_data: string(jsonStu), T_num: 30})
  191. Mqtt_publish(v.T_sn, string(jsonStu))
  192. return
  193. }
  194. func Set_DeviceRestart3(v Device.Device_task) {
  195. Rt_parameter := Ms2_Project{
  196. Type: 97,
  197. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  198. Data: map[string]interface{}{},
  199. }
  200. if v.T_task == "1" {
  201. Rt_parameter.Data["task"] = 1
  202. } else {
  203. Rt_parameter.Data["task"] = 0
  204. }
  205. // 回复
  206. jsonStu, err := json.Marshal(Rt_parameter)
  207. if err != nil {
  208. logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
  209. logs.Println("回复失败 [Rt_Parameter],err=", err)
  210. }
  211. logs.Println(v.T_sn, "==>", string(jsonStu))
  212. // 缓存数据
  213. DeviceMqttMapAdd(v.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: v.T_sn, T_data: string(jsonStu), T_num: 30})
  214. Mqtt_publish(v.T_sn, string(jsonStu))
  215. return
  216. }
  217. func Get_Device_Realtime3(T_sn string) {
  218. Rt_parameter := Ms2_Project{
  219. Type: 0,
  220. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  221. }
  222. // 回复
  223. jsonStu, err := json.Marshal(Rt_parameter)
  224. if err != nil {
  225. logs.Println("MqttServer", "实时数据 [Rt_realtime]", T_sn)
  226. logs.Println("回复失败 [Rt_realtime],err=", err)
  227. }
  228. logs.Println(string(jsonStu))
  229. Mqtt_publish(T_sn, string(jsonStu))
  230. return
  231. }
  232. func Up_DeviceProductUpgrade(v Product.ProductUpgrade_T) {
  233. Rt_parameter := Ms2_Project{
  234. Type: 14,
  235. Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
  236. Data: map[string]interface{}{},
  237. }
  238. Rt_parameter.Data["version"] = v.T_version
  239. Rt_parameter.Data["bin"] = v.T_file
  240. // 回复
  241. jsonStu, err := json.Marshal(Rt_parameter)
  242. if err != nil {
  243. logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
  244. logs.Println("回复失败 [Rt_Parameter],err=", err)
  245. }
  246. logs.Println(v.T_sn, "==>", string(jsonStu))
  247. // 缓存数据
  248. DeviceMqttMapAdd(v.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: v.T_sn, T_data: string(jsonStu), T_num: 30})
  249. Mqtt_publish(v.T_sn, string(jsonStu))
  250. return
  251. }
  252. func RTC_TimeStr(T_sn string, Msid int64, Type int) {
  253. if Type == 99 {
  254. if redis_V3MqttControllers.IsExist(T_sn + "RTC_TimeStr") {
  255. return
  256. }
  257. redis_V3MqttControllers.Put(T_sn+"RTC_TimeStr", "1", 2*time.Minute)
  258. }
  259. Time := time.Now()
  260. Rt_Project_Data_r := Ms2_Project{
  261. Type: Type,
  262. Msid: Msid,
  263. }
  264. var Data map[string]interface{}
  265. Data = make(map[string]interface{})
  266. Data["ut"] = Time.Unix()
  267. Data["date"] = Time.Format("2006-01-02 15:04:05")
  268. Rt_Project_Data_r.Data = Data
  269. // 回复
  270. jsonStu, err := json.Marshal(Rt_Project_Data_r)
  271. if err != nil {
  272. return
  273. }
  274. Mqtt_publish(T_sn, string(jsonStu))
  275. return
  276. }