workbenchService.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package service
  2. import (
  3. "fmt"
  4. dataDao "iot_manager_service/app/data/dao"
  5. "iot_manager_service/app/device/dao"
  6. "iot_manager_service/app/device/model"
  7. operationDao "iot_manager_service/app/operation/dao"
  8. "iot_manager_service/app/operation/edge_service"
  9. operationModel "iot_manager_service/app/operation/model"
  10. "iot_manager_service/util/common"
  11. "strconv"
  12. "time"
  13. )
  14. // 中间件管理服务
  15. var WorkbenchService = new(workbenchService)
  16. type workbenchService struct{}
  17. func (s *workbenchService) CountDevice(tenantId int) ([]dao.CountDevice, *common.Errors) {
  18. counts, err := dao.GetDeviceCount(tenantId)
  19. if err != nil {
  20. return nil, common.FailResponse(err.Error(), nil)
  21. }
  22. return counts, nil
  23. }
  24. func (s *workbenchService) CountAlarm(tenantId int) (*dao.CountAlarm, *common.Errors) {
  25. count, err := dao.GetAlarmCount(tenantId)
  26. if err != nil {
  27. return nil, common.FailResponse(err.Error(), nil)
  28. }
  29. return count, nil
  30. }
  31. func (s *workbenchService) CountJobTodo(tenantId int) (*dao.CountAlarm, *common.Errors) {
  32. count, err := dao.GetAlarmCount(tenantId)
  33. if err != nil {
  34. return nil, common.FailResponse(err.Error(), nil)
  35. }
  36. return count, nil
  37. }
  38. func (s *workbenchService) Notification(tenantId int) (*dao.Notification, *common.Errors) {
  39. notification, err := dao.GetNotification(tenantId)
  40. if err != nil {
  41. return nil, common.FailResponse(err.Error(), nil)
  42. }
  43. return notification, nil
  44. }
  45. func (s *workbenchService) LightRate(tenantId int, req model.ReqLightRates) (*model.RspLightRate, *common.Errors) {
  46. var list []operationModel.ResponseLightingRate
  47. var err error
  48. if req.QueryType == "month" {
  49. list, err = s.getLightRateData("day", tenantId, operationModel.RequestLightingRateFilter{
  50. StartTime: req.StartDate,
  51. EndTime: req.EndDate,
  52. QueryType: "day",
  53. })
  54. } else {
  55. list, err = s.getLightRateData("month", tenantId, operationModel.RequestLightingRateFilter{
  56. StartTime: req.StartDate,
  57. EndTime: req.EndDate,
  58. QueryType: "month",
  59. })
  60. }
  61. if err != nil {
  62. return nil, common.FailResponse(err.Error(), nil)
  63. }
  64. var lightRate []model.LightRate
  65. var energy []model.LightRate
  66. for _, rate := range list {
  67. parse, _ := time.Parse("2006-01-02", rate.CountTime)
  68. if req.QueryType == "month" {
  69. parse, _ = time.Parse("2006-01-02", rate.CountTime)
  70. }
  71. lightRate = append(lightRate, model.LightRate{
  72. ColumnValue: fmt.Sprintf("%.2f", rate.LightingRate),
  73. TimeLine: parse,
  74. })
  75. energy = append(energy, model.LightRate{
  76. ColumnValue: fmt.Sprintf("%.2f", rate.EnergyNum),
  77. TimeLine: parse,
  78. })
  79. }
  80. rsp := model.RspLightRate{
  81. LightRate: lightRate, Energy: energy,
  82. }
  83. return &rsp, nil
  84. }
  85. //工作台-采集点
  86. func (s *workbenchService) Aqi(tenantId int) (interface{}, interface{}) {
  87. aqi, err := dao.GetAqi(tenantId)
  88. if err != nil {
  89. return nil, common.FailResponse(err.Error(), nil)
  90. }
  91. s.getNewsDate(aqi)
  92. return aqi, nil
  93. }
  94. func (s *workbenchService) getNewsDate(vo *dao.OptoSensorVO) *dao.OptoSensorVO {
  95. //var meteorologicalDataList []omodel.EnvironmentDetail
  96. //meteorologicalDataVO := omodel.EnvironmentDetail{}
  97. //meteorologicalDataVO.Sn = vo.Sn
  98. //meteorologicalDataList = append(meteorologicalDataList, meteorologicalDataVO)
  99. devId := vo.ID
  100. environmentData := dataDao.EnvironmentData{DeviceId: devId}
  101. environmentData.Get()
  102. vo.RealTimeTemperature = fmt.Sprintf("%.2f ℃", environmentData.Temperature)
  103. vo.Pm25 = fmt.Sprintf("%.2f ug/m³", environmentData.Pm25)
  104. vo.AirIndex = int(environmentData.Air)
  105. vo.AirQuality = common.CalculateAirQuality(string(fmt.Sprintf("%.2f", environmentData.Air)))
  106. vo.EndLineTime = vo.UpdateTime.Format("2006-01-02 15:04:05")
  107. vo.Humidity = fmt.Sprintf("%.2f", environmentData.Humidity)
  108. vo.Pm10 = fmt.Sprintf("%.2f", environmentData.Pm10)
  109. vo.Noise = fmt.Sprintf("%.2f", environmentData.Noise)
  110. vo.Pressure = fmt.Sprintf("%.2f", environmentData.Hpa) //大气压
  111. vo.Direction = common.CalculateDirection(environmentData.WindDirection) //风向
  112. vo.RealTimeWindSpeed = common.CalculateSpeed(fmt.Sprintf("%.2f", environmentData.WindSpeed)) //风力等级
  113. return vo
  114. }
  115. // getLightRateData 能耗和光照
  116. func (s *workbenchService) getLightRateData(method string, tenantId int, req operationModel.RequestLightingRateFilter) ([]operationModel.ResponseLightingRate, error) {
  117. lightControl := operationDao.Operation{
  118. TenantId: tenantId,
  119. }
  120. codes, err := lightControl.GetSnList()
  121. if err != nil {
  122. return nil, err
  123. }
  124. flag := 1 //能耗请求类型:按天
  125. flag2 := 0 //亮灯率请求类型:按天
  126. if method == "month" {
  127. flag = 2 //能耗请求类型:按天
  128. flag2 = 1 //亮灯率请求类型:按月
  129. }
  130. //能耗
  131. forLightEnergy := edge_service.ForLightEnergy{}
  132. lightEnergys, err := forLightEnergy.GetLightEnergy(edge_service.ForLightEnergyReq{
  133. Codes: codes,
  134. Start: req.StartTime,
  135. End: req.EndTime,
  136. Flag: flag,
  137. })
  138. if err != nil {
  139. return nil, err
  140. }
  141. //亮灯率
  142. forLightRate := edge_service.ForLightRate{}
  143. lightRates, err := forLightRate.GetLightRate(edge_service.ForLightRateReq{
  144. Tenant: strconv.Itoa(tenantId),
  145. Start: req.StartTime,
  146. End: req.EndTime,
  147. Flag: flag2,
  148. })
  149. if err != nil {
  150. return nil, err
  151. }
  152. var list []operationModel.ResponseLightingRate
  153. //fmt.Printf("LightEnergys = %v", LightEnergys)
  154. var months []string
  155. if method == "month" {
  156. months = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
  157. } else {
  158. months = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
  159. }
  160. //fmt.Printf("months = %v \n", months)
  161. for _, month := range months {
  162. // 能耗数据汇总
  163. var monthEnergyNum float64 //能耗
  164. for _, LightEnergy := range lightEnergys {
  165. for _, data := range LightEnergy {
  166. if month == data.Date {
  167. monthEnergyNum += data.Difference
  168. }
  169. }
  170. }
  171. //亮灯率
  172. var lightingRate float64
  173. var lightControlNum, lightingNum int
  174. for _, rate := range lightRates {
  175. date := rate.Date
  176. if method == "month" {
  177. date = rate.Month + "-01"
  178. }
  179. if date == month {
  180. lightingRate += rate.Rate //亮灯率
  181. lightControlNum += rate.Total //灯数
  182. lightingNum += rate.Number //亮灯数
  183. }
  184. }
  185. //组合数据到前端接口展示
  186. list = append(list, operationModel.ResponseLightingRate{
  187. CountTime: month,
  188. EnergyNum: common.Decimal(monthEnergyNum),
  189. LightingRate: common.Decimal(lightingRate),
  190. LightControlNum: lightControlNum,
  191. LightingNum: lightingNum,
  192. })
  193. }
  194. return list, nil
  195. }