fwh_emsLog.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package fwh_pro
  2. import (
  3. "context"
  4. "fmt"
  5. "fwh-toolbox-server/domain"
  6. "net/url"
  7. "strconv"
  8. )
  9. // 召唤EMS日志
  10. func queryCallEmsLog(ctx context.Context, sn string, dateStr string, logType int) (*domain.FwhResponse[bool], error) {
  11. // 要发送的表单数据
  12. // form := url.Values{}
  13. // form.Set("gatewayId", sn) // SN
  14. // form.Set("logEqSn", sn) // SN
  15. // form.Set("logId", sn) // SN
  16. // form.Set("logTime", dateStr) // 日志时间
  17. // form.Set("logType", strconv.Itoa(logType)) // 日志类型 4-IBG运行日志 7-电池日志 9-设备系统资源监控日志 10-4G日志 11-AWS日志 12-主进程日志
  18. // form.Set("lang", "ZH_CN")
  19. form := map[string]interface{}{
  20. "gatewayId": sn,
  21. "logEqSn": sn,
  22. "logId": sn,
  23. "logTime": dateStr,
  24. "logType": logType,
  25. "lang": "ZH_CN",
  26. }
  27. response, err := FwhHttpPostUnmarshalContext[bool](ctx, getFullEnergyUrl("/hes-gateway/manage/logFileCall"), form)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return response, nil
  32. }
  33. // 召唤EMS日志
  34. func QueryCallEmsLog(sn string, dateStr string, logType int) error {
  35. return QueryCallEmsLogContext(context.Background(), sn, dateStr, logType)
  36. }
  37. func QueryCallEmsLogContext(ctx context.Context, sn string, dateStr string, logType int) error {
  38. // 查询告警快照数据
  39. callRes, err := queryCallEmsLog(ctx, sn, dateStr, logType)
  40. if err != nil {
  41. return fmt.Errorf("[QueryCallEmsLog] %s", err.Error())
  42. }
  43. if callRes.Code != 200 {
  44. return fmt.Errorf("[QueryCallEmsLog] 查询失败 code: %d, message: %s", callRes.Code, callRes.Message)
  45. }
  46. if callRes.Result == nil || !*callRes.Result {
  47. return fmt.Errorf("[QueryCallEmsLog] 查询失败 code: %d, result: %v, message: %s", callRes.Code, callRes.Result, callRes.Message)
  48. }
  49. return nil
  50. }
  51. // 查询EMS日志下载地址
  52. func queryEmsLogCallRecord(ctx context.Context, sn string) (*domain.FwhResponse[[]domain.EmsLogRecordInfo], error) {
  53. // 构造 query 参数
  54. params := url.Values{}
  55. params.Add("linkId", sn) // SN
  56. params.Add("pageIndex", strconv.Itoa(1))
  57. params.Add("pageSize", strconv.FormatUint(REQ_PAGE_SIZE, 10))
  58. params.Add("pageNum", strconv.Itoa(1))
  59. params.Add("lang", "ZH_CN")
  60. response, err := FwhHttpGetUnmarshalContext[[]domain.EmsLogRecordInfo](ctx, getFullEnergyUrl("/hes-gateway/manage/iotDeviceFileRecords"), params)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return response, nil
  65. }
  66. // 查询EMS日志下载地址
  67. func QueryEmsLogCallRecord(sn string, dateStr string, logType int) (*domain.EmsLogRecordInfo, error) {
  68. return QueryEmsLogCallRecordAfter(sn, dateStr, logType, -1)
  69. }
  70. // LatestEmsLogCallRecordID captures the existing record before a new call.
  71. // A later poll must ignore records at or below this ID.
  72. func LatestEmsLogCallRecordID(sn string, dateStr string, logType int) (int64, error) {
  73. return LatestEmsLogCallRecordIDContext(context.Background(), sn, dateStr, logType)
  74. }
  75. func LatestEmsLogCallRecordIDContext(ctx context.Context, sn string, dateStr string, logType int) (int64, error) {
  76. callRecordRes, err := queryEmsLogCallRecord(ctx, sn)
  77. if err != nil {
  78. return 0, fmt.Errorf("[LatestEmsLogCallRecordID] %w", err)
  79. }
  80. if callRecordRes.Code != 200 {
  81. return 0, fmt.Errorf("[LatestEmsLogCallRecordID] 查询失败 code: %d, message: %s", callRecordRes.Code, callRecordRes.Message)
  82. }
  83. if callRecordRes.Result == nil {
  84. return 0, nil
  85. }
  86. if latest := latestMatchingEmsLogRecord(*callRecordRes.Result, dateStr, logType, -1); latest != nil {
  87. return latest.Id, nil
  88. }
  89. return 0, nil
  90. }
  91. func QueryEmsLogCallRecordAfter(sn string, dateStr string, logType int, afterID int64) (*domain.EmsLogRecordInfo, error) {
  92. return QueryEmsLogCallRecordAfterContext(context.Background(), sn, dateStr, logType, afterID)
  93. }
  94. func QueryEmsLogCallRecordAfterContext(ctx context.Context, sn string, dateStr string, logType int, afterID int64) (*domain.EmsLogRecordInfo, error) {
  95. // 查询告警快照数据
  96. callRecordRes, err := queryEmsLogCallRecord(ctx, sn)
  97. if err != nil {
  98. return nil, fmt.Errorf("[QueryEmsLogCallRecord] %s", err.Error())
  99. }
  100. if callRecordRes.Code != 200 {
  101. return nil, fmt.Errorf("[QueryEmsLogCallRecord] 查询失败 code: %d, message: %s", callRecordRes.Code, callRecordRes.Message)
  102. }
  103. if callRecordRes.Result == nil {
  104. return nil, fmt.Errorf("[QueryEmsLogCallRecord] 结果为空")
  105. }
  106. latest := latestMatchingEmsLogRecord(*callRecordRes.Result, dateStr, logType, afterID)
  107. if latest == nil {
  108. return nil, fmt.Errorf("[QueryEmsLogCallRecord] 没有召唤日志")
  109. }
  110. return latest, nil
  111. }
  112. func latestMatchingEmsLogRecord(records []domain.EmsLogRecordInfo, dateStr string, logType int, afterID int64) *domain.EmsLogRecordInfo {
  113. var latest *domain.EmsLogRecordInfo
  114. for _, record := range records {
  115. if record.LogTime == dateStr && record.CodeType == logType && record.Id > afterID && (latest == nil || record.Id > latest.Id) {
  116. current := record
  117. latest = &current
  118. }
  119. }
  120. return latest
  121. }
  122. // 请求下载EMS日志文件
  123. func QueryDownloadEmsLogFile(filePath string) ([]byte, error) {
  124. return QueryDownloadEmsLogFileContext(context.Background(), filePath)
  125. }
  126. func QueryDownloadEmsLogFileContext(ctx context.Context, filePath string) ([]byte, error) {
  127. response, err := FwhHttpGetContext(ctx, getFullEnergyUrl("/hes-upload/v1/file/download/"+filePath), url.Values{})
  128. if err != nil {
  129. return nil, err
  130. }
  131. return response.Body, nil
  132. }