Skip to content

Commit

Permalink
Introduce /etc/default/mijia-hub for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
st3fan committed Apr 13, 2021
1 parent cec3fea commit 46754db
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

const defaultStateDirectory = "/var/lib/mijia-hub"
const defaultPin = "11223344"
const defaultPin = "12233445"
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"flag"
"log"
"os"
"path"
)

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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion nfpm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stefan@arentz.ca>"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions package/default/mijia-hub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Pin used to pair devices. The same pin is used for all
# devices. Must be 8 digits.

PIN="12233445"
3 changes: 2 additions & 1 deletion package/systemd/mijia-hub.service
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 46754db

Please sign in to comment.