Moved configuration from code to config file
This commit is contained in:
parent
3faba543bf
commit
4c2b397eb5
1 changed files with 32 additions and 13 deletions
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -15,14 +16,15 @@ import (
|
||||||
"github.com/fulr/rfm69/payload"
|
"github.com/fulr/rfm69/payload"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// Configuration defines the config options and file structure
|
||||||
encryptionKey = "0123456789012345"
|
type Configuration struct {
|
||||||
nodeID = 1
|
EncryptionKey string
|
||||||
networkID = 73
|
NodeID byte
|
||||||
isRfm69Hw = true
|
NetworkID byte
|
||||||
mqttBroker = "tcp://localhost:1883"
|
IsRfm69Hw bool
|
||||||
clientID = "rfmGate"
|
MqttBroker string
|
||||||
)
|
MqttClientID string
|
||||||
|
}
|
||||||
|
|
||||||
var defautlPubHandler = func(client *MQTT.Client, msg MQTT.Message) {
|
var defautlPubHandler = func(client *MQTT.Client, msg MQTT.Message) {
|
||||||
fmt.Printf("TOPIC: %s\n", msg.Topic())
|
fmt.Printf("TOPIC: %s\n", msg.Topic())
|
||||||
|
@ -59,9 +61,26 @@ func actorHandler(tx chan *rfm69.Data) func(client *MQTT.Client, msg MQTT.Messag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readConfig() (*Configuration, error) {
|
||||||
|
file, err := os.Open("conf.json")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
config := Configuration{}
|
||||||
|
err = decoder.Decode(&config)
|
||||||
|
file.Close()
|
||||||
|
return &config, err
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Print("Start")
|
log.Print("Reading config")
|
||||||
opts := MQTT.NewClientOptions().AddBroker(mqttBroker).SetClientID(clientID)
|
config, err := readConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Print(config)
|
||||||
|
opts := MQTT.NewClientOptions().AddBroker(config.MqttBroker).SetClientID(config.MqttClientID)
|
||||||
opts.SetDefaultPublishHandler(defautlPubHandler)
|
opts.SetDefaultPublishHandler(defautlPubHandler)
|
||||||
opts.SetCleanSession(true)
|
opts.SetCleanSession(true)
|
||||||
c := MQTT.NewClient(opts)
|
c := MQTT.NewClient(opts)
|
||||||
|
@ -69,12 +88,12 @@ func main() {
|
||||||
if token.Wait() && token.Error() != nil {
|
if token.Wait() && token.Error() != nil {
|
||||||
log.Fatal(token.Error())
|
log.Fatal(token.Error())
|
||||||
}
|
}
|
||||||
rfm, err := rfm69.NewDevice(nodeID, networkID, isRfm69Hw)
|
rfm, err := rfm69.NewDevice(config.NodeID, config.NetworkID, config.IsRfm69Hw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer rfm.Close()
|
defer rfm.Close()
|
||||||
err = rfm.Encrypt([]byte(encryptionKey))
|
err = rfm.Encrypt([]byte(config.EncryptionKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -92,7 +111,7 @@ func main() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case data := <-rx:
|
case data := <-rx:
|
||||||
if data.ToAddress != nodeID {
|
if data.ToAddress != config.NodeID {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
log.Println("got data from", data.FromAddress, ", RSSI", data.Rssi)
|
log.Println("got data from", data.FromAddress, ", RSSI", data.Rssi)
|
||||||
|
|
Loading…
Add table
Reference in a new issue