feat: expose realtime snapshot protocols
This commit is contained in:
@@ -10,6 +10,7 @@ type Snapshot struct {
|
|||||||
VehicleKey string `json:"vehicle_key"`
|
VehicleKey string `json:"vehicle_key"`
|
||||||
VIN string `json:"vin"`
|
VIN string `json:"vin"`
|
||||||
Protocol envelope.Protocol `json:"protocol,omitempty"`
|
Protocol envelope.Protocol `json:"protocol,omitempty"`
|
||||||
|
Protocols []envelope.Protocol `json:"protocols,omitempty"`
|
||||||
EventID string `json:"event_id,omitempty"`
|
EventID string `json:"event_id,omitempty"`
|
||||||
EventTimeMS int64 `json:"event_time_ms"`
|
EventTimeMS int64 `json:"event_time_ms"`
|
||||||
ReceivedAtMS int64 `json:"received_at_ms"`
|
ReceivedAtMS int64 `json:"received_at_ms"`
|
||||||
|
|||||||
@@ -53,6 +53,11 @@ func (r *Repository) Update(ctx context.Context, env envelope.FrameEnvelope) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protocols, err := r.addProtocol(ctx, vehicleKey, env.Protocol)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
merged, err := r.GetMerged(ctx, vehicleKey)
|
merged, err := r.GetMerged(ctx, vehicleKey)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && !errors.Is(err, redis.Nil) {
|
||||||
return err
|
return err
|
||||||
@@ -69,15 +74,12 @@ func (r *Repository) Update(ctx context.Context, env envelope.FrameEnvelope) err
|
|||||||
merged.ReceivedAtMS = env.ReceivedAtMS
|
merged.ReceivedAtMS = env.ReceivedAtMS
|
||||||
merged.SourceEndpoint = env.SourceEndpoint
|
merged.SourceEndpoint = env.SourceEndpoint
|
||||||
}
|
}
|
||||||
|
merged.Protocols = protocols
|
||||||
merged.UpdatedAtMS = nowMS
|
merged.UpdatedAtMS = nowMS
|
||||||
if err := r.setJSON(ctx, mergedKey(vehicleKey), merged, r.cfg.ttl()); err != nil {
|
if err := r.setJSON(ctx, mergedKey(vehicleKey), merged, r.cfg.ttl()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
protocols, err := r.addProtocol(ctx, vehicleKey, env.Protocol)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
online := OnlineStatus{
|
online := OnlineStatus{
|
||||||
VehicleKey: vehicleKey,
|
VehicleKey: vehicleKey,
|
||||||
VIN: vin,
|
VIN: vin,
|
||||||
|
|||||||
@@ -54,6 +54,9 @@ func TestRepositoryUpdatesMergedAndProtocolSnapshots(t *testing.T) {
|
|||||||
if merged.Fields[envelope.FieldLatitude] != 30.5 {
|
if merged.Fields[envelope.FieldLatitude] != 30.5 {
|
||||||
t.Fatalf("new latitude missing: %#v", merged.Fields)
|
t.Fatalf("new latitude missing: %#v", merged.Fields)
|
||||||
}
|
}
|
||||||
|
if got := protocolNames(merged.Protocols); strings.Join(got, ",") != "GB32960,JT808" {
|
||||||
|
t.Fatalf("merged protocols = %#v", got)
|
||||||
|
}
|
||||||
protocol, err := repo.GetProtocol(ctx, "VIN001", envelope.ProtocolJT808)
|
protocol, err := repo.GetProtocol(ctx, "VIN001", envelope.ProtocolJT808)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetProtocol() error = %v", err)
|
t.Fatalf("GetProtocol() error = %v", err)
|
||||||
@@ -237,3 +240,11 @@ func newTestRepository(t *testing.T) (*Repository, func()) {
|
|||||||
func stringsContains(value string, pattern string) bool {
|
func stringsContains(value string, pattern string) bool {
|
||||||
return strings.Contains(value, pattern)
|
return strings.Contains(value, pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func protocolNames(protocols []envelope.Protocol) []string {
|
||||||
|
names := make([]string, 0, len(protocols))
|
||||||
|
for _, protocol := range protocols {
|
||||||
|
names = append(names, string(protocol))
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user