Skip to content

Commit

Permalink
handle multiple response hex case
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesReate committed Oct 11, 2024
1 parent 7485351 commit bf923ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/loggers/dbc_formula_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func ExtractAndDecodeWithDBCFormula(hexData, pid, formula string) (float64, stri
// Find the index of PID in the sliced hex string
pidIndex := strings.Index(strings.ToLower(slicedHexData), strings.ToLower(pid))
if pidIndex == -1 {
// todo - is this always the case that the PID will be returned in resp?
return 0, "", errors.New("PID not found")
}

Expand Down
18 changes: 13 additions & 5 deletions internal/worker_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,8 @@ func (wr *workerRunner) queryOBD(powerStatus *api.PowerStatusResponse) {
} else {
// Python formulas to DBC project - CAN frame dumps for first 2 requests.
if wr.signalDumpFramesQ.ShouldCaptureReq(request) {
// what if we get error responses but then we get a success,
// we want to prioritize the successful capture
// ok to query multiple times until get successful response
// todo improvement: what if we get error responses but then we get a success,
// we want to prioritize the successful capture. Should query multiple times until get successful response
wr.queryPIDAndCaptureDump(request)
continue // skip this one
}
Expand Down Expand Up @@ -430,10 +429,19 @@ func (wr *workerRunner) queryOBDWithAP(request models.PIDRequest, powerStatus *a
// future: new formula type that could work for proprietary PIDs and could support text, int or float
var value interface{}
if request.FormulaType() == models.Dbc && obdResp.IsHex {
value, _, err = loggers.ExtractAndDecodeWithDBCFormula(obdResp.ValueHex[0], util.UintToHexStr(request.Pid), request.FormulaValue())
// in case there are multiple responses
lastHex := ""
for _, hex := range obdResp.ValueHex {
value, _, err = loggers.ExtractAndDecodeWithDBCFormula(hex, util.UintToHexStr(request.Pid), request.FormulaValue())
lastHex = hex
if err == nil {
break // if no error just continue
}
}
// the last error will be set
if err != nil {
msg := fmt.Sprintf("failed to convert hex response with formula: %s. signal: %s. hex: %s. template: %s",
request.FormulaValue(), request.Name, obdResp.ValueHex[0], wr.pids.TemplateName)
request.FormulaValue(), request.Name, lastHex, wr.pids.TemplateName)
hooks.LogError(wr.logger, err, msg, hooks.WithThresholdWhenLogMqtt(10), hooks.WithStopLogAfter(1))
return
}
Expand Down

0 comments on commit bf923ef

Please sign in to comment.