From 46754db2ba17bb9e6d359481a4f593f1232c5e7e Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Tue, 13 Apr 2021 09:13:28 -0400 Subject: [PATCH] Introduce /etc/default/mijia-hub for configuration --- defaults.go | 2 +- main.go | 10 +++++++--- nfpm.yaml | 4 +++- package/default/mijia-hub | 4 ++++ package/systemd/mijia-hub.service | 3 ++- sensor.go | 4 ++-- 6 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 package/default/mijia-hub diff --git a/defaults.go b/defaults.go index ca76316..db42945 100644 --- a/defaults.go +++ b/defaults.go @@ -1,4 +1,4 @@ package main const defaultStateDirectory = "/var/lib/mijia-hub" -const defaultPin = "11223344" +const defaultPin = "12233445" diff --git a/main.go b/main.go index c358bf3..c90ecb0 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "log" "os" "path" @@ -8,9 +9,9 @@ import ( var sensors map[string]*Sensor = map[string]*Sensor{} -func AddSensor(address string, data SensorData) { +func AddSensor(address string, data SensorData, pin string) { if _, found := sensors[address]; !found { - sensor, err := NewSensor(address, data) + sensor, err := NewSensor(address, data, pin) if err != nil { log.Printf("Could not create sensor <%s>: %s", address, err) return @@ -40,6 +41,9 @@ func createDirectory(path string) error { } func main() { + pin := flag.String("pin", defaultPin, "pin used to pair new sensors") + flag.Parse() + log.Printf("Creating directory <%s>", defaultStateDirectory) if err := createDirectory(defaultStateDirectory); err != nil { log.Fatalf("Could not create <%s>: %s", defaultStateDirectory, err) @@ -78,7 +82,7 @@ func main() { switch event.(type) { case EventDiscoveredSensor: log.Println("EventDiscoveredSensor") - AddSensor(event.(EventDiscoveredSensor).Address, event.(EventDiscoveredSensor).Data) + AddSensor(event.(EventDiscoveredSensor).Address, event.(EventDiscoveredSensor).Data, *pin) case EventReceivedSensorData: log.Println("EventReceivedSensorData") UpdateSensor(event.(EventReceivedSensorData).Address, event.(EventReceivedSensorData).Data) diff --git a/nfpm.yaml b/nfpm.yaml index 061ff08..70eff5b 100644 --- a/nfpm.yaml +++ b/nfpm.yaml @@ -5,7 +5,7 @@ name: "mijia-hub" arch: "armhf" platform: "linux" -version: "v0.1.0" +version: "v0.2.0" section: "default" priority: "extra" maintainer: "Stefan Arentz " @@ -17,6 +17,8 @@ contents: dst: /usr/sbin/mijia-hub - src: ./package/systemd/mijia-hub.service dst: /etc/systemd/system/mijia-hub.service + - src: ./package/default/mijia-hub + dst: /etc/default/mijia-hub empty_folders: - /var/lib/mijia-hub - /var/lib/mijia-hub/storage diff --git a/package/default/mijia-hub b/package/default/mijia-hub new file mode 100644 index 0000000..fcadce1 --- /dev/null +++ b/package/default/mijia-hub @@ -0,0 +1,4 @@ +# Pin used to pair devices. The same pin is used for all +# devices. Must be 8 digits. + +PIN="12233445" diff --git a/package/systemd/mijia-hub.service b/package/systemd/mijia-hub.service index d17a347..cf47aff 100644 --- a/package/systemd/mijia-hub.service +++ b/package/systemd/mijia-hub.service @@ -4,8 +4,9 @@ Requires=hciuart.service After=network.target hciuart.service [Service] +EnvironmentFile=/etc/default/mijia-hub Type=simple -ExecStart=/usr/sbin/mijia-hub +ExecStart=/usr/sbin/mijia-hub -pin $PIN WorkingDirectory=/var/lib/mijia-hub [Install] diff --git a/sensor.go b/sensor.go index 3fa5206..af8a0da 100644 --- a/sensor.go +++ b/sensor.go @@ -19,7 +19,7 @@ type Sensor struct { transport hc.Transport } -func NewSensor(address string, data SensorData) (*Sensor, error) { +func NewSensor(address string, data SensorData, pin string) (*Sensor, error) { info := accessory.Info{ Name: strings.ToUpper(fmt.Sprintf("ATC_%s%s%s", address[9:11], address[12:14], address[15:17])), Manufacturer: "Mijia", @@ -47,7 +47,7 @@ func NewSensor(address string, data SensorData) (*Sensor, error) { config := hc.Config{ StoragePath: path.Join(storageRoot, info.Name), - Pin: defaultPin, + Pin: pin, } transport, err := hc.NewIPTransport(config, sensor.Accessory)