12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- package Device
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "Cold_Api/logs"
- "Cold_Api/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"
- "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 // 类型
- T_DeviceSensorData DeviceData_R // 传感器最新数据
- T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
- T_DeviceSensorType DeviceSensorType_R // 传感器类型
- }
- type DeviceSensor_Applet 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 // 设备参数
- T_DeviceSensorType DeviceSensorType_R // 传感器类型
- T_Device Device_R // 传感器类型
- }
- // 传感器管理
- 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 // 标题
- }
- 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(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) {
- device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
- //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
- lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据
- 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)
- //DeviceData, _ := RedisDeviceData_Get(DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id))
- DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
- deviceSensorType := Read_DeviceSensorType_Get(DeviceSensor_.T_type)
- DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(deviceSensorType)
- return
- }
- func DeviceSensorToDeviceSensor_Applet(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_Applet) {
- device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
- //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
- lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据
- 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)
- // 最新数据
- DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
- DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
- deviceSensorType := Read_DeviceSensorType_Get(DeviceSensor_.T_type)
- DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(deviceSensorType)
- DeviceSensor_r.T_Device = DeviceToDevice_R(device)
- 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 int, T_type string, 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 len(T_type) > 0 {
- list := strings.Split(T_type, ",")
- cond1 = cond1.And("T_type__in", list)
- }
- 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)
- }
- var err error
- if page_z == 9999 {
- _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- } else {
- _, 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) {
- key := r.T_sn + "|" + strconv.Itoa(r.T_id)
- if r, is := Redis_DeviceSensor_Get(key); is {
- return r, true
- }
- 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
- }
- Redis_DeviceSensor_Set(r)
- 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
- }
- // 修改
- 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
- }
- // 获取列表
- 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_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))
- }
- _, 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_List_T_sn_T_datashow(T_sn string) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- _, err := qs.Filter("T_sn", T_sn).Filter("T_State", 1).Filter("T_datashow", 1).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 WHERE t__state=1 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 WHERE t__state=1 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, T_MapShow int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int
- if page <= 1 {
- offset = 0
- } else {
- offset = (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 if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_datashow", 1)
- } else {
- cond1 = cond1.And("T_datashow", 1)
- }
- if T_MapShow == 1 {
- cond1 = cond1.AndNot("T_Site", "")
- }
- cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- // 实时数据页面,温度或者湿度不在正常区间,排在最前面
- if T_RealTime == 1 {
- offset_z := offset + page_z
- if cnt < int64(offset_z) {
- offset_z = int(cnt)
- }
- if offset > offset_z {
- offset = offset_z
- }
- _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- 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[offset:offset_z], cnt
- }
- _, 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
- }
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- }
- return DeviceSensor_r, cnt
- }
- // 获取列表
- func Read_DeviceSensor_T_type(T_pid int) (lists orm2.ParamsList, err error) {
- o := orm.NewOrm()
- var pl_lists orm2.ParamsList
- _, err = o.Raw("SELECT DISTINCT t_type FROM device_sensor WHERE t_pid=? AND t_datashow=1 LIMIT 0,1000", T_pid).ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return pl_lists, nil
- }
- // 数据展示菜单下 传感器参数列表
- func Read_DeviceSensor_List_For_Applet(T_pid int, T_name, T_online string, T_RealTime, T_type, page, page_z int) (DeviceSensor_r []DeviceSensor_Applet, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var offset int
- if page <= 1 {
- offset = 0
- } else {
- offset = (page - 1) * page_z
- }
- qs := o.QueryTable(new(DeviceSensor))
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
- if T_type > 0 {
- cond1 = cond1.And("T_type", T_type)
- }
- if len(T_name) == 16 {
- cond1 = cond1.And("T_sn", T_name)
- } else if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_datashow", 1)
- } else {
- cond1 = cond1.And("T_datashow", 1)
- }
- if T_online == "1" {
- cond1 = cond1.AndCond(cond.Or("T_online", 1).Or("T_online_s", 1))
- } else if T_online == "2" {
- cond1 = cond1.AndCond(cond.AndCond(cond.And("T_online", 2).And("T_online_s", 0)).
- OrCond(cond.And("T_online", 0).And("T_online_s", 2)))
- } else if T_online == "0" {
- cond1 = cond1.And("T_online", 0).And("T_online_s", 0)
- }
- var err error
- if page_z == 9999 {
- _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- } else {
- _, 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_Applet(v))
- }
- return DeviceSensor_r, cnt
- }
- // 小程序-传感器类型
- func Read_DeviceSensor_T_sn_ByT_type(T_pid, T_type int) (lists orm2.ParamsList, err error) {
- o := orm.NewOrm()
- sql := "t_pid=? AND t__state=1 AND t_datashow=1 AND t_type = " + strconv.Itoa(T_type)
- var pl_lists orm2.ParamsList
- _, err = o.Raw("SELECT DISTINCT T_sn FROM device_sensor WHERE "+sql+" LIMIT 0,1000", T_pid).ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return pl_lists, nil
- }
- func Read_DeviceSensorCount_ByT_pids(T_pids []int) (cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid__in", T_pids).And("T_State", 1)
- //// 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常
- //cond1 = cond1.And("T_State", 1)
- //// 0 屏蔽数据展示 1 正常数据展示
- //cond1 = cond1.And("T_datashow", 1)
- cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return cnt
- }
|