Device.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. }
  50. func DeviceToDevice_R(t Device, T_id string) (r Device_R) {
  51. r.Id = t.Id
  52. r.T_sn = t.T_sn
  53. r.T_id = T_id
  54. r.T_t = t.T_t
  55. r.T_rh = t.T_rh
  56. if !t.T_time.IsZero() {
  57. r.T_time = t.T_time.Format("2006-01-02 15:04:05")
  58. }
  59. r.T_MSISDN = t.T_MSISDN
  60. r.T_version = t.T_version
  61. r.T_signal = t.T_signal
  62. r.T_electric = t.T_electric
  63. r.T_up_time_interval = t.T_up_time_interval
  64. r.T_note_time_interval = t.T_note_time_interval
  65. r.T_note_file_num = t.T_note_file_num
  66. r.T_State = t.T_State
  67. return r
  68. }
  69. func (t *Device) TableName() string {
  70. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  71. }
  72. var redisCache_Device cache.Cache
  73. func init() {
  74. //注册模型
  75. orm.RegisterModel(new(Device))
  76. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  77. "redis_"+"Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  78. logs.Println(config)
  79. var err error
  80. redisCache_Device, err = cache.NewCache("redis", config)
  81. if err != nil || redisCache_Device == nil {
  82. errMsg := "failed to init redis"
  83. logs.Println(errMsg, err)
  84. }
  85. }
  86. // ---------------- Redis -------------------
  87. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  88. func Redis_Device_Set(key string, r Device) (err error) {
  89. //json序列化
  90. str, err := json.Marshal(r)
  91. if err != nil {
  92. logs.Error(lib.FuncName(), err)
  93. return
  94. }
  95. err = redisCache_Device.Put(key, str, 24*time.Hour)
  96. if err != nil {
  97. logs.Println("set key:", key, ",value:", str, err)
  98. }
  99. return
  100. }
  101. // if r,is :=Redis_Get(T_sn);is{
  102. // return r,nil
  103. // }
  104. func Redis_Device_Get(key string) (r Device, is bool) {
  105. if redisCache_Device.IsExist(key) {
  106. logs.Println("找到key:", key)
  107. v := redisCache_Device.Get(key)
  108. json.Unmarshal(v.([]byte), &r)
  109. return r, true
  110. }
  111. logs.Println("没有 找到key:", key)
  112. return Device{}, false
  113. }
  114. func Redis_Device_DelK(key string) (err error) {
  115. err = redisCache_Device.Delete(key)
  116. if err != nil {
  117. logs.Error(lib.FuncName(), err)
  118. }
  119. return
  120. }
  121. // ---------------- 特殊方法 -------------------
  122. // 获取 ById
  123. func Read_Device_ById(id int) (r Device, is bool) {
  124. o := orm.NewOrm()
  125. r = Device{Id: id}
  126. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  127. if err != nil {
  128. logs.Error(lib.FuncName(), err)
  129. return r, false
  130. }
  131. return r, true
  132. }
  133. // 获取 By
  134. func Read_Device(T_sn string) (r Device, is bool) {
  135. if r, is = Redis_Device_Get(T_sn); is == true {
  136. return r, true
  137. }
  138. o := orm.NewOrm()
  139. qs := o.QueryTable(new(Device))
  140. err := qs.Filter("T_sn", T_sn).One(&r)
  141. if err != nil {
  142. logs.Println(lib.FuncName(), err)
  143. return r, false
  144. }
  145. Redis_Device_Set(T_sn, r)
  146. return r, true
  147. }
  148. // 添加
  149. func Add_Device(r Device) (id int64, is bool) {
  150. o := orm.NewOrm()
  151. id, err := o.Insert(&r)
  152. if err != nil {
  153. logs.Error(lib.FuncName(), err)
  154. return 0, false
  155. }
  156. CREATE_DeviceData(r.T_sn)
  157. Redis_Device_Set(r.T_sn, r)
  158. return id, true
  159. }
  160. // 删除
  161. func Delete_Device(v Device) bool {
  162. o := orm.NewOrm()
  163. if num, err := o.Delete(&v); err == nil {
  164. logs.Println("Number of records deleted in database:", num)
  165. } else {
  166. logs.Println(lib.FuncName(), err)
  167. return false
  168. }
  169. Redis_Device_DelK(v.T_sn)
  170. return true
  171. }
  172. // 删除
  173. func Delete_Device_(v Device) bool {
  174. o := orm.NewOrm()
  175. v.T_State = 0
  176. if num, err := o.Update(&v, "T_State"); err == nil {
  177. logs.Println("Number of records updated in database:", num)
  178. } else {
  179. logs.Println(lib.FuncName(), err)
  180. return false
  181. }
  182. Redis_Device_DelK(v.T_sn)
  183. return true
  184. }
  185. // 修改
  186. func Update_Device(m Device, cols ...string) bool {
  187. o := orm.NewOrm()
  188. if num, err := o.Update(&m, cols...); err == nil {
  189. logs.Println("Number of records updated in database:", num)
  190. Redis_Device_Set(m.T_sn, m)
  191. return true
  192. } else {
  193. logs.Println(lib.FuncName(), err)
  194. }
  195. return false
  196. }
  197. // 获取列表
  198. func Read_Device_List(T_sn string, T_MSISDN string, page int, page_z int) (r []Device_R, cnt int64) {
  199. o := orm.NewOrm()
  200. // 也可以直接使用 Model 结构体作为表名
  201. qs := o.QueryTable(new(Device))
  202. var offset int64
  203. var map_r []Device
  204. if page <= 1 {
  205. offset = 0
  206. } else {
  207. offset = int64((page - 1) * page_z)
  208. }
  209. cond := orm.NewCondition()
  210. 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)
  211. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  212. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  213. for _, v := range map_r {
  214. r = append(r, DeviceToDevice_R(v, "0"))
  215. }
  216. return r, cnt
  217. }