feat: track jt808 registration identity
This commit is contained in:
@@ -151,6 +151,24 @@ func ParseFrame(raw []byte, receivedAtMS int64, sourceEndpoint string) (envelope
|
||||
if location.TotalMileageKM != nil {
|
||||
fields[envelope.FieldTotalMileageKM] = *location.TotalMileageKM
|
||||
}
|
||||
} else if messageID == 0x0100 {
|
||||
registration, err := parseRegistration(body)
|
||||
if err != nil {
|
||||
return envelope.FrameEnvelope{}, err
|
||||
}
|
||||
parsed["registration"] = registration
|
||||
if deviceID, ok := registration["device_id"].(string); ok {
|
||||
fields["device_id"] = deviceID
|
||||
}
|
||||
if plate, ok := registration["plate"].(string); ok {
|
||||
fields["plate"] = plate
|
||||
}
|
||||
} else if messageID == 0x0102 {
|
||||
authentication := parseAuthentication(body)
|
||||
parsed["authentication"] = authentication
|
||||
if token, ok := authentication["token"].(string); ok {
|
||||
fields["auth_token"] = token
|
||||
}
|
||||
}
|
||||
|
||||
env := envelope.FrameEnvelope{
|
||||
@@ -166,10 +184,36 @@ func ParseFrame(raw []byte, receivedAtMS int64, sourceEndpoint string) (envelope
|
||||
Fields: fields,
|
||||
ParseStatus: envelope.ParseOK,
|
||||
}
|
||||
if registration, ok := parsed["registration"].(map[string]any); ok {
|
||||
env.DeviceID, _ = registration["device_id"].(string)
|
||||
env.Plate, _ = registration["plate"].(string)
|
||||
}
|
||||
env.EventID = env.StableEventID()
|
||||
return env, nil
|
||||
}
|
||||
|
||||
func parseRegistration(body []byte) (map[string]any, error) {
|
||||
if len(body) < 37 {
|
||||
return nil, fmt.Errorf("%w: registration body len=%d", ErrFrameTooShort, len(body))
|
||||
}
|
||||
registration := map[string]any{
|
||||
"province": binary.BigEndian.Uint16(body[0:2]),
|
||||
"city": binary.BigEndian.Uint16(body[2:4]),
|
||||
"manufacturer": fixedText(body[4:9]),
|
||||
"device_type": fixedText(body[9:29]),
|
||||
"device_id": fixedText(body[29:36]),
|
||||
"plate_color": body[36],
|
||||
"plate": freeText(body[37:]),
|
||||
}
|
||||
return registration, nil
|
||||
}
|
||||
|
||||
func parseAuthentication(body []byte) map[string]any {
|
||||
token := freeText(body)
|
||||
out := map[string]any{"token": token}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseLocation(body []byte) (Location, error) {
|
||||
if len(body) < 28 {
|
||||
return Location{}, fmt.Errorf("%w: location body len=%d", ErrFrameTooShort, len(body))
|
||||
@@ -269,6 +313,14 @@ func bcdString(data []byte) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
func fixedText(data []byte) string {
|
||||
return strings.Trim(freeText(data), "\x00 ")
|
||||
}
|
||||
|
||||
func freeText(data []byte) string {
|
||||
return strings.TrimRight(strings.TrimSpace(string(data)), "\x00")
|
||||
}
|
||||
|
||||
func parseBCDTime(data []byte) time.Time {
|
||||
if len(data) != 6 {
|
||||
return time.Time{}
|
||||
|
||||
Reference in New Issue
Block a user