nf.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package handler
  2. import (
  3. "GtDataStore/app/cmd/organization/internal/logic/job"
  4. "GtDataStore/app/cmd/organization/internal/svc"
  5. "GtDataStore/app/model"
  6. "context"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "sync"
  9. "time"
  10. )
  11. func DeviceNf(ctx context.Context, task *job.Task, technologyName string) error {
  12. // 1. 查询所有的设备
  13. devices, err := findDeviceNf(task.Job.SvcCtx, int64(task.Id))
  14. if err != nil {
  15. return err
  16. }
  17. // 2. 获得点位信息
  18. var wg sync.WaitGroup
  19. wg.Add(len(devices))
  20. for _, device := range devices {
  21. device := device
  22. go func() {
  23. defer wg.Done()
  24. if err := device.Items.FillCurrentValue(); err != nil {
  25. logx.Errorf("DeviceNf device.Items.FillCurrentValue error: %s", err.Error())
  26. }
  27. }()
  28. }
  29. wg.Wait()
  30. // 3. 转换为存储对象
  31. workings, err := transDeviceNfData(devices)
  32. if err != nil {
  33. return err
  34. }
  35. // 4. 批量入库
  36. if _, err := task.Job.SvcCtx.WorkingNf.MultiInsert(context.Background(), workings); err != nil {
  37. logx.Errorf("DeviceNf task.Job.SvcCtx.WorkingNf.MultiInsert error: %s", err.Error())
  38. }
  39. return nil
  40. }
  41. func findDeviceNf(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
  42. if data := deviceBindCacheTable.GetCache(DEVICE_NF); data != nil {
  43. return data, nil
  44. }
  45. if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_NF); err != nil {
  46. logx.Infof("findDeviceNf not found devices")
  47. return nil, err
  48. } else {
  49. deviceBindCacheTable.SetCache(DEVICE_NF, devices, time.Now().Add(300*time.Second))
  50. return devices, nil
  51. }
  52. }
  53. func transDeviceNfData(datas []model.DcDeviceBind) ([]model.DcWorkingNf, error) {
  54. ts := make([]model.DcWorkingNf, len(datas))
  55. for i, data := range datas {
  56. ts[i] = model.DcWorkingNf{
  57. Id: 0,
  58. ProjectId: data.ProjectId,
  59. DeviceCode: data.DeviceCode,
  60. WaterTemperature: data.Items.GetItemFloat64Value("water_temperature"),
  61. FeedFlow1St: data.Items.GetItemFloat64Value("feed_flow_1st"),
  62. ConFlow1St: data.Items.GetItemFloat64Value("con_flow_1st"),
  63. ProductFlow1St: data.Items.GetItemFloat64Value("product_flow_1st"),
  64. FeedPressure1St: data.Items.GetItemFloat64Value("feed_pressure_1st"),
  65. ConPressure1St: data.Items.GetItemFloat64Value("con_pressure_1st"),
  66. ProductPressure1St: data.Items.GetItemFloat64Value("product_pressure_1st"),
  67. Tmp1St: data.Items.GetItemFloat64Value("tmp_1st"),
  68. Flux1St: data.Items.GetItemFloat64Value("flux_1st"),
  69. Permeability1St: data.Items.GetItemFloat64Value("permeability_1st"),
  70. FeedFlow2Nd: data.Items.GetItemFloat64Value("feed_flow_2nd"),
  71. ConFlow2Nd: data.Items.GetItemFloat64Value("con_flow_2nd"),
  72. ProductFlow2Nd: data.Items.GetItemFloat64Value("product_flow_2nd"),
  73. FeedPressure2Nd: data.Items.GetItemFloat64Value("feed_pressure_2nd"),
  74. ConPressure2Nd: data.Items.GetItemFloat64Value("con_pressure_2nd"),
  75. ProductPressure2Nd: data.Items.GetItemFloat64Value("product_pressure_2nd"),
  76. Tmp2Nd: data.Items.GetItemFloat64Value("tmp_2nd"),
  77. Flux2Nd: data.Items.GetItemFloat64Value("flux_2nd"),
  78. Permeability2Nd: data.Items.GetItemFloat64Value("permeability_2nd"),
  79. FeedFlow3Th: data.Items.GetItemFloat64Value("feed_flow_3th"),
  80. ConFlow3Th: data.Items.GetItemFloat64Value("con_flow_3th"),
  81. ProductFlow3Th: data.Items.GetItemFloat64Value("product_flow_3th"),
  82. FeedPressure3Th: data.Items.GetItemFloat64Value("feed_pressure_3th"),
  83. ConPressure3Th: data.Items.GetItemFloat64Value("con_pressure_3th"),
  84. ProductPressure3Th: data.Items.GetItemFloat64Value("product_pressure_3th"),
  85. Tmp3Th: data.Items.GetItemFloat64Value("tmp_3th"),
  86. Flux3Th: data.Items.GetItemFloat64Value("flux_3th"),
  87. Permeability3Th: data.Items.GetItemFloat64Value("permeability_3th"),
  88. FeedWqTurbidity: data.Items.GetItemFloat64Value("feed_wq_turbidity"),
  89. FeedWqPh: data.Items.GetItemInt64Value("feed_wq_ph"),
  90. ProductWqPh: data.Items.GetItemInt64Value("product_wq_ph"),
  91. FeedWqAl: data.Items.GetItemFloat64Value("feed_wq_al"),
  92. ProductWqAl: data.Items.GetItemFloat64Value("product_wq_al"),
  93. FeedWqFe: data.Items.GetItemFloat64Value("feed_wq_fe"),
  94. ProductWqFe: data.Items.GetItemFloat64Value("product_wq_fe"),
  95. FeedWqMn: data.Items.GetItemFloat64Value("feed_wq_mn"),
  96. ProductWqMn: data.Items.GetItemFloat64Value("product_wq_mn"),
  97. FeedWqSio2: data.Items.GetItemFloat64Value("feed_wq_sio2"),
  98. ProductWqSio2: data.Items.GetItemFloat64Value("product_wq_sio2"),
  99. FeedWqCod: data.Items.GetItemFloat64Value("feed_wq_cod"),
  100. ProductWqCod: data.Items.GetItemFloat64Value("product_wq_cod"),
  101. FeedWqP: data.Items.GetItemFloat64Value("feed_wq_p"),
  102. ProductWqP: data.Items.GetItemFloat64Value("product_wq_p"),
  103. Step: data.Items.GetItemInt64Value("step"),
  104. FilterTime: data.Items.GetItemFloat64Value("filter_time"),
  105. CTime: time.Now(),
  106. }
  107. }
  108. return ts, nil
  109. }