Device.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package Device
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  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. _ "github.com/go-sql-driver/mysql"
  13. "time"
  14. )
  15. // 模版
  16. type Device struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  19. T_id int `orm:"size(256);null"` // 编号
  20. T_t float32 `orm:"size(10);null"` // 温度
  21. T_rh float32 `orm:"size(10);null"` // 湿度
  22. T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
  23. T_MSISDN string `orm:"size(256);null"` // 物联网卡
  24. T_version int `orm:"size(2);default(1)"` // 版本
  25. T_signal int `orm:"size(2);default(1)"` // 信号强度
  26. T_electric int `orm:"size(2);default(1)"` // 电量百分比
  27. T_up_time_interval int `orm:"size(2);default(1)"` // 上传时间间隔
  28. T_note_time_interval int `orm:"size(2);default(1)"` // 记录时间间隔
  29. T_note_file_num int `orm:"size(2);default(1)"` // 记录文件数量
  30. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  31. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  32. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  33. }
  34. type Device_R struct {
  35. Id int
  36. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  37. T_id string // 编号
  38. T_t float32 // 温度
  39. T_rh float32 // 湿度
  40. T_time string // 采集时间
  41. T_MSISDN string // 物联网卡
  42. T_version int // 版本
  43. T_signal int // 信号强度
  44. T_electric int // 电量百分比
  45. T_up_time_interval int // 上传时间间隔
  46. T_note_time_interval int // 记录时间间隔
  47. T_note_file_num int // 记录文件数量
  48. T_State int // 0 删除 1 正常
  49. UpdateTime string // 采集时间
  50. }
  51. func DeviceToDevice_R(t Device, T_id string) (r Device_R) {
  52. r.Id = t.Id
  53. r.T_sn = t.T_sn
  54. //r.T_id = T_id
  55. r.T_t = t.T_t
  56. r.T_rh = t.T_rh
  57. if !t.T_time.IsZero() {
  58. r.T_time = t.T_time.Format("2006-01-02 15:04:05")
  59. }
  60. r.T_MSISDN = t.T_MSISDN
  61. r.T_version = t.T_version
  62. r.T_signal = t.T_signal
  63. r.T_electric = t.T_electric
  64. r.T_up_time_interval = t.T_up_time_interval
  65. r.T_note_time_interval = t.T_note_time_interval
  66. r.T_note_file_num = t.T_note_file_num
  67. r.T_State = t.T_State
  68. if !t.UpdateTime.IsZero() {
  69. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  70. }
  71. return r
  72. }
  73. func (t *Device) TableName() string {
  74. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  75. }
  76. var redisCache_Device cache.Cache
  77. func init() {
  78. //注册模型
  79. orm.RegisterModel(new(Device))
  80. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  81. "redis_"+"Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  82. logs.Println(config)
  83. var err error
  84. redisCache_Device, err = cache.NewCache("redis", config)
  85. if err != nil || redisCache_Device == nil {
  86. errMsg := "failed to init redis"
  87. logs.Println(errMsg, err)
  88. }
  89. }
  90. // ---------------- Redis -------------------
  91. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  92. func Redis_Device_Set(key string, r Device) (err error) {
  93. //json序列化
  94. str, err := json.Marshal(r)
  95. if err != nil {
  96. logs.Error(lib.FuncName(), err)
  97. return
  98. }
  99. err = redisCache_Device.Put(key, str, 24*time.Hour)
  100. if err != nil {
  101. logs.Println("set key:", key, ",value:", str, err)
  102. }
  103. return
  104. }
  105. // if r,is :=Redis_Get(T_sn);is{
  106. // return r,nil
  107. // }
  108. func Redis_Device_Get(key string) (r Device, is bool) {
  109. if redisCache_Device.IsExist(key) {
  110. logs.Println("找到key:", key)
  111. v := redisCache_Device.Get(key)
  112. json.Unmarshal(v.([]byte), &r)
  113. return r, true
  114. }
  115. logs.Println("没有 找到key:", key)
  116. return Device{}, false
  117. }
  118. func Redis_Device_DelK(key string) (err error) {
  119. err = redisCache_Device.Delete(key)
  120. if err != nil {
  121. logs.Error(lib.FuncName(), err)
  122. }
  123. return
  124. }
  125. // ---------------- 特殊方法 -------------------
  126. // 获取 ById
  127. func Read_Device_ById(id int) (r Device, is bool) {
  128. o := orm.NewOrm()
  129. r = Device{Id: id}
  130. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  131. if err != nil {
  132. logs.Error(lib.FuncName(), err)
  133. return r, false
  134. }
  135. return r, true
  136. }
  137. // 获取 By
  138. func Read_Device(T_sn string) (r Device, is bool) {
  139. if r, is = Redis_Device_Get(T_sn); is == true {
  140. return r, true
  141. }
  142. o := orm.NewOrm()
  143. qs := o.QueryTable(new(Device))
  144. err := qs.Filter("T_sn", T_sn).One(&r)
  145. if err != nil {
  146. logs.Println(lib.FuncName(), err)
  147. return r, false
  148. }
  149. Redis_Device_Set(T_sn, r)
  150. return r, true
  151. }
  152. // 添加
  153. func Add_Device(r Device) (id int64, is bool) {
  154. o := orm.NewOrm()
  155. id, err := o.Insert(&r)
  156. if err != nil {
  157. logs.Error(lib.FuncName(), err)
  158. return 0, false
  159. }
  160. CREATE_DeviceData(r.T_sn)
  161. Redis_Device_Set(r.T_sn, r)
  162. return id, true
  163. }
  164. // 删除
  165. func Delete_Device(v Device) bool {
  166. o := orm.NewOrm()
  167. if num, err := o.Delete(&v); err == nil {
  168. logs.Println("Number of records deleted in database:", num)
  169. } else {
  170. logs.Println(lib.FuncName(), err)
  171. return false
  172. }
  173. Redis_Device_DelK(v.T_sn)
  174. return true
  175. }
  176. // 删除
  177. func Delete_Device_(v Device) bool {
  178. o := orm.NewOrm()
  179. v.T_State = 0
  180. if num, err := o.Update(&v, "T_State"); err == nil {
  181. logs.Println("Number of records updated in database:", num)
  182. } else {
  183. logs.Println(lib.FuncName(), err)
  184. return false
  185. }
  186. Redis_Device_DelK(v.T_sn)
  187. return true
  188. }
  189. // 修改
  190. func Update_Device(m Device, cols ...string) bool {
  191. o := orm.NewOrm()
  192. if num, err := o.Update(&m, cols...); err == nil {
  193. logs.Println("Number of records updated in database:", num)
  194. Redis_Device_Set(m.T_sn, m)
  195. return true
  196. } else {
  197. logs.Println(lib.FuncName(), err)
  198. }
  199. return false
  200. }
  201. // 获取列表
  202. func Read_Device_List(T_sn string, T_MSISDN string, page int, page_z int) (r []Device_R, cnt int64) {
  203. o := orm.NewOrm()
  204. // 也可以直接使用 Model 结构体作为表名
  205. qs := o.QueryTable(new(Device))
  206. var offset int64
  207. var map_r []Device
  208. if page <= 1 {
  209. offset = 0
  210. } else {
  211. offset = int64((page - 1) * page_z)
  212. }
  213. cond := orm.NewCondition()
  214. cond1 := cond.And("T_MSISDN__icontains", T_MSISDN).And("T_sn__icontains", T_sn).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  215. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  216. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  217. for _, v := range map_r {
  218. r = append(r, DeviceToDevice_R(v, "0"))
  219. }
  220. return r, cnt
  221. }