package fwh_pro import ( "context" "fmt" "fwh-toolbox-server/domain" "net/url" "strconv" ) // 召唤EMS日志 func queryCallEmsLog(ctx context.Context, sn string, dateStr string, logType int) (*domain.FwhResponse[bool], error) { // 要发送的表单数据 // form := url.Values{} // form.Set("gatewayId", sn) // SN // form.Set("logEqSn", sn) // SN // form.Set("logId", sn) // SN // form.Set("logTime", dateStr) // 日志时间 // form.Set("logType", strconv.Itoa(logType)) // 日志类型 4-IBG运行日志 7-电池日志 9-设备系统资源监控日志 10-4G日志 11-AWS日志 12-主进程日志 // form.Set("lang", "ZH_CN") form := map[string]interface{}{ "gatewayId": sn, "logEqSn": sn, "logId": sn, "logTime": dateStr, "logType": logType, "lang": "ZH_CN", } response, err := FwhHttpPostUnmarshalContext[bool](ctx, getFullEnergyUrl("/hes-gateway/manage/logFileCall"), form) if err != nil { return nil, err } return response, nil } // 召唤EMS日志 func QueryCallEmsLog(sn string, dateStr string, logType int) error { return QueryCallEmsLogContext(context.Background(), sn, dateStr, logType) } func QueryCallEmsLogContext(ctx context.Context, sn string, dateStr string, logType int) error { // 查询告警快照数据 callRes, err := queryCallEmsLog(ctx, sn, dateStr, logType) if err != nil { return fmt.Errorf("[QueryCallEmsLog] %s", err.Error()) } if callRes.Code != 200 { return fmt.Errorf("[QueryCallEmsLog] 查询失败 code: %d, message: %s", callRes.Code, callRes.Message) } if callRes.Result == nil || !*callRes.Result { return fmt.Errorf("[QueryCallEmsLog] 查询失败 code: %d, result: %v, message: %s", callRes.Code, callRes.Result, callRes.Message) } return nil } // 查询EMS日志下载地址 func queryEmsLogCallRecord(ctx context.Context, sn string) (*domain.FwhResponse[[]domain.EmsLogRecordInfo], error) { // 构造 query 参数 params := url.Values{} params.Add("linkId", sn) // SN params.Add("pageIndex", strconv.Itoa(1)) params.Add("pageSize", strconv.FormatUint(REQ_PAGE_SIZE, 10)) params.Add("pageNum", strconv.Itoa(1)) params.Add("lang", "ZH_CN") response, err := FwhHttpGetUnmarshalContext[[]domain.EmsLogRecordInfo](ctx, getFullEnergyUrl("/hes-gateway/manage/iotDeviceFileRecords"), params) if err != nil { return nil, err } return response, nil } // 查询EMS日志下载地址 func QueryEmsLogCallRecord(sn string, dateStr string, logType int) (*domain.EmsLogRecordInfo, error) { return QueryEmsLogCallRecordAfter(sn, dateStr, logType, -1) } // LatestEmsLogCallRecordID captures the existing record before a new call. // A later poll must ignore records at or below this ID. func LatestEmsLogCallRecordID(sn string, dateStr string, logType int) (int64, error) { return LatestEmsLogCallRecordIDContext(context.Background(), sn, dateStr, logType) } func LatestEmsLogCallRecordIDContext(ctx context.Context, sn string, dateStr string, logType int) (int64, error) { callRecordRes, err := queryEmsLogCallRecord(ctx, sn) if err != nil { return 0, fmt.Errorf("[LatestEmsLogCallRecordID] %w", err) } if callRecordRes.Code != 200 { return 0, fmt.Errorf("[LatestEmsLogCallRecordID] 查询失败 code: %d, message: %s", callRecordRes.Code, callRecordRes.Message) } if callRecordRes.Result == nil { return 0, nil } if latest := latestMatchingEmsLogRecord(*callRecordRes.Result, dateStr, logType, -1); latest != nil { return latest.Id, nil } return 0, nil } func QueryEmsLogCallRecordAfter(sn string, dateStr string, logType int, afterID int64) (*domain.EmsLogRecordInfo, error) { return QueryEmsLogCallRecordAfterContext(context.Background(), sn, dateStr, logType, afterID) } func QueryEmsLogCallRecordAfterContext(ctx context.Context, sn string, dateStr string, logType int, afterID int64) (*domain.EmsLogRecordInfo, error) { // 查询告警快照数据 callRecordRes, err := queryEmsLogCallRecord(ctx, sn) if err != nil { return nil, fmt.Errorf("[QueryEmsLogCallRecord] %s", err.Error()) } if callRecordRes.Code != 200 { return nil, fmt.Errorf("[QueryEmsLogCallRecord] 查询失败 code: %d, message: %s", callRecordRes.Code, callRecordRes.Message) } if callRecordRes.Result == nil { return nil, fmt.Errorf("[QueryEmsLogCallRecord] 结果为空") } latest := latestMatchingEmsLogRecord(*callRecordRes.Result, dateStr, logType, afterID) if latest == nil { return nil, fmt.Errorf("[QueryEmsLogCallRecord] 没有召唤日志") } return latest, nil } func latestMatchingEmsLogRecord(records []domain.EmsLogRecordInfo, dateStr string, logType int, afterID int64) *domain.EmsLogRecordInfo { var latest *domain.EmsLogRecordInfo for _, record := range records { if record.LogTime == dateStr && record.CodeType == logType && record.Id > afterID && (latest == nil || record.Id > latest.Id) { current := record latest = ¤t } } return latest } // 请求下载EMS日志文件 func QueryDownloadEmsLogFile(filePath string) ([]byte, error) { return QueryDownloadEmsLogFileContext(context.Background(), filePath) } func QueryDownloadEmsLogFileContext(ctx context.Context, filePath string) ([]byte, error) { response, err := FwhHttpGetContext(ctx, getFullEnergyUrl("/hes-upload/v1/file/download/"+filePath), url.Values{}) if err != nil { return nil, err } return response.Body, nil }