Device.go 8.9 KB

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