feat(db): get mapping dynamically from file
Assisted-by: Junie:claude-sonnet-4.6 Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
@@ -92,13 +92,9 @@ func influxConnection() *influx.InfluxGateway {
|
|||||||
// @BasePath /api/v1
|
// @BasePath /api/v1
|
||||||
// @securityDefinitions.basic BasicAuth
|
// @securityDefinitions.basic BasicAuth
|
||||||
func main() {
|
func main() {
|
||||||
// Load mapping configuration
|
// Load mapping configuration (reloaded dynamically on each access)
|
||||||
mappingPath := getEnv("MAPPING_CONFIG_PATH", "mapping.json")
|
mappingPath := getEnv("MAPPING_CONFIG_PATH", "mapping.json")
|
||||||
mapping, err := LoadMapping(mappingPath)
|
mapping := NewDynamicMapping(mappingPath)
|
||||||
if err != nil {
|
|
||||||
log.Printf("[Main] Warning: could not load mapping file: %v. Using defaults.", err)
|
|
||||||
mapping = EmptyMapping()
|
|
||||||
}
|
|
||||||
|
|
||||||
mqttGateway := mqttConnection()
|
mqttGateway := mqttConnection()
|
||||||
defer mqttGateway.Disconnect()
|
defer mqttGateway.Disconnect()
|
||||||
@@ -112,7 +108,7 @@ func main() {
|
|||||||
provenceMeasurement := point.CreateMeasurement[ProvenceData](measurementName)
|
provenceMeasurement := point.CreateMeasurement[ProvenceData](measurementName)
|
||||||
// The incoming MQTT topic structure is: <gateway_id>/<node_id>/update
|
// The incoming MQTT topic structure is: <gateway_id>/<node_id>/update
|
||||||
topicStructur := []string{"gateway", "node"}
|
topicStructur := []string{"gateway", "node"}
|
||||||
err = mqtt.SubscribeTyped(mqttGateway, "+/+/update", provenceMeasurement, topicStructur, func(dp point.DataPoint[ProvenceData]) {
|
err := mqtt.SubscribeTyped(mqttGateway, "+/+/update", provenceMeasurement, topicStructur, func(dp point.DataPoint[ProvenceData]) {
|
||||||
var gatewayID, nodeID string
|
var gatewayID, nodeID string
|
||||||
for _, tag := range dp.Tags() {
|
for _, tag := range dp.Tags() {
|
||||||
switch tag.Subject {
|
switch tag.Subject {
|
||||||
|
|||||||
@@ -23,6 +23,45 @@ type MappingConfig struct {
|
|||||||
nodeToRoom map[string]string
|
nodeToRoom map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DynamicMapping reloads the mapping file on every access so that changes
|
||||||
|
// to the JSON file take effect without restarting the programme.
|
||||||
|
type DynamicMapping struct {
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDynamicMapping creates a DynamicMapping that reads from the given path.
|
||||||
|
func NewDynamicMapping(path string) *DynamicMapping {
|
||||||
|
return &DynamicMapping{path: path}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DynamicMapping) load() *MappingConfig {
|
||||||
|
cfg, err := LoadMapping(d.path)
|
||||||
|
if err != nil {
|
||||||
|
return EmptyMapping()
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DynamicMapping) GetCampus(gatewayID string) (string, bool) {
|
||||||
|
return d.load().GetCampus(gatewayID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DynamicMapping) GetRoom(nodeID string) (string, bool) {
|
||||||
|
return d.load().GetRoom(nodeID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DynamicMapping) NodesForRoom(room string) []string {
|
||||||
|
return d.load().NodesForRoom(room)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DynamicMapping) AllNodes() []string {
|
||||||
|
return d.load().AllNodes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DynamicMapping) Rooms() []string {
|
||||||
|
return d.load().Rooms()
|
||||||
|
}
|
||||||
|
|
||||||
// EmptyMapping returns a MappingConfig with no entries.
|
// EmptyMapping returns a MappingConfig with no entries.
|
||||||
// GetCampus and GetRoom will return their "unknown_*" fallback values.
|
// GetCampus and GetRoom will return their "unknown_*" fallback values.
|
||||||
func EmptyMapping() *MappingConfig {
|
func EmptyMapping() *MappingConfig {
|
||||||
|
|||||||
Reference in New Issue
Block a user