Device.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/logs"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Product"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/astaxie/beego/cache"
  11. _ "github.com/astaxie/beego/cache/redis"
  12. orm2 "github.com/beego/beego/v2/client/orm"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. _ "github.com/go-sql-driver/mysql"
  15. "strconv"
  16. "time"
  17. )
  18. // 设备
  19. type Device struct {
  20. T_sn string `orm:"pk;size(256);null"` // 设备序列号
  21. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  22. T_devName string `orm:"size(256);null"` // 设备名称 20字
  23. T_protocol int `orm:"size(2);default(1)"` // 冷链通讯协议 1 :1.0协议 2 :2.0协议 3 :3.0协议
  24. T_mqttid string `orm:"size(256);null"` // MQTT 服务ID
  25. T_VerifyTime time.Time `orm:"type(timestamp);null"` // 验证时间
  26. T_CalibrationTime time.Time `orm:"type(timestamp);null"` // 校准时间
  27. T_PatrolTime time.Time `orm:"type(timestamp);null"` // 巡检时间
  28. T_ist int `orm:"size(2);default(1)"` // 温度 1开启 2关闭
  29. T_ish int `orm:"size(2);default(1)"` // 湿度 1开启 2关闭
  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(1)"` // 在线状态 0 未启用 1 在线 2 离线
  35. T_online_s int `orm:"index;size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线
  36. T_State int `orm:"index;size(2);default(1)"` // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到)
  37. // 硬件信息
  38. T_model string `orm:"size(200);null"` // KF200BG 设备型号
  39. T_sver string `orm:"size(200);null"` // "1.0.0",//软件版本
  40. T_hver string `orm:"size(200);null"` // "1.0.0",//硬件版本
  41. T_imei string `orm:"size(200);null"` // "867387060327718",//模组imei
  42. T_iccid string `orm:"size(200);null"` // "89860477102170049750",//sim卡号
  43. T_rssi string `orm:"size(200);null"` // "80",//信号强度
  44. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  45. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  46. }
  47. type Device_R struct {
  48. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  49. T_devName string // 设备名称
  50. T_protocol int // 1 1.0协议 2 2.0协议
  51. T_VerifyTime string // 验证时间
  52. T_CalibrationTime string // 校准时间
  53. T_PatrolTime string // 巡检时间
  54. T_ist int // 温度 1开启 2关闭
  55. T_ish int // 湿度 1开启 2关闭
  56. T_Dattery int // 电量
  57. T_Site string // GPS
  58. T_monitor int // 监控状态 0 未监控 1 监控 停止记录
  59. T_online int // 在线状态 0 未启用 1 在线 2 离线
  60. T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线
  61. T_State int // 0 屏蔽 1 正常
  62. // 硬件信息
  63. T_model string // KF200BG 产品型号
  64. T_ProductTypeName string // 验证工具LoRa 产品统称 + 类型
  65. T_sver string // "1.0.0",//软件版本
  66. T_hver string // "1.0.0",//硬件版本
  67. T_imei string // "867387060327718",//模组imei
  68. T_iccid string // "89860477102170049750",//sim卡号
  69. T_rssi string // "80",//信号强度
  70. CreateTime string //auto_now_add 第一次保存时才设置时间
  71. UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新
  72. T_DeviceSensor_Num int // 传感器数量
  73. }
  74. type Device_task struct {
  75. T_sn string
  76. T_task string
  77. }
  78. func (t *Device) TableName() string {
  79. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  80. }
  81. var redisCache_Device cache.Cache
  82. func init() {
  83. //注册模型
  84. orm.RegisterModel(new(Device))
  85. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  86. "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  87. fmt.Println(config)
  88. var err error
  89. redisCache_Device, err = cache.NewCache("redis", config)
  90. if err != nil || redisCache_Device == nil {
  91. errMsg := "failed to init redis"
  92. logs.Error(errMsg, err)
  93. panic(errMsg)
  94. }
  95. }
  96. func DeviceToDevice_R(r Device) (t Device_R) {
  97. t.T_sn = r.T_sn
  98. t.T_devName = r.T_devName
  99. t.T_protocol = r.T_protocol
  100. if !r.T_VerifyTime.IsZero() {
  101. t.T_VerifyTime = r.T_VerifyTime.Format("2006-01-02 15:04:05")
  102. }
  103. if !r.T_CalibrationTime.IsZero() {
  104. t.T_CalibrationTime = r.T_CalibrationTime.Format("2006-01-02 15:04:05")
  105. }
  106. if !r.T_PatrolTime.IsZero() {
  107. t.T_PatrolTime = r.T_PatrolTime.Format("2006-01-02 15:04:05")
  108. }
  109. t.T_ist = r.T_ist
  110. t.T_ish = r.T_ish
  111. t.T_Dattery = r.T_Dattery
  112. t.T_Site = r.T_Site
  113. t.T_monitor = r.T_monitor
  114. t.T_online = r.T_online
  115. t.T_online_s = r.T_online_s
  116. if r.T_online == 2 && (r.T_online_s == 0 || r.T_online_s == 2) && r.T_monitor == 1 {
  117. t.T_monitor = 2
  118. }
  119. t.T_State = r.T_State
  120. t.T_model = r.T_model
  121. t.T_ProductTypeName = Product.Read_ProductType_Get(r.T_model)
  122. t.T_sver = r.T_sver
  123. t.T_hver = r.T_hver
  124. t.T_imei = r.T_imei
  125. t.T_iccid = r.T_iccid
  126. t.T_rssi = r.T_rssi
  127. t.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  128. t.UpdateTime = r.UpdateTime.Format("2006-01-02 15:04:05")
  129. t.T_DeviceSensor_Num = Read_DeviceSensor_Num_ByT_sn(r.T_sn)
  130. return
  131. }
  132. // ---------------- Redis -------------------
  133. // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
  134. func Redis_Device_Set(r Device) (err error) {
  135. //json序列化
  136. str, err := json.Marshal(r)
  137. if err != nil {
  138. logs.Error(lib.FuncName(), err)
  139. return
  140. }
  141. err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour)
  142. if err != nil {
  143. logs.Error("set key:", r.T_sn, ",value:", str, err)
  144. }
  145. return
  146. }
  147. // if r,is :=Redis_Device_Get(T_sn);is{
  148. // return r,nil
  149. // }
  150. func Redis_Device_Get(key string) (r Device, is bool) {
  151. if redisCache_Device.IsExist(key) {
  152. //println("找到key:",key)
  153. v := redisCache_Device.Get(key)
  154. err := json.Unmarshal(v.([]byte), &r)
  155. if err != nil {
  156. logs.Error(lib.FuncName(), err)
  157. return Device{}, false
  158. }
  159. return r, true
  160. }
  161. return Device{}, false
  162. }
  163. func Redis_Device_DelK(key string) (err error) {
  164. err = redisCache_Device.Delete(key)
  165. if err != nil {
  166. logs.Error(lib.FuncName(), err)
  167. return
  168. }
  169. return
  170. }
  171. // ---------------- 特殊方法 -------------------
  172. // 获取 ById
  173. func Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  174. if r, is := Redis_Device_Get(T_sn); is {
  175. //println("Redis_Device_Get OK")
  176. return r, nil
  177. }
  178. //println("没有 Redis_Device_Get SN")
  179. o := orm.NewOrm()
  180. r = Device{T_sn: T_sn}
  181. err = o.Read(&r, "T_sn") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  182. if err != nil {
  183. logs.Error(lib.FuncName(), err)
  184. return r, err
  185. }
  186. Redis_Device_Set(r) // Redis 更新缓存
  187. return r, nil
  188. }
  189. // 添加
  190. func Add_Device(m Device) (err error) {
  191. o := orm.NewOrm()
  192. _, err = o.Insert(&m)
  193. if err != nil {
  194. logs.Error(lib.FuncName(), err)
  195. return err
  196. }
  197. Redis_Device_Set(m) // Redis 更新缓存
  198. return nil
  199. }
  200. // 删除
  201. func Delete_Device(m Device) (err error) {
  202. o := orm.NewOrm()
  203. _, err = o.Delete(&m)
  204. if err != nil {
  205. logs.Error(lib.FuncName(), err)
  206. return err
  207. }
  208. Redis_Device_DelK(m.T_sn)
  209. return
  210. }
  211. // 修改
  212. func Update_Device(r Device, cols ...string) bool {
  213. o := orm.NewOrm()
  214. num, err := o.Update(&r, cols...)
  215. if err != nil {
  216. logs.Error(lib.FuncName(), err)
  217. return false
  218. }
  219. fmt.Println("Number of records updated in database:", num)
  220. Redis_Device_Set(r) // Redis 更新缓存
  221. return true
  222. }
  223. // 获取列表
  224. func Read_Device_List(admin *Account.Admin, T_pid int, T_name string, T_monitor string, T_online string, page int, page_z int) (r []Device_R, cnt int64) {
  225. o := orm.NewOrm()
  226. // 也可以直接使用 Model 结构体作为表名
  227. qs := o.QueryTable(new(Device))
  228. var offset int64
  229. if page <= 1 {
  230. offset = 0
  231. } else {
  232. offset = int64((page - 1) * page_z)
  233. }
  234. cond := orm.NewCondition()
  235. cond1 := cond.And("T_pid", T_pid)
  236. // 非内部权限 - 不可查看屏蔽数据
  237. if admin.T_pid > 0 {
  238. cond1 = cond1.And("T_State", 1)
  239. }
  240. if len(T_name) > 0 {
  241. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).Or("T_devName__icontains", T_name))
  242. }
  243. if len(T_monitor) > 0 {
  244. T_monitor_int, err := strconv.Atoi(T_monitor)
  245. if err == nil {
  246. cond1 = cond1.AndCond(cond.And("T_monitor", T_monitor_int))
  247. }
  248. }
  249. //0 未启用 1 在线 2 离线
  250. if T_online == "1" {
  251. cond1 = cond1.AndCond(cond.And("T_online", 1))
  252. } else if T_online == "2" {
  253. cond1 = cond1.AndCond(cond.And("T_online", 2))
  254. } else if T_online == "0" {
  255. cond1 = cond1.AndCond(cond.And("T_online", 0).Or("T_online", 2))
  256. }
  257. var rx []Device
  258. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("CreateTime").All(&rx)
  259. if err != nil {
  260. logs.Error(lib.FuncName(), err)
  261. return
  262. }
  263. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  264. if err != nil {
  265. logs.Error(lib.FuncName(), err)
  266. return
  267. }
  268. for _, v := range rx {
  269. r = append(r, DeviceToDevice_R(v))
  270. }
  271. return r, cnt
  272. }
  273. func Read_Device_ALL_T_Type_Count(T_pid int, T_type int) (cnt int64) {
  274. o := orm.NewOrm()
  275. qs := o.QueryTable(new(Device))
  276. cnt, _ = qs.Filter("T_pid", T_pid).Filter("T_type", T_type).Filter("T_State", 1).Count()
  277. return cnt
  278. }
  279. func Read_Device_List_ByT_model(T_model string) (r []Device) {
  280. o := orm.NewOrm()
  281. // 也可以直接使用 Model 结构体作为表名
  282. qs := o.QueryTable(new(Device))
  283. _, err := qs.Filter("T_model", T_model).All(&r)
  284. if err != nil {
  285. logs.Error(lib.FuncName(), err)
  286. return
  287. }
  288. return r
  289. }