1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033 |
- package Device
- import (
- "ColdP_server/conf"
- "ColdP_server/controllers/lib"
- "ColdP_server/logs"
- "ColdP_server/models/Account"
- "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"
- "log"
- "strconv"
- "strings"
- "time"
- )
- // 模板
- type DeviceSensor 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_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
- T_Class string `orm:"size(256);null"` // Device.DeviceClass.Id 设备分类 C1|C2|
- T_Notice string `orm:"size(256);null"` // 通知绑定
- T_datashow int `orm:"index;size(2);default(1)"` // 0 屏蔽数据展示 1 正常数据展示 (屏蔽后 数据展示无法看到,设备管理中 不受影响)
- T_sort int `orm:"index;size(200);default(1)"` // 排序
- T_3dview string `orm:"size(256);null"` // 3D 视图ID
- T_type int `orm:"index;size(4);null"` // Device.DeviceSensorType 1库房 2移动
- // 设备同步参数
- T_Dattery int `orm:"size(4);null"` // 电量
- T_Site string `orm:"size(200);null"` // GPS
- 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_State int `orm:"index;size(2);default(1)"` // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
- 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 DeviceSensor_Del struct {
- T_sn string
- T_id int
- }
- func (t *DeviceSensor) TableName() string {
- return "device_sensor" // 数据库名称 // ************** 替换 FormulaList **************
- }
- // 模板
- type DeviceSensor_R struct {
- T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_id int // 传感器编号
- T_name string // 标题
- T_3dview string // 3D 视图ID
- T_sort int // 排序
- T_Dattery int // 电量
- T_Site string // GPS
- T_monitor int // 记录状态
- T_online int // 在线状态 1 在线 0 离线
- T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
- T_datashow int // 0 屏蔽数据展示 1 正常数据展示
- T_type int // 1库房 2移动
- T_DeviceSensorData DeviceData_R // 传感器最新数据
- T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
- }
- // DeviceSensorData 处理数据生成专用
- type DeviceSensorData struct {
- T_id int `json:"t_id"`
- T_sp string `json:"t_sp"`
- T_time string `json:"t_time"`
- T_t float64 `json:"t_t"`
- T_rh float64 `json:"t_rh"`
- T_site string `json:"t_site"`
- CreateTime string `json:"create_time"`
- }
- // 传感器管理
- type DeviceSensor_P struct {
- T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_id int // 传感器编号
- T_name string // 标题
- T_sort int // 排序
- T_datashow int // 0 屏蔽数据展示 1 正常数据展示
- T_State int // 0 屏蔽 1 正常
- // DeviceSensorParameter
- T_Tlower *float32 // 温度下限
- T_Tupper *float32 // 温度上限
- T_RHlower *float32 // 湿度下限
- T_RHupper *float32 // 湿度上限
- T_enprel *int // 是否启用预警 1开启 0 关闭
- T_tprel *float32 // 温度预警下限
- T_tpreu *float32 // 温度预警上限
- T_hprel *float32 // 湿度预警下限
- T_hpreu *float32 // 温度预警上限
- T_en *int // en:是否启用传感器,
- T_free *int // free:监测点是否为闲置状态(空库,只监测不报警)
- }
- type DeviceSensor_ struct {
- T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_id int // 传感器编号
- T_name string // 标题
- }
- type DataOld struct {
- ID int `json:"ID"`
- TSn string `json:"t_sn"`
- TId int `json:"t_id"`
- TTime string `json:"t_time"`
- TT float64 `json:"t_t"`
- TRh float64 `json:"t_rh"`
- TSite string `json:"t__site"`
- TOperation int `json:"t_operation"`
- TUuid string `json:"t_uuid"`
- CreateTime string `json:"create_time"`
- }
- var redisCache_DeviceSensor cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(DeviceSensor))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_DeviceSensor", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_DeviceSensor, err = cache.NewCache("redis", config)
- if err != nil || redisCache_DeviceSensor == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(any(errMsg))
- }
- }
- // ---------------- Redis -------------------
- // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_DeviceSensor_Set(r DeviceSensor) (err error) {
- key := r.T_sn + "|" + strconv.Itoa(r.T_id)
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_DeviceSensor.Put(key, str, 24*time.Hour)
- if err != nil {
- logs.Error("set key:", key, ",value:", str, err)
- }
- return
- }
- func Redis_DeviceSensor_Get(key string) (r DeviceSensor, is bool) {
- if redisCache_DeviceSensor.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_DeviceSensor.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return DeviceSensor{}, false
- }
- return r, true
- }
- return DeviceSensor{}, false
- }
- func Redis_DeviceSensor_DelK(r DeviceSensor) (err error) {
- key := r.T_sn + "|" + strconv.Itoa(r.T_id)
- err = redisCache_DeviceSensor.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- /*
- func DeviceSensorToDeviceSensor_R(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_R) {
- lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
- DeviceSensor_r.T_sn = DeviceSensor_.T_sn
- DeviceSensor_r.T_id = DeviceSensor_.T_id
- DeviceSensor_r.T_name = DeviceSensor_.T_name
- DeviceSensor_r.T_Site = DeviceSensor_.T_Site
- DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
- DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
- DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
- DeviceSensor_r.T_type = DeviceSensor_.T_type
- DeviceSensor_r.T_sort = DeviceSensor_.T_sort
- DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
- DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
- DeviceSensor_r.T_online = DeviceSensor_.T_online
- DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s
- DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
- if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 {
- DeviceSensor_r.T_monitor = 2
- }
- // 最新系统参数
- DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id)
- // 最新数据
- //key_data := DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id)
- //DeviceSensor_r.T_DeviceSensorData, _ = RedisDeviceData_Get(key_data)
- DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
- device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
- DeviceSensor_r.T_DeviceSensorData = DeviceData_ToDeviceData_R(device, DeviceData)
- return
- }
- */
- func DeviceSensorToDeviceSensor_(r DeviceSensor) (t DeviceSensor_) {
- t.T_sn = r.T_sn
- t.T_id = r.T_id
- t.T_name = r.T_name
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取列表 - 总数
- func Read_DeviceSensor_Num_ByT_sn(T_sn string) int {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- cnt, err := qs.Filter("T_sn", T_sn).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0
- }
- return int(cnt)
- }
- /*// 获取列表
- func Read_DeviceSensorList(admin_r *Account.Admin, T_pid int, T_sn string, T_name string, T_Class_id, T_datashow, T_type, T_State int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid)
- if T_Class_id > 0 {
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- fmt.Println("T_Class:", T_Class)
- cond1 = cond1.And("T_Class__icontains", T_Class)
- }
- if T_type > 0 {
- cond1 = cond1.And("T_type", T_type)
- }
- if len(T_sn) > 0 {
- cond1 = cond1.And("T_sn__icontains", T_sn)
- }
- if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name))
- }
- // 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常
- if admin_r.T_pid > 0 || T_State == 1 {
- cond1 = cond1.And("T_State", 1)
- }
- if T_datashow == 1 { // 0 屏蔽数据展示 1 正常数据展示
- cond1 = cond1.And("T_datashow", 1)
- }
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- }
- return DeviceSensor_r, cnt
- }
- */
- // 获取
- func Read_DeviceSensor_ByT_sn(T_sn string, T_id int) (r DeviceSensor, is bool) {
- o := orm.NewOrm()
- r = DeviceSensor{T_sn: T_sn, T_id: T_id}
- err := o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 删除
- func Delete_DeviceSensor_ById(T_sn string, T_id int) (err error) {
- fmt.Println("Delete_DeviceSensor : T_sn", T_sn, "T_id", T_id)
- o := orm.NewOrm()
- r := DeviceSensor{T_sn: T_sn, T_id: T_id}
- err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println("Delete_DeviceSensor_ById", err)
- return err
- }
- _, err = o.Delete(&DeviceSensor{Id: r.Id})
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_DeviceSensor_DelK(r)
- return
- }
- // Get_DeviceSensor_Tid_ByT_sn 获取列表探头列表
- func Get_DeviceSensor_Tid_ByT_sn(T_sn string) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- _, err := qs.Filter("t_sn", T_sn).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return r
- }
- // 修改
- func Update_DeviceSensor(r DeviceSensor, cols ...string) bool {
- o := orm.NewOrm()
- num, err := o.Update(&r, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- fmt.Println("Number of records updated in database:", num)
- Redis_DeviceSensor_Set(r)
- return true
- }
- // 修改T_Class,替换为""
- func DeviceSensor_Bind_T_Class_Del(T_sn string, T_id int, T_Class_id int) (err error) {
- o := orm.NewOrm()
- v := DeviceSensor{T_sn: T_sn, T_id: T_id}
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- err = o.Read(&v, "T_sn", "T_id")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
- _, err = o.Update(&v, "T_Class")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_DeviceSensor_Set(v)
- return nil
- }
- // 修改T_Class,追加
- func DeviceSensor_Bind_T_Class_Add(T_sn string, T_id int, T_Class_id int) (err error) {
- o := orm.NewOrm()
- v := DeviceSensor{T_sn: T_sn, T_id: T_id}
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- err = o.Read(&v, "T_sn", "T_id")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
- v.T_Class = v.T_Class + T_Class
- _, err = o.Update(&v, "T_Class")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_DeviceSensor_Set(v)
- return nil
- }
- // 获取 ById
- func Read_DeviceSensor_ByTsn_Tid(T_sn string, T_id int) (r DeviceSensor, err error) {
- o := orm.NewOrm()
- r = DeviceSensor{T_sn: T_sn, T_id: T_id}
- err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return r, err
- }
- // ReadDeviceSensorByTsnTidTimeRange 获取通过id和时间设备探头数据
- func ReadDeviceSensorByTsnTidTimeRange(T_sn string, T_id int, startTime, endTime string) (datas []DeviceSensorData, err error) {
- o := orm.NewOrm()
- sqlStatement := fmt.Sprintf("select t_id,t_sp,DATE_FORMAT(t_time,'%%Y-%%m-%%d %%H:%%i:%%s') as t_time,t_t,t_rh,DATE_FORMAT(create_time,'%%Y-%%m-%%d %%H:%%i:%%s') as create_time from z_device_data_%s where t_id = %d and t_time between '%s' and '%s'", T_sn, T_id, startTime, endTime)
- _, err = o.Raw(sqlStatement).QueryRows(&datas)
- return
- }
- // 获取列表
- func Read_DeviceSensor_ByTsn(T_sn string) ([]DeviceSensor, int) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var r []DeviceSensor
- _, err := qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, 0
- }
- cnt, err := qs.Filter("T_sn", T_sn).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, 0
- }
- return r, int(cnt)
- }
- // 获取列表
- func Read_DeviceSensor_List_T_ClassOr(T_pid int, T_Class_id int, T_sn string, T_name string, T_type int, page int, page_z int) (DeviceSensor_r []DeviceSensor_, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- T_Class := ""
- if T_Class_id != 0 {
- T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
- }
- fmt.Println("T_Class:", T_Class)
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Class__icontains", T_Class).And("T_sn__icontains", T_sn).Or("T_name__icontains", T_name).And("T_pid", T_pid).And("T_State", 1).And("T_Class__icontains", T_Class) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- if T_type > 0 {
- cond1 = cond1.AndCond(cond.And("T_type", T_type))
- }
- fmt.Printf("分页数据:%d ,%d\n\n\n\n", offset, page_z)
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
- }
- return DeviceSensor_r, cnt
- }
- // 获取列表
- func Read_DeviceSensor_List_T_Class(T_pid int, T_Class_id int, T_sn string, T_name string, T_type int, page int, page_z int) (DeviceSensor_r []DeviceSensor_, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- T_Class := ""
- if T_Class_id != 0 {
- T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
- }
- fmt.Println("T_Class:", T_Class)
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Class__icontains", T_Class).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- if T_type > 0 {
- cond1 = cond1.AndCond(cond.And("T_type", T_type))
- }
- fmt.Printf("分页数据:%d ,%d\n\n\n\n", offset, page_z)
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
- }
- return DeviceSensor_r, cnt
- }
- // 获取列表
- func Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn string, T_id int, T_Class_id int) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- _, err := qs.Filter("T_Class__icontains", T_Class).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return r
- }
- // 获取列表
- func Read_DeviceSensor_ALL_List_T_sn(T_sn string) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- _, err := qs.Filter("T_sn", T_sn).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return r
- }
- // 获取列表
- func Read_DeviceSensor_ALL_Class_Id(T_Class_id int) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- _, err := qs.Filter("T_Class__icontains", T_Class).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return r
- }
- func DELETE_DeviceSensor(SN string) bool {
- sql := "DELETE FROM `cold`.`device_sensor` WHERE `t_sn` = '" + SN + "' "
- o := orm.NewOrm()
- _, err := o.Raw(sql).Exec()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- return true
- }
- // 报警策略 -------------------
- // 通过T_sn T_id T_Notice_id 获取全部列表
- func Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn string, T_id int, T_Notice_id int) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
- _, err := qs.Filter("T_Notice__icontains", T_Notice).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return r
- }
- // 修改T_Notice,替换为""
- func DeviceSensor_Bind_T_Notice_Del(T_sn string, T_id int, T_Notice_id int) (err error) {
- o := orm.NewOrm()
- v := DeviceSensor{T_sn: T_sn, T_id: T_id}
- T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
- err = o.Read(&v, "T_sn", "T_id")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
- _, err = o.Update(&v, "T_Notice")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- Redis_DeviceSensor_Set(v)
- return nil
- }
- // 修改T_Notice,追加
- func DeviceSensor_Bind_T_Notice_Add(T_sn string, T_id int, T_Notice_id int) (err error) {
- o := orm.NewOrm()
- v := DeviceSensor{T_sn: T_sn, T_id: T_id}
- T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
- err = o.Read(&v, "T_sn", "T_id")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
- v.T_Notice = v.T_Notice + T_Notice
- _, err = o.Update(&v, "T_Notice")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- Redis_DeviceSensor_Set(v)
- return nil
- }
- // 获取列表
- func Read_DeviceSensor_ALL_Notice_Id(T_Notice_id int) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
- _, err := qs.Filter("T_Notice__icontains", T_Notice).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return r
- }
- // 获取列表
- func Read_DeviceSensor_List_T_Notice(T_pid int, T_Notice_id int, page int, page_z int, T_sn string, T_name string, T_type int) (DeviceSensor_r []DeviceSensor_, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- T_Notice := ""
- if T_Notice_id != 0 {
- T_Notice = "N" + strconv.Itoa(T_Notice_id) + "|"
- }
- fmt.Println("T_Notice:", T_Notice)
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Notice__icontains", T_Notice).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- if T_type > 0 {
- cond1 = cond1.AndCond(cond.And("T_type", T_type))
- }
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
- }
- return DeviceSensor_r, cnt
- }
- // 设备同步参数
- func Update_Device_To_DeviceSensor(r_Device Device) bool {
- DeviceSensor_list, _ := Read_DeviceSensor_ByTsn(r_Device.T_sn)
- for _, v := range DeviceSensor_list {
- // 设备同步参数
- v.T_Dattery = r_Device.T_Dattery // 电量
- v.T_Site = r_Device.T_Site // GPS
- v.T_monitor = r_Device.T_monitor // 监控状态 0 未监控 1 监控
- v.T_online = r_Device.T_online // 在线状态 0 未启用 1 在线 2 离线
- v.T_online_s = r_Device.T_online_s // 在线状态-备用 0 未启用 1 在线 2 离线
- v.T_State = r_Device.T_State // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
- Update_DeviceSensor(v, "T_Dattery", "T_Site", "T_type", "T_State", "T_monitor", "T_online", "T_online_s")
- }
- return true
- }
- // 传感器管理列列表 -------------------
- func Read_DeviceSensorManageList(admin_r *Account.Admin, T_pid int, T_name string, T_calss_id, T_en, T_free, T_datashow, T_sort, page, page_z int) (DeviceSensor_p []DeviceSensor_P, cnt int64) {
- o := orm.NewOrm()
- var maps []DeviceSensor_P
- var maps_z []orm2.ParamsList
- var offset int
- if page <= 1 {
- offset = 0
- } else {
- offset = (page - 1) * page_z
- }
- sql_WHERE := "t_pid = " + strconv.Itoa(T_pid)
- if admin_r.T_pid > 0 {
- sql_WHERE += " AND t__state = 1"
- }
- if len(T_name) > 0 {
- sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR t_name like '%" + T_name + "%')"
- }
- if T_calss_id > 0 {
- T_calss := "C" + strconv.Itoa(T_calss_id) + "|"
- sql_WHERE += " AND t__class like '%" + T_calss + "%'"
- }
- if T_en != -1 {
- sql_WHERE += " AND t_en = " + strconv.Itoa(T_en)
- }
- if T_free != -1 {
- sql_WHERE += " AND t_free = " + strconv.Itoa(T_free)
- }
- if T_datashow != -1 {
- sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow)
- }
- sql_ORDER := " ORDER BY t_sort ASC"
- if T_sort == 1 {
- sql_ORDER = " ORDER BY t_sort DESC"
- }
- // -------------
- sql := "SELECT COUNT(ds.ID) FROM " + "device_sensor ds " +
- "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_en,t_free " +
- "FROM device_sensor_parameter WHERE id IN (SELECT MAX(id) FROM device_sensor_parameter GROUP BY t_sn,t_id)) AS dsp " +
- "ON ds.t_sn = dsp.tsn AND ds.t_id = dsp.tid" + " WHERE " + sql_WHERE
- fmt.Println(sql)
- _, err := o.Raw(sql).ValuesList(&maps_z)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return DeviceSensor_p, 0
- }
- if len(maps_z) == 0 {
- return DeviceSensor_p, 0
- }
- //fmt.Println("maps_z;",maps_z[0][0])
- sql = "SELECT * FROM device_sensor ds " +
- "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu " +
- "FROM device_sensor_parameter WHERE id IN (SELECT MAX(id) FROM device_sensor_parameter GROUP BY t_sn,t_id)) AS dsp " +
- "ON ds.t_sn = dsp.tsn AND ds.t_id = dsp.tid " +
- "WHERE " + sql_WHERE + sql_ORDER
- if page_z != 9999 {
- sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
- }
- fmt.Println(sql)
- _, err = o.Raw(sql).QueryRows(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
- return maps, key
- }
- // 数据展示菜单下 传感器参数列表
- /*
- func Read_DeviceSensor_List_For_Data(T_pid int, T_name string, T_Class_id, T_type, T_RealTime int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
- if T_Class_id > 0 {
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- fmt.Println("T_Class:", T_Class)
- cond1 = cond1.And("T_Class__icontains", T_Class)
- }
- if T_type > 0 {
- cond1 = cond1.And("T_type", T_type)
- }
- if len(T_name) == 16 {
- cond1 = cond1.And("T_sn", T_name)
- } else {
- cond1 = cond1.And("T_name__icontains", T_name).And("T_datashow", 1)
- }
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- // 实时数据页面,温度或者湿度不在正常区间,排在最前面
- if T_RealTime == 1 {
- var DeviceSensor_unusual []DeviceSensor_R
- for _, v := range r {
- v_r := DeviceSensorToDeviceSensor_R(v)
- data := v_r.T_DeviceSensorData
- // 温度或者湿度不在正常区间,排在最前面
- if data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl {
- DeviceSensor_unusual = append(DeviceSensor_unusual, v_r)
- } else {
- DeviceSensor_r = append(DeviceSensor_r, v_r)
- }
- }
- DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...)
- return DeviceSensor_unusual, cnt
- }
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- }
- return DeviceSensor_r, cnt
- }
- */
- func ToSensorData(tid, tSp, tTime, tSite string, tt, trh float64, createTime string) DeviceSensorData {
- num, _ := strconv.ParseInt(tid, 10, 64)
- return DeviceSensorData{
- int(num),
- tSp,
- tTime,
- tt,
- trh,
- tSite,
- createTime,
- }
- }
- // UpdateDeviceSensorDataTemperatureAndHumidity 更新设备探头数据温湿度
- func UpdateDeviceSensorDataTemperatureAndHumidity(sn, id, startTime, endTime string, temperature, humidity float64) {
- sqlStatement := fmt.Sprintf("update z_device_data_%s set t_t = t_t + %f , t_rh = t_rh + %f where t_id = '%s' and t_time BETWEEN '%s' and '%s'", sn, temperature, humidity, id, startTime, endTime)
- o := orm.NewOrm()
- exec, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- fmt.Println(err.Error())
- }
- affected, err := exec.RowsAffected()
- if err != nil {
- fmt.Println(err.Error())
- }
- fmt.Printf("影响了%d行\n", affected)
- }
- // SelectDeviceSensorDataListByTimeRange 搜索设备列表通过时间
- func SelectDeviceSensorDataListByTimeRange(sn, tId, startTime, endTime string) (list []DeviceSensorData) {
- //sqlStatement := fmt.Sprintf("select t_id,t_sp,DATE_FORMAT(t_time,'%%Y-%%m-%%d %%k:%%i:%%s') as t_time,t_t,t_rh,t_site,DATE_FORMAT(create_time,'%%Y-%%m-%%d %%k:%%i:%%s') as create_time from z_device_data_%s where t_id = %s and t_time BETWEEN '%s' AND '%s' ORDER BY t_time ASC", sn, tId, startTime, endTime)
- sqlStatement := fmt.Sprintf("select t_id,t_sp,t_time,t_t,t_rh,t_site,create_time from z_device_data_%s where t_id = %s and t_time BETWEEN '%s' AND '%s' ORDER BY t_time ASC", sn, tId, startTime, endTime)
- o := orm.NewOrm()
- rows, err := o.Raw(sqlStatement).QueryRows(&list)
- if err != nil {
- fmt.Println(err.Error())
- }
- fmt.Println("影响行数:", rows)
- return
- }
- // UpdateDeviceSensorDataTemperatureAndHumidityRandom 随机更新
- func UpdateDeviceSensorDataTemperatureAndHumidityRandom(sn, id, startTime, endTime string, ttMax, ttMin, trhMax, trhMin int) {
- //sql语句
- sqlStatement := fmt.Sprintf("update z_device_data_%s set t_t = t_t + Round(FLOOR(%d + RAND() * (%d - %d + 1)) /100.0,1), t_rh = t_rh + Round(FLOOR(%d + RAND() * (%d - %d + 1)) /100.0,1) where t_id = '%s' AND t_time BETWEEN '%s' AND '%s'", sn, ttMin, ttMax, ttMin, trhMin, trhMax, trhMin, id, startTime, endTime)
- o := orm.NewOrm()
- exec, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- fmt.Println("执行错误:", sqlStatement)
- }
- affected, _ := exec.RowsAffected()
- fmt.Println("执行更新行数:", affected)
- }
- // UpdateDeviceSensorData 更新设备探头数据
- func UpdateDeviceSensorData(sn, tId string, old, newO DeviceSensorData) {
- sqlStatement := fmt.Sprintf("update z_device_data_%s set t_t = %f , t_rh = %f where t_id = %s and t_t = %f and t_rh = %f and t_time= '%s'", sn, newO.T_t, newO.T_rh, tId, old.T_t, old.T_rh, old.T_time)
- fmt.Println("执行SQL:", sqlStatement)
- o := orm.NewOrm()
- exec, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- fmt.Println("执行错误:", sqlStatement, err.Error())
- }
- affected, _ := exec.RowsAffected()
- fmt.Println("执行更新行数:", affected)
- }
- // DeleteDeviceSensorDataByTimeRange 删除时间范围内的数据
- func DeleteDeviceSensorDataByTimeRange(sn, id, start, end string) {
- //把原本的根据创建时间删除更改为更具上传时间删除
- sqlStatement := fmt.Sprintf("delete from z_device_data_%s where t_time between '%s' and '%s' and t_id = %s", sn, start, end, id)
- //sqlStatement := fmt.Sprintf("delete from z_device_data_%s where create_time between '%s' and '%s' and t_id = %s", sn, start, end, id)
- o := orm.NewOrm()
- log.Printf("执行SQL:", sqlStatement)
- exec, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- fmt.Println(err.Error())
- }
- affected, _ := exec.RowsAffected()
- fmt.Printf("从%s~%s时间段删除了%d条数据", start, end, affected)
- }
- // InsertDeviceSensorData 添加设备数据
- func InsertDeviceSensorData(sn string, d DeviceSensorData, admin Account.Admin) {
- sqlStatement := fmt.Sprintf("insert into z_device_data_%s(t_id,t_sp,t_time,t_t,t_rh,t_site,create_time) value(%d,'%s','%s',%f,%f,'%s','%s')", sn, d.T_id, d.T_sp, d.T_time, d.T_t, d.T_rh, d.T_site, d.CreateTime)
- o := orm.NewOrm()
- _, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- fmt.Println(err.Error())
- if strings.Contains(err.Error(), "Duplicate entry") {
- //已经存在该条目,更新
- sqlStatement = fmt.Sprintf("update z_device_data_%s set t_t = %f,t_rh = %f where t_time= '%s' and t_id = %d and t_sp = '%s'", sn, d.T_t, d.T_rh, d.T_time, d.T_id, d.T_sp)
- exec, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- fmt.Println("更新错误:", err.Error())
- } else {
- affected, _ := exec.RowsAffected()
- fmt.Println("修改了", affected, "条")
- }
- }
- } else {
- //向DataOld插入数据
- sqlStatement = fmt.Sprintf("insert into device_data_old(t_sn,t_id,t_time,t_t,t_rh,t__site,t_operation,t_uuid,create_time) value('%s',%d,'%s',%f,%f,'%s',%d,'%s','%s')", sn, d.T_id, d.T_time, d.T_t, d.T_rh, d.T_site, 2, admin.T_uuid, d.CreateTime)
- _, err := o.Raw(sqlStatement).Exec()
- if err != nil {
- log.Panicln("插入失败:", err.Error())
- } else {
- fmt.Println("插入成功!")
- }
- }
- }
- // ReadDeviceOldBySnTid 读取设备探头记录数据
- func ReadDeviceOldBySnTid(sn, tId string) (list []DataOld) {
- o := orm.NewOrm()
- sqlStatement := fmt.Sprintf("SELECT d.*,a.t_name as t_uuid FROM device_data_old as d LEFT JOIN admin as a on a.t_uuid = d.t_uuid WHERE t_sn = '%s' AND t_id = '%s'", sn, tId)
- o.Raw(sqlStatement).QueryRows(&list)
- return list
- }
|