package Warning import ( "Cold_DeductionNotice/conf" "Cold_DeductionNotice/lib" "Cold_DeductionNotice/logs" "Cold_DeductionNotice/models/Device" "fmt" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" _ "github.com/go-sql-driver/mysql" "strconv" "strings" "time" ) // 模板 type Warning struct { Id int64 `orm:"column(ID);size(11);auto;pk"` T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司 T_tp int `orm:"index;size(200);null"` // 报警类型 ->WarningList T_sn string `orm:"index;size(256);null"` // 设备序列号 T_D_name string `orm:"size(256);null"` // 设备名称 T_id int `orm:"index;size(200);null"` // 传感器 ID T_DS_name string `orm:"size(256);null"` // 传感器名称 T_Remark string `orm:"type(text);null"` // 采集内容 T_Ut time.Time `orm:"index;type(timestamp);null;"` // 采集时间 T_fUt time.Time `orm:"type(timestamp);null;"` // 首次采集时间 T_Text string `orm:"type(text);null"` // 处理备注 T_Log string `orm:"type(text);null"` // 通知日志 T_Msid int64 `orm:"size(256);null"` // 消息ID T_State int `orm:"size(2);1"` // 0 删除 1 不处理 2 已处理 3 未处理 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } func (t *Warning) TableName() string { return "warning" // 数据库名称 // ************** 替换 FormulaList ************** } var redisCache_Warning cache.Cache func init() { //注册模型 orm.RegisterModel(new(Warning)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_WarningNum", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) logs.Println(config) var err error redisCache_Warning, err = cache.NewCache("redis", config) if err != nil || redisCache_Warning == nil { errMsg := "failed to init redis" logs.Println(errMsg, err) } } // ---------------- Redis ------------------- // Redis_Set(m.T_sn,m) // Redis 更新缓存 func Redis_Warning_Set(key string) (err error) { err = redisCache_Warning.Put(key, "", 24*time.Hour) if err != nil { logs.Println("set key:", key) } return } // if r,is :=Redis_Get(T_sn);is{ // return r,nil // } func Redis_Warning_Repeat_T_sn_Msid(key string) (is bool) { if redisCache_Warning.IsExist(key) { //println("找到key:",key) return true } redisCache_Warning.Put(key, "", 1*time.Minute) return false } // 缓存报警次数 cut 持续秒 (计次,剩余时间s) func Redis_Warning_Num(key string) (int64, int) { if redisCache_Warning.IsExist(key) { //logs.Println("Redis_Warning_Num 找到key:", key) r := redisCache_Warning.Get(key) if r == nil { return 0, 0 } value_str := string(r.([]byte)) value_str_list := strings.Split(value_str, "|") if len(value_str_list) != 2 { return 0, 0 } logs.Println("value_str_list:",value_str_list) //value_str_list: [[1 1677724222]] value_int, _ := strconv.ParseInt(value_str_list[0], 10, 64) cut := int(time.Unix(lib.To_int64(value_str_list[1]), 0).Unix() - time.Now().Unix()) // 计算 还剩 s秒时间 return value_int, cut } return 0, 0 } // 缓存报警次数 cut 持续秒 (计次,剩余时间s) func Redis_Warning_Num_W(key,cuts string) (int64, int) { cut,err := strconv.Atoi(cuts) if err != nil { logs.PrintlnError("Redis_Warning_Num err:", err) } if redisCache_Warning.IsExist(key) { //logs.Println("Redis_Warning_Num 找到key:", key) r := redisCache_Warning.Get(key) if r == nil { goto breakHere // 跳转到标签 } value_str := string(r.([]byte)) value_str_list := strings.Split(value_str, "|") logs.Println("value_str_list:",value_str_list) //value_str_list: [[1 1677724222]] if len(value_str_list) != 2 { goto breakHere // 跳转到标签 } value_int, _ := strconv.ParseInt(value_str_list[0], 10, 64) value_int += 1 // 数量 //logs.Println("有数据 value_int:", value_int) cut = int(time.Unix(lib.To_int64(value_str_list[1]), 0).Unix() - time.Now().Unix()) // 计算 还剩 s秒时间 redisCache_Warning.Put(key, fmt.Sprintf("%d|%s", value_int, value_str_list[1]), time.Duration(cut)*time.Second) logs.Println("Redis_Warning_Num KEY:", key, "->", value_int, " 还剩时间 ->", cut) return value_int, cut } breakHere: //logs.Println("没有数据 value_int:", 1) s, _ := time.ParseDuration(fmt.Sprintf("%ds",cut)) redisCache_Warning.Put(key, fmt.Sprintf("1|%d", time.Now().Add(s).Unix()), time.Duration(cut)*time.Second) logs.Println("Redis_Warning_Num NewKEY:", key," cut:",cut) return 1, cut } // 管理员 缓存报警次数 func Redis_WarningToAdmin_Num(key string) bool { if redisCache_Warning.IsExist(key) { ////logs.Println("Redis_Warning_Num 找到key:", key) //r := redisCache_Warning.Get(key) //value_str := string(r.([]byte)) //value_int, _ := strconv.ParseInt(value_str, 10, 64) //value_int += 1 ////logs.Println("有数据 value_int:", value_int) ////redisCache_Warning.Put(key, strconv.FormatInt(value_int, 10), 11*time.Minute) return false } //logs.Println("没有数据 value_int:", 1) redisCache_Warning.Put(key, "1", 30*time.Minute) return true } // ---------------- 特殊方法 ------------------- // 添加 func Add_Warning(m Warning) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { logs.PrintlnError("Add_Warning err:", err) } return id, err } // 修改 func Update_Warning(r Warning, cols ...string) bool { o := orm.NewOrm() if num, err := o.Update(&r, cols...); err == nil { logs.Println("Number of records updated in database:", num) return true } return false } // 追加 日志 func Add_Warning_Log(r *Warning, T_Log string) { r.T_Log = r.T_Log + T_Log return } func Add_DeviceLogs(T_tp int,r_Device Device.Device, T_Remark string) Warning { var Warning_r Warning Warning_r.T_pid = r_Device.T_pid Warning_r.T_tp = T_tp Warning_r.T_sn = r_Device.T_sn Warning_r.T_D_name = r_Device.T_devName Warning_r.T_DS_name = r_Device.T_devName Warning_r.T_id = 0 Warning_r.T_Remark = T_Remark Warning_r.T_Ut = time.Now() Warning_r.T_State = 1 // 查看 sn 是否有归属 if Warning_r.T_pid == 0 { // 寻找报警归属 if len(Warning_r.T_sn) > 10 { r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn) if err != nil { logs.Println("MessageDisconnected 没有该设备:", Warning_r.T_sn) } Warning_r.T_pid = r_Device.T_pid } } // 添加报警 id, _ :=Add_Warning(Warning_r) Warning_r.Id = id return Warning_r }