V3MqttHandle.go 30 KB

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