DeviceData.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package Device
  2. import (
  3. "bzd_server/conf"
  4. "bzd_server/lib"
  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. "strings"
  14. "time"
  15. )
  16. // 模板
  17. type DeviceData struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_id string `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. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  24. }
  25. func (t *DeviceData) TableName() string {
  26. return "DeviceData" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. var redisCache_DeviceData cache.Cache
  29. func init() {
  30. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  31. "redis_DeviceData", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  32. fmt.Println(config)
  33. var err error
  34. redisCache_DeviceData, err = cache.NewCache("redis", config)
  35. if err != nil || redisCache_DeviceData == nil {
  36. errMsg := "failed to init redis"
  37. fmt.Println(errMsg, err)
  38. }
  39. }
  40. func DeviceData_Set(key string) (err error) {
  41. err = redisCache_DeviceData.Put(key, "", 2*time.Hour)
  42. if err != nil {
  43. fmt.Println("set key:", key, err)
  44. }
  45. return
  46. }
  47. // ---------------- Redis -------------------
  48. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  49. func RedisDeviceData_Set(key string, r DeviceData_New) (err error) {
  50. //json序列化
  51. str, err := json.Marshal(r)
  52. if err != nil {
  53. fmt.Print(err)
  54. return
  55. }
  56. err = redisCache_DeviceData.Put(key, str, 24*time.Hour)
  57. if err != nil {
  58. fmt.Println("set key:", key, ",value:", str, err)
  59. }
  60. return
  61. }
  62. //if r,is :=Redis_Get(T_sn);is{
  63. //return r,nil
  64. //}
  65. func RedisDeviceData_Get(key string) (r DeviceData_New, is bool) {
  66. v := redisCache_DeviceData.Get(key)
  67. json.Unmarshal(v.([]byte), &r)
  68. return r, true
  69. }
  70. //创建数据库 Device.CREATE_DeviceData("")
  71. func CREATE_DeviceData(SN string) bool {
  72. o := orm.NewOrm()
  73. sql := "CREATE TABLE IF NOT EXISTS `Z_DeviceData_" + SN + "` ( " +
  74. " `ID` int(11) NOT NULL AUTO_INCREMENT," +
  75. " `t_id` int(11) NULL DEFAULT NULL," +
  76. " `t_t` float(6, 1) NULL DEFAULT NULL," +
  77. " `t_rh` float(6, 1) NULL DEFAULT NULL," +
  78. " `t_time` datetime(0) NULL DEFAULT NULL," +
  79. " `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0)," +
  80. " PRIMARY KEY (`ID`) USING BTREE" +
  81. ") ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"
  82. _, err := o.Raw(sql).Exec()
  83. if err != nil {
  84. return false
  85. }
  86. return true
  87. }
  88. // ---------------- 特殊方法 -------------------
  89. // 获取 ById
  90. //func Read_List_ById(id int) (r DeviceData) {
  91. // o := orm.NewOrm()
  92. //
  93. // return r
  94. //}
  95. //
  96. //// 添加
  97. //func Add_DeviceData(SN string, t_id string, t_t string, t_rh string, t_time string) bool {
  98. // key := SN + "|" + t_id + "|" + t_time
  99. // //fmt.Println(key)
  100. // if redisCache_DeviceData.IsExist(key) {
  101. // //println("找到key:",key)
  102. // return true
  103. // }
  104. // //println("没有 找到key:",key)
  105. //
  106. // o := orm.NewOrm()
  107. // // 查看是否 有记录
  108. // var maps_z []orm2.ParamsList
  109. //
  110. // sql := "SELECT COUNT(ID) FROM Z_DeviceData_" + SN + " WHERE t_time = '" + t_time + "' AND t_id = " + t_id
  111. // //fmt.Println(sql)
  112. // _, err := o.Raw(sql).ValuesList(&maps_z)
  113. //
  114. // if err != nil {
  115. // fmt.Println(err)
  116. // return false
  117. // }
  118. // //fmt.Println("maps_z[0][0]:",maps_z[0][0])
  119. // if lib.To_int(maps_z[0][0]) > 0 {
  120. // // 存在 写入 redis
  121. // DeviceData_Set(key)
  122. // return true
  123. // }
  124. // // 开始插入数据
  125. // sql = "INSERT INTO Z_DeviceData_" + SN + " (`t_id`, `t_t`, `t_rh`, `t_time`) VALUES (" + t_id + ", " + t_t + ", " + t_rh + ", '" + t_time + "')"
  126. // // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  127. // //fmt.Println(sql)
  128. // res, err := o.Raw(sql).Exec()
  129. // if err != nil {
  130. // fmt.Println(err)
  131. // return false
  132. // }
  133. // res.RowsAffected()
  134. // // 存在 写入 redis
  135. // DeviceData_Set(key)
  136. // //fmt.Println("mysql row affected nums: ", num)
  137. // return true
  138. //}
  139. type DeviceData_ struct {
  140. T_id int `orm:"column(t_id);size(10);null"` // ID
  141. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  142. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  143. T_time string `orm:"column(t_time);type(timestamp);null;"` // 采集时间
  144. }
  145. type DeviceData_New struct {
  146. T_id int `orm:"column(t_id);size(10);null"` // ID
  147. T_t float32 `orm:"column(t_t);size(10);null"` // 温度
  148. T_rh float32 `orm:"column(t_rh);size(10);null"` // 湿度
  149. T_time time.Time `orm:"column(t_time);type(timestamp);null;"` // 采集时间
  150. }
  151. //
  152. func Read_DeviceSensorData_ById_List(SN string, T_id int, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  153. o := orm.NewOrm()
  154. var maps []DeviceData_
  155. var maps_z []orm2.ParamsList
  156. pagez := page_z
  157. var offset int
  158. if page <= 1 {
  159. offset = 0
  160. } else {
  161. page -= 1
  162. offset = page * pagez
  163. }
  164. sql_time := ""
  165. if len(Time_start_) > 1 {
  166. sql_time += " t_time >= '" + Time_start_ + "' AND "
  167. }
  168. if len(Time_end_) > 1 {
  169. sql_time += " t_time <= '" + Time_end_ + "' AND "
  170. }
  171. sql := "SELECT COUNT(ID) FROM Z_DeviceData_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id)
  172. fmt.Println(sql)
  173. _, err := o.Raw(sql).ValuesList(&maps_z)
  174. if err != nil {
  175. return maps, 0
  176. }
  177. if len(maps_z) == 0 {
  178. return maps, 0
  179. }
  180. //fmt.Println("maps_z;",maps_z[0][0])
  181. sql = "SELECT t_name,t_id,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM Z_DeviceData_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time DESC "
  182. if page_z != 9999 {
  183. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  184. }
  185. fmt.Println(sql)
  186. _, err = o.Raw(sql).QueryRows(&maps)
  187. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  188. key, _ := strconv.Atoi(maps_z[0][0].(string))
  189. return maps, key
  190. }
  191. //
  192. func Read_DeviceSensorData_List(SN string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  193. o := orm.NewOrm()
  194. var maps []DeviceData_
  195. var maps_z []orm2.ParamsList
  196. pagez := page_z
  197. var offset int
  198. if page <= 1 {
  199. offset = 0
  200. } else {
  201. page -= 1
  202. offset = page * pagez
  203. }
  204. sql_time := ""
  205. if len(Time_start_) > 1 && len(Time_end_) > 1 {
  206. sql_time = "WHERE "
  207. sql_time += " t_time >= '" + Time_start_ + "' AND "
  208. sql_time += " t_time <= '" + Time_end_ + "' "
  209. }
  210. sql := "SELECT COUNT(ID) FROM Z_DeviceData_" + SN + " " + sql_time
  211. fmt.Println(sql)
  212. _, err := o.Raw(sql).ValuesList(&maps_z)
  213. if err != nil {
  214. return maps, 0
  215. }
  216. if len(maps_z) == 0 {
  217. return maps, 0
  218. }
  219. //fmt.Println("maps_z;",maps_z[0][0])
  220. sql = "SELECT t_id,t_t,t_rh,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM Z_DeviceData_" + SN + " " + sql_time + " ORDER BY t_time DESC "
  221. if page_z != 9999 {
  222. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(pagez)
  223. }
  224. fmt.Println(sql)
  225. _, err = o.Raw(sql).QueryRows(&maps)
  226. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  227. key, _ := strconv.Atoi(maps_z[0][0].(string))
  228. return maps, key
  229. }
  230. //
  231. func Read_DeviceSensorData_List_z(SN string, Time_start_ string) ( int) {
  232. o := orm.NewOrm()
  233. var maps_z []orm2.ParamsList
  234. sql_time := ""
  235. if len(Time_start_) > 1 {
  236. sql_time = "WHERE "
  237. sql_time += " t_time >= '" + Time_start_ + "' "
  238. //sql_time += " t_time <= '" + Time_end_ + "' AND "
  239. }
  240. sql := "SELECT COUNT(ID) FROM Z_DeviceData_" + SN + " " + sql_time
  241. fmt.Println(sql)
  242. o.Raw(sql).ValuesList(&maps_z)
  243. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  244. key, _ := strconv.Atoi(maps_z[0][0].(string))
  245. return key
  246. }
  247. // 获取最新数据
  248. func Read_DeviceSensorData_ById_New(SN string, T_id int) (DeviceData_New, bool) {
  249. o := orm.NewOrm()
  250. var maps DeviceData_New
  251. key_data := SN + "|" + strconv.Itoa(T_id)
  252. if redisCache_DeviceData.IsExist(key_data) {
  253. r, _ := RedisDeviceData_Get(key_data)
  254. //println("Redis_Get OK")
  255. return r, true
  256. }
  257. //fmt.Println("maps_z;",maps_z[0][0])
  258. sql := "SELECT t_id,t_t,t_rh,t_time FROM Z_DeviceData_" + SN + " WHERE " + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time DESC "
  259. sql = sql + " LIMIT 0,1 "
  260. fmt.Println(sql)
  261. err := o.Raw(sql).QueryRow(&maps)
  262. if err != nil {
  263. fmt.Println(err)
  264. return maps, false
  265. }
  266. fmt.Println("T_time:", maps.T_time)
  267. RedisDeviceData_Set(key_data, maps)
  268. return maps, true
  269. }
  270. func Read_DeviceSensorData_By_T_snid_List(T_snid string, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_, int) {
  271. T_snid_list := strings.Split(T_snid, "|")
  272. var maps []DeviceData_
  273. var maps_num int
  274. var offset, offset_z int
  275. for _, v := range T_snid_list {
  276. sn_id := strings.Split(v, ",")
  277. if len(sn_id) == 2 {
  278. r_maps, r_maps_num := Read_DeviceSensorData_ById_List(sn_id[0], lib.To_int(sn_id[1]), Time_start_, Time_end_, 0, 9999)
  279. maps = append(maps, r_maps...)
  280. maps_num = maps_num + r_maps_num
  281. fmt.Println("加载数据:", sn_id[0], sn_id[1], r_maps_num)
  282. }
  283. }
  284. if page <= 1 {
  285. offset = 0
  286. } else {
  287. page -= 1
  288. offset = page * page_z
  289. }
  290. offset_z = offset + page_z
  291. if maps_num < offset_z {
  292. offset_z = maps_num
  293. }
  294. if page_z == 9999 {
  295. fmt.Println("总数据:", maps_num, " 导出")
  296. return maps, maps_num
  297. }
  298. fmt.Println("总数据:", maps_num, " 截取", offset, offset+page_z)
  299. return maps[offset:offset_z], maps_num
  300. }
  301. //
  302. func Read_DeviceData_ById_Year_List(SN string) []orm2.ParamsList {
  303. o := orm.NewOrm()
  304. var maps_z []orm2.ParamsList
  305. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM Z_DeviceData_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  306. sql := "SELECT DATE_FORMAT(t_time,\"%m\") AS m ,DATE_FORMAT(t_time,\"%d\") AS d FROM Z_DeviceData_" + SN + " WHERE t_time > '" + strconv.Itoa(time.Now().Year()) + "-0-0 00:00:00' GROUP BY DATE_FORMAT(t_time,\"%m\");"
  307. fmt.Println(sql)
  308. num, err := o.Raw(sql).ValuesList(&maps_z)
  309. if err == nil && num > 0 {
  310. fmt.Println(maps_z[0][0]) // slene
  311. }
  312. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  313. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  314. return maps_z
  315. } //
  316. func Read_DeviceData_ById_Month_List(SN string) []orm2.ParamsList {
  317. o := orm.NewOrm()
  318. var maps_z []orm2.ParamsList
  319. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  320. time_x := currentTime.Format("2006-01") + "-00 00:00:00"
  321. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM Z_DeviceData_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  322. sql := "SELECT DATE_FORMAT(t_time,\"%d\") AS d FROM Z_DeviceData_" + SN + " WHERE t_time > '" + time_x + "' GROUP BY DATE_FORMAT(t_time,\"%d\");"
  323. fmt.Println(sql)
  324. o.Raw(sql).ValuesList(&maps_z)
  325. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  326. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  327. return maps_z
  328. }
  329. func Read_DeviceSensorData_ById_Day_List(SN string) []orm2.ParamsList {
  330. o := orm.NewOrm()
  331. var maps_z []orm2.ParamsList
  332. currentTime := time.Now() //获取当前时间,类型是Go的时间类型Time
  333. time_x := currentTime.Format("2006-01-02") + " 00:00:00"
  334. //sql = "SELECT t_name,t_t,t_rh,t_tl,t_tu,t_rhl,t_rhu,t_site,DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM Z_DeviceData_"+SN+" WHERE "+sql_time+" t_id = "+ strconv.Itoa(T_id) +" ORDER BY t_time DESC "
  335. sql := "SELECT DATE_FORMAT(t_time,\"%H\") AS m FROM Z_DeviceData_" + SN + " WHERE t_time > '" + time_x + "' GROUP BY DATE_FORMAT(t_time,\"%H\");"
  336. fmt.Println(sql)
  337. o.Raw(sql).ValuesList(&maps_z)
  338. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  339. //key,_ := strconv.Atoi(maps_z[0][0].(string))
  340. return maps_z
  341. }
  342. func Read_DeviceSensorData_List_GROUP_BY_t_time(SN string, Time_start_ string, Time_end_ string) []orm2.ParamsList {
  343. o := orm.NewOrm()
  344. var maps_z []orm2.ParamsList
  345. sql_time := ""
  346. if len(Time_start_) > 1 {
  347. sql_time += " t_time >= '" + Time_start_ + "' AND " + " t_time <= '" + Time_end_ + "' "
  348. }
  349. sql := "SELECT DATE_FORMAT(t_time,'%Y-%c-%d %H:%i:%s') AS t_time FROM Z_DeviceData_" + SN + " WHERE " + sql_time + " GROUP BY t_time ORDER BY t_time DESC "
  350. fmt.Println(sql)
  351. o.Raw(sql).ValuesList(&maps_z)
  352. return maps_z
  353. }