项目作者: christianschmizz

项目描述 :
Makes Nuki's Bridge API accessible from the command-line.
高级语言: Go
项目地址: git://github.com/christianschmizz/go-nukibridgeapi.git
创建时间: 2020-10-26T17:58:25Z
项目社区:https://github.com/christianschmizz/go-nukibridgeapi

开源协议:MIT License

下载


go-nukibridgeapi

This project aims to make Nuki’s Bridge API accessible. Either through the
provided library or as a program from the command-line.

The Nuki Bridge acts as gateway between your Nuki
Smartlock
and your Wifi network, which enables
you to access your Nuki devices remotely.

Develop

Therefore it is introducing a command-line tool called nukibridgectl which
implements access to the basic functions of your Nuki bridge for now.

:warning: Encrypted token support: There is currently no automatic detection for
support of encrypted tokens. Users of the library have to make use of the UseEncryptedToken
option to enable the use of encrypted tokens. Therefor the CLI tool only supports hashed tokens.

Activate the Bridge API

Before you can access the bridge’s API you have to activate it. It’s not active
by default.

Check API status

If you are unsure about whether the bridge’s API is already activated or not
you can check with curl:

  1. $ curl http://<bridge-address>

It will come up with HTTP 400 Bad Request when active. Otherwise you’ll see
something like Connection refused.

Activate the API

To activate the bridge API you have to bring you bridge into maintenance mode
go to “Manage Bridge” in the Nuki app. During the Bridge’s setup procedure you
can configure the settings of your Wi-Fi as well as the developer mode. To use
the API you have to enable the developer mode.

The token you need for accessing the API is shown only once when activating
the developer mode. So, make a note of it.

Discover bridges

Note: The discovery of bridges does not require a valid token.

For discovering bridges you need a working internet-connection, and you have to
execute nukibridgectl from the LAN the bridge is in. Afterwards the discover
command should report your bridge.

  1. $ nukibridgectl discover
  2. ID IP Port Updated
  3. 123456789 192.168.178.1 8080 2020-10-01 20:00:00 +0000 UTC

Help

To get a quick overview of the capabilities nukibridgectl offers I recommend
having a look at the help:

  1. $ nukibridgectl --help

Which might look like this:

  1. Work seamlessly with your Nuki Bridge from the command line.
  2. Usage:
  3. nukibridgectl <command> <subcommand> [flags]
  4. nukibridgectl [command]
  5. Available Commands:
  6. bridge Bridge commands
  7. discover Discover bridges
  8. help Help about any command
  9. Flags:
  10. --config string config file (default is $HOME/.nukibridgectl.yaml)
  11. -h, --help help for nukibridgectl
  12. Use "nukibridgectl [command] --help" for more information about a command.

Configuration

Config file

For ease of use I would recommend the use of a settings-file named
.nukibridgectl.yaml in one of the subsequent locations:

  • /etc/nukibridgectl/
  • Your home directory
  • Working dir

The file should look like this:

  1. host: "192.168.178.1:8080"
  2. token: abcde6

To use a custom name or location for your configuration file:

  1. $ nukibridgectl --config /my/config/feelfree.yaml ...

Params

If you want to pass on your configuration at the command-line you can do so, too:

  1. $ nukibridgectl bridge --host 192.168.178.1:8080 --token abcde6 <command>

Examples

List devices

After you set up your configuration you can query the bridge for available devices:

  1. $ nukibridgectl bridge list
  2. Type ID Name Battery Firmware Version
  3. 0 123456789 Wohnungstür 68% 2.9.10
  4. 2 123456789 Haustür 0% 1.6.4

A type of 0 means a Smartlock, a type of 2 an Opener.

Simple Locking / Unlocking of a Nuki Smartlock

To just lock/unlock a smart lock (type: 0) you type:

  1. $ nukibridgectl bridge lock 0 <deviceID>
  2. $ nukibridgectl bridge unlock 0 <deviceID>

Applying more elaborate lock actions

See nuki Bridge’s API docs on lock action for
a list of available actions and their purpose.

  1. # Execute actions at a smart lock (type: 0) via lockAction
  2. $ nukibridgectl bridge lockAction 0 <deviceID> <lock|unlock|unlatch|lockandgo|lockandgowithunlatch>
  3. # Execute actions at an opener (type: 2) via lockAction
  4. $ nukibridgectl bridge lockAction 2 <deviceID> <rto_on|rto_off|esa|cm_on|cm_off>

Query the state

See nuki Bridge’s API docs on lock states for
further details on the available information.

Short reminder from the Nuki docs:

Warning: /lockstate gets the current state directly from the device and so should not be used for constant polling to avoid draining the batteries too fast. /list can be used to get regular updates on the state, as is it cached on the bridge.

  1. $ ./nukibridgectl bridge lockState 2 123456789

Tests

Integration tests

To run the tests on a physical bridge at your LAN:

  1. % make integration-test BRIDGE_HOST=<ip:port> BRIDGE_TOKEN=<token>

Debugging things

You noticed some unexpected behaviour or just want to know whats going on
behind the scenes? You can enable debug logging by setting DEBUG at your env.

  1. $ DEBUG=1 nukibridgectl bridge --host 192.168.178.1:8080 --token abcde6 info

DBus Bridge

nukibridgectl can also forward all events of your Nuki bridge to a DBus
instance. This is useful if you want to open up your Nuki events to a broader
audience, e.g. 3rd party applications.

Therefor nukibridgectl will launch a local webserver and registers itself for
callbacks.

Fire up a local DBus instance for testing

  1. $ MY_SESSION_BUS_SOCKET=/tmp/dbus/$USER.session.usock
  2. $ dbus-daemon --session --nofork --address unix:path=$MY_SESSION_BUS_SOCKET

Run nukibridgectl

  1. $ DBUS_SESSION_BUS_ADDRESS=unix:path=/tmp/dbus/$USER.session.usock DEBUG=1 go run cmd/nukibridgectl/main.go bridge watch en0
  2. 2020-11-21T15:43:58+01:00 DBG bridge.CallbackData{
  3. ... // 5 identical fields
  4. BatteryCritical: false,
  5. KeypadBatteryCritical: false,
  6. - DoorsensorState: 3,
  7. + DoorsensorState: 2,
  8. - DoorsensorStateName: "door opened",
  9. + DoorsensorStateName: "door closed",
  10. RingactionTimestamp: s"0001-01-01 00:00:00 +0000 UTC",
  11. RingactionState: false,
  12. }
  13. 2020-11-21T15:44:04+01:00 DBG bridge.CallbackData{
  14. ... // 5 identical fields
  15. BatteryCritical: false,
  16. KeypadBatteryCritical: false,
  17. - DoorsensorState: 2,
  18. + DoorsensorState: 3,
  19. - DoorsensorStateName: "door closed",
  20. + DoorsensorStateName: "door opened",
  21. RingactionTimestamp: s"0001-01-01 00:00:00 +0000 UTC",
  22. RingactionState: false,
  23. }

Monitoring

  1. $ dbus-monitor --address unix:path=/tmp/$USER.session.usock
  2. signal time=1605969838.072019 sender=:1.0 -> destination=(null destination) serial=7 path=/nuki/bridge; interface=nuki.bridge; member=Event
  3. struct {
  4. int32 597123456
  5. int32 0
  6. int32 2
  7. int32 3
  8. string "unlocked"
  9. boolean false
  10. boolean false
  11. int32 3
  12. string "door opened"
  13. boolean false
  14. }
  15. signal time=1605969844.810805 sender=:1.0 -> destination=(null destination) serial=8 path=/nuki/bridge; interface=nuki.bridge; member=Event
  16. struct {
  17. int32 597123456
  18. int32 0
  19. int32 2
  20. int32 3
  21. string "unlocked"
  22. boolean false
  23. boolean false
  24. int32 2
  25. string "door closed"
  26. boolean false
  27. }