-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MF-1582 - Fix lora-adapter MQTT client (#1583)
* MF-1582 - Fix lora-adapter MQTT clien Signed-off-by: Manuel Imperiale <[email protected]> * Add timeout config to the mqtt subscriber Signed-off-by: Manuel Imperiale <[email protected]> * Rm comment Signed-off-by: Manuel Imperiale <[email protected]> * Add sub timeout Signed-off-by: Manuel Imperiale <[email protected]>
- Loading branch information
Showing
5 changed files
with
142 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,59 @@ | ||
package mqtt | ||
|
||
// LoraSubscribe subscribe to lora server messages | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/mainflux/mainflux/logger" | ||
"github.com/mainflux/mainflux/lora" | ||
|
||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
// Subscriber represents the MQTT broker. | ||
type Subscriber interface { | ||
// Subscribes to given subject and receives events. | ||
Subscribe(string) error | ||
} | ||
|
||
type broker struct { | ||
svc lora.Service | ||
client mqtt.Client | ||
logger logger.Logger | ||
timeout time.Duration | ||
} | ||
|
||
// NewBroker returns new MQTT broker instance. | ||
func NewBroker(svc lora.Service, client mqtt.Client, t time.Duration, log logger.Logger) Subscriber { | ||
return broker{ | ||
svc: svc, | ||
client: client, | ||
logger: log, | ||
timeout: t, | ||
} | ||
} | ||
|
||
// Subscribe subscribes to the Lora MQTT message broker | ||
func (b broker) Subscribe(subject string) error { | ||
s := b.client.Subscribe(subject, 0, b.handleMsg) | ||
if err := s.Error(); s.WaitTimeout(b.timeout) && err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// handleMsg triggered when new message is received on Lora MQTT broker | ||
func (b broker) handleMsg(c mqtt.Client, msg mqtt.Message) { | ||
m := lora.Message{} | ||
if err := json.Unmarshal(msg.Payload(), &m); err != nil { | ||
b.logger.Warn(fmt.Sprintf("Failed to unmarshal message: %s", err.Error())) | ||
return | ||
} | ||
|
||
b.svc.Publish(context.Background(), m) | ||
return | ||
} |