DeviceSensorParameter.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package Device
  2. import (
  3. "ColdP_server/conf"
  4. "ColdP_server/models"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "strconv"
  13. "time"
  14. )
  15. // 模板
  16. type DeviceSensorParameter struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_sn string `orm:"index;size(256);"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  19. T_id int `orm:"index;size(11);"` // 传感器编号
  20. T_name string `orm:"size(256);"` //传感器名称
  21. // 报警
  22. T_Tlower float32 `orm:"size(200);"` // 温度下限
  23. T_Tupper float32 `orm:"size(200);"` // 温度上限
  24. T_RHlower float32 `orm:"size(200);"` // 湿度下限
  25. T_RHupper float32 `orm:"size(200);"` // 湿度上限
  26. // 预警
  27. T_enprel int `orm:"size(2);default(1)"` // 是否启用预警
  28. T_tprel float32 `orm:"size(200);"` // 温度预警下限
  29. T_tpreu float32 `orm:"size(200);"` // 温度预警上限
  30. T_hprel float32 `orm:"size(200);"` // 湿度预警下限
  31. T_hpreu float32 `orm:"size(200);"` // 温度预警上限
  32. //
  33. //T_speed int `orm:"size(5);"` // 传感器采样率 s(1~240) 默认:15 *
  34. //T_sense int `orm:"size(5);"` // 传感器灵敏度 s(0~10) 默认:5
  35. T_en int `orm:"size(2);default(1)"` // en:是否启用传感器, 0 停用 1 启用
  36. T_free int `orm:"size(2);default(1)"` // 是否为闲置状态 1:启用(正常 实时、记录, 不 报警、预警)
  37. //0:关闭(正常 实时、记录、报警、预警) 默认:0
  38. // 逻辑字段
  39. T_uuid string `orm:"size(256);"` //处理 人员
  40. T_Msid int64 `orm:"size(50);default(0)"` // 消息识别ID
  41. T_SendState int `orm:"size(2);default(0)"` // 发送状态 0 待发送 1 发送成功 2 失败 3 覆盖
  42. T_State int `orm:"size(2);default(1)"` // 1 系统获取 2 用户提交
  43. CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"` //auto_now_add 第一次保存时才设置时间
  44. UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  45. }
  46. type DeviceSensorParameter_R struct {
  47. // 报警
  48. T_Tlower float32 // 温度下限
  49. T_Tupper float32 // 温度上限
  50. T_RHlower float32 // 湿度下限
  51. T_RHupper float32 // 湿度上限
  52. // 预警
  53. T_enprel int // 是否启用预警
  54. T_tprel float32 // 温度预警下限
  55. T_tpreu float32 // 温度预警上限
  56. T_hprel float32 // 湿度预警下限
  57. T_hpreu float32 // 温度预警上限
  58. T_en int // en:是否启用传感器,
  59. T_free int // free:监测点是否为闲置状态(空库,只监测不报警)
  60. T_time models.JsonTime
  61. }
  62. type DeviceSensorParameter_M struct {
  63. T_name string // 标题
  64. // 报警
  65. T_sp int // 传感器编号
  66. T_Tlower float32 // 温度下限
  67. T_Tupper float32 // 温度上限
  68. T_RHlower float32 // 湿度下限
  69. T_RHupper float32 // 湿度上限
  70. }
  71. func DeviceSensorParameterTo_DeviceSensorParameter_R(r DeviceSensorParameter) (t DeviceSensorParameter_R) {
  72. t.T_Tlower = r.T_Tlower
  73. t.T_Tupper = r.T_Tupper
  74. t.T_RHlower = r.T_RHlower
  75. t.T_RHupper = r.T_RHupper
  76. t.T_enprel = r.T_enprel
  77. t.T_tprel = r.T_tprel
  78. t.T_tpreu = r.T_tpreu
  79. t.T_hprel = r.T_hprel
  80. t.T_hpreu = r.T_hpreu
  81. t.T_en = r.T_en
  82. t.T_free = r.T_free
  83. t.T_time.Time = r.CreateTime
  84. return t
  85. }
  86. func (t *DeviceSensorParameter) TableName() string {
  87. return "device_sensor_parameter" // 数据库名称 // ************** 替换 FormulaList **************
  88. }
  89. var redisCache_DeviceSensorParameter cache.Cache
  90. func init() {
  91. //注册模型
  92. orm.RegisterModel(new(DeviceSensorParameter))
  93. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  94. "redis_DeviceSensorParameter", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  95. fmt.Println(config)
  96. var err error
  97. redisCache_DeviceSensorParameter, err = cache.NewCache("redis", config)
  98. if err != nil || redisCache_DeviceSensorParameter == nil {
  99. errMsg := "failed to init redis"
  100. fmt.Println(errMsg, err)
  101. }
  102. }
  103. // ---------------- Redis -------------------
  104. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  105. func Redis_DeviceSensorParameter_Set(key string, r DeviceSensorParameter_R) (err error) {
  106. //json序列化
  107. str, err := json.Marshal(r)
  108. if err != nil {
  109. fmt.Print(err)
  110. return
  111. }
  112. err = redisCache_DeviceSensorParameter.Put(key, str, 8*time.Hour)
  113. if err != nil {
  114. fmt.Println("set key:", key, ",value:", str, err)
  115. }
  116. return
  117. }
  118. // if r,is :=Redis_Get(T_sn);is{
  119. // return r,nil
  120. // }
  121. func Redis_DeviceSensorParameter_Get(key string) (r DeviceSensorParameter_R, is bool) {
  122. if redisCache_DeviceSensorParameter.IsExist(key) {
  123. //println("找到key:",key)
  124. v := redisCache_DeviceSensorParameter.Get(key)
  125. json.Unmarshal(v.([]byte), &r)
  126. return r, true
  127. }
  128. //println("没有 找到key:",key)
  129. return DeviceSensorParameter_R{}, false
  130. }
  131. // ---------------- 特殊方法 -------------------
  132. // 获取设备参数
  133. func Read_V2DeviceSensorParameter(T_sn string, T_id int) (t DeviceSensorParameter_R, bool bool) {
  134. key := T_sn + "|" + strconv.Itoa(T_id)
  135. if t, is := Redis_DeviceSensorParameter_Get(key); is {
  136. return t, true
  137. }
  138. o := orm.NewOrm()
  139. qs := o.QueryTable(new(DeviceSensorParameter))
  140. var r []DeviceSensorParameter
  141. cond := orm.NewCondition()
  142. cond = cond.And("T_sn", T_sn).And("T_id", T_id).AndCond(cond.And("T_State", 1).OrCond(cond.And("T_State", 2).And("T_SendState", 1)))
  143. qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-UpdateTime").All(&r)
  144. // 判断是否有数据
  145. if len(r) == 0 {
  146. return t, false
  147. }
  148. return DeviceSensorParameterTo_DeviceSensorParameter_R(r[0]), true
  149. }
  150. // 获取列表 T_State 1 系统获取 2 用户提交
  151. func Read_V2DeviceSensorParameter_List(T_sn string, T_id int, T_State int, page_z int) (r []DeviceSensorParameter) {
  152. o := orm.NewOrm()
  153. // 也可以直接使用 Model 结构体作为表名
  154. qs := o.QueryTable(new(DeviceSensorParameter))
  155. cond := orm.NewCondition()
  156. cond = cond.And("T_sn", T_sn)
  157. cond = cond.And("T_id", T_id)
  158. if T_State != 0 {
  159. cond = cond.And("T_State", T_State)
  160. }
  161. qs.Limit(page_z, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  162. return r
  163. }
  164. // 添加
  165. func Add_DeviceSensorParameter(m DeviceSensorParameter) (int64, bool) {
  166. o := orm.NewOrm()
  167. id, err := o.Insert(&m)
  168. if err != nil {
  169. fmt.Println(err)
  170. return 0, false
  171. }
  172. return id, true
  173. }
  174. // 修改
  175. func Update_DeviceSensorParameter(r DeviceSensorParameter, cols ...string) bool {
  176. o := orm.NewOrm()
  177. if num, err := o.Update(&r, cols...); err == nil {
  178. fmt.Println("Number of records updated in database:", num)
  179. return true
  180. }
  181. return false
  182. }
  183. func DELETE_DeviceSensorParameter(SN string) bool {
  184. sql := "DELETE FROM `cold`.`device_sensor_parameter` WHERE `t_sn` = '" + SN + "' "
  185. o := orm.NewOrm()
  186. _, err := o.Raw(sql).Exec()
  187. if err != nil {
  188. return false
  189. }
  190. return true
  191. }
  192. // 获取列表 T_State 1 系统获取 2 用户提交
  193. func Read_DeviceSensorParameter_Map_All(T_sn string, T_id int) (maps map[int]DeviceSensorParameter_M) {
  194. o := orm.NewOrm()
  195. // 也可以直接使用 Model 结构体作为表名
  196. var list []DeviceSensorParameter
  197. qs := o.QueryTable(new(DeviceSensorParameter))
  198. cond := orm.NewCondition()
  199. cond = cond.And("T_sn", T_sn).And("T_id", T_id).AndCond(cond.And("T_State", 1).OrCond(cond.And("T_State", 2).And("T_SendState", 1)))
  200. qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&list)
  201. //sql := fmt.Sprintf("SELECT * FROM `device_sensor_parameter` WHERE t_sn='%s' and t_id =%s AND update_time < '2024-09-12 10:00:00' ORDER BY update_time DESC LIMIT 1", T_sn, T_id)
  202. maps = make(map[int]DeviceSensorParameter_M)
  203. for _, v := range list {
  204. maps[v.Id] = DeviceSensorParameter_M{
  205. T_sp: v.Id,
  206. T_name: v.T_name,
  207. T_Tlower: v.T_Tlower,
  208. T_Tupper: v.T_Tupper,
  209. T_RHlower: v.T_RHlower,
  210. T_RHupper: v.T_RHupper,
  211. }
  212. }
  213. return maps
  214. }