Skip to content

Commit

Permalink
add network interface to connectivity event proto (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
agbpatro authored Dec 4, 2024
1 parent f3cd65d commit 7a3be80
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 39 deletions.
9 changes: 5 additions & 4 deletions datastore/simple/transformers/vehicle_connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
// VehicleConnectivityToMap converts a VehicleConnectivity proto message to a map representation
func VehicleConnectivityToMap(vehicleConnectivity *protos.VehicleConnectivity) map[string]interface{} {
return map[string]interface{}{
"Vin": vehicleConnectivity.GetVin(),
"ConnectionID": vehicleConnectivity.GetConnectionId(),
"Status": vehicleConnectivity.GetStatus().String(),
"CreatedAt": vehicleConnectivity.CreatedAt.AsTime().Unix(),
"Vin": vehicleConnectivity.GetVin(),
"ConnectionID": vehicleConnectivity.GetConnectionId(),
"NetworkInterface": vehicleConnectivity.GetNetworkInterface(),
"Status": vehicleConnectivity.GetStatus().String(),
"CreatedAt": vehicleConnectivity.CreatedAt.AsTime().Unix(),
}
}
12 changes: 7 additions & 5 deletions datastore/simple/transformers/vehicle_connectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ var _ = Describe("VehicleConnectivity", func() {

BeforeEach(func() {
connectivity = &protos.VehicleConnectivity{
Vin: "Vin1",
ConnectionId: "connection1",
CreatedAt: timestamppb.New(time.Now()),
Status: protos.ConnectivityEvent_CONNECTED,
Vin: "Vin1",
ConnectionId: "connection1",
NetworkInterface: "wifi",
CreatedAt: timestamppb.New(time.Now()),
Status: protos.ConnectivityEvent_CONNECTED,
}
})

It("includes all expected data", func() {
result := transformers.VehicleConnectivityToMap(connectivity)
Expect(result).To(HaveLen(4))
Expect(result).To(HaveLen(5))
Expect(result["Vin"]).To(Equal("Vin1"))
Expect(result["ConnectionID"]).To(Equal("connection1"))
Expect(result["NetworkInterface"]).To(Equal("wifi"))
Expect(result["CreatedAt"]).To(BeNumerically("~", time.Now().Unix(), 1))
Expect(result["Status"]).To(Equal("CONNECTED"))
})
Expand Down
8 changes: 4 additions & 4 deletions protos/python/vehicle_connectivity_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protos/ruby/vehicle_connectivity_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 25 additions & 14 deletions protos/vehicle_connectivity.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protos/vehicle_connectivity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ message VehicleConnectivity {
string connection_id = 2;
ConnectivityEvent status = 3;
google.protobuf.Timestamp created_at = 4;
string network_interface = 5;
}

// ConnectivityEvent represents connection state of the vehicle
Expand Down
9 changes: 5 additions & 4 deletions server/streaming/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ func (s *Server) dispatchConnectivityEvent(sm *SocketManager, serializer *teleme
}

connectivityMessage := &protos.VehicleConnectivity{
Vin: sm.requestIdentity.DeviceID,
ConnectionId: sm.UUID,
CreatedAt: timestamppb.Now(),
Status: event,
Vin: sm.requestIdentity.DeviceID,
ConnectionId: sm.UUID,
NetworkInterface: sm.GetNetworkInterface(),
CreatedAt: timestamppb.Now(),
Status: event,
}

payload, err := proto.Marshal(connectivityMessage)
Expand Down
9 changes: 9 additions & 0 deletions server/streaming/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ func buildRequestContext(ctx context.Context) (logInfo map[string]interface{}, s
return
}

// GetNetworkInterface returns value from request headers
func (sm *SocketManager) GetNetworkInterface() string {
networkInterfaceData, ok := sm.requestInfo["network_interface"]
if !ok {
return ""
}
return networkInterfaceData.(string)
}

// ListenToWriteChannel to the write channel
func (sm *SocketManager) ListenToWriteChannel() SocketMessage {
msg := <-sm.writeChan
Expand Down
27 changes: 21 additions & 6 deletions server/streaming/socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package streaming_test

import (
"context"
"net/http"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -18,17 +19,18 @@ import (

var _ = Describe("Socket test", func() {
var (
conf *config.Config
logger *logrus.Logger
hook *test.Hook
serializer *telemetry.BinarySerializer
sm *streaming.SocketManager
conf *config.Config
logger *logrus.Logger
hook *test.Hook
serializer *telemetry.BinarySerializer
sm *streaming.SocketManager
requestIdentity *telemetry.RequestIdentity
)

BeforeEach(func() {
conf = CreateTestConfig()
logger, hook = logrus.NoOpLogger()
requestIdentity := &telemetry.RequestIdentity{
requestIdentity = &telemetry.RequestIdentity{
DeviceID: "42",
SenderID: "vehicle_device.42",
}
Expand Down Expand Up @@ -127,5 +129,18 @@ var _ = Describe("Socket test", func() {

Expect(string(streamMessage.MessageTopic)).To(Equal("canlogs"))
})

It("empty network interface", func() {
Expect(sm.GetNetworkInterface()).To(BeEmpty())
})

It("with request in context", func() {
req, err := http.NewRequest("GET", "/test", nil)
Expect(err).NotTo(HaveOccurred())
req.Header.Add("X-Network-Interface", "cellular")
ctx := context.WithValue(context.Background(), streaming.SocketContext, map[string]interface{}{"request": req})
sm := streaming.NewSocketManager(ctx, requestIdentity, nil, conf, logger)
Expect(sm.GetNetworkInterface()).To(Equal("cellular"))
})
})
})
1 change: 1 addition & 0 deletions test/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func VerifyConnectivityMessageBody(body []byte) {
err := proto.Unmarshal(body, payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload.GetVin()).To(Equal(deviceID))
Expect(payload.GetNetworkInterface()).To(Equal("wifi"))
Expect(payload.GetConnectionId()).NotTo(BeEmpty())
Expect(payload.GetCreatedAt().AsTime().Unix()).NotTo(BeEquivalentTo(0))
Expect(payload.GetStatus()).To(Or(Equal(protos.ConnectivityEvent_CONNECTED), Equal(protos.ConnectivityEvent_DISCONNECTED)))
Expand Down
4 changes: 3 additions & 1 deletion test/integration/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func CreateWebSocket(tlsConfig *tls.Config) *websocket.Conn {
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: tlsConfig,
}
c, _, err := tlsDialer.Dial(u.String(), http.Header{})
headers := http.Header{}
headers.Add("X-Network-Interface", "wifi")
c, _, err := tlsDialer.Dial(u.String(), headers)
Expect(err).NotTo(HaveOccurred())
return c
}
Expand Down

0 comments on commit 7a3be80

Please sign in to comment.