123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package Device
- import (
- "ColdP_server/conf"
- "ColdP_server/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/go-sql-driver/mysql"
- "strconv"
- "time"
- )
- // 模板
- type DeviceSensorParameter struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_sn string `orm:"index;size(256);"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_id int `orm:"index;size(11);"` // 传感器编号
- T_name string `orm:"size(256);"` //传感器名称
- // 报警
- T_Tlower float32 `orm:"size(200);"` // 温度下限
- T_Tupper float32 `orm:"size(200);"` // 温度上限
- T_RHlower float32 `orm:"size(200);"` // 湿度下限
- T_RHupper float32 `orm:"size(200);"` // 湿度上限
- // 预警
- T_enprel int `orm:"size(2);default(1)"` // 是否启用预警
- T_tprel float32 `orm:"size(200);"` // 温度预警下限
- T_tpreu float32 `orm:"size(200);"` // 温度预警上限
- T_hprel float32 `orm:"size(200);"` // 湿度预警下限
- T_hpreu float32 `orm:"size(200);"` // 温度预警上限
- //
- //T_speed int `orm:"size(5);"` // 传感器采样率 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)"` // 是否为闲置状态 1:启用(正常 实时、记录, 不 报警、预警)
- //0:关闭(正常 实时、记录、报警、预警) 默认:0
- // 逻辑字段
- T_uuid string `orm:"size(256);"` //处理 人员
- 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);auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- type DeviceSensorParameter_R struct {
- // 报警
- 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_en int // en:是否启用传感器,
- T_free int // free:监测点是否为闲置状态(空库,只监测不报警)
- T_time models.JsonTime
- }
- type DeviceSensorParameter_M struct {
- T_name string // 标题
- // 报警
- T_sp int // 传感器编号
- T_Tlower float32 // 温度下限
- T_Tupper float32 // 温度上限
- T_RHlower float32 // 湿度下限
- T_RHupper float32 // 湿度上限
- }
- func DeviceSensorParameterTo_DeviceSensorParameter_R(r DeviceSensorParameter) (t DeviceSensorParameter_R) {
- 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_en = r.T_en
- t.T_free = r.T_free
- t.T_time.Time = r.CreateTime
- return t
- }
- func (t *DeviceSensorParameter) TableName() string {
- return "device_sensor_parameter" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_DeviceSensorParameter cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(DeviceSensorParameter))
- 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"
- fmt.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_DeviceSensorParameter_Set(key string, r DeviceSensorParameter_R) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- fmt.Print(err)
- return
- }
- err = redisCache_DeviceSensorParameter.Put(key, str, 8*time.Hour)
- if err != nil {
- fmt.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_DeviceSensorParameter_Get(key string) (r DeviceSensorParameter_R, is bool) {
- if redisCache_DeviceSensorParameter.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_DeviceSensorParameter.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- //println("没有 找到key:",key)
- return DeviceSensorParameter_R{}, false
- }
- // ---------------- 特殊方法 -------------------
- // 获取设备参数
- func Read_V2DeviceSensorParameter(T_sn string, T_id int) (t DeviceSensorParameter_R, bool bool) {
- key := T_sn + "|" + strconv.Itoa(T_id)
- if t, is := Redis_DeviceSensorParameter_Get(key); is {
- return t, 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).AndCond(cond.And("T_State", 1).OrCond(cond.And("T_State", 2).And("T_SendState", 1)))
- qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-UpdateTime").All(&r)
- // 判断是否有数据
- if len(r) == 0 {
- return t, false
- }
- return DeviceSensorParameterTo_DeviceSensorParameter_R(r[0]), true
- }
- // 获取列表 T_State 1 系统获取 2 用户提交
- func Read_V2DeviceSensorParameter_List(T_sn string, T_id int, T_State int, page_z int) (r []DeviceSensorParameter) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensorParameter))
- cond := orm.NewCondition()
- 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)
- }
- qs.Limit(page_z, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
- return r
- }
- // 添加
- func Add_DeviceSensorParameter(m DeviceSensorParameter) (int64, bool) {
- o := orm.NewOrm()
- id, err := o.Insert(&m)
- if err != nil {
- fmt.Println(err)
- return 0, false
- }
- return id, true
- }
- // 修改
- func Update_DeviceSensorParameter(r DeviceSensorParameter, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&r, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- return true
- }
- return false
- }
- func DELETE_DeviceSensorParameter(SN string) bool {
- sql := "DELETE FROM `cold`.`device_sensor_parameter` WHERE `t_sn` = '" + SN + "' "
- o := orm.NewOrm()
- _, err := o.Raw(sql).Exec()
- if err != nil {
- return false
- }
- return true
- }
- // 获取列表 T_State 1 系统获取 2 用户提交
- func Read_DeviceSensorParameter_Map_All(T_sn string, T_id int) (maps map[int]DeviceSensorParameter_M) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var list []DeviceSensorParameter
- qs := o.QueryTable(new(DeviceSensorParameter))
- cond := orm.NewCondition()
- cond = cond.And("T_sn", T_sn).And("T_id", T_id).AndCond(cond.And("T_State", 1).OrCond(cond.And("T_State", 2).And("T_SendState", 1)))
- qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&list)
- //sql := fmt.Sprintf("SELECT * FROM `device_sensor_parameter` WHERE t_sn='%s' and t_id =%s AND update_time < '2024-09-12 10:00:00' ORDER BY update_time DESC LIMIT 1", T_sn, T_id)
- maps = make(map[int]DeviceSensorParameter_M)
- for _, v := range list {
- maps[v.Id] = DeviceSensorParameter_M{
- T_sp: v.Id,
- T_name: v.T_name,
- T_Tlower: v.T_Tlower,
- T_Tupper: v.T_Tupper,
- T_RHlower: v.T_RHlower,
- T_RHupper: v.T_RHupper,
- }
- }
- return maps
- }
|