DeviceSensorParameter.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. "github.com/beego/beego/v2/core/logs"
  13. _ "github.com/go-sql-driver/mysql"
  14. "strconv"
  15. "sync"
  16. "time"
  17. )
  18. // 模板
  19. type DeviceSensorParameter struct {
  20. Id int `orm:"column(ID);size(11);auto;pk"`
  21. T_sn string `orm:"index;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  22. T_id int `orm:"index;size(11);null"` // 传感器编号
  23. T_name string `orm:"size(256);null"` // 标题
  24. // 报警
  25. T_Tlower float32 `orm:"size(200);null"` // 温度下限
  26. T_Tupper float32 `orm:"size(200);null"` // 温度上限
  27. T_RHlower float32 `orm:"size(200);null"` // 湿度下限
  28. T_RHupper float32 `orm:"size(200);null"` // 湿度上限
  29. // 预警
  30. T_enprel int `orm:"size(2);default(1)"` // 是否启用预警 1开启 0 关闭
  31. T_tprel float32 `orm:"size(200);null"` // 温度预警下限
  32. T_tpreu float32 `orm:"size(200);null"` // 温度预警上限
  33. T_hprel float32 `orm:"size(200);null"` // 湿度预警下限
  34. T_hpreu float32 `orm:"size(200);null"` // 温度预警上限
  35. T_enprelnote int `orm:"size(2);default(1)"` // 是否启用预警记录数据
  36. //T_speed int `orm:"size(5);null"` // 传感器采样率 s(1~240) 默认:15 *
  37. //T_sense int `orm:"size(5);"` // 传感器灵敏度 s(0~10) 默认:5
  38. T_en int `orm:"size(2);default(1)"` // en:是否启用传感器, 0 停用 1 启用
  39. T_free int `orm:"size(2);default(1)"` // free:监测点是否为闲置状态(空库,只监测不报警) 0 正常 1 空库
  40. // 逻辑字段
  41. T_uuid string `orm:"size(256);null"` // 处理 人员
  42. T_Msid int64 `orm:"size(50);default(0)"` // 消息识别ID
  43. T_SendState int `orm:"size(2);default(0)"` // 发送状态 0 待发送 1 发送成功 2 失败 3 覆盖
  44. T_State int `orm:"size(2);default(1)"` // 1 系统获取 2 用户提交
  45. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  46. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  47. }
  48. type DeviceSensorParameter_R struct {
  49. Id int
  50. T_name string
  51. // 报警
  52. T_Tlower float32 // 温度下限
  53. T_Tupper float32 // 温度上限
  54. T_RHlower float32 // 湿度下限
  55. T_RHupper float32 // 湿度上限
  56. // 预警
  57. T_enprel int // 是否启用预警
  58. T_tprel float32 // 温度预警下限
  59. T_tpreu float32 // 温度预警上限
  60. T_hprel float32 // 湿度预警下限
  61. T_hpreu float32 // 温度预警上限
  62. T_enprelnote int // 预警记录数据
  63. //T_speed int // 传感器采样率 s(1~240) 默认:15 *
  64. //T_sense int // 传感器灵敏度 s(0~10) 默认:5
  65. T_en int // en:是否启用传感器,
  66. T_free int // free:监测点是否为闲置状态(空库,只监测不报警)
  67. T_time models.JsonTime
  68. }
  69. type DeviceSensorParameter_M struct {
  70. T_name string // 标题
  71. // 报警
  72. T_Tlower float32 // 温度下限
  73. T_Tupper float32 // 温度上限
  74. T_RHlower float32 // 湿度下限
  75. T_RHupper float32 // 湿度上限
  76. T_free int
  77. }
  78. type DeviceSensorParameter_T struct {
  79. T_SendState int // 发送状态 0 待发送 1 发送成功 2 失败 3 覆盖
  80. T_text string
  81. }
  82. func DeviceSensorParameterToDeviceSensorParameter_R(r DeviceSensorParameter) (t DeviceSensorParameter_R) {
  83. t.Id = r.Id
  84. t.T_name = r.T_name
  85. t.T_Tlower = r.T_Tlower
  86. t.T_Tupper = r.T_Tupper
  87. t.T_RHlower = r.T_RHlower
  88. t.T_RHupper = r.T_RHupper
  89. t.T_enprel = r.T_enprel
  90. t.T_tprel = r.T_tprel
  91. t.T_tpreu = r.T_tpreu
  92. t.T_hprel = r.T_hprel
  93. t.T_hpreu = r.T_hpreu
  94. t.T_enprelnote = r.T_enprelnote
  95. //t.T_speed = r.T_speed
  96. //t.T_sense = r.T_sense
  97. t.T_en = r.T_en
  98. t.T_free = r.T_free
  99. t.T_time.Time = r.CreateTime
  100. return t
  101. }
  102. func DeviceSensorParameterToDeviceSensorParameter_M(r DeviceSensorParameter) (t DeviceSensorParameter_M) {
  103. t.T_name = r.T_name
  104. t.T_Tlower = r.T_Tlower
  105. t.T_Tupper = r.T_Tupper
  106. t.T_RHlower = r.T_RHlower
  107. t.T_RHupper = r.T_RHupper
  108. t.T_free = r.T_free
  109. return t
  110. }
  111. func DeviceSensorParameterToDeviceSensorParameter_T(r DeviceSensorParameter) (t DeviceSensorParameter_T) {
  112. t.T_text = DeviceSensorParameter_T_text(r)
  113. t.T_SendState = r.T_SendState
  114. return t
  115. }
  116. func DeviceSensorParameter_T_text(r DeviceSensorParameter) string {
  117. s := ""
  118. s += "操作人UUID:" + r.T_uuid
  119. s += " 设备名称:" + r.T_name
  120. s += fmt.Sprintf(" 温度下限:%.2f", r.T_Tlower)
  121. s += fmt.Sprintf(" 温度上限:%.2f", r.T_Tupper)
  122. s += fmt.Sprintf(" 湿度下限:%.2f", r.T_RHlower)
  123. s += fmt.Sprintf(" 湿度上限:%.2f", r.T_RHupper)
  124. s += fmt.Sprintf(" 是否启用预警:%d", r.T_enprel)
  125. s += fmt.Sprintf(" 温度预警下限:%.2f", r.T_tprel)
  126. s += fmt.Sprintf(" 温度预警上限:%.2f", r.T_tpreu)
  127. s += fmt.Sprintf(" 湿度预警下限:%.2f", r.T_hprel)
  128. s += fmt.Sprintf(" 湿度预警上限:%.2f", r.T_hpreu)
  129. s += fmt.Sprintf(" 是否启用预警记录数据:%d", r.T_enprelnote)
  130. //s += fmt.Sprintf(" 传感器采样率:%d", r.T_speed)
  131. //s += fmt.Sprintf(" 传感器灵敏度:%d", r.T_sense)
  132. s += fmt.Sprintf(" 是否启用传感器:%d", r.T_en)
  133. s += fmt.Sprintf(" 监测点是否为闲置状态:%d", r.T_free)
  134. s += " 修改时间:" + r.UpdateTime.Format("2006-01-02 15:04:05")
  135. return s
  136. }
  137. func (t *DeviceSensorParameter) TableName() string {
  138. return "device_sensor_parameter" // 数据库名称 // ************** 替换 FormulaList **************
  139. }
  140. var redisCache_DeviceSensorParameter cache.Cache
  141. // var DeviceSensorParameter_list map[int]DeviceSensorParameter_M // 泛型
  142. var DeviceSensorParameter_list *sync.Map
  143. func init() {
  144. //注册模型
  145. orm.RegisterModel(new(DeviceSensorParameter))
  146. DeviceSensorParameter_list = new(sync.Map)
  147. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  148. "redis_DeviceSensorParameter", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  149. fmt.Println(config)
  150. var err error
  151. redisCache_DeviceSensorParameter, err = cache.NewCache("redis", config)
  152. if err != nil || redisCache_DeviceSensorParameter == nil {
  153. errMsg := "failed to init redis"
  154. logs.Error(errMsg, err)
  155. panic(errMsg)
  156. }
  157. }
  158. // ---------------- Redis -------------------
  159. // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存
  160. func Redis_DeviceSensorParameter_Set(key string, r DeviceSensorParameter) (err error) {
  161. //json序列化
  162. str, err := json.Marshal(r)
  163. if err != nil {
  164. logs.Error(lib.FuncName(), err)
  165. return
  166. }
  167. err = redisCache_DeviceSensorParameter.Put(key, str, 8*time.Hour)
  168. if err != nil {
  169. logs.Error("set key:", key, ",value:", str, err)
  170. }
  171. return
  172. }
  173. // if r,is :=Redis_Device_Get(T_sn);is{
  174. // return r,nil
  175. // }
  176. func Redis_DeviceSensorParameter_Get(key string) (r DeviceSensorParameter, is bool) {
  177. if redisCache_DeviceSensorParameter.IsExist(key) {
  178. //println("找到key:",key)
  179. v := redisCache_DeviceSensorParameter.Get(key)
  180. err := json.Unmarshal(v.([]byte), &r)
  181. if err != nil {
  182. logs.Error(lib.FuncName(), err)
  183. return r, false
  184. }
  185. return r, true
  186. }
  187. //println("没有 找到key:",key)
  188. return DeviceSensorParameter{}, false
  189. }
  190. // ---------------- 特殊方法 -------------------
  191. // 获取设备参数
  192. func Read_DeviceSensorParameter(T_sn string, T_id int) (t DeviceSensorParameter_R, bool bool) {
  193. key := T_sn + "|" + strconv.Itoa(T_id)
  194. if v, is := Redis_DeviceSensorParameter_Get(key); is {
  195. return DeviceSensorParameterToDeviceSensorParameter_R(v), true
  196. }
  197. o := orm.NewOrm()
  198. qs := o.QueryTable(new(DeviceSensorParameter))
  199. var r []DeviceSensorParameter
  200. cond := orm.NewCondition()
  201. cond = cond.And("T_sn", T_sn).And("T_id", T_id).And("T_State", 1)
  202. _, err := qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-CreateTime").All(&r)
  203. if err != nil {
  204. logs.Error(lib.FuncName(), err)
  205. return t, false
  206. }
  207. // 判断是否有数据
  208. if len(r) == 0 {
  209. Redis_DeviceSensorParameter_Set(key, DeviceSensorParameter{})
  210. return t, false
  211. }
  212. t = DeviceSensorParameterToDeviceSensorParameter_R(r[0])
  213. Redis_DeviceSensorParameter_Set(key, r[0])
  214. return t, true
  215. }
  216. // 获取列表 T_State 1 系统获取 2 用户提交
  217. func Read_DeviceSensorParameter_T_sn_T_id(T_sn string, T_id int) (r []DeviceSensorParameter) {
  218. o := orm.NewOrm()
  219. qs := o.QueryTable(new(DeviceSensorParameter))
  220. cond := orm.NewCondition()
  221. cond = cond.And("T_sn", T_sn).And("T_id", T_id).And("T_State", 1)
  222. _, err := qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  223. if err != nil {
  224. logs.Error(lib.FuncName(), err)
  225. }
  226. return r
  227. }
  228. // 获取列表 T_State 1 系统获取 2 用户提交
  229. func Read_DeviceSensorParameter_List(T_sn string, T_id int, T_State int, page, page_z int) (r []DeviceSensorParameter_T) {
  230. o := orm.NewOrm()
  231. // 也可以直接使用 Model 结构体作为表名
  232. qs := o.QueryTable(new(DeviceSensorParameter))
  233. cond := orm.NewCondition()
  234. var offset int64
  235. if page <= 1 {
  236. offset = 0
  237. } else {
  238. offset = int64((page - 1) * page_z)
  239. }
  240. cond = cond.And("T_sn", T_sn)
  241. cond = cond.And("T_id", T_id)
  242. if T_State != 0 {
  243. cond = cond.And("T_State", T_State)
  244. }
  245. var maps []DeviceSensorParameter
  246. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  247. if err != nil {
  248. logs.Error(lib.FuncName(), err)
  249. return r
  250. }
  251. for _, v := range maps {
  252. r = append(r, DeviceSensorParameterToDeviceSensorParameter_T(v))
  253. }
  254. return r
  255. }
  256. // 添加
  257. func Add_DeviceSensorParameter(m DeviceSensorParameter) (DeviceSensorParameter, bool) {
  258. o := orm.NewOrm()
  259. id, err := o.Insert(&m)
  260. if err != nil {
  261. logs.Error(lib.FuncName(), err)
  262. return m, false
  263. }
  264. m.Id = int(id)
  265. return m, true
  266. }
  267. // 获取列表 T_State 1 系统获取 2 用户提交
  268. func Read_DeviceSensorParameter_All_Map(T_sn string, T_id int) {
  269. o := orm.NewOrm()
  270. // 也可以直接使用 Model 结构体作为表名
  271. var list []DeviceSensorParameter
  272. qs := o.QueryTable(new(DeviceSensorParameter))
  273. cond := orm.NewCondition()
  274. cond = cond.And("T_State", 1)
  275. if len(T_sn) > 0 {
  276. cond = cond.And("T_sn", T_sn)
  277. }
  278. if T_id > 0 {
  279. cond = cond.And("T_id", T_id)
  280. }
  281. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&list)
  282. if err != nil {
  283. logs.Error(lib.FuncName(), err)
  284. return
  285. }
  286. for _, v := range list {
  287. DeviceSensorParameter_list.Store(v.Id, DeviceSensorParameterToDeviceSensorParameter_M(v))
  288. }
  289. }
  290. func Read_DeviceSensorParameter_Map_Get(id int) DeviceSensorParameter_M {
  291. // 有先加入 给全部人发消息
  292. v, ok := DeviceSensorParameter_list.Load(id) /*如果确定是真实的,则存在,否则不存在 */
  293. if ok {
  294. return v.(DeviceSensorParameter_M)
  295. } else {
  296. return DeviceSensorParameter_M{}
  297. }
  298. }