WarningNotice.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package WarningNotice
  2. import (
  3. "Cold_mqtt/Nats/NatsServer"
  4. "Cold_mqtt/conf"
  5. "Cold_mqtt/lib"
  6. "Cold_mqtt/logs"
  7. "Cold_mqtt/models/Account"
  8. "Cold_mqtt/models/Company"
  9. "Cold_mqtt/models/Device"
  10. "Cold_mqtt/models/Warning"
  11. "encoding/json"
  12. "fmt"
  13. "time"
  14. "strconv"
  15. "strings"
  16. )
  17. // 报警策略通知 fut 持续时间间隔 秒
  18. func WarningCompanyNotice(Warning_r *Warning.Warning, T, HR float32) {
  19. defer func() {
  20. if Warning_r.Id > 0 {
  21. Warning.Update_Warning(*Warning_r, "T_Log")
  22. }
  23. }()
  24. if Warning_r.T_State == 0 {
  25. logs.Println("Warning_r.T_State == 0", Warning_r.T_State)
  26. return
  27. }
  28. logs.Println("==>WarningCompanyNotice Msid", Warning_r.T_Msid)
  29. CompanyNotice_List := Company.Read_CompanyNotice_List(Warning_r.T_pid)
  30. CompanyNotice_List_is := true
  31. logs.Println("==>WarningCompanyNotice CompanyNotice_List.len", len(CompanyNotice_List))
  32. // 通知策略
  33. for _, CompanyNotice_v := range CompanyNotice_List {
  34. if Warning_r.T_id == -1 {
  35. // 主机 通知
  36. if !strings.Contains(CompanyNotice_v.T_Notice_bind, Warning_r.T_sn) {
  37. logs.Println("==>WarningCompanyNotice 不匹配!", CompanyNotice_v.T_Notice_bind, Warning_r.T_sn)
  38. continue
  39. }
  40. } else {
  41. // 传感器 通知
  42. if !strings.Contains(CompanyNotice_v.T_Notice_bind, Warning_r.T_sn+","+strconv.Itoa(Warning_r.T_id)) {
  43. logs.Println("==>WarningCompanyNotice 不匹配!", CompanyNotice_v.T_Notice_bind, Warning_r.T_sn+","+strconv.Itoa(Warning_r.T_id))
  44. continue
  45. }
  46. }
  47. CompanyNotice_List_is = false
  48. WarningCompanyNotice_CompanyNotice(Warning_r, CompanyNotice_v, T, HR)
  49. }
  50. if CompanyNotice_List_is {
  51. // 策略 有问题
  52. Warning.Add_Warning_Log(Warning_r, "!!!没有报警策略,请及时去添加!!!\n")
  53. return
  54. }
  55. }
  56. // 处理分类
  57. func WarningCompanyNotice_CompanyNotice(Warning_r *Warning.Warning, CompanyNotice_r Company.CompanyNotice, T, HR float32) {
  58. logs.Println("==>WarningCompanyNotice_CompanyNotice", "Msid", Warning_r.T_Msid, " CompanyNotice:", CompanyNotice_r.Id, " T_name:", CompanyNotice_r.T_name, " T_Notice_mechanism:", CompanyNotice_r.T_Notice_mechanism)
  59. cx_time := time.Now().Unix() - Warning_r.T_fUt.Unix() // 持续时间 s
  60. Warning.Add_Warning_Log(Warning_r, "执行通知策略 ->["+CompanyNotice_r.T_name+"] ["+time.Now().Format("15:04:05")+"]\n")
  61. // 遍历 所有绑定策略
  62. // W报警编号,记录,处理,w启用,数量,上限,~|
  63. // W15,1,1,0,1,0,9999,0,0,0,0,0,0,0,0,0|
  64. T_Notice_mechanism_list := strings.Split(CompanyNotice_r.T_Notice_mechanism, "|")
  65. if len(T_Notice_mechanism_list) == 0 {
  66. // 策略 有问题 !!!!
  67. Warning.Add_Warning_Log(Warning_r, " !!!策略规则错误!!!\n"+CompanyNotice_r.T_Notice_mechanism+" , 内容:"+Warning_r.T_Remark)
  68. }
  69. for _, v := range T_Notice_mechanism_list {
  70. if len(v) == 0 {
  71. continue
  72. }
  73. //T_nm_list W报警编号-0,处理-1,w启用-2,持续秒-3,间隔秒-4,发送条数-5,d启用-6,持续秒-7,间隔秒-8,发送条数-9,p启用-10,持续秒-11,间隔秒-12,发送条数-13|
  74. // 解析策略
  75. T_nm_list := strings.Split(v, ",")
  76. //if len(T_nm_list) != 14 {
  77. // logs.Println("==>WarningCompanyNotice_CompanyNotice 策略 ->", v, " 策略下标长度 != 15 ->", len(T_nm_list))
  78. // continue
  79. //}
  80. // 判断符合 报警类型
  81. T_tp_str := Warning.Read_WarningType_Get(Warning_r.T_tp)
  82. if !strings.Contains(T_tp_str, T_nm_list[0]) {
  83. logs.Println("==>WarningCompanyNotice_CompanyNotice 不符合 报警类型!!", T_tp_str, T_nm_list[0])
  84. continue
  85. }
  86. logs.Println("执行策略 ->", v)
  87. // 是否绑定 SN,ID
  88. if Warning_r.T_id == -1 {
  89. if !strings.Contains(CompanyNotice_r.T_Notice_bind, Warning_r.T_sn) {
  90. logs.Println("==>WarningCompanyNotice_CompanyNotice !!!没有绑定设备,跳过", CompanyNotice_r.T_Notice_bind, Warning_r.T_sn+",")
  91. Warning.Add_Warning_Log(Warning_r, " !!!没有绑定设备,跳过\n")
  92. continue
  93. }
  94. } else {
  95. if !strings.Contains(CompanyNotice_r.T_Notice_bind, Warning_r.T_sn+","+strconv.Itoa(Warning_r.T_id)+"|") {
  96. logs.Println("!!!没有绑定设备,跳过", CompanyNotice_r.T_Notice_bind, Warning_r.T_sn+","+strconv.Itoa(Warning_r.T_id)+"|")
  97. Warning.Add_Warning_Log(Warning_r, " !!!没有绑定设备传感器,跳过\n")
  98. continue
  99. }
  100. }
  101. // 是否处理
  102. if T_nm_list[1] == "1" {
  103. logs.Println("==>WarningCompanyNotice_CompanyNotice 策略结果 ->", "要 处理")
  104. Warning_r.T_State = 3
  105. }
  106. WarningCompanyNotice_CompanyNotice_Send(Warning_r, CompanyNotice_r, T_nm_list, T, HR, cx_time)
  107. return
  108. }
  109. }
  110. // 发送报警信息
  111. func WarningCompanyNotice_CompanyNotice_Send(Warning_r *Warning.Warning, CompanyNotice_r Company.CompanyNotice, T_nm_list []string, T, HR float32, cx_time int64) {
  112. logs.Println("==>WarningDeviceHandle_CompanyNotice_Send ", "T_Msid", Warning_r.T_Msid, " T_nm_list:", T_nm_list)
  113. //T_nm_list W报警编号-0,处理-1,w启用-2,持续秒-3,间隔秒-4,发送条数-5,d启用-6,持续秒-7,间隔秒-8,发送条数-9,p启用-10,持续秒-11,间隔秒-12,发送条数-13|
  114. _, Company_r := Account.Read_Company_id(Warning_r.T_pid)
  115. Company_Charging_r := Company_r
  116. //记账扣费
  117. if Company_r.T_Charging > 0 {
  118. _, Company_Charging_r = Account.Read_Company_id(Company_r.T_Charging)
  119. }
  120. // 微信
  121. N_index := 2
  122. if T_nm_list[N_index] == "1" { // 是否开启
  123. Warning_num, Warning_sUt := Warning.Redis_Warning_Num(Warning_r.T_sn + "|" + strconv.Itoa(Warning_r.T_id) + "|" + strconv.Itoa(Warning_r.T_tp) + "|wx")
  124. Warning.Add_Warning_Log(Warning_r, fmt.Sprintf("[微信]持续时间:%d秒, 间隔时间:%s秒, 内发送次数:%d次, 间隔还剩时间:%d秒\n", cx_time, T_nm_list[N_index+2], Warning_num, Warning_sUt))
  125. if int_x, _ := strconv.Atoi(T_nm_list[N_index+1]); int64(int_x) <= cx_time { // 是否满足 持续时间 秒
  126. if int_u, _ := strconv.Atoi(T_nm_list[N_index+3]); (int64(int_u) > Warning_num) || int_u == 0 { //是否 间隔时间内 超过发送条数
  127. Warning.Redis_Warning_Num_W(Warning_r.T_sn+"|"+strconv.Itoa(Warning_r.T_id)+"|"+strconv.Itoa(Warning_r.T_tp)+"|wx", T_nm_list[N_index+2])
  128. // 开始推送
  129. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", CompanyNotice_r.T_Notice_wx)
  130. //reqdata_wx := "{\"first\":{\"value\":\"" + Warning_r.T_Ut.Format("2006-01-02 15:04:05") + "\", \"color\":\"#ff450a\"}, \"keyword1\":{\"value\":\"" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\", \"color\":\"#ff450a\"}, \"keyword2\":{\"value\":\"" + Warning_r.T_D_name + "[" + Warning_r.T_DS_name + "] " + "\", \"color\":\"#056cff\"}, \"remark\":{\"value\":\"" + Warning_r.T_Remark + "\", \"color\":\"#ffcb0a\"}}"
  131. arr_c := strings.Split(CompanyNotice_r.T_Notice_wx, "|")
  132. for _, v_c := range arr_c {
  133. if len(v_c) == 0 {
  134. continue
  135. }
  136. v_c_l := strings.Split(v_c, "/")
  137. if len(v_c_l) != 2 {
  138. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " len(v_c_l) != 2")
  139. continue
  140. }
  141. if len(v_c_l[0]) == 0 {
  142. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " len(v_c_l[0]) == 0")
  143. continue
  144. }
  145. admin_r, err := Account.Read_Admin_ByUuid(v_c_l[0])
  146. if err != nil {
  147. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " Read_Admin_ByUuid!!")
  148. continue
  149. }
  150. if len(admin_r.T_wx) == 0 {
  151. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " admin_r.T_wx!!")
  152. continue
  153. }
  154. // 用户下 多个微信
  155. arr_b := strings.Split(admin_r.T_wx, "|")
  156. for _, v_b := range arr_b {
  157. v_b_l := strings.Split(v_b, "/")
  158. if len(v_b_l) != 2 {
  159. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_b, " len(v_b_l) != 2")
  160. continue
  161. }
  162. if len(v_b_l[0]) == 0 {
  163. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_b, " len(v_b_l[0]) == 0")
  164. continue
  165. }
  166. // 微信
  167. Warning.Add_Warning_Log(Warning_r, "----> "+v_b)
  168. var data_json_str []byte
  169. switch Warning_r.T_tp {
  170. case 1, 2, 3, 7, 8, 9: //温度
  171. data_json_t := Wx_t_struct{
  172. Corporate: Wx_struct{Value: lib.Limit_len(Company_r.T_name, 20)},
  173. Type: Wx_struct{Value: Warning.Read_WarningType_Get(Warning_r.T_tp)},
  174. Name: Wx_struct{Value: lib.Limit_len(Warning_r.T_DS_name, 20)},
  175. Time: Wx_struct{Value: Warning_r.T_Ut.Format("2006-01-02 15:04:05")},
  176. //T: Wx_struct{Value: fmt.Sprintf("%.1f℃", T), Color: "#ff0099"},
  177. Remark: Wx_struct{Value: lib.Limit_len("当前:"+fmt.Sprintf("%.1f℃", T)+Warning_r.T_Remark, 20)},
  178. }
  179. data_json_str, _ = json.Marshal(data_json_t)
  180. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 通知内容:", string(data_json_str))
  181. NatsServer.Wx_MessageTemplate(Company_Charging_r, Warning_r, v_b_l[0], "8bPXmJwtd7RnbI2iVNatxez9Vby_0Y-69ggN57_-Sh8", string(data_json_str))
  182. break
  183. case 4, 5, 6, 10, 11, 12: //湿度
  184. data_json_t := Wx_h_struct{
  185. Corporate: Wx_struct{Value: lib.Limit_len(Company_r.T_name, 20)},
  186. Type: Wx_struct{Value: Warning.Read_WarningType_Get(Warning_r.T_tp)},
  187. Name: Wx_struct{Value: lib.Limit_len(Warning_r.T_DS_name, 20)},
  188. Time: Wx_struct{Value: Warning_r.T_Ut.Format("2006-01-02 15:04:05")},
  189. //H: Wx_struct{Value: },
  190. Remark: Wx_struct{Value: lib.Limit_len("当前:"+fmt.Sprintf("%.1f", HR)+"%"+Warning_r.T_Remark, 20)},
  191. }
  192. data_json_str, _ = json.Marshal(data_json_t)
  193. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 通知内容:", string(data_json_str))
  194. NatsServer.Wx_MessageTemplate(Company_Charging_r, Warning_r, v_b_l[0], "833Kd1-zaQJ5sOJ9F2I1RzSNS249KU_t3L7610cbgaw", string(data_json_str))
  195. break
  196. default:
  197. data_json_t := Wx_d_struct{
  198. Corporate: Wx_struct{Value: lib.Limit_len(Company_r.T_name, 20)},
  199. Type: Wx_struct{Value: Warning.Read_WarningType_Get(Warning_r.T_tp)},
  200. Name: Wx_struct{Value: lib.Limit_len(Warning_r.T_D_name, 20)},
  201. Time: Wx_struct{Value: Warning_r.T_Ut.Format("2006-01-02 15:04:05")},
  202. Remark: Wx_struct{Value: lib.Limit_len(Warning_r.T_Remark, 20)},
  203. }
  204. data_json_str, _ = json.Marshal(data_json_t)
  205. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 通知内容:", string(data_json_str))
  206. NatsServer.Wx_MessageTemplate(Company_Charging_r, Warning_r, v_b_l[0], "gnpERD6ilOQK6zvp1W_PmY5SEDOcSxiHh_KJbCdeAqw", string(data_json_str))
  207. }
  208. }
  209. }
  210. } else { // 间隔时间内 超过发送条数
  211. int_x, _ := strconv.Atoi(T_nm_list[N_index+3])
  212. Warning.Add_Warning_Log(Warning_r, "!微信-> 间隔时间内 超过发送条数,当期值:"+strconv.Itoa(int_x)+"\n")
  213. }
  214. } else { // 未达到 持续时间 秒
  215. int_x, _ := strconv.Atoi(T_nm_list[N_index+1])
  216. Warning.Add_Warning_Log(Warning_r, "!微信-> 未达到持续时间:"+strconv.Itoa(int_x)+"\n")
  217. }
  218. }
  219. // 短信
  220. N_index = 6
  221. if T_nm_list[N_index] == "1" { // 是否开启
  222. Warning_num, Warning_sUt := Warning.Redis_Warning_Num(Warning_r.T_sn + "|" + strconv.Itoa(Warning_r.T_id) + "|" + strconv.Itoa(Warning_r.T_tp) + "|dx")
  223. Warning.Add_Warning_Log(Warning_r, fmt.Sprintf("[短信]持续时间:%d秒, 间隔时间:%s秒, 内发送次数:%d次, 间隔还剩时间:%d秒\n", cx_time, T_nm_list[N_index+2], Warning_num, Warning_sUt))
  224. if int_x, _ := strconv.Atoi(T_nm_list[N_index+1]); int64(int_x) <= cx_time { // 是否满足 持续时间 秒
  225. if int_u, _ := strconv.Atoi(T_nm_list[N_index+3]); (int64(int_u) > Warning_num) || int_u == 0 { //是否 间隔时间内 超过发送条数
  226. Warning.Redis_Warning_Num_W(Warning_r.T_sn+"|"+strconv.Itoa(Warning_r.T_id)+"|"+strconv.Itoa(Warning_r.T_tp)+"|dx", T_nm_list[N_index+2])
  227. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_message:", CompanyNotice_r.T_Notice_message)
  228. arr_c := strings.Split(CompanyNotice_r.T_Notice_message, "|")
  229. for _, v_c := range arr_c {
  230. if len(v_c) == 0 {
  231. continue
  232. }
  233. v_c_l := strings.Split(v_c, "/")
  234. if len(v_c_l) != 2 {
  235. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " len(v_c_l) != 2")
  236. continue
  237. }
  238. if len(v_c_l[0]) == 0 {
  239. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " len(v_c_l[0]) == 0")
  240. continue
  241. }
  242. admin_r, err := Account.Read_Admin_ByUuid(v_c_l[0])
  243. if err != nil {
  244. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " Read_Admin_ByUuid!!")
  245. continue
  246. }
  247. if len(admin_r.T_phone) == 0 {
  248. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " admin_r.T_wx!!")
  249. continue
  250. }
  251. v_c_l[0] = admin_r.T_phone
  252. if len("18777951277") != len(v_c_l[0]) {
  253. Warning.Add_Warning_Log(Warning_r, "----> "+v_c_l[0]+" 号码异常! \n")
  254. continue
  255. }
  256. //电话
  257. Warning.Add_Warning_Log(Warning_r, "----> "+v_c_l[0])
  258. // 查看余额是否有钱
  259. if Company_Charging_r.T_money <= 0 {
  260. Warning.Add_Warning_Log(Warning_r, "["+Company_Charging_r.T_name+"]余额不足,请及时联系管理员充值!!!\n")
  261. continue
  262. }
  263. // 发送模板
  264. switch Warning_r.T_tp {
  265. case 1, 2, 3, 7, 8, 9: //温度
  266. reqdata_Message := "{\"name1\":\"" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\",\"name2\":\"" + Warning_r.T_Ut.Format("2006-01-02 15:04:05") + "\",\"name3\":\"" + Warning_r.T_DS_name + "\",\"name4\":\"" + Warning_r.T_Remark + "\",\"T_T\":\"" + strconv.FormatFloat(float64(T), 'f', -1, 32) + "\"}"
  267. NatsServer.Sb_Message(Company_Charging_r, Warning_r, v_c_l[0], "dks8a", reqdata_Message)
  268. break
  269. case 4, 5, 6, 10, 11, 12: //湿度
  270. reqdata_Message := "{\"name1\":\"" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\",\"name2\":\"" + Warning_r.T_Ut.Format("2006-01-02 15:04:05") + "\",\"name3\":\"" + Warning_r.T_DS_name + "\",\"name4\":\"" + Warning_r.T_Remark + "\",\"T_RH\":\"" + strconv.FormatFloat(float64(HR), 'f', -1, 32) + "\"}"
  271. NatsServer.Sb_Message(Company_Charging_r, Warning_r, v_c_l[0], "JDHNV4", reqdata_Message)
  272. break
  273. default: // 通知
  274. reqdata_Message := "{\"name1\":\"" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\",\"name2\":\"" + Warning_r.T_Ut.Format("2006-01-02 15:04:05") + "\",\"name3\":\"" + Warning_r.T_DS_name + "\",\"name4\":\"" + Warning_r.T_Remark + "\"}"
  275. NatsServer.Sb_Message(Company_Charging_r, Warning_r, v_c_l[0], "HeWYR3", reqdata_Message)
  276. }
  277. }
  278. } else { // 间隔时间内 超过发送条数
  279. int_x, _ := strconv.Atoi(T_nm_list[N_index+3])
  280. Warning.Add_Warning_Log(Warning_r, "--!短信-> 间隔时间内 超过发送条数,当期值:"+strconv.Itoa(int_x)+"\n")
  281. }
  282. } else { // 未达到 持续时间 秒
  283. int_x, _ := strconv.Atoi(T_nm_list[N_index+1])
  284. Warning.Add_Warning_Log(Warning_r, "--!短信-> 未达到持续时间:"+strconv.Itoa(int_x)+"\n")
  285. }
  286. }
  287. // 电话
  288. N_index = 10
  289. if T_nm_list[N_index] == "1" { // 是否开启
  290. Warning_num, Warning_sUt := Warning.Redis_Warning_Num(Warning_r.T_sn + "|" + strconv.Itoa(Warning_r.T_id) + "|" + strconv.Itoa(Warning_r.T_tp) + "|dh")
  291. Warning.Add_Warning_Log(Warning_r, fmt.Sprintf("[电话]持续时间:%d秒, 间隔时间:%s秒, 内发送次数:%d次, 间隔还剩时间:%d秒\n", cx_time, T_nm_list[N_index+2], Warning_num, Warning_sUt))
  292. if int_x, _ := strconv.Atoi(T_nm_list[N_index+1]); int64(int_x) <= cx_time { // 是否满足 持续时间 秒
  293. if int_u, _ := strconv.Atoi(T_nm_list[N_index+3]); (int64(int_u) > Warning_num) || int_u == 0 { //是否 间隔时间内 超过发送条数
  294. Warning.Redis_Warning_Num_W(Warning_r.T_sn+"|"+strconv.Itoa(Warning_r.T_id)+"|"+strconv.Itoa(Warning_r.T_tp)+"|dh", T_nm_list[N_index+2])
  295. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_phone:", CompanyNotice_r.T_Notice_phone)
  296. arr_c := strings.Split(CompanyNotice_r.T_Notice_phone, "|")
  297. for _, v_c := range arr_c {
  298. if len(v_c) == 0 {
  299. continue
  300. }
  301. v_c_l := strings.Split(v_c, "/")
  302. if len(v_c_l) != 2 {
  303. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " len(v_c_l) != 2")
  304. continue
  305. }
  306. if len(v_c_l[0]) == 0 {
  307. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " len(v_c_l[0]) == 0")
  308. continue
  309. }
  310. admin_r, err := Account.Read_Admin_ByUuid(v_c_l[0])
  311. if err != nil {
  312. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " Read_Admin_ByUuid!!")
  313. continue
  314. }
  315. if len(admin_r.T_phone) == 0 {
  316. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_wx:", v_c, " admin_r.T_wx!!")
  317. continue
  318. }
  319. v_c_l[0] = admin_r.T_phone
  320. if len("18777951277") != len(v_c_l[0]) {
  321. Warning.Add_Warning_Log(Warning_r, "----> "+v_c_l[0]+" 号码异常! \n")
  322. continue
  323. }
  324. //电话
  325. Warning.Add_Warning_Log(Warning_r, "---->"+v_c_l[0])
  326. // 查看余额是否有钱
  327. if Company_Charging_r.T_money <= 0 {
  328. Warning.Add_Warning_Log(Warning_r, "["+Company_Charging_r.T_name+"] 余额不足,请及时联系管理员充值!!!\n")
  329. continue
  330. }
  331. // 语音实体
  332. Hw_VoiceNotice_is := NatsServer.Hw_VoiceNotice(Company_Charging_r, Warning_r, v_c_l[0], []byte(fmt.Sprintf(`{"displayNbr":"+862022509874","calleeNbr":"%s","playInfoList":[{"templateId":"03bf51a5d8094db8ab1001aaa426ac60","templateParas":["%s","%s","%s"]}],"userData":"testUserData"}`, v_c_l[0], lib.Limit_len(Company_r.T_name, 29), lib.Limit_len(Warning_r.T_DS_name, 29), Warning.Read_WarningType_Get(Warning_r.T_tp))))
  333. if Hw_VoiceNotice_is {
  334. type V_struct struct {
  335. Corporate string `json:"company"`
  336. Device string `json:"device"`
  337. Name string `json:"name"`
  338. }
  339. Warning.Add_Warning_Log(Warning_r, "--!电话-> 华为云 通知失败,切换为 赛邮云"+"\n")
  340. V_struct_r := V_struct{Corporate: Company_r.T_name, Device: Warning_r.T_DS_name, Name: Warning.Read_WarningType_Get(Warning_r.T_tp)}
  341. V_struct_str, _ := json.Marshal(V_struct_r)
  342. NatsServer.Submail_Voice(Company_Charging_r, Warning_r, v_c_l[0], "i2ixE1", string(V_struct_str))
  343. }
  344. }
  345. } else { // 间隔时间内 超过发送条数
  346. int_x, _ := strconv.Atoi(T_nm_list[N_index+3])
  347. Warning.Add_Warning_Log(Warning_r, "--!电话-> 间隔时间内 超过发送条数,当期值:"+strconv.Itoa(int_x)+"\n")
  348. }
  349. } else { // 未达到 持续时间 秒
  350. int_x, _ := strconv.Atoi(T_nm_list[N_index+1])
  351. Warning.Add_Warning_Log(Warning_r, "--!电话-> 未达到持续时间:"+strconv.Itoa(int_x)+"\n")
  352. }
  353. }
  354. // APP
  355. N_index = 14
  356. if len(T_nm_list) > 14 { // 兼容后加的APP ,防止数组 溢出
  357. if T_nm_list[N_index] == "1" { // 是否开启
  358. Warning_num, Warning_sUt := Warning.Redis_Warning_Num(Warning_r.T_sn + "|" + strconv.Itoa(Warning_r.T_id) + "|" + strconv.Itoa(Warning_r.T_tp) + "|app")
  359. Warning.Add_Warning_Log(Warning_r, fmt.Sprintf("[APP]持续时间:%d秒, 间隔时间:%s秒, 内发送次数:%d次, 间隔还剩时间:%d秒\n", cx_time, T_nm_list[N_index+2], Warning_num, Warning_sUt))
  360. if int_x, _ := strconv.Atoi(T_nm_list[N_index+1]); int64(int_x) <= cx_time { // 是否满足 持续时间 秒
  361. if int_u, _ := strconv.Atoi(T_nm_list[N_index+3]); (int64(int_u) > Warning_num) || int_u == 0 { //是否 间隔时间内 超过发送条数
  362. Warning.Redis_Warning_Num_W(Warning_r.T_sn+"|"+strconv.Itoa(Warning_r.T_id)+"|"+strconv.Itoa(Warning_r.T_tp)+"|app", T_nm_list[N_index+2])
  363. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_APP:", CompanyNotice_r.T_Notice_app)
  364. arr_c := strings.Split(CompanyNotice_r.T_Notice_app, "|")
  365. for _, v_c := range arr_c {
  366. if len(v_c) == 0 {
  367. continue
  368. }
  369. v_c_l := strings.Split(v_c, "/")
  370. if len(v_c_l) != 2 {
  371. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_app:", v_c, " len(v_c_l) != 2")
  372. continue
  373. }
  374. if len(v_c_l[0]) == 0 {
  375. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_app:", v_c, " len(v_c_l[0]) == 0")
  376. continue
  377. }
  378. admin_r, err := Account.Read_Admin_ByUuid(v_c_l[0])
  379. if err != nil {
  380. logs.Println("==> WarningCompanyNotice_CompanyNotice_Send 推送 T_Notice_app:", v_c, " Read_Admin_ByUuid!!")
  381. continue
  382. }
  383. //APP
  384. Warning.Add_Warning_Log(Warning_r, "---->"+admin_r.T_uuid+"/"+admin_r.T_name)
  385. reqdata_Message := "{\"WarningType\":\"" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\",\"T_Ut\":\"" + Warning_r.T_Ut.Format("2006-01-02 15:04:05") + "\",\"T_DS_name\":\"" + Warning_r.T_DS_name + "\",\"T_Remark\":\"" + Warning_r.T_Remark + "\",\"T_T\":\"" + strconv.FormatFloat(float64(T), 'f', -1, 32) + "\"}"
  386. NatsServer.App_Warn(Company_Charging_r, Warning_r, admin_r.T_uuid, reqdata_Message)
  387. }
  388. } else { // 间隔时间内 超过发送条数
  389. int_x, _ := strconv.Atoi(T_nm_list[N_index+3])
  390. Warning.Add_Warning_Log(Warning_r, "--!APP-> 间隔时间内 超过发送条数,当期值:"+strconv.Itoa(int_x)+"\n")
  391. }
  392. } else { // 未达到 持续时间 秒
  393. int_x, _ := strconv.Atoi(T_nm_list[N_index+1])
  394. Warning.Add_Warning_Log(Warning_r, "--!APP-> 未达到持续时间:"+strconv.Itoa(int_x)+"\n")
  395. }
  396. }
  397. }
  398. }
  399. // 报警发送给用户表
  400. func WarningToAdminId(Warning_r *Warning.Warning, T_Admin_list []int) {
  401. logs.Println("==>WarningToAdmin ", "Msid", Warning_r.T_Msid, " T_Admin_list:", T_Admin_list)
  402. // 查看 sn 是否有归属
  403. if Warning_r.T_pid == 0 { // 寻找报警归属
  404. if len(Warning_r.T_sn) > 10 {
  405. r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn)
  406. if err != nil {
  407. logs.Println("==> WarningToAdminId MessageDisconnected 没有该设备:", Warning_r.T_sn)
  408. }
  409. Warning_r.T_pid = r_Device.T_pid
  410. Warning_r.T_D_name = r_Device.T_devName
  411. }
  412. }
  413. // 补齐公司归属
  414. var Company_r Account.Company
  415. if Warning_r.T_pid != 0 {
  416. _, Company_r = Account.Read_Company_id(Warning_r.T_pid)
  417. }
  418. // 如果 还是为空
  419. if len(Company_r.T_name) == 0 {
  420. Company_r.T_name = "未知公司"
  421. }
  422. // 补齐传感器名称
  423. if len(Warning_r.T_DS_name) == 0 {
  424. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(Warning_r.T_sn, Warning_r.T_id)
  425. if is {
  426. Warning_r.T_DS_name = DeviceSensor_r.T_name
  427. }
  428. }
  429. // 如果 还是为空 就等于 SN
  430. if len(Warning_r.T_DS_name) == 0 {
  431. Warning_r.T_DS_name = Warning_r.T_sn
  432. }
  433. // 选择相关 管理员
  434. if len(T_Admin_list) == 0 {
  435. if Warning_r.T_pid > 0 {
  436. // 有绑定公司的 管理员
  437. Warning.Add_Warning_Log(Warning_r, "有绑定公司ID -> ["+strconv.Itoa(Warning_r.T_pid)+"]\n")
  438. Tpids := Account.Read_Admin_Tpids_List(Warning_r.T_pid)
  439. for _, v := range Tpids {
  440. T_Admin_list = append(T_Admin_list, v.Id)
  441. }
  442. } else {
  443. // 默认发给管理员
  444. Warning.Add_Warning_Log(Warning_r, "默认发给管理员 -> [conf.EWarningAdmin]\n")
  445. T_Admin_list = conf.EWarningAdmin
  446. }
  447. }
  448. if len(T_Admin_list) == 0 {
  449. Warning.Add_Warning_Log(Warning_r, "!!!没有要通知的管理员!!!\n")
  450. return
  451. }
  452. T_Admin_list_str := ""
  453. for _, T_Admin_list_r := range T_Admin_list {
  454. T_Admin_list_str += strconv.Itoa(T_Admin_list_r) + "|"
  455. }
  456. Warning.Add_Warning_Log(Warning_r, "通知 管理员ID列表 -> ["+T_Admin_list_str+"]\n")
  457. // 通知策略
  458. for _, T_Admin_list_r := range T_Admin_list {
  459. logs.Println("==> WarningToAdminId T_Admin_id:", T_Admin_list_r)
  460. Admin_r, err := Account.Read_Admin_Byid(T_Admin_list_r)
  461. if err != nil {
  462. logs.Println("==> WarningToAdminId 没找到 T_Admin_id:", err)
  463. Warning.Add_Warning_Log(Warning_r, "没找到 T_Admin_id:-> "+strconv.Itoa(T_Admin_list_r)+"\n")
  464. continue
  465. }
  466. Warning.Add_Warning_Log(Warning_r, "通知-> ["+strconv.Itoa(Admin_r.Id)+"|"+Admin_r.T_name+"]\n")
  467. // 累积报警记录数
  468. key_Warning_num := "A" + strconv.Itoa(Admin_r.Id) + "|" + Warning_r.T_sn + "|" + strconv.Itoa(Warning_r.T_tp) // 用户+报警类型
  469. if !Warning.Redis_WarningToAdmin_Num(key_Warning_num) {
  470. Warning.Add_Warning_Log(Warning_r, "-> "+Admin_r.T_name+"["+strconv.Itoa(Admin_r.Id)+"]"+"相同报警(30分钟内),跳过该用户通知\n")
  471. continue
  472. }
  473. // 微信通知
  474. if len(Admin_r.T_wx) > 3 {
  475. data_json_t := Wx_d_struct{
  476. Corporate: Wx_struct{Value: lib.Limit_len(Company_r.T_name, 20)},
  477. Type: Wx_struct{Value: Warning.Read_WarningType_Get(Warning_r.T_tp)},
  478. Name: Wx_struct{Value: lib.Limit_len(Warning_r.T_DS_name, 20)},
  479. Time: Wx_struct{Value: Warning_r.T_Ut.Format("2006-01-02 15:04:05")},
  480. Remark: Wx_struct{Value: lib.Limit_len(Warning_r.T_Remark, 20)},
  481. }
  482. data_json_str, _ := json.Marshal(data_json_t)
  483. logs.Println("==> WarningToAdminId 通知内容:", string(data_json_str))
  484. // 用户下 多个微信
  485. arr_b := strings.Split(Admin_r.T_wx, "|")
  486. for _, v_b := range arr_b {
  487. v_b_l := strings.Split(v_b, "/")
  488. if len(v_b_l) != 2 {
  489. logs.Println("==> WarningToAdminId 推送 T_Notice_wx:", v_b, " len(v_b_l) != 2")
  490. continue
  491. }
  492. if len(v_b_l[0]) == 0 {
  493. logs.Println("==> WarningToAdminId 推送 T_Notice_wx:", v_b, " len(v_b_l[0]) == 0")
  494. continue
  495. }
  496. NatsServer.Wx_MessageTemplate(Account.Company{Id: Warning_r.T_pid}, Warning_r, v_b_l[0], "gnpERD6ilOQK6zvp1W_PmY5SEDOcSxiHh_KJbCdeAqw", string(data_json_str))
  497. }
  498. //Warning.Add_Warning_Log(Warning_r, "微信-> "+Admin_r.T_name+"\n")
  499. }
  500. // 短信通知
  501. //if len(Admin_r.T_phone) > 3 {
  502. // reqdata_Message := ""
  503. // NatsServer.Ali_Message(Warning_r, Admin_r.T_phone, "SMS_232170337", reqdata_Message)
  504. // Warning.Add_Warning_Log(Warning_r, "微信-> "+Admin_r.T_name+"\n")
  505. //}
  506. }
  507. defer func() {
  508. if Warning_r.Id > 0 {
  509. Warning.Update_Warning(*Warning_r, "T_Log")
  510. }
  511. }()
  512. }
  513. // 报警发送给用户表
  514. func WarningToUser(Warning_r *Warning.Warning, T_pid int) {
  515. logs.Println("==>WarningToUserId ", "Msid", Warning_r.T_Msid, " T_pid:", T_pid)
  516. Warning_r.T_pid = T_pid
  517. var T_Admin_list []int
  518. Tpid := Account.Read_Admin_Tpid_List(Warning_r.T_pid)
  519. for _, v := range Tpid {
  520. T_Admin_list = append(T_Admin_list, v.Id)
  521. }
  522. if len(T_Admin_list) == 0 {
  523. Warning.Add_Warning_Log(Warning_r, "!!!没有要通知的用户!!!\n")
  524. return
  525. }
  526. T_Admin_list_str := ""
  527. for _, T_Admin_list_r := range T_Admin_list {
  528. T_Admin_list_str += strconv.Itoa(T_Admin_list_r) + "|"
  529. }
  530. Warning.Add_Warning_Log(Warning_r, "通知 用户ID列表 -> ["+T_Admin_list_str+"]\n")
  531. // 通知策略
  532. for _, T_Admin_list_r := range T_Admin_list {
  533. logs.Println("==> WarningToAdminId T_Admin_id:", T_Admin_list_r)
  534. Admin_r, err := Account.Read_Admin_Byid(T_Admin_list_r)
  535. if err != nil {
  536. logs.Println("==> WarningToAdminId 没找到 T_Admin_id:", err)
  537. Warning.Add_Warning_Log(Warning_r, "没找到 T_Admin_id:-> "+strconv.Itoa(T_Admin_list_r)+"\n")
  538. continue
  539. }
  540. Warning.Add_Warning_Log(Warning_r, "通知-> ["+strconv.Itoa(Admin_r.Id)+"|"+Admin_r.T_name+"]\n")
  541. // 累积报警记录数
  542. key_Warning_num := "A" + strconv.Itoa(Admin_r.Id) + "|" + Warning_r.T_sn + "|" + strconv.Itoa(Warning_r.T_tp) // 用户+报警类型
  543. if !Warning.Redis_WarningToAdmin_Num(key_Warning_num) {
  544. Warning.Add_Warning_Log(Warning_r, "-> "+Admin_r.T_name+"["+strconv.Itoa(Admin_r.Id)+"]"+"相同报警(30分钟内),跳过该用户通知\n")
  545. continue
  546. }
  547. // 获取公司名称
  548. _, Company_r := Account.Read_Company_id(Warning_r.T_pid)
  549. if Company_r.Id == 0 {
  550. Company_r.T_name = "冷链系统"
  551. }
  552. // 微信通知
  553. if len(Admin_r.T_wx) > 3 {
  554. data_json_t := Wx_d_struct{
  555. Corporate: Wx_struct{Value: lib.Limit_len(Company_r.T_name, 20)},
  556. Type: Wx_struct{Value: Warning.Read_WarningType_Get(Warning_r.T_tp)},
  557. Name: Wx_struct{Value: lib.Limit_len(Warning_r.T_D_name, 20)},
  558. Time: Wx_struct{Value: Warning_r.T_Ut.Format("2006-01-02 15:04:05")},
  559. Remark: Wx_struct{Value: lib.Limit_len(Warning_r.T_Remark, 20)},
  560. }
  561. data_json_str, _ := json.Marshal(data_json_t)
  562. logs.Println("==> WarningToAdminId 通知内容:", string(data_json_str))
  563. // 用户下 多个微信
  564. arr_b := strings.Split(Admin_r.T_wx, "|")
  565. for _, v_b := range arr_b {
  566. v_b_l := strings.Split(v_b, "/")
  567. if len(v_b_l) != 2 {
  568. logs.Println("==> WarningToAdminId 推送 T_Notice_wx:", v_b, " len(v_b_l) != 2")
  569. continue
  570. }
  571. if len(v_b_l[0]) == 0 {
  572. logs.Println("==> WarningToAdminId 推送 T_Notice_wx:", v_b, " len(v_b_l[0]) == 0")
  573. continue
  574. }
  575. NatsServer.Wx_MessageTemplate(Account.Company{Id: Warning_r.T_pid}, Warning_r, v_b_l[0], "gnpERD6ilOQK6zvp1W_PmY5SEDOcSxiHh_KJbCdeAqw", string(data_json_str))
  576. }
  577. //Warning.Add_Warning_Log(Warning_r, "微信-> "+Admin_r.T_name+"\n")
  578. }
  579. // 短信通知
  580. //if len(Admin_r.T_phone) > 3 {
  581. // reqdata_Message := ""
  582. // NatsServer.Ali_Message(Warning_r, Admin_r.T_phone, "SMS_232170337", reqdata_Message)
  583. // Warning.Add_Warning_Log(Warning_r, "微信-> "+Admin_r.T_name+"\n")
  584. //}
  585. }
  586. defer func() {
  587. if Warning_r.Id > 0 {
  588. Warning.Update_Warning(*Warning_r, "T_Log")
  589. }
  590. }()
  591. }