Device.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "Cold_Api/models/Product"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego/cache"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. "github.com/beego/beego/v2/core/logs"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. _ "github.com/go-sql-driver/mysql"
  15. "time"
  16. )
  17. // 设备
  18. type Device struct {
  19. T_sn string `orm:"pk;size(256);null"` // 设备序列号
  20. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  21. T_devName string `orm:"size(256);null"` // 设备名称 20字
  22. T_protocol int `orm:"size(2);default(3)"` // 冷链通讯协议 1 :1.0协议 2 :2.0协议 3 :3.0协议
  23. T_mqttid string `orm:"size(256);null"` // MQTT 服务ID
  24. T_VerifyTime time.Time `orm:"type(timestamp);null"` // 验证时间
  25. T_VerifyEndTime time.Time `orm:"type(timestamp);null"` // 验证到期时间
  26. T_CalibrationTime time.Time `orm:"type(timestamp);null"` // 校准时间
  27. T_CalibrationEndTime time.Time `orm:"type(timestamp);null"` // 校准到期时间
  28. T_PatrolTime time.Time `orm:"type(timestamp);null"` // 巡检时间
  29. T_abandonTime time.Time `orm:"type(timestamp);null"` // 弃用时间
  30. T_ist int `orm:"size(2);default(1)"` // 温度 1开启 2关闭
  31. T_ish int `orm:"size(2);default(1)"` // 湿度 1开启 2关闭
  32. T_State int `orm:"index;size(2);default(1)"` // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  33. // 设备同步参数
  34. T_Dattery int `orm:"size(4);null"` // 电量
  35. T_Site string `orm:"size(200);null"` // GPS
  36. T_monitor int `orm:"index;size(2);null"` // 监控状态 0 未监控 1 监控 停止记录
  37. T_online int `orm:"index;size(2);default(0)"` // 在线状态 0 未启用 1 在线 2 离线
  38. T_online_s int `orm:"index;size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线
  39. IsLocal int `orm:"size(2);default(0);column(Islocal)"` //是否本公司设备
  40. // 硬件信息
  41. T_model string `orm:"size(200);null"` // KF200BG 设备型号
  42. T_sver string `orm:"size(200);null"` // "1.0.0",//软件版本
  43. T_hver string `orm:"size(200);null"` // "1.0.0",//硬件版本
  44. T_imei string `orm:"size(200);null"` // "867387060327718",//模组imei
  45. T_iccid string `orm:"size(200);null"` // "89860477102170049750",//sim卡号
  46. T_rssi string `orm:"size(200);null"` // "80",//信号强度
  47. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  48. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  49. }
  50. type Device_R struct {
  51. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  52. T_devName string // 设备名称
  53. T_protocol int // 1 1.0协议 2 2.0协议
  54. T_VerifyTime string // 验证时间
  55. T_CalibrationTime string // 校准时间
  56. T_PatrolTime string // 巡检时间
  57. T_abandonTime string // 弃用时间
  58. T_ist int // 温度 1开启 2关闭
  59. T_ish int // 湿度 1开启 2关闭
  60. T_Dattery int // 电量
  61. T_Site string // GPS
  62. T_monitor int // 监控状态 0 未监控 1 监控 停止记录
  63. T_online int // 在线状态 0 未启用 1 在线 2 离线
  64. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  65. T_State int // 0 屏蔽 1 正常
  66. // 硬件信息
  67. T_model string // KF200BG 产品型号
  68. T_ProductTypeName string // 验证工具LoRa 产品统称 + 类型
  69. T_sver string // "1.0.0",//软件版本
  70. T_hver string // "1.0.0",//硬件版本
  71. T_imei string // "867387060327718",//模组imei
  72. T_iccid string // "89860477102170049750",//sim卡号
  73. T_rssi string // "80",//信号强度
  74. CreateTime string //auto_now_add 第一次保存时才设置时间
  75. UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新
  76. T_DeviceSensor_Num int // 传感器数量
  77. }
  78. type Device_task struct {
  79. T_sn string
  80. T_task string
  81. }
  82. func (t *Device) TableName() string {
  83. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  84. }
  85. var redisCache_Device cache.Cache
  86. func init() {
  87. //注册模型
  88. orm.RegisterModel(new(Device))
  89. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  90. "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  91. fmt.Println(config)
  92. var err error
  93. redisCache_Device, err = cache.NewCache("redis", config)
  94. if err != nil || redisCache_Device == nil {
  95. errMsg := "failed to init redis"
  96. logs.Error(errMsg, err)
  97. panic(errMsg)
  98. }
  99. }
  100. func DeviceToDevice_R(r Device) (t Device_R) {
  101. t.T_sn = r.T_sn
  102. t.T_devName = r.T_devName
  103. t.T_protocol = r.T_protocol
  104. if !r.T_VerifyTime.IsZero() {
  105. t.T_VerifyTime = r.T_VerifyTime.Format("2006-01-02 15:04:05")
  106. }
  107. if !r.T_VerifyEndTime.IsZero() {
  108. t.T_VerifyTime = r.T_VerifyTime.Format("2006-01-02 15:04:05")
  109. }
  110. if !r.T_CalibrationEndTime.IsZero() {
  111. t.T_VerifyTime = r.T_VerifyTime.Format("2006-01-02 15:04:05")
  112. }
  113. if !r.T_CalibrationTime.IsZero() {
  114. t.T_CalibrationTime = r.T_CalibrationTime.Format("2006-01-02 15:04:05")
  115. }
  116. if !r.T_PatrolTime.IsZero() {
  117. t.T_PatrolTime = r.T_PatrolTime.Format("2006-01-02 15:04:05")
  118. }
  119. if !r.T_abandonTime.IsZero() {
  120. t.T_abandonTime = r.T_abandonTime.Format("2006-01-02 15:04:05")
  121. }
  122. t.T_ist = r.T_ist
  123. t.T_ish = r.T_ish
  124. t.T_Dattery = r.T_Dattery
  125. t.T_Site = r.T_Site
  126. t.T_monitor = r.T_monitor
  127. t.T_online = r.T_online
  128. t.T_online_s = r.T_online_s
  129. //if (r.T_online == 0 || r.T_online == 2) && (r.T_online_s == 0 || r.T_online_s == 2) && r.T_monitor == 1 {
  130. // t.T_monitor = 2
  131. //}
  132. t.T_State = r.T_State
  133. t.T_model = r.T_model
  134. t.T_ProductTypeName = Product.Read_ProductType_Get(r.T_model)
  135. t.T_sver = r.T_sver
  136. t.T_hver = r.T_hver
  137. t.T_imei = r.T_imei
  138. t.T_iccid = r.T_iccid
  139. t.T_rssi = r.T_rssi
  140. t.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  141. t.UpdateTime = r.UpdateTime.Format("2006-01-02 15:04:05")
  142. t.T_DeviceSensor_Num = Read_DeviceSensor_Num_ByT_sn(r.T_sn)
  143. return
  144. }
  145. // ---------------- Redis -------------------
  146. // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
  147. func Redis_Device_Set(r Device) (err error) {
  148. //json序列化
  149. str, err := json.Marshal(r)
  150. if err != nil {
  151. logs.Error(lib.FuncName(), err)
  152. return
  153. }
  154. err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour)
  155. if err != nil {
  156. logs.Error("set key:", r.T_sn, ",value:", str, err)
  157. }
  158. return
  159. }
  160. // if r,is :=Redis_Device_Get(T_sn);is{
  161. // return r,nil
  162. // }
  163. func Redis_Device_Get(key string) (r Device, is bool) {
  164. if redisCache_Device.IsExist(key) {
  165. //println("找到key:",key)
  166. v := redisCache_Device.Get(key)
  167. err := json.Unmarshal(v.([]byte), &r)
  168. if err != nil {
  169. logs.Error(lib.FuncName(), err)
  170. return Device{}, false
  171. }
  172. return r, true
  173. }
  174. return Device{}, false
  175. }
  176. func Redis_Device_DelK(key string) (err error) {
  177. err = redisCache_Device.Delete(key)
  178. if err != nil {
  179. logs.Error(lib.FuncName(), err)
  180. return
  181. }
  182. return
  183. }
  184. // ---------------- 特殊方法 -------------------
  185. // 获取 ById
  186. func Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  187. if r, is := Redis_Device_Get(T_sn); is {
  188. //println("Redis_Device_Get OK")
  189. return r, nil
  190. }
  191. //println("没有 Redis_Device_Get SN")
  192. o := orm.NewOrm()
  193. r = Device{T_sn: T_sn}
  194. err = o.Read(&r, "T_sn") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  195. if err != nil {
  196. logs.Error(lib.FuncName(), err)
  197. return r, err
  198. }
  199. Redis_Device_Set(r) // Redis 更新缓存
  200. return r, nil
  201. }
  202. // 添加
  203. func Add_Device(m Device) (err error) {
  204. o := orm.NewOrm()
  205. _, err = o.Insert(&m)
  206. if err != nil {
  207. logs.Error(lib.FuncName(), err)
  208. return err
  209. }
  210. Redis_Device_Set(m) // Redis 更新缓存
  211. return nil
  212. }
  213. // 删除
  214. func Delete_Device(m Device) (err error) {
  215. o := orm.NewOrm()
  216. _, err = o.Delete(&m)
  217. if err != nil {
  218. logs.Error(lib.FuncName(), err)
  219. return err
  220. }
  221. Redis_Device_DelK(m.T_sn)
  222. return
  223. }
  224. // 修改
  225. func Update_Device(r Device, cols ...string) bool {
  226. o := orm.NewOrm()
  227. num, err := o.Update(&r, cols...)
  228. if err != nil {
  229. logs.Error(lib.FuncName(), err)
  230. return false
  231. }
  232. logs.Info("Number of records updated in database:", num)
  233. Redis_Device_Set(r) // Redis 更新缓存
  234. return true
  235. }
  236. // 获取列表
  237. func Read_Device_List(admin *Account.Admin, bindSN []string, T_pid int, T_name string, T_monitor string, T_online string, T_abandon int, page int, page_z int) (r []Device_R, cnt int64) {
  238. o := orm.NewOrm()
  239. // 也可以直接使用 Model 结构体作为表名
  240. qs := o.QueryTable(new(Device))
  241. var offset int64
  242. if page <= 1 {
  243. offset = 0
  244. } else {
  245. offset = int64((page - 1) * page_z)
  246. }
  247. cond := orm.NewCondition()
  248. cond1 := cond.And("T_pid", T_pid)
  249. if len(bindSN) > 0 {
  250. cond1 = cond1.And("T_sn__in", bindSN)
  251. }
  252. //T_abandon 1-查询弃用设备 -1-查询全部 其他-查询全部
  253. if T_abandon == 1 {
  254. cond1 = cond1.And("T_State", 0)
  255. } else if T_abandon != -1 {
  256. cond1 = cond1.And("T_State", 1)
  257. }
  258. // 非内部权限 - 不可查看屏蔽数据
  259. //if admin != nil && admin.T_pid > 0 {
  260. // if len(T_name) >= 16 {
  261. // cond1 = cond1.And("T_sn", T_name)
  262. // } else if len(T_name) > 0 && len(T_name) < 16 {
  263. // cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name)).And("T_State", 1)
  264. // } else {
  265. // cond1 = cond1.And("T_State", 1)
  266. // }
  267. //} else {
  268. // if len(T_name) >= 16 {
  269. // cond1 = cond1.And("T_sn", T_name)
  270. // } else if len(T_name) > 0 && len(T_name) < 16 {
  271. // cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name))
  272. // }
  273. //}
  274. if len(T_name) >= 16 {
  275. cond1 = cond1.And("T_sn", T_name)
  276. } else if len(T_name) > 0 && len(T_name) < 16 {
  277. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name))
  278. }
  279. // 0 未监控 1 监控 2 未知状态
  280. if T_monitor == "0" {
  281. // 未监控
  282. cond1 = cond1.And("T_monitor", 0)
  283. } else if T_monitor == "1" {
  284. // 监控
  285. cond1 = cond1.And("T_monitor", 1)
  286. } else if T_monitor == "2" {
  287. // 未知状态 - 网络为未启用或离线状态 监控状态
  288. cond1 = cond1.And("T_monitor", 1).AndCond(cond.And("T_online", 0).Or("T_online", 2)).
  289. AndCond(cond.And("T_online_s", 0).Or("T_online_s", 2))
  290. }
  291. //0 未启用 1 在线 2 离线
  292. // T_online T_online_s
  293. // 0 0 未启用
  294. // 0 1 在线
  295. // 0 2 离线
  296. // 1 0 在线
  297. // 1 1 在线
  298. // 1 2 在线
  299. // 2 0 离线
  300. // 2 1 在线
  301. // 2 2 离线
  302. if T_online == "1" {
  303. cond1 = cond1.AndCond(cond.Or("T_online", 1).Or("T_online_s", 1))
  304. } else if T_online == "2" {
  305. cond1 = cond1.AndCond(cond.AndCond(cond.And("T_online", 2).And("T_online_s", 0)).
  306. OrCond(cond.And("T_online", 0).And("T_online_s", 2)))
  307. } else if T_online == "0" {
  308. cond1 = cond1.And("T_online", 0).And("T_online_s", 0)
  309. }
  310. var rx []Device
  311. var err error
  312. if page_z == 9999 {
  313. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("CreateTime").All(&rx)
  314. } else {
  315. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("CreateTime").All(&rx)
  316. }
  317. if err != nil {
  318. logs.Error(lib.FuncName(), err)
  319. return
  320. }
  321. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  322. if err != nil {
  323. logs.Error(lib.FuncName(), err)
  324. return
  325. }
  326. for _, v := range rx {
  327. r = append(r, DeviceToDevice_R(v))
  328. }
  329. return r, cnt
  330. }
  331. func Read_Device_ALL_T_Type_Count(T_pid int, T_type int) (cnt int64) {
  332. o := orm.NewOrm()
  333. qs := o.QueryTable(new(Device))
  334. cnt, _ = qs.Filter("T_pid", T_pid).Filter("T_type", T_type).Filter("T_State", 1).Count()
  335. return cnt
  336. }
  337. func Read_Device_List_ByT_model(T_model string) (r []Device) {
  338. o := orm.NewOrm()
  339. // 也可以直接使用 Model 结构体作为表名
  340. qs := o.QueryTable(new(Device))
  341. _, err := qs.Filter("T_model", T_model).All(&r)
  342. if err != nil {
  343. logs.Error(lib.FuncName(), err)
  344. return
  345. }
  346. return r
  347. }
  348. // 小程序 T_type = 2 分类管理统计
  349. // 通过T_monitor判断主机监控状态
  350. func Read_Device_Count_ByT_online(snList []string, T_online string) (cnt int64) {
  351. o := orm2.NewOrm()
  352. // 也可以直接使用 Model 结构体作为表名
  353. qs := o.QueryTable(new(Device))
  354. cond := orm2.NewCondition()
  355. cond1 := cond.And("T_State", 1)
  356. if len(snList) > 0 {
  357. cond1 = cond1.And("T_sn__in", snList)
  358. }
  359. // T_online 0 未启用 1 在线 2 离线
  360. //if T_online == "1" {
  361. // cond1 = cond1.AndCond(cond.Or("T_online", 1).Or("T_online_s", 1))
  362. //} else if T_online == "2" {
  363. // cond1 = cond1.AndCond(cond.And("T_online", 2).And("T_online_s", 0)).
  364. // OrCond(cond.And("T_online", 0).And("T_online_s", 2))
  365. //} else if T_online == "0" {
  366. // cond1 = cond1.And("T_online", 0).And("T_online_s", 0)
  367. //}
  368. // 0 未监控 1 监控中 2 无网络 3 全部
  369. if T_online == "1" {
  370. cond1 = cond1.And("T_monitor", 1)
  371. } else if T_online == "2" {
  372. cond1 = cond1.AndCond(cond.AndCond(cond.AndNot("T_online", 1)).
  373. AndCond(cond.AndNot("T_online_s", 1)))
  374. } else if T_online == "0" {
  375. cond1 = cond1.And("T_monitor", 0)
  376. }
  377. cnt, err := qs.SetCond(cond1).Count()
  378. if err != nil {
  379. logs.Error(lib.FuncName(), err)
  380. return
  381. }
  382. return cnt
  383. }
  384. func Read_User_Unbind_Device_List(admin *Account.Admin, bindSN []string, T_pid int, T_name string, page int, page_z int) (r []Device_R, cnt int64) {
  385. o := orm.NewOrm()
  386. // 也可以直接使用 Model 结构体作为表名
  387. qs := o.QueryTable(new(Device))
  388. var offset int64
  389. if page <= 1 {
  390. offset = 0
  391. } else {
  392. offset = int64((page - 1) * page_z)
  393. }
  394. cond := orm.NewCondition()
  395. cond1 := cond.And("T_pid", T_pid)
  396. if len(bindSN) > 0 {
  397. cond1 = cond1.AndNot("T_sn__in", bindSN)
  398. }
  399. // 非内部权限 - 不可查看屏蔽数据
  400. if admin.T_pid > 0 {
  401. if len(T_name) >= 16 {
  402. cond1 = cond1.And("T_sn", T_name)
  403. } else if len(T_name) > 0 && len(T_name) < 16 {
  404. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name)).And("T_State", 1)
  405. } else {
  406. cond1 = cond1.And("T_State", 1)
  407. }
  408. } else {
  409. if len(T_name) >= 16 {
  410. cond1 = cond1.And("T_sn", T_name)
  411. } else if len(T_name) > 0 && len(T_name) < 16 {
  412. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name))
  413. }
  414. }
  415. var rx []Device
  416. var err error
  417. if page_z == 9999 {
  418. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("CreateTime").All(&rx)
  419. } else {
  420. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("CreateTime").All(&rx)
  421. }
  422. if err != nil {
  423. logs.Error(lib.FuncName(), err)
  424. return
  425. }
  426. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  427. if err != nil {
  428. logs.Error(lib.FuncName(), err)
  429. return
  430. }
  431. for _, v := range rx {
  432. r = append(r, DeviceToDevice_R(v))
  433. }
  434. return r, cnt
  435. }
  436. func Read_Device_List_ByT_State(bindSN []string, T_pid int, T_State int) (r []Device, cnt int64) {
  437. o := orm.NewOrm()
  438. // 也可以直接使用 Model 结构体作为表名
  439. qs := o.QueryTable(new(Device))
  440. cond := orm.NewCondition()
  441. cond1 := cond.And("T_pid", T_pid).And("T_State", T_State)
  442. if len(bindSN) > 0 {
  443. cond1 = cond1.And("T_sn__in", bindSN)
  444. }
  445. var rx []Device
  446. var err error
  447. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  448. if err != nil {
  449. logs.Error(lib.FuncName(), err)
  450. return
  451. }
  452. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("CreateTime").All(&rx)
  453. if err != nil {
  454. logs.Error(lib.FuncName(), err)
  455. return
  456. }
  457. return rx, cnt
  458. }