-
Notifications
You must be signed in to change notification settings - Fork 2
feat: DAQ Heartbeat #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicholasching
wants to merge
4
commits into
daq
Choose a base branch
from
daq-nick-heartbeat
base: daq
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: DAQ Heartbeat #546
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type HeartbeatHandler struct { | ||
| serverURL string | ||
| vehicleID string | ||
| sessionID string | ||
| can0 net.Conn | ||
| can1 net.Conn | ||
| logger *zap.Logger | ||
| } | ||
|
|
||
| func NewHeartbeatHandler(can0, can1 net.Conn, logger *zap.Logger) *HeartbeatHandler { | ||
|
|
||
| // Configuring Vehicle ID | ||
| vehicleID := os.Getenv("VEHICLE_ID") | ||
| if vehicleID == "" { | ||
| logger.Error("VEHICLE_ID not found, using default.") | ||
| vehicleID = "default" | ||
| } | ||
|
|
||
| // Generating Session ID | ||
| sessionBytes := make([]byte, 16) | ||
| _, err := rand.Read(sessionBytes) | ||
| var sessionID string | ||
| if err != nil { | ||
| logger.Warn("Failed to generate session ID, defaulting to time based session ID.") | ||
| sessionID = fmt.Sprintf("fallback-%d", time.Now().UnixNano()) | ||
| } else { | ||
| sessionID = hex.EncodeToString(sessionBytes) | ||
| } | ||
|
|
||
| serverURL := os.Getenv("SERVER_URL") | ||
| if serverURL == "" { | ||
| logger.Error("SERVER_URL for heartbeatnot found") | ||
| } | ||
|
|
||
| return &HeartbeatHandler{ | ||
| serverURL: serverURL, | ||
| vehicleID: vehicleID, | ||
| sessionID: sessionID, | ||
| can0: can0, | ||
| can1: can1, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| func (h *HeartbeatHandler) SendHeartbeat() error { | ||
| can0Active, can1Active := h.checkCAN() | ||
|
|
||
| payload := map[string]interface{}{ | ||
| "timestamp": time.Now().UnixMilli(), | ||
| "vehicle_id": h.vehicleID, | ||
| "session_id": h.sessionID, | ||
| "can_status": map[string]bool{ | ||
| "can0": can0Active, | ||
| "can1": can1Active, | ||
| }, | ||
| } | ||
|
|
||
| jsonPayload, err := json.Marshal(payload) | ||
| if err != nil { | ||
| h.logger.Error("Failed to convert heartbeat payload to JSON", zap.Error(err)) | ||
| return err | ||
| } | ||
|
|
||
| response, err := http.Post(h.serverURL, "application/json", bytes.NewBuffer(jsonPayload)) | ||
| if err != nil { | ||
| h.logger.Error("Failed to send heartbeat", zap.Error(err)) | ||
| return err | ||
| } | ||
| defer response.Body.Close() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (h *HeartbeatHandler) checkCAN() (bool, bool) { | ||
| can0Active := h.can0 != nil | ||
| can1Active := h.can1 != nil | ||
| return can0Active, can1Active | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,8 @@ func main() { | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| logger, _ := zap.NewDevelopment() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| heartbeat := NewHeartbeatHandler(can0, can1, logger) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| telemetry, err := NewTelemetryHandler("./can_cache.sqlite") | ||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| panic(err) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -41,13 +43,22 @@ func main() { | |||||||||||||||||||||||||||||||||||||||||||
| manager1.Start(context.Background()) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| uploadTimer := time.NewTimer(time.Second) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| heartbeatInterval := time.NewTicker(3 * time.Second) | ||||||||||||||||||||||||||||||||||||||||||||
| defer heartbeatInterval.Stop() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||||||||||||||||||
| select { | ||||||||||||||||||||||||||||||||||||||||||||
| case <-uploadTimer.C: | ||||||||||||||||||||||||||||||||||||||||||||
| err = telemetry.Upload() | ||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| fmt.Printf("failed to upload telemetry data: %v\n", err) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| case <-heartbeatInterval.C: | ||||||||||||||||||||||||||||||||||||||||||||
| err = heartbeat.SendHeartbeat() | ||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| logger.Error("Failed to send heartbeat", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we name both to Interval? Feels weird to have one be uploadTimer and heartbeatInterval
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't touched go in a while but can you confirm if this actually checks if the can channels are active? (assuming that this is the goal)
are we certain that if HeartbeatHandler -> can0 : net.Conn is simply initialized that it would mean its active/online?
Again I'm not really sure how this works so just lmk if its right or not.