package Device import ( "Cold_Api/conf" "Cold_Api/models/Account" "Cold_Api/models/Product" "encoding/json" "fmt" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/adapter/orm" _ "github.com/go-sql-driver/mysql" "strconv" "time" ) // 模板 type Device struct { T_sn string `orm:"pk;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司 T_MSISDN string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_devName string `orm:"size(256);null"` // 设备名称 T_protocol int `orm:"size(2);default(1)"` // 冷链通讯协议 1 :1.0协议 2 :2.0协议 3 :3.0协议 T_VerifyTime time.Time `orm:"type(timestamp);null"` // 验证时间 T_CalibrationTime time.Time `orm:"type(timestamp);null"` // 校准时间 T_ist int `orm:"size(2);default(1)"` // 温度 1开启 2关闭 T_ish int `orm:"size(2);default(1)"` // 湿度 1开启 2关闭 // 设备同步参数 T_Dattery int `orm:"size(4);null"` // 电量 T_Site string `orm:"size(200);null"` // GPS T_type int `orm:"index;size(4);null"` // Device.DeviceType 1库房 2移动 T_give int `orm:"index;size(2);default(1)"` // 屏蔽状态 0 屏蔽 1 正常 T_monitor int `orm:"index;size(2);null"` // 监控状态 0 未监控 1 监控 T_online int `orm:"index;size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线 T_online_s int `orm:"index;size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线 // 硬件信息 T_model string `orm:"size(200);null"` // KF200BG 设备型号 T_sver string `orm:"size(200);null"` // "1.0.0",//软件版本 T_hver string `orm:"size(200);null"` // "1.0.0",//硬件版本 T_imei string `orm:"size(200);null"` // "867387060327718",//模组imei T_iccid string `orm:"size(200);null"` // "89860477102170049750",//sim卡号 T_State int `orm:"index;size(2);default(1)"` // 0 删除 1 正常 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 Device_R struct { T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_MSISDN string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_devName string // 设备名称 T_protocol int // 1 1.0协议 2 2.0协议 T_VerifyTime string // 验证时间 T_CalibrationTime string // 校准时间 T_ist int // 温度 1开启 2关闭 T_ish int // 湿度 1开启 2关闭 T_Dattery int // 电量 T_Site string // GPS T_type int // 1库房 2移动 T_give int // 屏蔽状态 0 屏蔽 1 正常 T_monitor int // 监控状态 0 未监控 1 监控 T_online int // 在线状态 0 未启用 1 在线 2 离线 T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线 // 硬件信息 T_model string // KF200BG 产品型号 T_ProductTypeName string // 验证工具LoRa 产品统称 + 类型 T_sver string // "1.0.0",//软件版本 T_hver string // "1.0.0",//硬件版本 T_imei string // "867387060327718",//模组imei T_iccid string // "89860477102170049750",//sim卡号 CreateTime string //auto_now_add 第一次保存时才设置时间 UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新 } type Device_task struct { T_sn string T_task string } func (t *Device) TableName() string { return "device" // 数据库名称 // ************** 替换 FormulaList ************** } var redisCache_Device cache.Cache func init() { //注册模型 orm.RegisterModel(new(Device)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_Device, err = cache.NewCache("redis", config) if err != nil || redisCache_Device == nil { errMsg := "failed to init redis" fmt.Println(errMsg, err) } } func DeviceToDevice_R(r Device) (t Device_R) { t.T_sn = r.T_sn t.T_MSISDN = r.T_MSISDN t.T_devName = r.T_devName t.T_protocol = r.T_protocol t.T_VerifyTime = r.T_VerifyTime.Format("2006-01-02 15:04:05") t.T_CalibrationTime = r.T_CalibrationTime.Format("2006-01-02 15:04:05") t.T_ist = r.T_ist t.T_ish = r.T_ish t.T_Dattery = r.T_Dattery t.T_Site = r.T_Site t.T_type = r.T_type t.T_give = r.T_give t.T_monitor = r.T_monitor t.T_online = r.T_online t.T_online_s = r.T_online_s t.T_model = r.T_model t.T_ProductTypeName = Product.Read_ProductType_Get(r.T_model) t.T_sver = r.T_sver t.T_hver = r.T_hver t.T_imei = r.T_imei t.T_iccid = r.T_iccid t.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05") t.UpdateTime = r.UpdateTime.Format("2006-01-02 15:04:05") return } // ---------------- Redis ------------------- // Redis_Set(m.T_sn,m) // Redis 更新缓存 func Redis_Set(r Device) (err error) { //json序列化 str, err := json.Marshal(r) if err != nil { fmt.Print(err) return } err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour) if err != nil { fmt.Println("set key:", r.T_sn, ",value:", str, err) } return } // if r,is :=Redis_Get(T_sn);is{ // return r,nil // } func Redis_Get(key string) (r Device, is bool) { if redisCache_Device.IsExist(key) { //println("找到key:",key) v := redisCache_Device.Get(key) json.Unmarshal(v.([]byte), &r) return r, true } //println("没有 找到key:",key) return Device{}, false } func Redis_DelK(key string) (err error) { err = redisCache_Device.Delete(key) return } // ---------------- 特殊方法 ------------------- // 获取 ById func Read_Device_ByT_sn(T_sn string) (r Device, err error) { if r, is := Redis_Get(T_sn); is { //println("Redis_Get OK") return r, nil } //println("没有 Redis_Get SN") o := orm.NewOrm() r = Device{T_sn: T_sn, T_State: 1} err = o.Read(&r, "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { fmt.Println(err) } else { Redis_Set(r) // Redis 更新缓存 } return r, err } // 添加 func Add_Device(m Device) (err error) { o := orm.NewOrm() _, err = o.Insert(&m) if err != nil { fmt.Println(err) } Redis_Set(m) // Redis 更新缓存 return err } // 删除 func Delete_Device(m Device) (err error) { o := orm.NewOrm() if num, err := o.Delete(&m); err == nil { fmt.Println("Number of records deleted in database:", num) } Redis_DelK(m.T_sn) return } // 修改 func Update_Device(r Device, cols ...string) bool { o := orm.NewOrm() if num, err := o.Update(&r, cols...); err == nil { fmt.Println("Number of records updated in database:", num) Redis_Set(r) // Redis 更新缓存 return true } return false } // 获取列表 func Read_Device_List(admin Account.Admin, T_pid int, T_name string, T_monitor string, T_online string, T_type int, page int, page_z int) (r []Device_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Device)) var offset int64 if page_z == 0 { page_z = conf.Page_size } if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("T_State", 1).And("T_pid", T_pid) if len(T_name) > 0 { cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name).Or("T_MSISDN__icontains", T_name)) } if len(T_monitor) > 0 { T_monitor_int, err := strconv.Atoi(T_monitor) if err == nil { cond1 = cond1.AndCond(cond.And("T_monitor", T_monitor_int)) } } if T_type > 0 { cond1 = cond1.AndCond(cond.And("T_type", T_type)) } //0 未启用 1 在线 2 离线 if T_online == "1" { cond1 = cond1.AndCond(cond.And("T_online", 1)) } else if T_online == "2" { cond1 = cond1.AndCond(cond.And("T_online", 2)) } else if T_online == "0" { cond1 = cond1.AndCond(cond.And("T_online", 0).Or("T_online", 2)) } // 非内部权限 - 丢弃 if admin.T_pid > 0 { cond1 = cond1.AndCond(cond.And("T_give", 1)) } var rx []Device qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").OrderBy("-T_give").All(&rx) cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count() for _, v := range rx { r = append(r, DeviceToDevice_R(v)) } return r, cnt } func Read_Device_ALL_T_Type_Count(T_pid int, T_type int) (cnt int64) { o := orm.NewOrm() qs := o.QueryTable(new(Device)) cnt, _ = qs.Filter("T_pid", T_pid).Filter("T_type", T_type).Filter("T_State", 1).Count() return cnt } func Read_Device_List_T_model(T_model string) (r []Device) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Device)) qs.Filter("T_State", 1).Filter("T_model", T_model).All(&r) return r }