Difference between revisions of "Install actions"

From xPL
Jump to: navigation, search
(Ubuntu)
 
(18 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
The [http://en.wikipedia.org/wiki/XPL_Protocol xPL] actions device allows to launch scripts or executables with parameters from any xPL device.
 +
This allows to build complex home control sequences, even with condition checks.
 +
In this point of view, the actions device allows more elaborate control than the [[install central|central xPL device]].
  
::<html>
+
As an example, pushing an EIB button can result not only in opening the entrance door,
<a title="By Dsmurat (+) [LGPL (http://www.gnu.org/licenses/lgpl.html)], via Wikimedia Commons" href="http://commons.wikimedia.org/wiki/File%3AUnder_construction_icon-blue.svg"><img width="512" alt="Under construction icon-blue" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Under_construction_icon-blue.svg/512px-Under_construction_icon-blue.svg.png"/></a>
+
but also in turning-on selected lights if the button has been pushed after sunset.
</html>
+
  
 +
The actions device is often controlled by xPL messages originating from the [[install central|central xPL device]].
 +
Additionally to sending xPL messages to controlled devices such as lights and shutters,
 +
it can perform local actions on a computer, such as displaying an alert message, sending an e-mail or backing-up files.
  
The [http://en.wikipedia.org/wiki/XPL_Protocol xPL] home state device stores the current state of different devices
+
Before all, make sure you have installed an [[install xpl hub|xPL hub]].
such as lights, shutters, etc…
+
This information can be retrieved by clients such as [https://en.wikipedia.org/wiki/Web_2.0 web 2.0] pages
+
in order to display the proper state of the devices to be controlled.
+
  
The state update is best controlled with the help of the [[install central|central xPL device]].
+
= Install xPL actions device =
  
Before all,make sure you have installed an [[install xpl hub|xPL hub]].
+
Download the [http://www.dspc.ch/xPL/Downloads/xpl-actions.pl xPL actions] script.
 
+
= Home state information =
+
 
+
The home state information is stored in an [https://en.wikipedia.org/wiki/XML XML] file.
+
It is categorised in 4 hierarchy levels:
+
* the ''room''
+
* the ''kind'', such as <code>lights</code>, <code>shutters</code>, etc…
+
* the ''object'': an object of the kind given by an identifier, such as <code>table</code> for the lamp above the table
+
* the ''value'', such as <code>on</code> or <code>off</code>
+
 
+
The following example shows a possible content of <code>/home/control/Documents/Controls/state.xml</code>:
+
<?xml version="1.0" encoding="ISO-8859-1"?>
+
<state>
+
  <lounge name="lights" center="off" entry="off" stand="off" table="off" />
+
  <lounge name="shutters" center="up" east="up" west="up" />
+
  <lounge name="audio" balance="0" power="on" mute="off" source="CD" surround="stereoSub" volume="50" />
+
  <lounge name="screen" aspect="43" input="RGB" power="on" />
+
  <study name="lights" ceiling="off" stand="off" />
+
  <study name="shutters" window="louvresDown" />
+
  <study name="audio" balance="0" source="CD" power="off" volume="30" />
+
</state>
+
 
+
The home state information is both updated and queried with the help of [https://en.wikipedia.org/wiki/XPL_Protocol#Protocol xPL messages].
+
 
+
= Install xPL bash actions device =
+
 
+
Download the [http://www.dspc.ch/xPL/Downloads/xpl-actions.pl xPL home state] script.
+
  
 
Move the script to xPL scripts directory:
 
Move the script to xPL scripts directory:
Line 47: Line 22:
 
  chmod 775 $SCRIPTS_BASE_DIR/*.pl
 
  chmod 775 $SCRIPTS_BASE_DIR/*.pl
  
Create the <code>/home/control/Documents/Controls/state.xml</code> state file based on the preceeding example, but adapted to your needs.
+
Create the <code>/home/control/Documents/Controls/Actions</code> directory.
The file doesn't need to be complete: objects will be added by the xPL <code>update</code> or <code>set</code> messages.
+
  
= Test xPL bash actions device =
+
Create the <code>/home/control/Documents/Controls/Actions/play.bash</code> file, changing the constants accordingly to your setup:
 +
#!/bin/bash
 +
 +
#-------------------------------------------------------------------------------
 +
# Constants
 +
#
 +
DEFAULT_SOUND_FILE='/home/control/Documents/Controls/Sounds/pop.wav'
 +
PLAY_COMMAND='/usr/bin/aplay'
 +
 +
#-------------------------------------------------------------------------------
 +
# Command line arguments
 +
#
 +
sound_file=''
 +
while [ "$1" != <nowiki>''</nowiki> ] ; do
 +
  case $1 in
 +
    -f | --file  ) shift ; sound_file=$1 ;;
 +
  esac
 +
  shift
 +
done
 +
 +
#===============================================================================
 +
# Play sound
 +
#
 +
 +
if [ -e "$sound_file" ] ; then
 +
  echo "Playing file: $sound_file"
 +
  $PLAY_COMMAND $sound_file > /dev/null 2>&1
 +
else
 +
  sound_file=$DEFAULT_SOUND_FILE
 +
  $PLAY_COMMAND $sound_file > /dev/null 2>&1
 +
  echo "File \"$sound_file\" not found"
 +
fi
 +
 
 +
Test it:
 +
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 +
$SCRIPTS_BASE_DIR/Actions/play.bash -f /home/control/Documents/Controls/Sounds/ring.wav
 +
 
 +
= Test xPL actions device =
  
 
Test it on a [[install BeagleBone|BeagleBone]]:
 
Test it on a [[install BeagleBone|BeagleBone]]:
Line 56: Line 67:
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  $SCRIPTS_BASE_DIR/xpl-actions.pl -h
 
  $SCRIPTS_BASE_DIR/xpl-actions.pl -h
  $SCRIPTS_BASE_DIR/xpl-actions.pl -v -n lounge
+
  $SCRIPTS_BASE_DIR/xpl-actions.pl -v -n test
  
 
Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:
 
Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  $SCRIPTS_BASE_DIR/xpl-monitor.pl -vf
 
  $SCRIPTS_BASE_DIR/xpl-monitor.pl -vf
The monitor will help by showing the responses of the home state device.
 
  
Open another terminal and query a value:
+
Open another terminal and have xPL play a sound:
su control
+
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
cat $SCRIPTS_BASE_DIR/state.xml | grep 'study.*lights'
+
  $SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-actions.test -c actions.basic command=play.bash file=/home/control/Documents/Controls/Sounds/ring.wav
  $SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling command=ask
+
  
Modify a value:
+
= Launch the xPL actions device at startup =
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling value=on command=update
+
cat $SCRIPTS_BASE_DIR/state.xml | grep 'study.*lights'
+
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling command=ask
+
  
Modify a value and trigger an action:
+
The xPL actions launcher is to start after the xPL hub.
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling value=off command=set
+
cat $SCRIPTS_BASE_DIR/state.xml | grep 'study.*lights'
+
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling command=ask
+
Additionally to updating the state file, this command will trigger an <code>act</code> message
+
which can be used by the [[install central|central xPL device]] to actually turn off the light.
+
  
Try to modify a value and don't trigger an action:
+
A different instance of the actions launcher can run on different machines.
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling value=off command=set
+
If this is the case, they are distinguished by their name.
cat $SCRIPTS_BASE_DIR/state.xml | grep 'study.*lights'
+
$SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling command=ask
+
As the value of the <code>ceiling</code> lamp was already <code>off</code>,
+
this command will not trigger an <code>act</code> message.
+
This feature will prevent objects to flicker or temporarily mute when an identical command is sent more than once.
+
  
= Launch the xPL bash actions device at startup =
+
== Debian with SystemD ==
  
The xPL bash actions is to be launched after the xPL hub.
+
On Debian with [https://wiki.debian.org/systemd systemd] (such as [https://www.raspbian.org/ Raspbian]
 +
or [https://www.ubuntu.com/ Ubuntu]), the scripts can be defined as services.
 +
 
 +
Edit <code>/lib/systemd/system/xpl-actions.service</code>:
 +
[Unit]
 +
Description=xPL actions
 +
After=xpl-hub.service
 +
 +
[Service]
 +
Type=simple
 +
User=control
 +
Group=users
 +
ExecStart=/home/control/Documents/Controls/xpl-actions.pl -n home
 +
Restart=always
 +
 +
[Install]
 +
WantedBy=multi-user.target
 +
 
 +
Activate the service:
 +
su
 +
systemctl enable xpl-actions.service
 +
service xpl-actions start
 +
 
 +
Reboot and check:
 +
ps aux | grep -v grep | grep -i xpl
 +
ps ax | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'
 +
systemctl list-units --type=service --state=running | grep xpl
 +
service xpl-actions status
  
== Ubuntu ==
+
== Ubuntu with Upstart ==
  
On Ubuntu, the xPL bash actions device starts-up once <code>xpl-hub_started</code> has been emitted.
+
On Ubuntu, the xPL actions device starts-up once <code>xpl-hub_started</code> has been emitted.
  
Edit <code>/etc/init/xpl-homeState.conf</code>:
+
Edit <code>/etc/init/xpl-actions.conf</code>:
 
  ################################################################################
 
  ################################################################################
  # xPL home state manager
+
  # xPL actions launcher
 
  #
 
  #
  description "xPL home state manager"
+
  description "xPL actions launcher"
 
  version    "1.0"
 
  version    "1.0"
 
  author      "Francois Corthay"
 
  author      "Francois Corthay"
Line 110: Line 132:
 
  env SCRIPTS_DIR='/home/control/Documents/Controls'
 
  env SCRIPTS_DIR='/home/control/Documents/Controls'
 
  env SCRIPT_NAME='xpl-actions.pl'
 
  env SCRIPT_NAME='xpl-actions.pl'
  env PARAMETERS='-n home'
+
  env PARAMETERS='-n lounge'
 
   
 
   
 
  #-------------------------------------------------------------------------------
 
  #-------------------------------------------------------------------------------
Line 119: Line 141:
 
  respawn
 
  respawn
 
  setuid control
 
  setuid control
  setgid users
+
  setgid audio
 
   
 
   
 
  #-------------------------------------------------------------------------------
 
  #-------------------------------------------------------------------------------
Line 130: Line 152:
 
  service xpl-actions start
 
  service xpl-actions start
 
  service xpl-actions status
 
  service xpl-actions status
 +
initctl list | grep xpl
 
   
 
   
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
  $SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling command=ask
+
  $SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-actions.lounge -c actions.basic command=play.bash
  
 
== Mac OS ==
 
== Mac OS ==
  
On Mac OS, the xPL bash actions device starts-up later than the hub.
+
On Mac OS, the xPL actions device starts-up later than the hub.
  
 
Edit <code>/Library/LaunchDaemons/xpl-actions.plist</code>:
 
Edit <code>/Library/LaunchDaemons/xpl-actions.plist</code>:
Line 167: Line 190:
 
   
 
   
 
  SCRIPTS_BASE_DIR=/Users/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/Users/control/Documents/Controls
  $SCRIPTS_BASE_DIR/xpl-send.pl -v -c state.basic room=study kind=lights object=ceiling command=ask
+
  $SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-actions.lounge -c actions.basic command=play.bash
  
[[Category: install]]
+
[[Category: all]] [[Category: install]] [[Category: xPL]]

Latest revision as of 11:31, 2 January 2018

The xPL actions device allows to launch scripts or executables with parameters from any xPL device. This allows to build complex home control sequences, even with condition checks. In this point of view, the actions device allows more elaborate control than the central xPL device.

As an example, pushing an EIB button can result not only in opening the entrance door, but also in turning-on selected lights if the button has been pushed after sunset.

The actions device is often controlled by xPL messages originating from the central xPL device. Additionally to sending xPL messages to controlled devices such as lights and shutters, it can perform local actions on a computer, such as displaying an alert message, sending an e-mail or backing-up files.

Before all, make sure you have installed an xPL hub.

Install xPL actions device

Download the xPL actions script.

Move the script to xPL scripts directory:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
mv xpl-actions.pl $SCRIPTS_BASE_DIR
chown control:users $SCRIPTS_BASE_DIR/*.pl
chmod 775 $SCRIPTS_BASE_DIR/*.pl

Create the /home/control/Documents/Controls/Actions directory.

Create the /home/control/Documents/Controls/Actions/play.bash file, changing the constants accordingly to your setup:

#!/bin/bash

#-------------------------------------------------------------------------------
# Constants
#
DEFAULT_SOUND_FILE='/home/control/Documents/Controls/Sounds/pop.wav'
PLAY_COMMAND='/usr/bin/aplay'

#-------------------------------------------------------------------------------
# Command line arguments
#
sound_file=
while [ "$1" != '' ] ; do
  case $1 in
    -f | --file   ) shift ; sound_file=$1 ;;
  esac
  shift
done

#===============================================================================
# Play sound
#

if [ -e "$sound_file" ] ; then
  echo "Playing file: $sound_file"
  $PLAY_COMMAND $sound_file > /dev/null 2>&1
else
  sound_file=$DEFAULT_SOUND_FILE
  $PLAY_COMMAND $sound_file > /dev/null 2>&1
  echo "File \"$sound_file\" not found"
fi

Test it:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/Actions/play.bash -f /home/control/Documents/Controls/Sounds/ring.wav

Test xPL actions device

Test it on a BeagleBone:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-actions.pl -h
$SCRIPTS_BASE_DIR/xpl-actions.pl -v -n test

Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-monitor.pl -vf

Open another terminal and have xPL play a sound:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-actions.test -c actions.basic command=play.bash file=/home/control/Documents/Controls/Sounds/ring.wav

Launch the xPL actions device at startup

The xPL actions launcher is to start after the xPL hub.

A different instance of the actions launcher can run on different machines. If this is the case, they are distinguished by their name.

Debian with SystemD

On Debian with systemd (such as Raspbian or Ubuntu), the scripts can be defined as services.

Edit /lib/systemd/system/xpl-actions.service:

[Unit]
Description=xPL actions
After=xpl-hub.service

[Service]
Type=simple
User=control
Group=users
ExecStart=/home/control/Documents/Controls/xpl-actions.pl -n home
Restart=always

[Install]
WantedBy=multi-user.target

Activate the service:

su
systemctl enable xpl-actions.service
service xpl-actions start

Reboot and check:

ps aux | grep -v grep | grep -i xpl
ps ax | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'
systemctl list-units --type=service --state=running | grep xpl
service xpl-actions status

Ubuntu with Upstart

On Ubuntu, the xPL actions device starts-up once xpl-hub_started has been emitted.

Edit /etc/init/xpl-actions.conf:

################################################################################
# xPL actions launcher
#
description "xPL actions launcher"
version     "1.0"
author      "Francois Corthay"

#-------------------------------------------------------------------------------
# Configuration variables
#
env SCRIPTS_DIR='/home/control/Documents/Controls'
env SCRIPT_NAME='xpl-actions.pl'
env PARAMETERS='-n lounge'

#-------------------------------------------------------------------------------
# Start and stop conditions
#
start on xpl-hub_started
stop on shutdown
respawn
setuid control
setgid audio

#-------------------------------------------------------------------------------
# Start daemon
#
exec $SCRIPTS_DIR/$SCRIPT_NAME $PARAMETERS

Start the device and test it:

su root
service xpl-actions start
service xpl-actions status
initctl list | grep xpl

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-actions.lounge -c actions.basic command=play.bash

Mac OS

On Mac OS, the xPL actions device starts-up later than the hub.

Edit /Library/LaunchDaemons/xpl-actions.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>xPL actions launcher</string>
    <key>OnDemand</key>
    <false/>
    <key>UserName</key>
    <string>francois</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/control/Documents/Controls/xpl-actions.pl</string>
      <string>-n</string>
      <string>lounge</string>
      <string>-w</string>
      <string>25</string>
      <string>-d</string>
      <string>/Users/control/Documents/Controls/Actions</string>
    </array>
  </dict>
</plist>

Start the device and test it:

su root
launchctl load /Library/LaunchDaemons/xpl-actions.plist

SCRIPTS_BASE_DIR=/Users/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-actions.lounge -c actions.basic command=play.bash