Adding a new device type

From ago control wiki
(Difference between revisions)
Jump to: navigation, search
m (Update makefile)
(Update cmake)
 
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
= Tutorial for adding a new device type =
 
= Tutorial for adding a new device type =
This tutorial will take you through all steps and files needed to add a new device type to aGo control. As an example the "Computer" device type used by Wake-on-LAN (WOL) will be used.
+
This tutorial will take you through all steps and files needed to add a new device type to agocontrol. As an example the "Computer" device type used by Wake-on-LAN (WOL) will be used.
  
 
== Add to schema  ==
 
== Add to schema  ==
'''File name: conf/schema.yaml'''
+
If you're introducing a new device type to agocontrol, you need to make the system aware of it. This is done by adding it to a configuration file.
 +
 
 +
'''File name: conf/schema.d/ - select a relevant .yaml-file'''
 +
This example: conf/schema.d/10-automation.yaml'''
 
<pre>
 
<pre>
 
  computer:
 
  computer:
Line 14: Line 17:
 
Add your device type to supported_devices<br/>
 
Add your device type to supported_devices<br/>
 
'''File name: core/rpc/html/js/app.js'''<br/>
 
'''File name: core/rpc/html/js/app.js'''<br/>
<pre>
+
 
var supported_devices = [ "switch", "dimmer", "binarysensor", "dimmerrgb", "multilevelsensor",
+
var supported_devices = [ "switch", "dimmer", "binarysensor", "dimmerrgb", "multilevelsensor",
"scenario", "drapes", "brightnesssensor", "powermeter", "energysensor", "humiditysensor", "phone",
+
"scenario", "drapes", "brightnesssensor", "powermeter", "energysensor", "humiditysensor", "phone",
"pushbutton", "placeholder", "temperaturesensor", "energymeter", "squeezebox", '''"computer"''' ];
+
"pushbutton", "placeholder", "temperaturesensor", "energymeter", "squeezebox", '''"computer"''' ];
</pre>
+
  
 
== HTLM template for device ==
 
== HTLM template for device ==
 +
This template is a copy of switch.in.html. The OFF-command is obviously taken away and the icon is changed from icon-lamp to icon-keyboard.<br/>
 +
N.B! The file name has to be the same as the device type.<br/>
 
'''File name: core/rpc/html/templates/devices/computer.in.html<br/>'''
 
'''File name: core/rpc/html/templates/devices/computer.in.html<br/>'''
  
Line 68: Line 72:
 
opt/agocontrol/bin/pinger.py </pre>
 
opt/agocontrol/bin/pinger.py </pre>
  
== Update makefile ==
+
== debian package ==
Add wake_on_lan to the directory list<br/>
+
Add section to the main install script<br/>
'''File name: devices/Makefile<br/>'''
+
'''File name: debian/control<br/>'''
<pre>DIRS = enocean3 blinkm chromoflex firmata i2c irtrans_ethernet kwikwai mediaproxy rain8net zwave 1wire syslog
+
<pre>
enigma2 asterisk agoapc agojointspace raspiGPIO raspi1wGPIO raspiMCP3xxxGPIO gc100 onkyo x10 webcam wake_on_lan</pre>
+
Package: agocontrol-wake_on_lan
 +
Architecture: all
 +
Depends: ${shlibs:Depends}, ${misc:Depends}, libagoclient1.0
 +
Description: Wake on LAN support</pre>
  
== New make file ==
+
== Update cmake ==
File name: devices/wake_on_lan/Makefile
+
Create a CMakeLists.txt
 
<pre>
 
<pre>
all:
+
cmake_minimum_required (VERSION 2.6)
 +
install (PROGRAMS agowake_on_lan.py pinger.py configobj.py DESTINATION ${BINDIR})
 +
</pre>
  
install:
+
Update devices/CMakeLists.txt
$(INSTALL_DIR) $(BINDIR)
+
$(INSTALL_PROGRAM) agowake_on_lan.py $(BINDIR)
+
$(INSTALL_PROGRAM) pinger.py $(BINDIR)
+
$(INSTALL_PROGRAM) configobj.py $(BINDIR)
+
  
clean:
+
<pre>
 +
set (AGODEVICES
 +
  shvt250
 +
  ...
 +
  ...
 +
  webcam
 +
  temperatur.nu
 +
  wake_on_lan      <<--- Add this line
 +
)
 
</pre>
 
</pre>
  
 
== Device driver ==
 
== Device driver ==
 
=== Main logic ===
 
=== Main logic ===
File name: devices/wake_on_lan/agowake_on_lan.py <br>
+
Finally, a device driver that uses the new device type. This is a plain vanilla task as outlined in [[Example Device]].<br/>
Finally, a device driver that uses the new device type. This is a plain vanilla task as outlined in [[Example Device]].
+
'''File name: devices/wake_on_lan/agowake_on_lan.py''' <br>
 
     client.addDevice(deviceId, "computer")
 
     client.addDevice(deviceId, "computer")
  
 
=== Config file ===
 
=== Config file ===
File name: devices/wake_on_lan/wake_on_lan.conf
+
'''File name: devices/wake_on_lan/wake_on_lan.conf'''
 
<pre>
 
<pre>
 
[wake_on_lan]
 
[wake_on_lan]
Line 111: Line 124:
 
host=thisshouldfail
 
host=thisshouldfail
 
</pre>
 
</pre>
 +
 +
=== New cmake list ===
 +
===Compiling on the hardware running agocontrol===
 +
Ago utilizes cmake to build your C++ program.  CMake requires a CMakeList.txt in your program's folder and Ago requires another to know which programs to build.  It is probably easiest to copy and edit the CMakeList.txt from the example folder to your folder:
 +
 +
For installations with a common install structure, the files are here (where USER is your username):
 +
 +
'''File name: agcontrol/devices/'''
 +
 +
Within your folder, edit the CMakeList.txt to match your conditions.  For example:
 +
<pre>
 +
cmake_minimum_required (VERSION 2.6)
 +
 +
file (GLOB_RECURSE DEVICE_SOURCE_FILES *.cpp)
 +
 +
set (DEVICE_EXTRA_LIBRARIES
 +
    agoclient
 +
)
 +
 +
#set a device name. This will be the 'target'
 +
set (DEVICE "(YOUR_DEVICE_NAME_HERE")
 +
 +
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
 +
 +
# add the executable
 +
add_executable (${DEVICE} ${DEVICE_SOURCE_FILES})
 +
target_link_libraries (${DEVICE} ${DEVICE_EXTRA_LIBRARIES})
 +
 +
install (TARGETS ${DEVICE} RUNTIME DESTINATION ${BINDIR})
 +
</pre>
 +
 +
Then, you must add your device to the build CMakeList.txt (located within the devices folder):
 +
 +
<pre>
 +
cmake_minimum_required (VERSION 2.6)
 +
 +
set (AGODEVICES
 +
  enocean3
 +
  blinkm
 +
  chromoflex
 +
  firmata
 +
  i2c
 +
  ...
 +
  YOUR_DEVICE_NAME_HERE
 +
)
 +
</pre>
 +
 +
Once completed, we just need to run CMake from our target location to start the build process.  I build into a new folder I've created at /home/run.  A simple script simplifies the process for future builds.  Here are the contents from my "build.sh" file that I store in /home:
 +
 +
<pre>
 +
#!/bin/bash
 +
cd /home/run/
 +
rm -r *
 +
cmake /home/USER/agocontrol/
 +
cd devices/YOUR_DEVICE_NAME_HERE
 +
make
 +
chmod a+rx YOUR_DEVICE_NAME_HERE
 +
chown agocontrol YOUR_DEVICE_NAME_HERE
 +
chgrp agocontrol YOUR_DEVICE_NAME_HERE
 +
</pre>
 +
 +
This simply enters into the /home/run folder, removes all previous files, runs cmake on the available devices, and the makes only your new device.  The last three lines add premissions and change the owner to agocontrol
 +
 +
===TODO- Cross compiling===
  
 
=== Service definition ===
 
=== Service definition ===
File name: conf/systemd/agowake_on_lan.service
+
'''File name: conf/systemd/agowake_on_lan.service'''
 
<pre>
 
<pre>
 
[Unit]
 
[Unit]

Latest revision as of 14:56, 30 November 2016

Contents

[edit] Tutorial for adding a new device type

This tutorial will take you through all steps and files needed to add a new device type to agocontrol. As an example the "Computer" device type used by Wake-on-LAN (WOL) will be used.

[edit] Add to schema

If you're introducing a new device type to agocontrol, you need to make the system aware of it. This is done by adding it to a configuration file.

File name: conf/schema.d/ - select a relevant .yaml-file This example: conf/schema.d/10-automation.yaml

 computer:
   name: General computer (for WOL)
   description: A computer
   commands: ["on"]

[edit] Add to main web script

Add your device type to supported_devices
File name: core/rpc/html/js/app.js

var supported_devices = [ "switch", "dimmer", "binarysensor", "dimmerrgb", "multilevelsensor",
"scenario", "drapes", "brightnesssensor", "powermeter", "energysensor", "humiditysensor", "phone",
"pushbutton", "placeholder", "temperaturesensor", "energymeter", "squeezebox", "computer" ];

[edit] HTLM template for device

This template is a copy of switch.in.html. The OFF-command is obviously taken away and the icon is changed from icon-lamp to icon-keyboard.
N.B! The file name has to be the same as the device type.
File name: core/rpc/html/templates/devices/computer.in.html

 <div class="four columns grid-item device"  data-bind="attr: {'data-uuid': uuid}"> 
	<div class="row">
		<div class="three columns">
			<div class="pretty large primary btn grid-item-icon handle">
				<i class="icon-keyboard" data-bind="click: showDetails"></i>
			</div>
		</div>
		<div class="nine columns">
			<div class="grid-item-header">
				<h3 data-bind="text: name, if: name"></h3>
				<ko opts="if: room ">
					<p class="room"><i class="icon-location"></i><span data-bind="text: room"></span></p>
				</ko>
                                <p class="info"><i class="icon-info"></i>
                                        <ko opts="if: state() == 0">
                                                <_span data-translateable="true">OFF</_span>
                                        </ko>
                                        <ko opts="if: state() == 255">
                                                <_span data-translateable="true">ON</_span>
                                        </ko>
                                </p>
			</div>
		</div>
	</div>
	<div class="row grid-item-controls">
		<div class="six columns">
		    <ko opts="if: state ">
                <_div data-translateable="true" class="cmd-btn pretty medium default btn btn-full grid-command" data-bind="click: turnOn" data-value="on">ON</_div>
            </ko>
            <ko opts="ifnot: state ">
                <_div data-translateable="true" class="cmd-btn pretty medium primary btn btn-full grid-command" data-bind="click: turnOn" data-value="on">ON</_div>
            </ko>
		</div>
	</div>
 </div>

[edit] debian install script

configobj.py and pinger.py are additional programs used by this example, likelly irrelevant for other device types.
File name: debian/agocontrol-wake_on_lan.install

lib/systemd/system/agowake_on_lan.service
opt/agocontrol/bin/agowake_on_lan.py
opt/agocontrol/bin/configobj.py
opt/agocontrol/bin/pinger.py 

[edit] debian package

Add section to the main install script
File name: debian/control

Package: agocontrol-wake_on_lan
Architecture: all
Depends: ${shlibs:Depends}, ${misc:Depends}, libagoclient1.0
Description: Wake on LAN support

[edit] Update cmake

Create a CMakeLists.txt

cmake_minimum_required (VERSION 2.6)
install (PROGRAMS agowake_on_lan.py pinger.py configobj.py DESTINATION ${BINDIR})

Update devices/CMakeLists.txt

set (AGODEVICES
  shvt250
  ...
  ...
  webcam
  temperatur.nu
  wake_on_lan       <<--- Add this line
)

[edit] Device driver

[edit] Main logic

Finally, a device driver that uses the new device type. This is a plain vanilla task as outlined in Example Device.
File name: devices/wake_on_lan/agowake_on_lan.py

    client.addDevice(deviceId, "computer")

[edit] Config file

File name: devices/wake_on_lan/wake_on_lan.conf

[wake_on_lan]
debug=false

[Computers]
	[[DCErouter]]
	name=DCErouter
	mac=01:14:DD:D1:B9:5D
	host=dcerouter

	[[Test]]
	name=test
	mac=00:01:02:03:04:FF
	host=thisshouldfail

[edit] New cmake list

[edit] Compiling on the hardware running agocontrol

Ago utilizes cmake to build your C++ program. CMake requires a CMakeList.txt in your program's folder and Ago requires another to know which programs to build. It is probably easiest to copy and edit the CMakeList.txt from the example folder to your folder:

For installations with a common install structure, the files are here (where USER is your username):

File name: agcontrol/devices/

Within your folder, edit the CMakeList.txt to match your conditions. For example:

cmake_minimum_required (VERSION 2.6)

file (GLOB_RECURSE DEVICE_SOURCE_FILES *.cpp)

set (DEVICE_EXTRA_LIBRARIES
     agoclient
)

#set a device name. This will be the 'target'
set (DEVICE "(YOUR_DEVICE_NAME_HERE")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}")

# add the executable
add_executable (${DEVICE} ${DEVICE_SOURCE_FILES})
target_link_libraries (${DEVICE} ${DEVICE_EXTRA_LIBRARIES})

install (TARGETS ${DEVICE} RUNTIME DESTINATION ${BINDIR})

Then, you must add your device to the build CMakeList.txt (located within the devices folder):

cmake_minimum_required (VERSION 2.6)

set (AGODEVICES
  enocean3
  blinkm
  chromoflex
  firmata
  i2c
  ...
  YOUR_DEVICE_NAME_HERE
)

Once completed, we just need to run CMake from our target location to start the build process. I build into a new folder I've created at /home/run. A simple script simplifies the process for future builds. Here are the contents from my "build.sh" file that I store in /home:

#!/bin/bash
cd /home/run/
rm -r *
cmake /home/USER/agocontrol/
cd devices/YOUR_DEVICE_NAME_HERE
make
chmod a+rx YOUR_DEVICE_NAME_HERE
chown agocontrol YOUR_DEVICE_NAME_HERE
chgrp agocontrol YOUR_DEVICE_NAME_HERE

This simply enters into the /home/run folder, removes all previous files, runs cmake on the available devices, and the makes only your new device. The last three lines add premissions and change the owner to agocontrol

[edit] TODO- Cross compiling

[edit] Service definition

File name: conf/systemd/agowake_on_lan.service

[Unit]
Description=ago control Wake on LAN handler

[Service]
ExecStart=/opt/agocontrol/bin/agowake_on_lan.py
WorkingDirectory=/opt/agocontrol/bin
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=2
User=root

[Install]
WantedBy=multi-user.target
Personal tools