V3MqttHandle.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. package MqttServer
  2. import (
  3. "Cold_mqtt/MqttServer/WarningNotice"
  4. "Cold_mqtt/Nats/NatsServer"
  5. "Cold_mqtt/lib"
  6. "Cold_mqtt/logs"
  7. "Cold_mqtt/models/Device"
  8. "Cold_mqtt/models/Product"
  9. "Cold_mqtt/models/Warning"
  10. "encoding/json"
  11. "fmt"
  12. "io/ioutil"
  13. "math"
  14. "net/http"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. func AsyncFuncV3(r_Device *Device.Device, Ms_project Ms2m_Project, msg []byte) {
  20. ms2_Return := Ms2_Return{
  21. Type: Ms_project.Type,
  22. Msid: Ms_project.Msid,
  23. Status: 1,
  24. }
  25. switch Ms_project.Type {
  26. case 0: // 0:实时数据
  27. var ms2_Project_list Ms2_Project_list
  28. err := json.Unmarshal(msg, &ms2_Project_list)
  29. if err != nil {
  30. logs.Println("MqttServer", "JSON反序列化失败[Ms_project_0]", string(msg))
  31. logs.Println("JSON反序列化失败[Ms_project_0],err=", err)
  32. return
  33. }
  34. for _, v := range ms2_Project_list.Data {
  35. // 是否存在传感器 不存在 跳过
  36. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, lib.To_int(v["id"]))
  37. if !is {
  38. logs.Println("MqttServer", "实时数据 传感器不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
  39. continue
  40. }
  41. // lost 字段为传感器离线状态(1代表离线,在线不传此字段),如果离线 温湿度时间数据为空
  42. if _, lost_is := v["lost"]; lost_is {
  43. DeviceSensor_r.T_link = 0
  44. Device.Update_DeviceSensor(DeviceSensor_r, "T_link")
  45. Warning.Add_DeviceLogs(103, *r_Device, fmt.Sprintf("[%d]%s: 传感器异常断开!", DeviceSensor_r.T_id, DeviceSensor_r.T_name))
  46. // 发现 lost 此字段 代表 传感器异常!!!!!
  47. continue
  48. }
  49. int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
  50. UT := time.Unix(int64_, 0)
  51. logs.Println(UT.Format("2006-01-02 15:04:05"))
  52. DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(r_Device.T_sn, DeviceSensor_r.T_id)
  53. if !is {
  54. logs.Println("MqttServer", "记录数据 传感器参数不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
  55. continue
  56. }
  57. //// 更新记录 - 缓存
  58. DeviceData_t := Device.DeviceData_R{
  59. T_t: float32(math.Ceil(float64(lib.To_float32(v["t"])*10)) / 10),
  60. T_rh: float32(math.Ceil(float64(lib.To_float32(v["h"])*10)) / 10),
  61. T_Site: lib.To_string(v["s"]),
  62. T_time: UT,
  63. T_sp: DeviceSensorParameter_r.Id,
  64. }
  65. Device.RedisDeviceData_Set(r_Device.T_sn, DeviceSensor_r.T_id, DeviceData_t)
  66. r_Device.T_Site = DeviceData_t.T_Site
  67. // 传感器属于 连接状态
  68. if DeviceSensor_r.T_link != 1 {
  69. DeviceSensor_r.T_link = 1
  70. Device.Update_DeviceSensor(DeviceSensor_r, "T_link")
  71. }
  72. }
  73. Device.Update_Device_new(*r_Device, "T_Site")
  74. // 反馈透传
  75. NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
  76. return
  77. case 1: // 1:记录数据
  78. var ms2_Project_list Ms2_Project_list
  79. err := json.Unmarshal(msg, &ms2_Project_list)
  80. if err != nil {
  81. logs.Println("MqttServer", "JSON反序列化失败[Ms_project_1]", string(msg))
  82. logs.Println("JSON反序列化失败[Ms_project_1],err=", err)
  83. return
  84. }
  85. // 回复
  86. ms2_Return.Status = 1
  87. for _, v := range ms2_Project_list.Data {
  88. int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
  89. UT := time.Unix(int64_, 0)
  90. //时间异常 大于10s,直接丢掉数据
  91. if (UT.Unix() - time.Now().Unix()) > 10 {
  92. ms2_Return.Status = 3
  93. continue
  94. }
  95. // 是否存在传感器 不存在 跳过
  96. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, lib.To_int(v["id"]))
  97. if !is {
  98. logs.Println("MqttServer", "记录数据 传感器不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
  99. continue
  100. }
  101. // 获取传感器参数
  102. DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(r_Device.T_sn, DeviceSensor_r.T_id)
  103. if !is {
  104. logs.Println("MqttServer", "记录数据 传感器参数不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
  105. continue
  106. }
  107. //// 更新记录 - 缓存
  108. DeviceData_t := Device.DeviceData_R{
  109. T_t: float32(math.Ceil(float64(lib.To_float32(v["t"])*10)) / 10),
  110. T_rh: float32(math.Ceil(float64(lib.To_float32(v["h"])*10)) / 10),
  111. T_Site: lib.To_string(v["s"]),
  112. T_time: UT,
  113. T_sp: DeviceSensorParameter_r.Id,
  114. }
  115. if r_, DeviceData_old_r := Device.Add_DeviceData(r_Device.T_sn, DeviceSensor_r.T_id, DeviceData_t); r_ {
  116. // 被替换
  117. Device.Add_DeviceDataOld(Device.DeviceDataOld{
  118. T_sn: r_Device.T_sn,
  119. T_id: DeviceSensor_r.T_id,
  120. T_t: DeviceData_old_r.T_t,
  121. T_rh: DeviceData_old_r.T_rh,
  122. T_Site: DeviceData_old_r.T_site,
  123. T_time: UT,
  124. T_operation: 2,
  125. T_uuid: "",
  126. })
  127. }
  128. r_Device.T_Site = DeviceData_t.T_Site // GPS
  129. // 监控状态 数据有效
  130. if (time.Now().Unix() - UT.Unix()) < 30 {
  131. r_Device.T_monitor = 1
  132. }
  133. }
  134. Device.Update_Device_new(*r_Device, "T_monitor", "T_Site")
  135. NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
  136. break
  137. case 2: // 2:设备报警
  138. var ms2_Project Ms2_Project_list
  139. err := json.Unmarshal(msg, &ms2_Project)
  140. if err != nil {
  141. logs.PrintlnError("MqttServer", "设备报警", string(msg))
  142. break
  143. }
  144. // 缓存报警ID,防止重复发送
  145. if Warning.Redis_Warning_Repeat_T_sn_Msid(r_Device.T_sn + "|" + strconv.FormatInt(ms2_Project.Msid, 10)) {
  146. logs.Println("MqttServer", " 存在报警 跳过:", string(msg))
  147. break
  148. }
  149. for _, v := range ms2_Project.Data {
  150. var Warning_r Warning.Warning
  151. Warning_r.T_pid = r_Device.T_pid
  152. Warning_r.T_tp = lib.To_int(v["tp"]) // 报警类型
  153. Warning_r.T_sn = r_Device.T_sn
  154. Warning_r.T_id = lib.To_int(v["id"])
  155. Warning_r.T_Msid = Ms_project.Msid
  156. Warning_r.T_D_name = r_Device.T_devName
  157. Warning_r.T_State = 1
  158. // 获取 传感器 参数
  159. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, Warning_r.T_id)
  160. if !is {
  161. logs.Println("MqttServer", " 传感器ID 不存在", string(msg))
  162. break
  163. }
  164. Warning_r.T_DS_name = DeviceSensor_r.T_name
  165. // 获取 传感器 配置参数
  166. DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(Warning_r.T_sn, Warning_r.T_id)
  167. if !is {
  168. logs.Println("MqttServer", " 传感器ID 配置参数不存在", string(msg))
  169. break
  170. }
  171. // 温度湿度 封装
  172. T := float32(math.Ceil(float64(lib.To_float32(v["t"])*10)) / 10)
  173. RH := float32(math.Ceil(float64(lib.To_float32(v["h"])*10)) / 10)
  174. // 持续时间计算
  175. time_int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
  176. time_int64_f, _ := strconv.ParseInt(lib.To_string(v["fut"]), 10, 64)
  177. if time_int64_f == 0 {
  178. time_int64_f = time_int64_
  179. }
  180. Warning_r.T_fUt = time.Unix(time_int64_f, 0)
  181. time_int64_f_x := (int(time_int64_) - int(time_int64_f))
  182. logs.Println(strconv.FormatInt(Warning_r.T_Msid, 10)+"持续时间:", time_int64_f_x)
  183. Warning_r.T_Ut = time.Unix(time_int64_, 0)
  184. // 如果记录时间大于当前时间
  185. //if time.Now().Unix() < time_int64_ {
  186. // Warning_r.T_Ut = time.Now()
  187. // Warning_r.T_Remark += "设备时间异常!"
  188. // // 推送同步 时间
  189. // go func() {
  190. // time.Sleep(time.Second * 3)
  191. // logs.Println(fmt.Sprintf("%s 时间相差:%d", r_Device.T_sn, int(math.Abs(float64(time_int64_-time.Now().Unix())))))
  192. // Warning.Add_DeviceLogs(103, *r_Device, fmt.Sprintf("时间相差:%d", int(math.Abs(float64(time_int64_-time.Now().Unix())))))
  193. // RTC_TimeStr(r_Device.T_sn, time.Now().Unix(), 99)
  194. // }()
  195. //}
  196. //时间异常 大于10s,直接丢掉数据
  197. if (time_int64_ - time.Now().Unix()) > 10 {
  198. ms2_Return.Status = 3
  199. continue
  200. }
  201. // 查看 sn 是否有归属
  202. if Warning_r.T_pid == 0 { // 寻找报警归属
  203. if len(Warning_r.T_sn) > 10 {
  204. r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn)
  205. if err != nil {
  206. logs.Println("MessageDisconnected 没有该设备:", Warning_r.T_sn)
  207. }
  208. Warning_r.T_pid = r_Device.T_pid
  209. }
  210. }
  211. // 报警数据没有按照两分钟间隔推送
  212. if Warning_r.T_tp == 1 || Warning_r.T_tp == 2 || Warning_r.T_tp == 4 || Warning_r.T_tp == 5 {
  213. continue
  214. }
  215. //tp 报警类型
  216. // 1 温度超上限报警
  217. // 2 温度超下限报警
  218. // 3 温度恢复正常
  219. // 4 湿度超上限报警
  220. // 5 湿度超下限报警
  221. // 6 湿度恢复正常
  222. // 7 温度超上限预警
  223. // 8 温度超下限预警
  224. // 9 温度预警恢复正常
  225. // 10 湿度超上限预警
  226. // 11 湿度超下限预警
  227. // 12 湿度预警恢复正常
  228. //当期湿度:88.0%(↓23.0~↑73.0),已持续1小时33秒
  229. if time_int64_f_x > 0 {
  230. Warning_r.T_Remark += "已持续 " + lib.MinuteToDataTime(time_int64_f_x)
  231. }
  232. //// 报警处理
  233. WarningNotice.WarningCompanyNotice(&Warning_r, T, RH)
  234. //封装具体信息
  235. switch {
  236. case Warning_r.T_tp == 1, Warning_r.T_tp == 2, Warning_r.T_tp == 3:
  237. Warning_r.T_Remark = "当前温度:" + strconv.FormatFloat(float64(T), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_Tlower), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_Tupper), 'f', -1, 32) + ")," + Warning_r.T_Remark
  238. break
  239. case Warning_r.T_tp == 4, Warning_r.T_tp == 5, Warning_r.T_tp == 6:
  240. Warning_r.T_Remark = "当前湿度:" + strconv.FormatFloat(float64(RH), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_RHlower), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_RHupper), 'f', -1, 32) + ")," + Warning_r.T_Remark
  241. break
  242. case Warning_r.T_tp == 7, Warning_r.T_tp == 8, Warning_r.T_tp == 9:
  243. Warning_r.T_Remark = "当前温度:" + strconv.FormatFloat(float64(T), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_tprel), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_tpreu), 'f', -1, 32) + ")," + Warning_r.T_Remark
  244. break
  245. case Warning_r.T_tp == 10, Warning_r.T_tp == 11, Warning_r.T_tp == 12:
  246. Warning_r.T_Remark = "当前湿度:" + strconv.FormatFloat(float64(RH), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_hprel), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_hpreu), 'f', -1, 32) + ")," + Warning_r.T_Remark
  247. break
  248. }
  249. // 添加报警
  250. _, err = Warning.Add_Warning(Warning_r)
  251. //// 不能 同时增加 记录数据,因为缺失GPS数据
  252. }
  253. NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
  254. break
  255. case 3: // 3:读取主机参数
  256. var ms2_Project Ms2_Project
  257. err := json.Unmarshal(msg, &ms2_Project)
  258. if err != nil {
  259. logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
  260. return
  261. }
  262. T_name := lib.To_string(ms2_Project.Data["name"])
  263. if len(T_name) == 0 {
  264. return
  265. }
  266. if T_name == "*" {
  267. T_name = r_Device.T_devName
  268. }
  269. if r_Device.T_devName == "未知类型" {
  270. T_name = Product.Read_ProductType_Get(r_Device.T_model)
  271. }
  272. if T_name == r_Device.T_sn {
  273. T_name = Product.Read_ProductType_Get(r_Device.T_model)
  274. }
  275. r_Device.T_devName = T_name
  276. Deviceparameter := Device.DeviceParameter{
  277. T_sn: r_Device.T_sn,
  278. T_name: T_name,
  279. T_saveT: lib.To_int(ms2_Project.Data["saveT"]), //数据保存间隔 (s)*最小值60
  280. T_overA: lib.To_int(ms2_Project.Data["overA"]), //超限报警触发间隔 (s)*最小值60
  281. T_outA: lib.To_int(ms2_Project.Data["outA"]), //断电报警触发间隔 (s)*最小值60
  282. T_lostA: lib.To_int(ms2_Project.Data["lostA"]), //传感器掉线报警触发间隔 (s)*最小值60
  283. T_warnD: lib.To_int(ms2_Project.Data["warnD"]), // 超限预警延时 (s)*
  284. T_scan: lib.To_int(ms2_Project.Data["scan"]), // 显示轮播间隔 s(1~240) 默认:5 *
  285. T_warn: lib.To_int(ms2_Project.Data["warn"]), // 超限预警触发间隔 (s)*为0时预警只触发一次 *最小值60
  286. T_bat: lib.To_int(ms2_Project.Data["bat"]), //电池电量下限 (%)*
  287. T_dormancy: lib.To_int(ms2_Project.Data["dormancy"]), //是否进入休眠 0:关闭 1:开启 默认:0
  288. T_snum: lib.To_int(ms2_Project.Data["snum"]), // 【管理主机】 - 传感器数量 (范围0~255)
  289. T_btname: lib.To_string(ms2_Project.Data["btname"]), //--蓝牙打印机名称 用于移动端连接蓝牙打印机
  290. T_btsid: lib.To_int(ms2_Project.Data["btsid"]), //--打印机服务号 用于移动端连接蓝牙打印机
  291. T_btchar: lib.To_int(ms2_Project.Data["btchar"]), //--蓝牙特征码 用于移动端连接蓝牙打印机
  292. //T_speed: lib.To_int(ms2_Project.Data["speed"]), //--蓝牙特征码 用于移动端连接蓝牙打印机
  293. T_State: 1,
  294. T_SendState: 1,
  295. }
  296. if Device.Add_DeviceParameter_fun(Deviceparameter) {
  297. Warning.Add_DeviceLogs(102, *r_Device, "修改主机参数")
  298. }
  299. Device.Update_Device_new(*r_Device, "T_devName")
  300. // 获取所有传感器
  301. //Read_DeviceSensorParameter(r_Device.T_sn)
  302. // 不用反馈
  303. return
  304. case 4: // 4:设置主机参数 - 反馈
  305. var ms2_Return_r Ms2_Return
  306. err := json.Unmarshal(msg, &ms2_Return_r)
  307. if err != nil {
  308. logs.PrintlnError("MqttServer", "JSON反序列化失败[4:设置主机参数]", string(msg), err.Error())
  309. return
  310. }
  311. DeviceParameter_r, err := Device.Read_DeviceParameter_SN_Msid(r_Device.T_sn, ms2_Return_r.Msid)
  312. if err != nil {
  313. logs.PrintlnError("MqttServer", " SN_Msid Err![4:设置主机参数]", string(msg), err.Error())
  314. return
  315. }
  316. DeviceParameter_r.T_SendState = 2
  317. // 判断参数 //1设置成功,0设置失败
  318. if ms2_Return_r.Status == 1 {
  319. DeviceParameter_r.T_SendState = 1
  320. r_Device.T_devName = DeviceParameter_r.T_name
  321. Device.Update_Device_new(*r_Device, "T_devName")
  322. }
  323. // 状态更新
  324. Device.Update_DeviceParameter_(DeviceParameter_r)
  325. // 同步参数
  326. DeviceParameter_r.T_State = 1
  327. Device.Add_DeviceParameter_fun(DeviceParameter_r)
  328. Warning.Add_DeviceLogs(102, *r_Device, "修改主机参数-反馈成功")
  329. // 取消掉 其他参数
  330. Device.UPDATE_DeviceParameter_SN_T_SendState_2_sql(r_Device.T_sn)
  331. return
  332. case 5: // 读取传感器参数
  333. var ms2_Project Ms2_Project_list
  334. err := json.Unmarshal(msg, &ms2_Project)
  335. if err != nil {
  336. logs.Println("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
  337. logs.Println("JSON反序列化失败[Ms_Parameter],err=", err)
  338. return
  339. }
  340. for _, v := range ms2_Project.Data {
  341. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, lib.To_int(v["id"]))
  342. T_name := lib.To_string(v["name"])
  343. if len(T_name) == 0 {
  344. continue
  345. }
  346. if T_name == "*" {
  347. T_name = r_Device.T_sn + "-" + lib.To_string(v["id"])
  348. if is {
  349. T_name = DeviceSensor_r.T_name
  350. }
  351. }
  352. // 不存在
  353. if !is {
  354. DeviceSensor_r.T_sn = r_Device.T_sn
  355. DeviceSensor_r.T_pid = r_Device.T_pid
  356. DeviceSensor_r.T_id = lib.To_int(v["id"])
  357. DeviceSensor_r.T_name = T_name
  358. DeviceSensor_r.T_datashow = 1
  359. DeviceSensor_r.T_sort = 1
  360. DeviceSensor_r.T_monitor = r_Device.T_monitor
  361. // 设备同步参数
  362. DeviceSensor_r.T_Dattery = r_Device.T_Dattery
  363. DeviceSensor_r.T_Site = r_Device.T_Site
  364. DeviceSensor_r.T_State = r_Device.T_State
  365. DeviceSensor_r.T_online = r_Device.T_online
  366. DeviceSensor_r.Id = Device.Add_DeviceSensor(*r_Device, DeviceSensor_r)
  367. }
  368. // 记录传感器
  369. Devicesensorparameter := Device.DeviceSensorParameter{
  370. T_sn: r_Device.T_sn,
  371. T_id: lib.To_int(v["id"]),
  372. T_name: T_name,
  373. // 报警
  374. T_Tlower: lib.To_float32(v["tl"]),
  375. T_Tupper: lib.To_float32(v["tu"]),
  376. T_RHlower: lib.To_float32(v["hl"]),
  377. T_RHupper: lib.To_float32(v["hu"]),
  378. // 预警
  379. T_enprel: lib.To_int(v["enprel"]),
  380. T_tprel: lib.To_float32(v["tprel"]),
  381. T_tpreu: lib.To_float32(v["tpreu"]),
  382. T_hprel: lib.To_float32(v["hprel"]),
  383. T_hpreu: lib.To_float32(v["hpreu"]),
  384. T_enprelnote: lib.To_int(v["enprelnote"]),
  385. //T_speed: lib.To_int(v["speed"]),
  386. //T_sense: lib.To_int(v["sense"]),
  387. T_en: lib.To_int(v["en"]),
  388. T_free: lib.To_int(v["free"]),
  389. T_Msid: ms2_Project.Msid, // 消息识别ID
  390. T_SendState: 1, // 发送状态 0 待发送 1 发送成功 2 失败 3 覆盖
  391. T_State: 1, // 1 系统获取 2 用户提交
  392. }
  393. // 过滤参数 相同
  394. id := Device.Add_DeviceSensorParameter_fun(Devicesensorparameter)
  395. Warning.Add_DeviceLogs(102, *r_Device, "修改设备参数")
  396. logs.Println("DeviceSensorParameter ID:", id)
  397. // 对 传感器 的状态更新
  398. DeviceSensor_r.T_name = T_name
  399. DeviceSensor_r.T_sp = id
  400. Device.Update_DeviceSensor(DeviceSensor_r, "T_name", "T_sp")
  401. }
  402. go func() {
  403. time.Sleep(time.Second * 3)
  404. Get_Device_Realtime(r_Device.T_sn)
  405. }()
  406. // 不用反馈
  407. return
  408. case 6: // 设置传感器参数
  409. var ms2_Return_r Ms2_Return
  410. err := json.Unmarshal(msg, &ms2_Return_r)
  411. if err != nil {
  412. logs.Println("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
  413. logs.Println("JSON反序列化失败[Ms_Parameter],err=", err)
  414. return
  415. }
  416. r, is := Device.Read_DeviceSensorParameter_SN_T_id_Msid(r_Device.T_sn, ms2_Return_r.Msid)
  417. if !is {
  418. logs.Println("设备配置", "设置设备参数 SN_Msid Err! [Ms_Parameter_r]", string(msg))
  419. return
  420. }
  421. r.T_SendState = 2
  422. // 判断参数 //1设置成功,0设置失败
  423. if ms2_Return_r.Status == 1 {
  424. r.T_SendState = 1
  425. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r.T_sn, r.T_id)
  426. if !is {
  427. logs.Println("设备配置", "设置传感器参数 r_DeviceSensor Err! [Ms_Parameter_r]", r.T_sn+"|"+strconv.Itoa(r.T_id))
  428. logs.Println("设置传感器参数 r_DeviceSensor Err![Ms_Parameter_r],err=", r.T_sn+"|"+strconv.Itoa(r.T_id))
  429. logs.Println("MqttServer", r_Device.T_sn+"["+lib.To_string(r.T_id)+"] 没有找到传感器:", string(msg))
  430. break
  431. }
  432. // 添加一份最新的
  433. r.T_State = 0
  434. id := Device.Add_DeviceSensorParameter_fun(r)
  435. Warning.Add_DeviceLogs(102, *r_Device, "修改设备参数")
  436. logs.Println("DeviceSensorParameter ID:", id)
  437. // 对 传感器 的状态更新
  438. DeviceSensor_r.T_name = r.T_name
  439. DeviceSensor_r.T_sp = id
  440. Device.Update_DeviceSensor(DeviceSensor_r, "T_name", "T_sp")
  441. }
  442. // 状态更新
  443. Device.Update_DeviceSensorParameter(r, "T_SendState")
  444. // 取消掉 其他参数
  445. Device.UPDATE_DeviceSensorParamete_SN_T_SendState_2_sql(r_Device.T_sn, strconv.Itoa(r.T_id))
  446. //// 更新主机参数
  447. //go func() {
  448. // time.Sleep(time.Second * 1)
  449. // Read_DeviceSensorParameter3(r_Device.T_sn)
  450. //}()
  451. return
  452. case 12:
  453. var ms2_Project Ms2_Project_list
  454. err := json.Unmarshal(msg, &ms2_Project)
  455. if err != nil {
  456. logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_project_2]", string(msg), err.Error())
  457. break
  458. }
  459. // 缓存报警ID,防止重复发送
  460. if Warning.Redis_Warning_Repeat_T_sn_Msid(r_Device.T_sn + "|" + strconv.FormatInt(ms2_Project.Msid, 10)) {
  461. logs.Println("MqttServer", " 存在报警 跳过:", string(msg))
  462. break
  463. }
  464. // {
  465. // "sn": "YD0123456789",
  466. // "type": 12,
  467. // "msid": 125456001,
  468. // "data": [
  469. // {
  470. // "tp":7, // 对照 报警类型对应表
  471. // "msg": "报警信息或故障信息", //如:1号传感器故障 (非必填,但是 要带字段)
  472. // "ut": 1626857518 //产生时间
  473. // },
  474. // ..........
  475. //]
  476. //}
  477. for _, v := range ms2_Project.Data {
  478. var Warning_r Warning.Warning
  479. Warning_r.T_tp = lib.To_int(v["tp"]) + 100 // 报警类型
  480. Warning_r.T_sn = r_Device.T_sn
  481. Warning_r.T_id = -1
  482. Warning_r.T_Msid = Ms_project.Msid
  483. Warning_r.T_D_name = r_Device.T_devName
  484. Warning_r.T_DS_name = r_Device.T_devName
  485. time_int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
  486. Warning_r.T_Ut = time.Unix(time_int64_, 0)
  487. Warning_r.T_Remark = lib.To_string(v["msg"])
  488. Warning_r.T_State = 1
  489. //时间异常 大于10s,直接丢掉数据
  490. if (time_int64_ - time.Now().Unix()) > 10 {
  491. ms2_Return.Status = 3
  492. continue
  493. }
  494. // 持续时间计算
  495. time_int64_f, _ := strconv.ParseInt(lib.To_string(v["fut"]), 10, 64)
  496. if time_int64_f == 0 {
  497. time_int64_f = time_int64_
  498. }
  499. Warning_r.T_fUt = time.Unix(time_int64_f, 0)
  500. time_int64_f_x := (int(time_int64_) - int(time_int64_f))
  501. logs.Println(strconv.FormatInt(Warning_r.T_Msid, 10)+"持续时间:", time_int64_f_x)
  502. if time_int64_f_x > 0 {
  503. Warning_r.T_Remark += " 已持续" + lib.MinuteToDataTime(time_int64_f_x)
  504. }
  505. // 查看 sn 是否有归属
  506. if Warning_r.T_pid == 0 { // 寻找报警归属
  507. if len(Warning_r.T_sn) > 10 {
  508. r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn)
  509. if err != nil {
  510. logs.Println("MessageDisconnected 没有该设备:", Warning_r.T_sn)
  511. }
  512. Warning_r.T_pid = r_Device.T_pid
  513. }
  514. }
  515. switch Warning_r.T_tp {
  516. case 101: // 主机报警
  517. Warning_r.T_State = 3
  518. WarningNotice.WarningToAdminId(&Warning_r, []int{}) // 发送给 绑定公司管理员
  519. break
  520. case 115:
  521. Warning_r.T_Remark = "开始监测任务"
  522. // 1 start:开始监测任务,
  523. // 防止没有结束命令
  524. DeviceTask_r, ok := Device.Read_DeviceTask_All_Limit_1(r_Device.T_sn)
  525. logs.Println("DeviceTask_r.id:", DeviceTask_r.Id)
  526. if ok {
  527. if DeviceTask_r.T_Ut_end.IsZero() {
  528. logs.Println("没有结束 强制 更新上一条")
  529. DeviceTask_r.T_Ut_end = Warning_r.T_Ut
  530. Device.Update_DeviceTask(DeviceTask_r)
  531. }
  532. }
  533. logs.Println("插入新任务")
  534. // 插入开始 任务
  535. dt := Device.DeviceTask{
  536. T_sn: r_Device.T_sn,
  537. T_Ut_start: Warning_r.T_Ut,
  538. T_Ut: Warning_r.T_Ut,
  539. T_State: 1,
  540. }
  541. Device.Add_DeviceTask(dt)
  542. r_Device.T_monitor = 1
  543. break
  544. case 116:
  545. Warning_r.T_Remark = "中途打印任务"
  546. DeviceTask_r, ok := Device.Read_DeviceTask_All_Limit_1(r_Device.T_sn)
  547. if ok {
  548. if DeviceTask_r.T_Ut_end.IsZero() {
  549. DeviceTask_r.T_Ut_end = Warning_r.T_Ut
  550. DeviceTask_r.T_State = 3
  551. Device.Update_DeviceTask(DeviceTask_r)
  552. // 插入开始 任务
  553. dt := Device.DeviceTask{
  554. T_sn: r_Device.T_sn,
  555. T_Ut_start: DeviceTask_r.T_Ut_start,
  556. T_Ut: Warning_r.T_Ut,
  557. T_State: 1,
  558. }
  559. logs.Println("插入新任务")
  560. Device.Add_DeviceTask(dt)
  561. }
  562. }
  563. r_Device.T_monitor = 1
  564. break
  565. case 117:
  566. Warning_r.T_Remark = "结束监测任务"
  567. // 防止没有结束命令
  568. DeviceTask_r, ok := Device.Read_DeviceTask_All_Limit_1(r_Device.T_sn)
  569. if ok {
  570. if DeviceTask_r.T_Ut_end.IsZero() {
  571. DeviceTask_r.T_Ut_end = Warning_r.T_Ut
  572. DeviceTask_r.T_State = 2
  573. Device.Update_DeviceTask(DeviceTask_r)
  574. }
  575. }
  576. r_Device.T_monitor = 0
  577. break
  578. }
  579. //// 推送运维信息
  580. WarningNotice.WarningCompanyNotice(&Warning_r, 0, 0)
  581. // 添加报警
  582. _, err = Warning.Add_Warning(Warning_r)
  583. }
  584. Device.Update_Device_new(*r_Device, "T_monitor")
  585. break
  586. case 13: // 上电推送信息
  587. var ms2_Project Ms2_Project
  588. err := json.Unmarshal(msg, &ms2_Project)
  589. if err != nil {
  590. logs.Println("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
  591. logs.Println("JSON反序列化失败[Ms_Parameter],err=", err)
  592. return
  593. }
  594. var Warning_r Warning.Warning
  595. Warning_r.T_tp = 106 // 报警类型
  596. Warning_r.T_sn = r_Device.T_sn
  597. Warning_r.T_D_name = r_Device.T_devName
  598. Warning_r.T_id = 0
  599. Warning_r.T_DS_name = ""
  600. Warning_r.T_Msid = Ms_project.Msid
  601. r_Device.T_model = lib.To_string(ms2_Project.Data["model"])
  602. r_Device.T_sver = lib.To_string(ms2_Project.Data["sver"])
  603. r_Device.T_hver = lib.To_string(ms2_Project.Data["hver"])
  604. r_Device.T_imei = lib.To_string(ms2_Project.Data["imei"])
  605. r_Device.T_iccid = lib.To_string(ms2_Project.Data["iccid"])
  606. Device.Update_Device(*r_Device, "T_model", "T_sver", "T_hver", "T_imei", "T_iccid")
  607. // 加入日志
  608. // "rtc" : 1671173184,//设备时间
  609. // "rtl" : 3,//设备开机累计运行秒数
  610. // "st" : 1671173177,//设备开机时间
  611. // "lst" : 0//设备上次关机时间
  612. UT_rtc := time.Unix(int64(lib.To_int64(ms2_Project.Data["rtc"])), 0).Format("2006-01-02 15:04:05")
  613. UT_st := time.Unix(int64(lib.To_int64(ms2_Project.Data["st"])), 0).Format("2006-01-02 15:04:05")
  614. UT_lst := time.Unix(int64(lib.To_int64(ms2_Project.Data["lst"])), 0).Format("2006-01-02 15:04:05")
  615. UT_rtl := lib.To_string(ms2_Project.Data["rtl"])
  616. str := "----- 设备信息 -----\n"
  617. str += "设备型号:" + r_Device.T_model + " |\n"
  618. str += "软件版本:" + r_Device.T_sver + " |\n"
  619. str += "硬件版本:" + r_Device.T_hver + " |\n"
  620. str += "模组imei:" + r_Device.T_imei + " |\n"
  621. str += "sim卡号:" + r_Device.T_iccid + " |\n"
  622. str += "设备时间:" + UT_rtc + " |\n"
  623. str += "设备开机时间:" + UT_st + " |\n"
  624. str += "设备上次关机时间:" + UT_lst + " |\n"
  625. str += "设备开机累计运行秒数:" + UT_rtl + " |\n"
  626. Warning_r.T_Remark = str
  627. Warning_r.T_Ut = time.Now()
  628. Warning_r.T_State = 1
  629. // 添加报警
  630. _, err = Warning.Add_Warning(Warning_r)
  631. return
  632. case 15: // 网络延时测试
  633. Rt_Project_Data_r := Ms2_Project{
  634. Type: Ms_project.Type,
  635. Msid: Ms_project.Msid,
  636. }
  637. var Data map[string]interface{}
  638. Data = make(map[string]interface{})
  639. Data["ut"] = Ms_project.Dut
  640. Rt_Project_Data_r.Data = Data
  641. // 回复
  642. jsonStu, err := json.Marshal(Rt_Project_Data_r)
  643. if err != nil {
  644. return
  645. }
  646. Mqtt_publish(r_Device.T_sn, string(jsonStu))
  647. return
  648. case 88: // 临时授权
  649. var ms2_Project Ms2_Project
  650. err := json.Unmarshal(msg, &ms2_Project)
  651. if err != nil {
  652. logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
  653. return
  654. }
  655. url := "192.168.0.33:3991/CreateAuth"
  656. method := "POST"
  657. payload := strings.NewReader("model=" + lib.To_string(ms2_Project.Data["model"]) + "&sver=" + lib.To_string(ms2_Project.Data["sver"]) + "&hver=" + lib.To_string(ms2_Project.Data["hver"]) + "&imei=" + lib.To_string(ms2_Project.Data["imei"]) + "&iccid=" + lib.To_string(ms2_Project.Data["iccid"]) + "&sn=" + lib.To_string(ms2_Project.Data["sn"]))
  658. client := &http.Client{}
  659. req, err := http.NewRequest(method, url, payload)
  660. if err != nil {
  661. fmt.Println(err)
  662. return
  663. }
  664. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  665. res, err := client.Do(req)
  666. if err != nil {
  667. fmt.Println(err)
  668. return
  669. }
  670. defer res.Body.Close()
  671. body, err := ioutil.ReadAll(res.Body)
  672. if err != nil {
  673. fmt.Println(err)
  674. return
  675. }
  676. fmt.Println(string(body))
  677. // 实体类
  678. type Ms2_Projectccc struct {
  679. Pass string `json:"psaa"`
  680. Status string `json:"status"`
  681. }
  682. var ms2_Projectccc Ms2_Projectccc
  683. err = json.Unmarshal(body, &ms2_Projectccc)
  684. if err != nil {
  685. logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms2_Projectccc]", string(msg))
  686. return
  687. }
  688. Rt_Project_Data_r := Ms2_Project{
  689. Type: Ms_project.Type,
  690. Msid: Ms_project.Msid,
  691. }
  692. var Data map[string]interface{}
  693. Data = make(map[string]interface{})
  694. Data["Pass"] = ms2_Projectccc.Pass
  695. Data["Status"] = ms2_Projectccc.Status
  696. Rt_Project_Data_r.Data = Data
  697. // 回复
  698. jsonStu, err := json.Marshal(Rt_Project_Data_r)
  699. if err != nil {
  700. return
  701. }
  702. Mqtt_publish(r_Device.T_sn, string(jsonStu))
  703. return
  704. case 98: // 时间同步
  705. RTC_TimeStr(r_Device.T_sn, Ms_project.Msid, 98)
  706. return
  707. case 100: // 设备实时状态推送
  708. var ms2_Project Ms2_Project
  709. err := json.Unmarshal(msg, &ms2_Project)
  710. if err != nil {
  711. logs.Println("MqttServer", "JSON反序列化失败[Ms_project_]", string(msg))
  712. logs.Println("JSON反序列化失败[Ms_project],err=", err)
  713. return
  714. }
  715. r_Device.T_Dattery = lib.To_int(ms2_Project.Data["power"]) //#设备剩余电量百分比
  716. r_Device.T_rssi = lib.To_string(ms2_Project.Data["rssi"]) //#信号强度
  717. Device.Update_Device(*r_Device, "T_Dattery", "T_rssi")
  718. // 反馈透传
  719. NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
  720. Device.Update_Device_new(*r_Device, "T_Dattery")
  721. return
  722. default:
  723. return
  724. }
  725. // 回复
  726. jsonStu, err := json.Marshal(ms2_Return)
  727. if err != nil {
  728. logs.PrintlnError("MqttServer", "json.Marshal ", string(msg))
  729. logs.Println("回复失败 [Ms_project_0],err=", err)
  730. return
  731. }
  732. Mqtt_publish(r_Device.T_sn, string(jsonStu))
  733. }