package Device import ( "Cold_Api/conf" "Cold_Api/controllers/lib" "Cold_Api/models" "encoding/json" "fmt" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" _ "github.com/go-sql-driver/mysql" "strconv" "sync" "time" ) // 模板 type DeviceSensorParameter struct { Id int `orm:"column(ID);size(11);auto;pk"` T_sn string `orm:"index;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int `orm:"index;size(11);null"` // 传感器编号 T_name string `orm:"size(256);null"` // 标题 // 报警 T_Tlower float32 `orm:"size(200);null"` // 温度下限 T_Tupper float32 `orm:"size(200);null"` // 温度上限 T_RHlower float32 `orm:"size(200);null"` // 湿度下限 T_RHupper float32 `orm:"size(200);null"` // 湿度上限 // 预警 T_enprel int `orm:"size(2);default(1)"` // 是否启用预警 1开启 0 关闭 T_tprel float32 `orm:"size(200);null"` // 温度预警下限 T_tpreu float32 `orm:"size(200);null"` // 温度预警上限 T_hprel float32 `orm:"size(200);null"` // 湿度预警下限 T_hpreu float32 `orm:"size(200);null"` // 温度预警上限 T_enprelnote int `orm:"size(2);default(1)"` // 是否启用预警记录数据 //T_speed int `orm:"size(5);null"` // 传感器采样率 s(1~240) 默认:15 * //T_sense int `orm:"size(5);"` // 传感器灵敏度 s(0~10) 默认:5 T_en int `orm:"size(2);default(1)"` // en:是否启用传感器, 0 停用 1 启用 T_free int `orm:"size(2);default(1)"` // free:监测点是否为闲置状态(空库,只监测不报警) 0 正常 1 空库 // 逻辑字段 T_uuid string `orm:"size(256);null"` // 处理 人员 T_Msid int64 `orm:"size(50);default(0)"` // 消息识别ID T_SendState int `orm:"size(2);default(0)"` // 发送状态 0 待发送 1 发送成功 2 失败 3 覆盖 T_State int `orm:"size(2);default(1)"` // 1 系统获取 2 用户提交 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 保存时都会对时间自动更新 } type DeviceSensorParameter_R struct { Id int T_name string // 报警 T_Tlower float32 // 温度下限 T_Tupper float32 // 温度上限 T_RHlower float32 // 湿度下限 T_RHupper float32 // 湿度上限 // 预警 T_enprel int // 是否启用预警 T_tprel float32 // 温度预警下限 T_tpreu float32 // 温度预警上限 T_hprel float32 // 湿度预警下限 T_hpreu float32 // 温度预警上限 T_enprelnote int // 预警记录数据 //T_speed int // 传感器采样率 s(1~240) 默认:15 * //T_sense int // 传感器灵敏度 s(0~10) 默认:5 T_en int // en:是否启用传感器, T_free int // free:监测点是否为闲置状态(空库,只监测不报警) T_time models.JsonTime } type DeviceSensorParameter_M struct { T_name string // 标题 // 报警 T_Tlower float32 // 温度下限 T_Tupper float32 // 温度上限 T_RHlower float32 // 湿度下限 T_RHupper float32 // 湿度上限 T_free int } type DeviceSensorParameter_T struct { T_SendState int // 发送状态 0 待发送 1 发送成功 2 失败 3 覆盖 T_text string } func DeviceSensorParameterToDeviceSensorParameter_R(r DeviceSensorParameter) (t DeviceSensorParameter_R) { t.Id = r.Id t.T_name = r.T_name t.T_Tlower = r.T_Tlower t.T_Tupper = r.T_Tupper t.T_RHlower = r.T_RHlower t.T_RHupper = r.T_RHupper t.T_enprel = r.T_enprel t.T_tprel = r.T_tprel t.T_tpreu = r.T_tpreu t.T_hprel = r.T_hprel t.T_hpreu = r.T_hpreu t.T_enprelnote = r.T_enprelnote //t.T_speed = r.T_speed //t.T_sense = r.T_sense t.T_en = r.T_en t.T_free = r.T_free t.T_time.Time = r.CreateTime return t } func DeviceSensorParameterToDeviceSensorParameter_M(r DeviceSensorParameter) (t DeviceSensorParameter_M) { t.T_name = r.T_name t.T_Tlower = r.T_Tlower t.T_Tupper = r.T_Tupper t.T_RHlower = r.T_RHlower t.T_RHupper = r.T_RHupper t.T_free = r.T_free return t } func DeviceSensorParameterToDeviceSensorParameter_T(r DeviceSensorParameter) (t DeviceSensorParameter_T) { t.T_text = DeviceSensorParameter_T_text(r) t.T_SendState = r.T_SendState return t } func DeviceSensorParameter_T_text(r DeviceSensorParameter) string { s := "" s += "操作人UUID:" + r.T_uuid s += " 设备名称:" + r.T_name s += fmt.Sprintf(" 温度下限:%.2f", r.T_Tlower) s += fmt.Sprintf(" 温度上限:%.2f", r.T_Tupper) s += fmt.Sprintf(" 湿度下限:%.2f", r.T_RHlower) s += fmt.Sprintf(" 湿度上限:%.2f", r.T_RHupper) s += fmt.Sprintf(" 是否启用预警:%d", r.T_enprel) s += fmt.Sprintf(" 温度预警下限:%.2f", r.T_tprel) s += fmt.Sprintf(" 温度预警上限:%.2f", r.T_tpreu) s += fmt.Sprintf(" 湿度预警下限:%.2f", r.T_hprel) s += fmt.Sprintf(" 湿度预警上限:%.2f", r.T_hpreu) s += fmt.Sprintf(" 是否启用预警记录数据:%d", r.T_enprelnote) //s += fmt.Sprintf(" 传感器采样率:%d", r.T_speed) //s += fmt.Sprintf(" 传感器灵敏度:%d", r.T_sense) s += fmt.Sprintf(" 是否启用传感器:%d", r.T_en) s += fmt.Sprintf(" 监测点是否为闲置状态:%d", r.T_free) s += " 修改时间:" + r.UpdateTime.Format("2006-01-02 15:04:05") return s } func (t *DeviceSensorParameter) TableName() string { return "device_sensor_parameter" // 数据库名称 // ************** 替换 FormulaList ************** } var redisCache_DeviceSensorParameter cache.Cache // var DeviceSensorParameter_list map[int]DeviceSensorParameter_M // 泛型 var DeviceSensorParameter_list *sync.Map func init() { //注册模型 orm.RegisterModel(new(DeviceSensorParameter)) DeviceSensorParameter_list = new(sync.Map) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_DeviceSensorParameter", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_DeviceSensorParameter, err = cache.NewCache("redis", config) if err != nil || redisCache_DeviceSensorParameter == nil { errMsg := "failed to init redis" logs.Error(errMsg, err) panic(errMsg) } } // ---------------- Redis ------------------- // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存 func Redis_DeviceSensorParameter_Set(key string, r DeviceSensorParameter) (err error) { //json序列化 str, err := json.Marshal(r) if err != nil { logs.Error(lib.FuncName(), err) return } err = redisCache_DeviceSensorParameter.Put(key, str, 8*time.Hour) if err != nil { logs.Error("set key:", key, ",value:", str, err) } return } // if r,is :=Redis_Device_Get(T_sn);is{ // return r,nil // } func Redis_DeviceSensorParameter_Get(key string) (r DeviceSensorParameter, is bool) { if redisCache_DeviceSensorParameter.IsExist(key) { //println("找到key:",key) v := redisCache_DeviceSensorParameter.Get(key) err := json.Unmarshal(v.([]byte), &r) if err != nil { logs.Error(lib.FuncName(), err) return r, false } return r, true } //println("没有 找到key:",key) return DeviceSensorParameter{}, false } // ---------------- 特殊方法 ------------------- // 获取设备参数 func Read_DeviceSensorParameter(T_sn string, T_id int) (t DeviceSensorParameter_R, bool bool) { key := T_sn + "|" + strconv.Itoa(T_id) if v, is := Redis_DeviceSensorParameter_Get(key); is { return DeviceSensorParameterToDeviceSensorParameter_R(v), true } o := orm.NewOrm() qs := o.QueryTable(new(DeviceSensorParameter)) var r []DeviceSensorParameter cond := orm.NewCondition() cond = cond.And("T_sn", T_sn).And("T_id", T_id).And("T_State", 1) _, err := qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-CreateTime").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return t, false } // 判断是否有数据 if len(r) == 0 { Redis_DeviceSensorParameter_Set(key, DeviceSensorParameter{}) return t, false } t = DeviceSensorParameterToDeviceSensorParameter_R(r[0]) Redis_DeviceSensorParameter_Set(key, r[0]) return t, true } // 获取列表 T_State 1 系统获取 2 用户提交 func Read_DeviceSensorParameter_T_sn_T_id(T_sn string, T_id int) (r []DeviceSensorParameter) { o := orm.NewOrm() qs := o.QueryTable(new(DeviceSensorParameter)) cond := orm.NewCondition() cond = cond.And("T_sn", T_sn).And("T_id", T_id).And("T_State", 1) _, err := qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) } return r } // 获取列表 T_State 1 系统获取 2 用户提交 func Read_DeviceSensorParameter_List(T_sn string, T_id int, T_State int, page, page_z int) (r []DeviceSensorParameter_T) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensorParameter)) cond := orm.NewCondition() var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond = cond.And("T_sn", T_sn) cond = cond.And("T_id", T_id) if T_State != 0 { cond = cond.And("T_State", T_State) } var maps []DeviceSensorParameter _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps) if err != nil { logs.Error(lib.FuncName(), err) return r } for _, v := range maps { r = append(r, DeviceSensorParameterToDeviceSensorParameter_T(v)) } return r } // 添加 func Add_DeviceSensorParameter(m DeviceSensorParameter) (DeviceSensorParameter, bool) { o := orm.NewOrm() id, err := o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) return m, false } m.Id = int(id) return m, true } // 获取列表 T_State 1 系统获取 2 用户提交 func Read_DeviceSensorParameter_All_Map(T_sn string, T_id int) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var list []DeviceSensorParameter qs := o.QueryTable(new(DeviceSensorParameter)) cond := orm.NewCondition() cond = cond.And("T_State", 1) if len(T_sn) > 0 { cond = cond.And("T_sn", T_sn) } if T_id > 0 { cond = cond.And("T_id", T_id) } _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&list) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range list { DeviceSensorParameter_list.Store(v.Id, DeviceSensorParameterToDeviceSensorParameter_M(v)) } } func Read_DeviceSensorParameter_Map_Get(id int) DeviceSensorParameter_M { // 有先加入 给全部人发消息 v, ok := DeviceSensorParameter_list.Load(id) /*如果确定是真实的,则存在,否则不存在 */ if ok { return v.(DeviceSensorParameter_M) } else { return DeviceSensorParameter_M{} } }