feat(gateway): added reply to the POST request with the latest measurement

Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
2025-05-25 12:06:41 +02:00
parent bf3d7cbb59
commit cebe5e4930
6 changed files with 98 additions and 23 deletions

View File

@@ -41,6 +41,7 @@ func (m *Gateway) processUpdate(_ paho.Client, message paho.Message) {
err := json.Unmarshal(message.Payload(), &event)
if err != nil {
log.Errorf("invalid update event payload: %s", string(message.Payload()))
return
}
// Skip processing if no data points are provided.
@@ -48,6 +49,39 @@ func (m *Gateway) processUpdate(_ paho.Client, message paho.Message) {
return
}
// Check if there's a pending request for this user, room, and device
if m.pendingUser == user && m.pendingRoom == room && m.pendingDevice == device {
// Create a slice to hold the measurements
var measurements []map[string]interface{}
currentTime := time.Now().Format(time.RFC3339Nano)
// Convert each data point to a measurement
for field, value := range event.DataPoints {
// Create a measurement for this field
measurement := map[string]interface{}{
"time": currentTime,
"type": field,
"value": value,
}
measurements = append(measurements, measurement)
}
// Convert measurements to JSON
if len(measurements) > 0 {
responseData, err := json.Marshal(measurements)
if err == nil {
// Send the data as a response
select {
case m.pendingChan <- string(responseData):
// Response sent successfully
default:
// Channel is full or closed, log an error
log.Errorf("failed to send response to pending channel")
}
}
}
}
// Prepare the InfluxDB point with tags and fields.
MEASUREMENT_NAME, ok := os.LookupEnv("MEASUREMENT_NAME")
if !ok {