hr.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Package postgre
  2. /**
  3. * @ File:
  4. * @ Date: 2021/5/6 11:30
  5. * @ Author: JYQ
  6. * @ Description:
  7. */
  8. package postgre
  9. import (
  10. "sportfitness/base/assembly/heartRate/model"
  11. "sync"
  12. "time"
  13. "github.com/sirupsen/logrus"
  14. )
  15. type PGHr struct {
  16. }
  17. func (PGHr) PublicSensorQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  18. err = getClient().Raw("select fn_hr_sensors_public_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  19. Scan(&result).Error
  20. return
  21. }
  22. func (PGHr) PrivateSensorQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  23. err = getClient().Raw("select fn_hr_sensors_private_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  24. Scan(&result).Error
  25. return
  26. }
  27. func (PGHr) PublicSensorsAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  28. err = getClient().Raw("select fn_hr_sensors_public_sensors_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  29. Scan(&result).Error
  30. return
  31. }
  32. func (PGHr) PrivateSensorsAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  33. err = getClient().Raw("select fn_hr_sensors_private_sensors_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  34. Scan(&result).Error
  35. return
  36. }
  37. func (PGHr) SensorsEdit(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  38. err = getClient().Raw("select fn_hr_sensors_sensors_edit(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  39. Scan(&result).Error
  40. return
  41. }
  42. func (PGHr) SensorsDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  43. err = getClient().Raw("select fn_hr_sensors_sensors_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  44. Scan(&result).Error
  45. return
  46. }
  47. func (PGHr) PkGroupChg(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  48. err = getClient().Raw("select fn_pk_group_chg(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  49. Scan(&result).Error
  50. return
  51. }
  52. func (PGHr) CrBind(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  53. err = getClient().Raw("select fn_hr_sensors_cr_bind(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  54. Scan(&result).Error
  55. return
  56. }
  57. func (PGHr) CrUnBind(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  58. err = getClient().Raw("select fn_hr_sensors_cr_unbind(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  59. Scan(&result).Error
  60. return
  61. }
  62. func (PGHr) UnBindSensorsQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  63. err = getClient().Raw("select fn_hr_sensors_unbind_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  64. Scan(&result).Error
  65. return
  66. }
  67. func (PGHr) HrClassDetailStatusEdit(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  68. err = getClient().Raw("select fn_hr_class_detail_status_edit(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  69. Scan(&result).Error
  70. return
  71. }
  72. func (PGHr) HrClassStartPrepare(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  73. err = getClient().Raw("select fn_hr_class_start_prepare(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  74. Scan(&result).Error
  75. return
  76. }
  77. func (PGHr) HrClassDetailAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  78. err = getClient().Raw("select fn_hr_class_detail_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  79. Scan(&result).Error
  80. return
  81. }
  82. func (PGHr) HrClassDetailDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  83. err = getClient().Raw("select fn_hr_class_detail_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  84. Scan(&result).Error
  85. return
  86. }
  87. func (PGHr) HrClassStartConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  88. err = getClient().Raw("select fn_hr_class_start_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  89. Scan(&result).Error
  90. return
  91. }
  92. func (PGHr) HrClassGiveUpConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  93. err = getClient().Raw("select fn_hr_class_give_up_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  94. Scan(&result).Error
  95. return
  96. }
  97. const hrSaveCount = 1000
  98. const hrSaveTime = time.Second * 10
  99. // key: sn
  100. var hrSaveMap = map[string][]model.BandData{}
  101. var hrSaveMapMu = sync.Mutex{}
  102. func (PGHr) getSNByBandId(id int) (sn string) {
  103. //TODO 实现
  104. return
  105. }
  106. func (p PGHr) SaveHrBatch(data []model.BandData) {
  107. hrSaveMapMu.Lock()
  108. defer hrSaveMapMu.Unlock()
  109. for _, one := range data {
  110. sn := p.getSNByBandId(one.BandId)
  111. if list, ok := hrSaveMap[sn]; ok {
  112. hrSaveMap[sn] = append(list, one)
  113. } else {
  114. hrSaveMap[sn] = []model.BandData{one}
  115. }
  116. }
  117. p.trySaveHrSaveMap()
  118. }
  119. func (PGHr) saveOneBandToDB(sn string, data []model.BandData) {
  120. //TODO 保存
  121. }
  122. func (p PGHr) workSaveHrSaveMapLoop() {
  123. defer func() {
  124. if p := recover(); p != nil {
  125. logrus.Errorf("workSaveHrSaveMapLoop: %s", p)
  126. }
  127. }()
  128. hrSaveMapMu.Lock()
  129. defer hrSaveMapMu.Unlock()
  130. p.trySaveHrSaveMap()
  131. }
  132. func (p PGHr) workSaveHrSaveMap() {
  133. logrus.Info("心率保存循环开启")
  134. for {
  135. time.Sleep(time.Second * 30)
  136. p.workSaveHrSaveMapLoop()
  137. }
  138. }
  139. // trySaveHrSaveMap 维护R3
  140. func (p PGHr) trySaveHrSaveMap() {
  141. var deleteKeys []string
  142. for sn, v := range hrSaveMap {
  143. needSave := false
  144. if len(v) >= hrSaveCount {
  145. needSave = true
  146. }
  147. if len(v) > 0 {
  148. firstTime := time.Unix(0, v[0].TimeMiles*1000000)
  149. if time.Now().Sub(firstTime) > hrSaveTime {
  150. needSave = true
  151. }
  152. }
  153. if needSave {
  154. p.saveOneBandToDB(sn, v)
  155. deleteKeys = append(deleteKeys, sn)
  156. }
  157. }
  158. for _, sn := range deleteKeys {
  159. delete(hrSaveMap, sn)
  160. }
  161. }