DeviceSensorParameter.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package Device
  2. import (
  3. "Cold_mqtt/conf"
  4. "Cold_mqtt/logs"
  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. T_enprelnote int `orm:"size(2);default(1)"` // 是否启用预警记录
  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. func (t *DeviceSensorParameter) TableName() string {
  47. return "device_sensor_parameter" // 数据库名称 // ************** 替换 FormulaList **************
  48. }
  49. var redisCache_DeviceSensorParameter cache.Cache
  50. func init() {
  51. //注册模型
  52. orm.RegisterModel(new(DeviceSensorParameter))
  53. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  54. "redis_DeviceSensorParameter", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  55. logs.Println(config)
  56. var err error
  57. redisCache_DeviceSensorParameter, err = cache.NewCache("redis", config)
  58. if err != nil || redisCache_DeviceSensorParameter == nil {
  59. errMsg := "failed to init redis"
  60. logs.Println(errMsg, err)
  61. }
  62. }
  63. // ---------------- Redis -------------------
  64. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  65. func Redis_DeviceSensorParameter_Set(key string, r DeviceSensorParameter) (err error) {
  66. //json序列化
  67. str, err := json.Marshal(r)
  68. if err != nil {
  69. logs.PrintlnError("Redis_DeviceSensorParameter_Set", err)
  70. return
  71. }
  72. err = redisCache_DeviceSensorParameter.Put(key, str, 8*time.Hour)
  73. if err != nil {
  74. logs.Println("set key:", key, ",value:", str, err)
  75. }
  76. return
  77. }
  78. // if r,is :=Redis_Get(T_sn);is{
  79. // return r,nil
  80. // }
  81. func Redis_DeviceSensorParameter_Get(key string) (r DeviceSensorParameter, is bool) {
  82. if redisCache_DeviceSensorParameter.IsExist(key) {
  83. //println("找到key:",key)
  84. v := redisCache_DeviceSensorParameter.Get(key)
  85. json.Unmarshal(v.([]byte), &r)
  86. return r, true
  87. }
  88. //println("没有 找到key:",key)
  89. return DeviceSensorParameter{}, false
  90. }
  91. // ---------------- 特殊方法 -------------------
  92. func Add_DeviceSensorParameter_fun(Devicesensorparameter DeviceSensorParameter) int {
  93. Devicesensorparameter_r, is := Read_DeviceSensorParameter(Devicesensorparameter.T_sn, Devicesensorparameter.T_id)
  94. if is {
  95. if Devicesensorparameter_r.T_name != Devicesensorparameter.T_name ||
  96. Devicesensorparameter_r.T_Tlower != Devicesensorparameter.T_Tlower ||
  97. Devicesensorparameter_r.T_Tupper != Devicesensorparameter.T_Tupper ||
  98. Devicesensorparameter_r.T_RHlower != Devicesensorparameter.T_RHlower ||
  99. Devicesensorparameter_r.T_RHupper != Devicesensorparameter.T_RHupper ||
  100. Devicesensorparameter_r.T_enprel != Devicesensorparameter.T_enprel ||
  101. Devicesensorparameter_r.T_tprel != Devicesensorparameter.T_tprel ||
  102. Devicesensorparameter_r.T_tpreu != Devicesensorparameter.T_tpreu ||
  103. Devicesensorparameter_r.T_hprel != Devicesensorparameter.T_hprel ||
  104. Devicesensorparameter_r.T_hpreu != Devicesensorparameter.T_hpreu ||
  105. Devicesensorparameter_r.T_enprelnote != Devicesensorparameter.T_enprelnote ||
  106. Devicesensorparameter_r.T_en != Devicesensorparameter.T_en ||
  107. Devicesensorparameter_r.T_free != Devicesensorparameter.T_free {
  108. // 添加记录
  109. id, is := Add_DeviceSensorParameter(Devicesensorparameter)
  110. if !is {
  111. logs.PrintlnError("Add_DeviceSensorParameter_fun", "插入失败 [DeviceParameter]")
  112. }
  113. return int(id)
  114. }
  115. }
  116. //else {
  117. // // 添加记录
  118. // id, is := Add_DeviceSensorParameter(Devicesensorparameter)
  119. // if !is {
  120. // logs.PrintlnError("Add_DeviceSensorParameter_fun", "插入失败 [DeviceParameter]")
  121. // }
  122. // return int(id), true
  123. //}
  124. return Devicesensorparameter_r.Id
  125. }
  126. // 添加
  127. func Add_DeviceSensorParameter(m DeviceSensorParameter) (int64, bool) {
  128. o := orm.NewOrm()
  129. m.Id = 0
  130. key := m.T_sn + "|" + strconv.Itoa(m.T_id)
  131. m.T_SendState = 0
  132. m.T_State = 1
  133. id, err := o.Insert(&m)
  134. if err != nil {
  135. logs.PrintlnError("Add_DeviceSensorParameter", err)
  136. return id, false
  137. }
  138. Redis_DeviceSensorParameter_Set(key, m)
  139. return id, true
  140. }
  141. // 获取设备参数
  142. func Read_DeviceSensorParameter(T_sn string, T_id int) (t DeviceSensorParameter, bool bool) {
  143. key := T_sn + "|" + strconv.Itoa(T_id)
  144. if t, is := Redis_DeviceSensorParameter_Get(key); is {
  145. return t, true
  146. }
  147. o := orm.NewOrm()
  148. qs := o.QueryTable(new(DeviceSensorParameter))
  149. cond := orm.NewCondition()
  150. cond = cond.And("T_sn", T_sn).And("T_id", T_id).And("T_State", 1)
  151. qs.SetCond((*orm2.Condition)(cond)).OrderBy("-UpdateTime").One(&t)
  152. // 判断是否有数据
  153. if t.Id == 0 {
  154. return t, false
  155. }
  156. Redis_DeviceSensorParameter_Set(key, t)
  157. return t, true
  158. }
  159. // 修改
  160. func Update_DeviceSensorParameter(r DeviceSensorParameter, cols ...string) bool {
  161. o := orm.NewOrm()
  162. if _, err := o.Update(&r, cols...); err == nil {
  163. //logs.Println("Number of records updated in database:", num)
  164. return true
  165. }
  166. return false
  167. }
  168. // 获取最新数据
  169. func UPDATE_DeviceSensorParamete_SN_T_SendState_2_sql(T_sn string, T_id string) {
  170. o := orm.NewOrm()
  171. res, err := o.Raw("UPDATE device_sensor_parameter SET `t__send_state` = 3 WHERE `t_sn` LIKE '%" + T_sn + "%' AND `t_id` = " + T_id + " AND `t__send_state` = '0' AND `t__state` = '2'").Exec()
  172. if err == nil {
  173. num, _ := res.RowsAffected()
  174. logs.Println("mysql row affected nums: ", num)
  175. }
  176. }
  177. // 获取列表
  178. func Read_DeviceSensorParameter_SN_T_id_Msid(T_sn string, T_Msid int64) (DeviceSensorParameter, bool) {
  179. o := orm.NewOrm()
  180. // 也可以直接使用 Model 结构体作为表名
  181. var r []DeviceSensorParameter
  182. qs := o.QueryTable(new(DeviceSensorParameter))
  183. qs.Filter("T_sn", T_sn).Filter("T_Msid", T_Msid).OrderBy("-Id").All(&r)
  184. if len(r) == 0 {
  185. return DeviceSensorParameter{}, false
  186. }
  187. return r[0], true
  188. }
  189. // 获取最新数据
  190. func Read_DeviceSensorParameter_SN_T_SendState_0_sql(sn string) (maps []orm2.ParamsList) {
  191. o := orm.NewOrm()
  192. sql := "SELECT DISTINCT t_sn,t_id FROM device_sensor_parameter WHERE `t_sn` = '" + sn + "' AND `t__send_state` = '0' AND `t__state` = '2'"
  193. logs.Println(sql)
  194. o.Raw(sql).ValuesList(&maps)
  195. return maps
  196. }
  197. // 获取列表
  198. func Read_DeviceSensorParameter_SN_T_id(T_sn string, T_id int) (r []DeviceSensorParameter) {
  199. o := orm.NewOrm()
  200. // 也可以直接使用 Model 结构体作为表名
  201. qs := o.QueryTable(new(DeviceSensorParameter))
  202. qs.Limit(3, 0).Filter("T_sn", T_sn).Filter("T_id", T_id).Filter("T_State", 2).OrderBy("-Id").All(&r)
  203. return r
  204. }