Skip to content

Commit 60bd526

Browse files
committed
[fix] run multi sensors too :)
1 parent 3178f04 commit 60bd526

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

cmd/mqtt-executor/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func main() {
5858

5959
//register trigger and sensors
6060
trigger.Initialise(byte(*Config.SubscribeQOS), byte(*Config.PublishQOS), Config.TopicConfigurations.Trigger)
61-
sensorWorker.Initialise(byte(*Config.PublishQOS), Config.TopicConfigurations.Sensor)
61+
sensorWorker.Initialise(byte(*Config.PublishQOS), Config.TopicConfigurations.Sensors())
6262

6363
// wait for interrupt
6464
<-signals

internal/mqtt/config/topic.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,25 @@ type Trigger struct {
3333
Command Command `json:"command"`
3434
}
3535

36-
type Sensor struct {
37-
Name string `json:"name"`
36+
type GeneralSensor struct {
3837
ResultTopic string `json:"topic"`
3938
Retained bool `json:"retained"`
4039
Interval Interval `json:"interval"`
41-
Unit string `json:"unit"`
42-
Icon string `json:"icon"`
4340
Command Command `json:"command"`
4441
}
4542

43+
type Sensor struct {
44+
GeneralSensor
45+
46+
Name string `json:"name"`
47+
Unit string `json:"unit"`
48+
Icon string `json:"icon"`
49+
}
50+
4651
type MultiSensor struct {
47-
ResultTopic string `json:"topic"`
48-
Retained bool `json:"retained"`
49-
Interval Interval `json:"interval"`
50-
Command Command `json:"command"`
51-
Values []MultiSensorValue `json:"values"`
52+
GeneralSensor
53+
54+
Values []MultiSensorValue `json:"values"`
5255
}
5356

5457
type MultiSensorValue struct {
@@ -104,6 +107,17 @@ func LoadTopicConfiguration(configFilePath, deviceId string) (TopicConfiguration
104107
return topicConfig, nil
105108
}
106109

110+
func (t *TopicConfigurations) Sensors() []GeneralSensor {
111+
sensors := make([]GeneralSensor, 0, len(t.Sensor)+len(t.MultiSensor))
112+
for _, sensor := range t.Sensor {
113+
sensors = append(sensors, sensor.GeneralSensor)
114+
}
115+
for _, sensor := range t.MultiSensor {
116+
sensors = append(sensors, sensor.GeneralSensor)
117+
}
118+
return sensors
119+
}
120+
107121
func (t *TopicConfigurations) validate() error {
108122
if t.Availability != nil {
109123
if err := checkTopicName(t.Availability.Topic); err != nil {

internal/mqtt/sensor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type SensorWorker struct {
1818
MqttClient MQTT.Client
1919
}
2020

21-
func (s *SensorWorker) Initialise(publishQOS byte, sensorConfigs []config.Sensor) {
21+
func (s *SensorWorker) Initialise(publishQOS byte, sensorConfigs []config.GeneralSensor) {
2222

2323
//generate a context so that we can cancel it later (see Close func)
2424
var ctx context.Context
@@ -30,7 +30,7 @@ func (s *SensorWorker) Initialise(publishQOS byte, sensorConfigs []config.Sensor
3030
}
3131
}
3232

33-
func (s *SensorWorker) runSensor(ctx context.Context, publishQOS byte, sensorConf config.Sensor) {
33+
func (s *SensorWorker) runSensor(ctx context.Context, publishQOS byte, sensorConf config.GeneralSensor) {
3434
defer s.waitGroup.Done()
3535

3636
//first execution
@@ -48,7 +48,7 @@ func (s *SensorWorker) runSensor(ctx context.Context, publishQOS byte, sensorCon
4848
}
4949
}
5050

51-
func (s *SensorWorker) executeCommand(ctx context.Context, publishQOS byte, sensorConf config.Sensor) {
51+
func (s *SensorWorker) executeCommand(ctx context.Context, publishQOS byte, sensorConf config.GeneralSensor) {
5252
output, execErr := s.Executor.ExecuteCommandWithContext(sensorConf.Command.Name, sensorConf.Command.Arguments, ctx)
5353
if execErr != nil {
5454
s.MqttClient.Publish(sensorConf.ResultTopic, publishQOS, sensorConf.Retained, "<FAILED>;"+execErr.Error())

0 commit comments

Comments
 (0)