Jump to content

Shell Bindings Prototype


photron

Recommended Posts

Here's a first prototype of the shell bindings. They are written in Python for portability and use the argparse module for the command line handling.

 

Host and port default to localhost and 4223, they can be changed with the --host and --port option:

 

tinkerforge --host 192.168.178.42

 

Enumerate

 

It can enumerate connected devices:

 

$ tinkerforge enumerate
uid=68aT1f
connected-uid=0
position=0
hardware-version=1,0,0
firmware-version=2,0,6
device-identifier=13
uid=SCT31
connected-uid=68aT1f
position=a
hardware-version=1,1,0
firmware-version=2,0,0
device-identifier=216

 

The output is in <key>=<value> format can can then be processed further with xargs, awk, etc.

 

By default the bindings will wait 250ms for enumerate callbacks, this can be changed with the --duration option.

 

Functions

 

Calling the get-temperature function of a Temperature Bricklet with UID SCT31:

 

$ tinkerforge call temperature-bricklet --uid SCT31 get-temperature
temperature=2325

 

Callbacks

 

Setting the temperature callback period to 500ms:

 

$ tinkerforge call temperature-bricklet --uid SCT31 set-temperature-callback-period 500

 

and waiting for temperature callbacks

 

$ tinkerforge dispatch temperature-bricklet --uid SCT31 temperature
temperature=2418
temperature=2431

 

By default the bindings will dispatch callbacks forever, this can be changed with the --duration option.

 

Output Processing

 

There are two options for further output processing: --execute and --replace.

 

The --execute option takes a shell command. The bindings then append the output values to this command and execute it:

 

$ tinkerforge enumerate --execute echo
68aT1f 0 0 1,0,0 2,0,6 13 0
SCT31 68aT1f a 1,1,0 2,0,0 216 0

 

With the --replace option the bindings don't append the output values to the shell command but apply the Python string format function to the command string:

 

$ tinkerforge dispatch temperature-bricklet --uid SCT31 temperature --execute 'echo "scale=2; {temperature} / 100" | bc | xargs printf "`date`, the temperature is: %s °C\n"' --replace
Thu Apr 25 13:44:25 CEST 2013, the temperature is: 24.25 °C

 

This allows to use the key from the <key>=<value> output pairs as placeholders (wrapped in curly braces) in the shell command. The bindings replace them with the corresponding values before executing the shell command.

 

This two options are mainly intended for platforms such as Windows that don't have advanced shell commands (xargs, awk, etc) ready to use.

 

Bash Completion

 

For tab completion in bash the tinkerforge.bash_completion file has to be moved to /etc/bash_completion.d/ as tinkerforge and the tinkerforge script has to be in a location in PATH. Now tab completion should work.

tinkerforge

tinkerforge.bash_completion

Link zu diesem Kommentar
Share on other sites

  • 3 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...