DeviceSensor.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/logs"
  6. "Cold_Api/models/Account"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego/cache"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. "github.com/beego/beego/v2/adapter/orm"
  12. orm2 "github.com/beego/beego/v2/client/orm"
  13. _ "github.com/go-sql-driver/mysql"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 模板
  19. type DeviceSensor struct {
  20. Id int `orm:"column(ID);size(11);auto;pk"`
  21. T_sn string `orm:"index;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  22. T_id int `orm:"index;size(11);null"` // 传感器编号
  23. T_name string `orm:"size(256);null"` // 标题
  24. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  25. T_Class string `orm:"size(256);null"` // Device.DeviceClass.Id 设备分类 C1|C2|
  26. T_Notice string `orm:"size(256);null"` // 通知绑定
  27. T_datashow int `orm:"index;size(2);default(1)"` // 0 屏蔽数据展示 1 正常数据展示 (屏蔽后 数据展示无法看到,设备管理中 不受影响)
  28. T_sort int `orm:"index;size(200);default(1)"` // 排序
  29. T_3dview string `orm:"size(256);null"` // 3D 视图ID
  30. T_type int `orm:"index;size(4);null"` // Device.DeviceSensorType 1库房 2移动
  31. // 设备同步参数
  32. T_Dattery int `orm:"size(4);null"` // 电量
  33. T_Site string `orm:"size(200);null"` // GPS
  34. T_monitor int `orm:"index;size(2);null"` // 监控状态 0 未监控 1 监控
  35. T_online int `orm:"index;size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线
  36. T_online_s int `orm:"index;size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线
  37. T_State int `orm:"index;size(2);default(1)"` // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  38. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  39. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  40. }
  41. type DeviceSensor_Del struct {
  42. T_sn string
  43. T_id int
  44. }
  45. func (t *DeviceSensor) TableName() string {
  46. return "device_sensor" // 数据库名称 // ************** 替换 FormulaList **************
  47. }
  48. // 模板
  49. type DeviceSensor_R struct {
  50. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  51. T_id int // 传感器编号
  52. T_name string // 标题
  53. T_3dview string // 3D 视图ID
  54. T_sort int // 排序
  55. T_Dattery int // 电量
  56. T_Site string // GPS
  57. T_monitor int // 记录状态
  58. T_online int // 在线状态 1 在线 0 离线
  59. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  60. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  61. T_type int // 类型
  62. T_DeviceSensorData DeviceData_R // 传感器最新数据
  63. T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
  64. T_DeviceSensorType DeviceSensorType_R // 传感器类型
  65. }
  66. type DeviceSensor_Applet struct {
  67. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  68. T_id int // 传感器编号
  69. T_name string // 标题
  70. T_3dview string // 3D 视图ID
  71. T_sort int // 排序
  72. T_Dattery int // 电量
  73. T_Site string // GPS
  74. T_monitor int // 记录状态
  75. T_online int // 在线状态 1 在线 0 离线
  76. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  77. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  78. T_type int // 1库房 2移动
  79. T_DeviceSensorData DeviceData_R // 传感器最新数据
  80. T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
  81. T_DeviceSensorType DeviceSensorType_R // 传感器类型
  82. T_Device Device_R // 传感器类型
  83. }
  84. // 传感器管理
  85. type DeviceSensor_P struct {
  86. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  87. T_id int // 传感器编号
  88. T_name string // 标题
  89. T_sort int // 排序
  90. T_datashow int // 0 屏蔽数据展示 1 正常数据展示
  91. T_State int // 0 屏蔽 1 正常
  92. // DeviceSensorParameter
  93. T_Tlower *float32 // 温度下限
  94. T_Tupper *float32 // 温度上限
  95. T_RHlower *float32 // 湿度下限
  96. T_RHupper *float32 // 湿度上限
  97. T_enprel *int // 是否启用预警 1开启 0 关闭
  98. T_tprel *float32 // 温度预警下限
  99. T_tpreu *float32 // 温度预警上限
  100. T_hprel *float32 // 湿度预警下限
  101. T_hpreu *float32 // 温度预警上限
  102. T_en *int // en:是否启用传感器,
  103. T_free *int // free:监测点是否为闲置状态(空库,只监测不报警)
  104. }
  105. type DeviceSensor_ struct {
  106. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  107. T_id int // 传感器编号
  108. T_name string // 标题
  109. }
  110. var redisCache_DeviceSensor cache.Cache
  111. func init() {
  112. //注册模型
  113. orm.RegisterModel(new(DeviceSensor))
  114. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  115. "redis_DeviceSensor", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  116. fmt.Println(config)
  117. var err error
  118. redisCache_DeviceSensor, err = cache.NewCache("redis", config)
  119. if err != nil || redisCache_DeviceSensor == nil {
  120. errMsg := "failed to init redis"
  121. logs.Error(errMsg, err)
  122. panic(errMsg)
  123. }
  124. }
  125. // ---------------- Redis -------------------
  126. // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
  127. func Redis_DeviceSensor_Set(r DeviceSensor) (err error) {
  128. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  129. //json序列化
  130. str, err := json.Marshal(r)
  131. if err != nil {
  132. logs.Error(lib.FuncName(), err)
  133. return
  134. }
  135. err = redisCache_DeviceSensor.Put(key, str, 24*time.Hour)
  136. if err != nil {
  137. logs.Error("set key:", key, ",value:", str, err)
  138. }
  139. return
  140. }
  141. func Redis_DeviceSensor_Get(key string) (r DeviceSensor, is bool) {
  142. if redisCache_DeviceSensor.IsExist(key) {
  143. //println("找到key:",key)
  144. v := redisCache_DeviceSensor.Get(key)
  145. err := json.Unmarshal(v.([]byte), &r)
  146. if err != nil {
  147. logs.Error(lib.FuncName(), err)
  148. return DeviceSensor{}, false
  149. }
  150. return r, true
  151. }
  152. return DeviceSensor{}, false
  153. }
  154. func Redis_DeviceSensor_DelK(r DeviceSensor) (err error) {
  155. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  156. err = redisCache_DeviceSensor.Delete(key)
  157. if err != nil {
  158. logs.Error(lib.FuncName(), err)
  159. }
  160. return
  161. }
  162. // ---------------- 特殊方法 -------------------
  163. func DeviceSensorToDeviceSensor_R(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_R) {
  164. //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
  165. lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn, 3) // 连续请求 实时数据
  166. DeviceSensor_r.T_sn = DeviceSensor_.T_sn
  167. DeviceSensor_r.T_id = DeviceSensor_.T_id
  168. DeviceSensor_r.T_name = DeviceSensor_.T_name
  169. DeviceSensor_r.T_Site = DeviceSensor_.T_Site
  170. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  171. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  172. DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
  173. DeviceSensor_r.T_type = DeviceSensor_.T_type
  174. DeviceSensor_r.T_sort = DeviceSensor_.T_sort
  175. DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
  176. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  177. DeviceSensor_r.T_online = DeviceSensor_.T_online
  178. DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s
  179. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  180. if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 {
  181. DeviceSensor_r.T_monitor = 2
  182. }
  183. // 最新系统参数
  184. DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  185. // 最新数据
  186. //key_data := DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id)
  187. //DeviceSensor_r.T_DeviceSensorData, _ = RedisDeviceData_Get(key_data)
  188. DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  189. //DeviceData, _ := RedisDeviceData_Get(DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id))
  190. device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
  191. DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
  192. deviceSensorType := Read_DeviceSensorType_Get(DeviceSensor_.T_type)
  193. DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(deviceSensorType)
  194. return
  195. }
  196. func DeviceSensorToDeviceSensor_Applet(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_Applet) {
  197. //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据
  198. lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn, 3) // 连续请求 实时数据
  199. DeviceSensor_r.T_sn = DeviceSensor_.T_sn
  200. DeviceSensor_r.T_id = DeviceSensor_.T_id
  201. DeviceSensor_r.T_name = DeviceSensor_.T_name
  202. DeviceSensor_r.T_Site = DeviceSensor_.T_Site
  203. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  204. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  205. DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview
  206. DeviceSensor_r.T_type = DeviceSensor_.T_type
  207. DeviceSensor_r.T_sort = DeviceSensor_.T_sort
  208. DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
  209. DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
  210. DeviceSensor_r.T_online = DeviceSensor_.T_online
  211. DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s
  212. DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
  213. if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 {
  214. DeviceSensor_r.T_monitor = 2
  215. }
  216. // 最新系统参数
  217. DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  218. // 最新数据
  219. DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id)
  220. device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn)
  221. DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData)
  222. deviceSensorType := Read_DeviceSensorType_Get(DeviceSensor_.T_type)
  223. DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(deviceSensorType)
  224. DeviceSensor_r.T_Device = DeviceToDevice_R(device)
  225. return
  226. }
  227. func DeviceSensorToDeviceSensor_(r DeviceSensor) (t DeviceSensor_) {
  228. t.T_sn = r.T_sn
  229. t.T_id = r.T_id
  230. t.T_name = r.T_name
  231. return
  232. }
  233. // ---------------- 特殊方法 -------------------
  234. // 获取列表 - 总数
  235. func Read_DeviceSensor_Num_ByT_sn(T_sn string) int {
  236. o := orm.NewOrm()
  237. // 也可以直接使用 Model 结构体作为表名
  238. qs := o.QueryTable(new(DeviceSensor))
  239. cnt, err := qs.Filter("T_sn", T_sn).Count()
  240. if err != nil {
  241. logs.Error(lib.FuncName(), err)
  242. return 0
  243. }
  244. return int(cnt)
  245. }
  246. // 获取列表
  247. 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) {
  248. o := orm.NewOrm()
  249. // 也可以直接使用 Model 结构体作为表名
  250. qs := o.QueryTable(new(DeviceSensor))
  251. var offset int64
  252. if page <= 1 {
  253. offset = 0
  254. } else {
  255. offset = int64((page - 1) * page_z)
  256. }
  257. var r []DeviceSensor
  258. cond := orm.NewCondition()
  259. cond1 := cond.And("T_pid", T_pid)
  260. if T_Class_id > 0 {
  261. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  262. fmt.Println("T_Class:", T_Class)
  263. cond1 = cond1.And("T_Class__icontains", T_Class)
  264. }
  265. if len(T_type) > 0 {
  266. list := strings.Split(T_type, ",")
  267. cond1 = cond1.And("T_type__in", list)
  268. }
  269. if len(T_sn) > 0 {
  270. cond1 = cond1.And("T_sn__icontains", T_sn)
  271. }
  272. if len(T_name) > 0 {
  273. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name))
  274. }
  275. // 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常
  276. if admin_r.T_pid > 0 || T_State == 1 {
  277. cond1 = cond1.And("T_State", 1)
  278. }
  279. if T_datashow == 1 { // 0 屏蔽数据展示 1 正常数据展示
  280. cond1 = cond1.And("T_datashow", 1)
  281. }
  282. var err error
  283. if page_z == 9999 {
  284. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  285. } else {
  286. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  287. }
  288. if err != nil {
  289. logs.Error(lib.FuncName(), err)
  290. return
  291. }
  292. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  293. if err != nil {
  294. logs.Error(lib.FuncName(), err)
  295. return
  296. }
  297. for _, v := range r {
  298. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  299. }
  300. return DeviceSensor_r, cnt
  301. }
  302. // 获取
  303. func Read_DeviceSensor_ByT_sn(T_sn string, T_id int) (r DeviceSensor, is bool) {
  304. key := r.T_sn + "|" + strconv.Itoa(r.T_id)
  305. if r, is := Redis_DeviceSensor_Get(key); is {
  306. return r, true
  307. }
  308. o := orm.NewOrm()
  309. r = DeviceSensor{T_sn: T_sn, T_id: T_id}
  310. err := o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  311. if err != nil {
  312. logs.Error(lib.FuncName(), err)
  313. return r, false
  314. }
  315. Redis_DeviceSensor_Set(r)
  316. return r, true
  317. }
  318. // 删除
  319. func Delete_DeviceSensor_ById(T_sn string, T_id int) (err error) {
  320. fmt.Println("Delete_DeviceSensor : T_sn", T_sn, "T_id", T_id)
  321. o := orm.NewOrm()
  322. r := DeviceSensor{T_sn: T_sn, T_id: T_id}
  323. err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  324. if err != nil {
  325. fmt.Println("Delete_DeviceSensor_ById", err)
  326. return err
  327. }
  328. _, err = o.Delete(&DeviceSensor{Id: r.Id})
  329. if err != nil {
  330. logs.Error(lib.FuncName(), err)
  331. return err
  332. }
  333. Redis_DeviceSensor_DelK(r)
  334. return
  335. }
  336. // 修改
  337. func Update_DeviceSensor(r DeviceSensor, cols ...string) bool {
  338. o := orm.NewOrm()
  339. num, err := o.Update(&r, cols...)
  340. if err != nil {
  341. logs.Error(lib.FuncName(), err)
  342. return false
  343. }
  344. fmt.Println("Number of records updated in database:", num)
  345. Redis_DeviceSensor_Set(r)
  346. return true
  347. }
  348. // 修改T_Class,替换为""
  349. func DeviceSensor_Bind_T_Class_Del(T_sn string, T_id int, T_Class_id int) (err error) {
  350. o := orm.NewOrm()
  351. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  352. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  353. err = o.Read(&v, "T_sn", "T_id")
  354. if err != nil {
  355. logs.Error(lib.FuncName(), err)
  356. return err
  357. }
  358. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  359. _, err = o.Update(&v, "T_Class")
  360. if err != nil {
  361. logs.Error(lib.FuncName(), err)
  362. return err
  363. }
  364. Redis_DeviceSensor_Set(v)
  365. return nil
  366. }
  367. // 修改T_Class,追加
  368. func DeviceSensor_Bind_T_Class_Add(T_sn string, T_id int, T_Class_id int) (err error) {
  369. o := orm.NewOrm()
  370. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  371. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  372. err = o.Read(&v, "T_sn", "T_id")
  373. if err != nil {
  374. logs.Error(lib.FuncName(), err)
  375. return err
  376. }
  377. v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
  378. v.T_Class = v.T_Class + T_Class
  379. _, err = o.Update(&v, "T_Class")
  380. if err != nil {
  381. logs.Error(lib.FuncName(), err)
  382. return err
  383. }
  384. Redis_DeviceSensor_Set(v)
  385. return nil
  386. }
  387. // 获取 ById
  388. func Read_DeviceSensor_ByTsn_Tid(T_sn string, T_id int) (r DeviceSensor, err error) {
  389. o := orm.NewOrm()
  390. r = DeviceSensor{T_sn: T_sn, T_id: T_id}
  391. err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  392. if err != nil {
  393. logs.Error(lib.FuncName(), err)
  394. }
  395. return r, err
  396. }
  397. // 获取列表
  398. func Read_DeviceSensor_ByTsn(T_sn string) ([]DeviceSensor, int) {
  399. o := orm.NewOrm()
  400. // 也可以直接使用 Model 结构体作为表名
  401. qs := o.QueryTable(new(DeviceSensor))
  402. var r []DeviceSensor
  403. _, err := qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
  404. if err != nil {
  405. logs.Error(lib.FuncName(), err)
  406. return r, 0
  407. }
  408. cnt, err := qs.Filter("T_sn", T_sn).Count()
  409. if err != nil {
  410. logs.Error(lib.FuncName(), err)
  411. return r, 0
  412. }
  413. return r, int(cnt)
  414. }
  415. // 获取列表
  416. 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) {
  417. o := orm.NewOrm()
  418. // 也可以直接使用 Model 结构体作为表名
  419. qs := o.QueryTable(new(DeviceSensor))
  420. var offset int64
  421. if page <= 1 {
  422. offset = 0
  423. } else {
  424. offset = int64((page - 1) * page_z)
  425. }
  426. T_Class := ""
  427. if T_Class_id != 0 {
  428. T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
  429. }
  430. fmt.Println("T_Class:", T_Class)
  431. var r []DeviceSensor
  432. cond := orm.NewCondition()
  433. 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)
  434. if T_type > 0 {
  435. cond1 = cond1.AndCond(cond.And("T_type", T_type))
  436. }
  437. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  438. if err != nil {
  439. logs.Error(lib.FuncName(), err)
  440. return
  441. }
  442. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  443. if err != nil {
  444. logs.Error(lib.FuncName(), err)
  445. return
  446. }
  447. for _, v := range r {
  448. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
  449. }
  450. return DeviceSensor_r, cnt
  451. }
  452. // 获取列表
  453. func Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn string, T_id int, T_Class_id int) (r []DeviceSensor) {
  454. o := orm.NewOrm()
  455. // 也可以直接使用 Model 结构体作为表名
  456. qs := o.QueryTable(new(DeviceSensor))
  457. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  458. _, err := qs.Filter("T_Class__icontains", T_Class).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
  459. if err != nil {
  460. logs.Error(lib.FuncName(), err)
  461. return
  462. }
  463. return r
  464. }
  465. // 获取列表
  466. func Read_DeviceSensor_ALL_List_T_sn(T_sn string) (r []DeviceSensor) {
  467. o := orm.NewOrm()
  468. // 也可以直接使用 Model 结构体作为表名
  469. qs := o.QueryTable(new(DeviceSensor))
  470. _, err := qs.Filter("T_sn", T_sn).All(&r)
  471. if err != nil {
  472. logs.Error(lib.FuncName(), err)
  473. return
  474. }
  475. return r
  476. }
  477. func Read_DeviceSensor_List_T_sn_T_datashow(T_sn string) (r []DeviceSensor) {
  478. o := orm.NewOrm()
  479. // 也可以直接使用 Model 结构体作为表名
  480. qs := o.QueryTable(new(DeviceSensor))
  481. _, err := qs.Filter("T_sn", T_sn).Filter("T_State", 1).Filter("T_datashow", 1).All(&r)
  482. if err != nil {
  483. logs.Error(lib.FuncName(), err)
  484. return
  485. }
  486. return r
  487. }
  488. // 获取列表
  489. func Read_DeviceSensor_ALL_Class_Id(T_Class_id int) (r []DeviceSensor) {
  490. o := orm.NewOrm()
  491. // 也可以直接使用 Model 结构体作为表名
  492. qs := o.QueryTable(new(DeviceSensor))
  493. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  494. _, err := qs.Filter("T_Class__icontains", T_Class).All(&r)
  495. if err != nil {
  496. logs.Error(lib.FuncName(), err)
  497. return
  498. }
  499. return r
  500. }
  501. func DELETE_DeviceSensor(SN string) bool {
  502. sql := "DELETE FROM `cold`.`device_sensor` WHERE `t_sn` = '" + SN + "' "
  503. o := orm.NewOrm()
  504. _, err := o.Raw(sql).Exec()
  505. if err != nil {
  506. logs.Error(lib.FuncName(), err)
  507. return false
  508. }
  509. return true
  510. }
  511. // 报警策略 -------------------
  512. // 通过T_sn T_id T_Notice_id 获取全部列表
  513. func Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn string, T_id int, T_Notice_id int) (r []DeviceSensor) {
  514. o := orm.NewOrm()
  515. // 也可以直接使用 Model 结构体作为表名
  516. qs := o.QueryTable(new(DeviceSensor))
  517. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  518. _, err := qs.Filter("T_Notice__icontains", T_Notice).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
  519. if err != nil {
  520. logs.Error(lib.FuncName(), err)
  521. return
  522. }
  523. return r
  524. }
  525. // 修改T_Notice,替换为""
  526. func DeviceSensor_Bind_T_Notice_Del(T_sn string, T_id int, T_Notice_id int) (err error) {
  527. o := orm.NewOrm()
  528. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  529. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  530. err = o.Read(&v, "T_sn", "T_id")
  531. if err != nil {
  532. logs.Error(lib.FuncName(), err)
  533. return
  534. }
  535. v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
  536. _, err = o.Update(&v, "T_Notice")
  537. if err != nil {
  538. logs.Error(lib.FuncName(), err)
  539. return
  540. }
  541. Redis_DeviceSensor_Set(v)
  542. return nil
  543. }
  544. // 修改T_Notice,追加
  545. func DeviceSensor_Bind_T_Notice_Add(T_sn string, T_id int, T_Notice_id int) (err error) {
  546. o := orm.NewOrm()
  547. v := DeviceSensor{T_sn: T_sn, T_id: T_id}
  548. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  549. err = o.Read(&v, "T_sn", "T_id")
  550. if err != nil {
  551. logs.Error(lib.FuncName(), err)
  552. return
  553. }
  554. v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
  555. v.T_Notice = v.T_Notice + T_Notice
  556. _, err = o.Update(&v, "T_Notice")
  557. if err != nil {
  558. logs.Error(lib.FuncName(), err)
  559. return
  560. }
  561. Redis_DeviceSensor_Set(v)
  562. return nil
  563. }
  564. // 获取列表
  565. func Read_DeviceSensor_ALL_Notice_Id(T_Notice_id int) (r []DeviceSensor) {
  566. o := orm.NewOrm()
  567. // 也可以直接使用 Model 结构体作为表名
  568. qs := o.QueryTable(new(DeviceSensor))
  569. T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
  570. _, err := qs.Filter("T_Notice__icontains", T_Notice).All(&r)
  571. if err != nil {
  572. logs.Error(lib.FuncName(), err)
  573. return
  574. }
  575. return r
  576. }
  577. // 获取列表
  578. 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) {
  579. o := orm.NewOrm()
  580. // 也可以直接使用 Model 结构体作为表名
  581. qs := o.QueryTable(new(DeviceSensor))
  582. var offset int64
  583. if page <= 1 {
  584. offset = 0
  585. } else {
  586. offset = int64((page - 1) * page_z)
  587. }
  588. T_Notice := ""
  589. if T_Notice_id != 0 {
  590. T_Notice = "N" + strconv.Itoa(T_Notice_id) + "|"
  591. }
  592. fmt.Println("T_Notice:", T_Notice)
  593. var r []DeviceSensor
  594. cond := orm.NewCondition()
  595. 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)
  596. if T_type > 0 {
  597. cond1 = cond1.AndCond(cond.And("T_type", T_type))
  598. }
  599. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  600. if err != nil {
  601. logs.Error(lib.FuncName(), err)
  602. return
  603. }
  604. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  605. if err != nil {
  606. logs.Error(lib.FuncName(), err)
  607. return
  608. }
  609. for _, v := range r {
  610. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v))
  611. }
  612. return DeviceSensor_r, cnt
  613. }
  614. // 设备同步参数
  615. func Update_Device_To_DeviceSensor(r_Device Device) bool {
  616. DeviceSensor_list, _ := Read_DeviceSensor_ByTsn(r_Device.T_sn)
  617. for _, v := range DeviceSensor_list {
  618. // 设备同步参数
  619. v.T_Dattery = r_Device.T_Dattery // 电量
  620. v.T_Site = r_Device.T_Site // GPS
  621. v.T_monitor = r_Device.T_monitor // 监控状态 0 未监控 1 监控
  622. v.T_online = r_Device.T_online // 在线状态 0 未启用 1 在线 2 离线
  623. v.T_online_s = r_Device.T_online_s // 在线状态-备用 0 未启用 1 在线 2 离线
  624. v.T_State = r_Device.T_State // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  625. Update_DeviceSensor(v, "T_Dattery", "T_Site", "T_type", "T_State", "T_monitor", "T_online", "T_online_s")
  626. }
  627. return true
  628. }
  629. // 传感器管理列列表 -------------------
  630. 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) {
  631. o := orm.NewOrm()
  632. var maps []DeviceSensor_P
  633. var maps_z []orm2.ParamsList
  634. var offset int
  635. if page <= 1 {
  636. offset = 0
  637. } else {
  638. offset = (page - 1) * page_z
  639. }
  640. sql_WHERE := "t_pid = " + strconv.Itoa(T_pid)
  641. if admin_r.T_pid > 0 {
  642. sql_WHERE += " AND t__state = 1"
  643. }
  644. if len(T_name) > 0 {
  645. sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR t_name like '%" + T_name + "%')"
  646. }
  647. if T_calss_id > 0 {
  648. T_calss := "C" + strconv.Itoa(T_calss_id) + "|"
  649. sql_WHERE += " AND t__class like '%" + T_calss + "%'"
  650. }
  651. if T_en != -1 {
  652. sql_WHERE += " AND t_en = " + strconv.Itoa(T_en)
  653. }
  654. if T_free != -1 {
  655. sql_WHERE += " AND t_free = " + strconv.Itoa(T_free)
  656. }
  657. if T_datashow != -1 {
  658. sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow)
  659. }
  660. sql_ORDER := " ORDER BY t_sort ASC"
  661. if T_sort == 1 {
  662. sql_ORDER = " ORDER BY t_sort DESC"
  663. }
  664. // -------------
  665. sql := "SELECT COUNT(ds.ID) FROM " + "device_sensor ds " +
  666. "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_en,t_free " +
  667. "FROM device_sensor_parameter WHERE id IN (SELECT MAX(id) FROM device_sensor_parameter GROUP BY t_sn,t_id)) AS dsp " +
  668. "ON ds.t_sn = dsp.tsn AND ds.t_id = dsp.tid" + " WHERE " + sql_WHERE
  669. fmt.Println(sql)
  670. _, err := o.Raw(sql).ValuesList(&maps_z)
  671. if err != nil {
  672. logs.Error(lib.FuncName(), err)
  673. return DeviceSensor_p, 0
  674. }
  675. if len(maps_z) == 0 {
  676. return DeviceSensor_p, 0
  677. }
  678. //fmt.Println("maps_z;",maps_z[0][0])
  679. sql = "SELECT * FROM device_sensor ds " +
  680. "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 " +
  681. "FROM device_sensor_parameter WHERE id IN (SELECT MAX(id) FROM device_sensor_parameter GROUP BY t_sn,t_id)) AS dsp " +
  682. "ON ds.t_sn = dsp.tsn AND ds.t_id = dsp.tid " +
  683. "WHERE " + sql_WHERE + sql_ORDER
  684. if page_z != 9999 {
  685. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  686. }
  687. fmt.Println(sql)
  688. _, err = o.Raw(sql).QueryRows(&maps)
  689. if err != nil {
  690. logs.Error(lib.FuncName(), err)
  691. return
  692. }
  693. key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  694. return maps, key
  695. }
  696. // 数据展示菜单下 传感器参数列表
  697. // 实时数据页面 温湿度一场数据往前排
  698. // 轨迹展示页面
  699. 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) {
  700. o := orm.NewOrm()
  701. // 也可以直接使用 Model 结构体作为表名
  702. qs := o.QueryTable(new(DeviceSensor))
  703. var offset int
  704. if page <= 1 {
  705. offset = 0
  706. } else {
  707. offset = (page - 1) * page_z
  708. }
  709. var r []DeviceSensor
  710. cond := orm.NewCondition()
  711. cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
  712. if T_Class_id > 0 {
  713. T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
  714. fmt.Println("T_Class:", T_Class)
  715. cond1 = cond1.And("T_Class__icontains", T_Class)
  716. }
  717. if T_type > 0 {
  718. cond1 = cond1.And("T_type", T_type)
  719. }
  720. if len(T_name) == 16 {
  721. cond1 = cond1.And("T_sn", T_name)
  722. } else if len(T_name) > 0 {
  723. cond1 = cond1.And("T_name__icontains", T_name).And("T_datashow", 1)
  724. }
  725. if T_MapShow == 1 {
  726. cond1 = cond1.AndNot("T_Site", "")
  727. }
  728. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  729. if err != nil {
  730. logs.Error(lib.FuncName(), err)
  731. return
  732. }
  733. // 实时数据页面,温度或者湿度不在正常区间,排在最前面
  734. if T_RealTime == 1 {
  735. offset_z := offset + page_z
  736. if cnt < int64(offset_z) {
  737. offset_z = int(cnt)
  738. }
  739. if offset > offset_z {
  740. offset = offset_z
  741. }
  742. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  743. if err != nil {
  744. logs.Error(lib.FuncName(), err)
  745. return
  746. }
  747. var DeviceSensor_unusual []DeviceSensor_R
  748. for _, v := range r {
  749. v_r := DeviceSensorToDeviceSensor_R(v)
  750. data := v_r.T_DeviceSensorData
  751. // 温度或者湿度不在正常区间,排在最前面
  752. 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 {
  753. DeviceSensor_unusual = append(DeviceSensor_unusual, v_r)
  754. } else {
  755. DeviceSensor_r = append(DeviceSensor_r, v_r)
  756. }
  757. }
  758. DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...)
  759. return DeviceSensor_unusual[offset:offset_z], cnt
  760. }
  761. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  762. if err != nil {
  763. logs.Error(lib.FuncName(), err)
  764. return
  765. }
  766. for _, v := range r {
  767. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
  768. }
  769. return DeviceSensor_r, cnt
  770. }
  771. // 获取列表
  772. func Read_DeviceSensor_T_type(T_pid int) (lists orm2.ParamsList, err error) {
  773. o := orm.NewOrm()
  774. var pl_lists orm2.ParamsList
  775. _, 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)
  776. if err != nil {
  777. logs.Error(lib.FuncName(), err)
  778. return
  779. }
  780. return pl_lists, nil
  781. }
  782. // 数据展示菜单下 传感器参数列表
  783. 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) {
  784. o := orm.NewOrm()
  785. // 也可以直接使用 Model 结构体作为表名
  786. var offset int
  787. if page <= 1 {
  788. offset = 0
  789. } else {
  790. offset = (page - 1) * page_z
  791. }
  792. qs := o.QueryTable(new(DeviceSensor))
  793. var r []DeviceSensor
  794. cond := orm.NewCondition()
  795. cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
  796. if T_type > 0 {
  797. cond1 = cond1.And("T_type", T_type)
  798. }
  799. if len(T_name) == 16 {
  800. cond1 = cond1.And("T_sn", T_name)
  801. } else if len(T_name) > 0 {
  802. cond1 = cond1.And("T_name__icontains", T_name).And("T_datashow", 1)
  803. } else {
  804. cond1 = cond1.And("T_datashow", 1)
  805. }
  806. if T_online == "1" {
  807. cond1 = cond1.AndCond(cond.Or("T_online", 1).Or("T_online_s", 1))
  808. } else if T_online == "2" {
  809. cond1 = cond1.AndCond(cond.AndCond(cond.And("T_online", 2).And("T_online_s", 0)).
  810. OrCond(cond.And("T_online", 0).And("T_online_s", 2)))
  811. } else if T_online == "0" {
  812. cond1 = cond1.And("T_online", 0).And("T_online_s", 0)
  813. }
  814. var err error
  815. if page_z == 9999 {
  816. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  817. } else {
  818. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
  819. }
  820. if err != nil {
  821. logs.Error(lib.FuncName(), err)
  822. return
  823. }
  824. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  825. if err != nil {
  826. logs.Error(lib.FuncName(), err)
  827. return
  828. }
  829. for _, v := range r {
  830. DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_Applet(v))
  831. }
  832. return DeviceSensor_r, cnt
  833. }
  834. // 数据展示菜单下 传感器参数列表
  835. func Read_DeviceSensor_T_sn_ByT_type(T_pid, T_type int) (lists orm2.ParamsList, err error) {
  836. o := orm.NewOrm()
  837. sql := "t_pid=? AND t__state=1 AND t_datashow=1 AND t_type = " + strconv.Itoa(T_type)
  838. var pl_lists orm2.ParamsList
  839. _, err = o.Raw("SELECT DISTINCT T_sn FROM device_sensor WHERE "+sql+" LIMIT 0,1000", T_pid).ValuesFlat(&pl_lists)
  840. if err != nil {
  841. logs.Error(lib.FuncName(), err)
  842. return
  843. }
  844. return pl_lists, nil
  845. }