Warning.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package Warning
  2. import (
  3. "Cold_DeductionNotice/conf"
  4. "Cold_DeductionNotice/lib"
  5. "Cold_DeductionNotice/logs"
  6. "Cold_DeductionNotice/models/Device"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 模板
  17. type Warning struct {
  18. Id int64 `orm:"column(ID);size(11);auto;pk"`
  19. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
  20. T_tp int `orm:"index;size(200);null"` // 报警类型 ->WarningList
  21. T_sn string `orm:"index;size(256);null"` // 设备序列号
  22. T_D_name string `orm:"size(256);null"` // 设备名称
  23. T_id int `orm:"index;size(200);null"` // 传感器 ID
  24. T_DS_name string `orm:"size(256);null"` // 传感器名称
  25. T_Remark string `orm:"type(text);null"` // 采集内容
  26. T_Ut time.Time `orm:"index;type(timestamp);null;"` // 采集时间
  27. T_fUt time.Time `orm:"type(timestamp);null;"` // 首次采集时间
  28. T_Text string `orm:"type(text);null"` // 处理备注
  29. T_Log string `orm:"type(text);null"` // 通知日志
  30. T_Msid int64 `orm:"size(256);null"` // 消息ID
  31. T_State int `orm:"size(2);1"` // 0 删除 1 不处理 2 已处理 3 未处理
  32. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  33. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  34. }
  35. func (t *Warning) TableName() string {
  36. return "warning" // 数据库名称 // ************** 替换 FormulaList **************
  37. }
  38. var redisCache_Warning cache.Cache
  39. func init() {
  40. //注册模型
  41. orm.RegisterModel(new(Warning))
  42. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  43. "redis_WarningNum", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  44. logs.Println(config)
  45. var err error
  46. redisCache_Warning, err = cache.NewCache("redis", config)
  47. if err != nil || redisCache_Warning == nil {
  48. errMsg := "failed to init redis"
  49. logs.Println(errMsg, err)
  50. }
  51. }
  52. // ---------------- Redis -------------------
  53. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  54. func Redis_Warning_Set(key string) (err error) {
  55. err = redisCache_Warning.Put(key, "", 24*time.Hour)
  56. if err != nil {
  57. logs.Println("set key:", key)
  58. }
  59. return
  60. }
  61. // if r,is :=Redis_Get(T_sn);is{
  62. // return r,nil
  63. // }
  64. func Redis_Warning_Repeat_T_sn_Msid(key string) (is bool) {
  65. if redisCache_Warning.IsExist(key) {
  66. //println("找到key:",key)
  67. return true
  68. }
  69. redisCache_Warning.Put(key, "", 1*time.Minute)
  70. return false
  71. }
  72. // 缓存报警次数 cut 持续秒 (计次,剩余时间s)
  73. func Redis_Warning_Num(key string) (int64, int) {
  74. if redisCache_Warning.IsExist(key) {
  75. //logs.Println("Redis_Warning_Num 找到key:", key)
  76. r := redisCache_Warning.Get(key)
  77. if r == nil {
  78. return 0, 0
  79. }
  80. value_str := string(r.([]byte))
  81. value_str_list := strings.Split(value_str, "|")
  82. if len(value_str_list) != 2 {
  83. return 0, 0
  84. }
  85. logs.Println("value_str_list:",value_str_list) //value_str_list: [[1 1677724222]]
  86. value_int, _ := strconv.ParseInt(value_str_list[0], 10, 64)
  87. cut := int(time.Unix(lib.To_int64(value_str_list[1]), 0).Unix() - time.Now().Unix()) // 计算 还剩 s秒时间
  88. return value_int, cut
  89. }
  90. return 0, 0
  91. }
  92. // 缓存报警次数 cut 持续秒 (计次,剩余时间s)
  93. func Redis_Warning_Num_W(key,cuts string) (int64, int) {
  94. cut,err := strconv.Atoi(cuts)
  95. if err != nil {
  96. logs.PrintlnError("Redis_Warning_Num err:", err)
  97. }
  98. if redisCache_Warning.IsExist(key) {
  99. //logs.Println("Redis_Warning_Num 找到key:", key)
  100. r := redisCache_Warning.Get(key)
  101. if r == nil {
  102. goto breakHere // 跳转到标签
  103. }
  104. value_str := string(r.([]byte))
  105. value_str_list := strings.Split(value_str, "|")
  106. logs.Println("value_str_list:",value_str_list) //value_str_list: [[1 1677724222]]
  107. if len(value_str_list) != 2 {
  108. goto breakHere // 跳转到标签
  109. }
  110. value_int, _ := strconv.ParseInt(value_str_list[0], 10, 64)
  111. value_int += 1 // 数量
  112. //logs.Println("有数据 value_int:", value_int)
  113. cut = int(time.Unix(lib.To_int64(value_str_list[1]), 0).Unix() - time.Now().Unix()) // 计算 还剩 s秒时间
  114. redisCache_Warning.Put(key, fmt.Sprintf("%d|%s", value_int, value_str_list[1]), time.Duration(cut)*time.Second)
  115. logs.Println("Redis_Warning_Num KEY:", key, "->", value_int, " 还剩时间 ->", cut)
  116. return value_int, cut
  117. }
  118. breakHere:
  119. //logs.Println("没有数据 value_int:", 1)
  120. s, _ := time.ParseDuration(fmt.Sprintf("%ds",cut))
  121. redisCache_Warning.Put(key, fmt.Sprintf("1|%d", time.Now().Add(s).Unix()), time.Duration(cut)*time.Second)
  122. logs.Println("Redis_Warning_Num NewKEY:", key," cut:",cut)
  123. return 1, cut
  124. }
  125. // 管理员 缓存报警次数
  126. func Redis_WarningToAdmin_Num(key string) bool {
  127. if redisCache_Warning.IsExist(key) {
  128. ////logs.Println("Redis_Warning_Num 找到key:", key)
  129. //r := redisCache_Warning.Get(key)
  130. //value_str := string(r.([]byte))
  131. //value_int, _ := strconv.ParseInt(value_str, 10, 64)
  132. //value_int += 1
  133. ////logs.Println("有数据 value_int:", value_int)
  134. ////redisCache_Warning.Put(key, strconv.FormatInt(value_int, 10), 11*time.Minute)
  135. return false
  136. }
  137. //logs.Println("没有数据 value_int:", 1)
  138. redisCache_Warning.Put(key, "1", 30*time.Minute)
  139. return true
  140. }
  141. // ---------------- 特殊方法 -------------------
  142. // 添加
  143. func Add_Warning(m Warning) (id int64, err error) {
  144. o := orm.NewOrm()
  145. id, err = o.Insert(&m)
  146. if err != nil {
  147. logs.PrintlnError("Add_Warning err:", err)
  148. }
  149. return id, err
  150. }
  151. // 修改
  152. func Update_Warning(r Warning, cols ...string) bool {
  153. o := orm.NewOrm()
  154. if num, err := o.Update(&r, cols...); err == nil {
  155. logs.Println("Number of records updated in database:", num)
  156. return true
  157. }
  158. return false
  159. }
  160. // 追加 日志
  161. func Add_Warning_Log(r *Warning, T_Log string) {
  162. r.T_Log = r.T_Log + T_Log
  163. return
  164. }
  165. func Add_DeviceLogs(T_tp int,r_Device Device.Device, T_Remark string) Warning {
  166. var Warning_r Warning
  167. Warning_r.T_pid = r_Device.T_pid
  168. Warning_r.T_tp = T_tp
  169. Warning_r.T_sn = r_Device.T_sn
  170. Warning_r.T_D_name = r_Device.T_devName
  171. Warning_r.T_DS_name = r_Device.T_devName
  172. Warning_r.T_id = 0
  173. Warning_r.T_Remark = T_Remark
  174. Warning_r.T_Ut = time.Now()
  175. Warning_r.T_State = 1
  176. // 查看 sn 是否有归属
  177. if Warning_r.T_pid == 0 { // 寻找报警归属
  178. if len(Warning_r.T_sn) > 10 {
  179. r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn)
  180. if err != nil {
  181. logs.Println("MessageDisconnected 没有该设备:", Warning_r.T_sn)
  182. }
  183. Warning_r.T_pid = r_Device.T_pid
  184. }
  185. }
  186. // 添加报警
  187. id, _ :=Add_Warning(Warning_r)
  188. Warning_r.Id = id
  189. return Warning_r
  190. }