Device.go 14 KB

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