Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

Commit 9f504b8

Browse files
committed
* use the right qos level
* use retained flag where it is meaningful
1 parent 646d033 commit 9f504b8

4 files changed

Lines changed: 11 additions & 18 deletions

File tree

cmd/samsung-remote-mqtt/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ var Config applicationConfig
3131
func LoadConfig() {
3232
Config = applicationConfig{
3333
Broker: flag.String("broker", "", "The broker URI. ex: tcp://127.0.0.1:1883"),
34-
SubscribeQOS: flag.Int("sub-qos", 0, "The Quality of Service for subscription 0,1,2 (default 0)"),
35-
PublishQOS: flag.Int("pub-qos", 0, "The Quality of Service for publishing 0,1,2 (default 0)"),
34+
SubscribeQOS: flag.Int("sub-qos", 1, "The Quality of Service for subscription 0,1,2 (default 1)"),
35+
PublishQOS: flag.Int("pub-qos", 1, "The Quality of Service for publishing 0,1,2 (default 1)"),
3636
Username: flag.String("user", "", "The User (optional)"),
3737
Password: flag.String("password", "", "The password (optional)"),
3838
ClientId: flag.String("client-id", "samsung-remote", "The ClientID (default samsung-remote)"),
@@ -83,6 +83,7 @@ func (c *applicationConfig) GetMQTTOpts(
8383
}
8484

8585
opts.WillEnabled = true
86+
opts.WillRetained = true
8687
opts.WillQos = byte(*c.PublishQOS)
8788
opts.WillPayload = []byte(internal.StatusOffline)
8889
opts.WillTopic = fmt.Sprintf("%s/status", *c.TopicPrefix)

internal/mqtt/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (e *Executor) stateWrapper(delegate MQTT.MessageHandler) MQTT.MessageHandle
198198
}
199199

200200
func (e *Executor) sendStatus(client MQTT.Client, message MQTT.Message, state []byte) {
201-
client.Publish(fmt.Sprintf("%s/state", message.Topic()), e.publishQOS, false, state)
201+
client.Publish(fmt.Sprintf("%s/state", message.Topic()), e.publishQOS, true, state)
202202
}
203203

204204
func (e *Executor) sendAnswer(client MQTT.Client, message MQTT.Message, answer []byte, err error) {

internal/mqtt/hassio/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ func (c *Client) PublishDiscoveryConfig() {
8888

8989
targetTopic := fmt.Sprintf("%ssensor/%s_status/config", c.HassioTopicPrefix, c.deviceId())
9090
payload := c.generatePayloadForApplicationStatus()
91-
c.MqttClient.Publish(targetTopic, byte(0), false, payload)
91+
c.MqttClient.Publish(targetTopic, byte(1), true, payload)
9292

9393
targetTopic = fmt.Sprintf("%ssensor/%s_tv_status/config", c.HassioTopicPrefix, c.deviceId())
9494
payload = c.generatePayloadForTvStatus()
95-
c.MqttClient.Publish(targetTopic, byte(0), false, payload)
95+
c.MqttClient.Publish(targetTopic, byte(1), true, payload)
9696

9797
for topic, payload := range c.generatePayloadsForTvWakeUp() {
98-
c.MqttClient.Publish(topic, byte(0), false, payload)
98+
c.MqttClient.Publish(topic, byte(1), true, payload)
9999
}
100100
for keyName, description := range SamsungRemoteKeys {
101101
for topic, payload := range c.generatePayloadsForSendKey(keyName, description) {
102-
c.MqttClient.Publish(topic, byte(0), false, payload)
102+
c.MqttClient.Publish(topic, byte(1), true, payload)
103103
}
104104
}
105105
}

internal/status.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,8 @@ func (s *StatusWorker) runApplicationStatus(statusTopic string, ctx context.Cont
4747
//first one
4848
s.sendStatus(statusTopic, true)
4949

50-
ticker := time.Tick(30 * time.Second)
51-
for {
52-
//wait until next tick or shutdown
53-
select {
54-
case <-ticker:
55-
s.sendStatus(statusTopic, true)
56-
case <-ctx.Done():
57-
return
58-
}
59-
}
50+
//wait until shutdown
51+
<-ctx.Done()
6052
}
6153

6254
func (s *StatusWorker) runTvStatus(statusTopic, tvHost string, ctx context.Context) {
@@ -104,7 +96,7 @@ func (s *StatusWorker) sendStatus(statusTopic string, status bool) MQTT.Token {
10496
payload = StatusOffline
10597
}
10698

107-
return s.MqttClient.Publish(statusTopic, byte(0), false, payload)
99+
return s.MqttClient.Publish(statusTopic, byte(1), true, payload)
108100
}
109101

110102
func isPortOpen(tvHost string) bool {

0 commit comments

Comments
 (0)