### Device Actions

This is an example of a B4J Server which gets actions from a Domoticz device.
Background
Several Domoticz devices have the option to define an "Action Trigger".
The trigger can be defined as a script (Lua, Python, others) or as a HTTP URL with parameter.
HTTP Trigger
As B4J enables to create servers, the Domoticz Device HTTP trigger can be defined as the URL of a B4J Server.
The URL contains parameter, which are handled by the B4J Server:
URL Syntax: IP Address:Port?Parameter1=Action1?Parameter2=Action2 etc.
This solution offers full access to B4J functionality.
In essence we talk about Server to Server communication.
As Domoticz has an open API using JSON, the B4J Server can also send commands to the Domotics Server.
Domoticz Setup
Define the GPIO Ports. Go http://www.domoticz.com/wiki/GPIO on hot to setup.
For a Domoticz Switch define the On / Off Action Triggers
• On Action = http://192.168.0.2:51042?GPIO18=On
• Off Action = http://192.168.0.2:51042?GPIO18=Off
Note:
B4J Server: IP Address:Port?Parameter1=Action1?Parameter2=Action2 etc.
The B4J Server IP to be defined accordingly.
The actions for the B4J Server to perform are in this case to trigger GPIO18 On and Off.
B4J Code (only snippet shown)
'Handle request from domoticz server
'Example request url triggered by Domoticz device on action:
'http://192.168.0.2:51042?GPIO18=On
`Sub Handle(req As ServletRequest, resp As ServletResponse)
Try
Dim m As Map = req.ParameterMap
'Get the keys and the values
For i = 0 To m.size - 1
Dim key As String = m.GetKeyAt(i)
Dim values() As String = req.GetParameterValues(key)
For j = 0 To values.Length - 1
If key.EqualsIgnoreCase("GPIO18") Then
'For tests log the action to a file in the dirapp folder
File.WriteString(File.DirApp, "action.txt", DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now) & " :: " & key & " = " & values(0))
End If
Next
Next
Catch
File.WriteString(File.DirApp, "action.txt", DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now) & " :: Error " & LastException.Message)
End Try
End Sub`
Output Example from the B4J Server
Server started
FullRequestURI:http://192.168.0.2:51042/?GPIO18=On
Parameter:1
GPIO18 = On
FullRequestURI:http://192.168.0.2:51042/?GPIO18=Off
Parameter:1
GPIO18 = Off
Content of the file action.txt
07/02/2015 20:26:58 :: GPIO18 = Off

Changed: 2018-06-07 09:10, Category: Domoticz Home Automation, File: T6C186M1344.htm, ID: 1344