### Event Data

This is an example of a Domoticz Event Script that sends data to a B4J server.
Both servers are running on Raspberry Pi's.
Concept
• Domoticz server executes a Lua script if device data has changed.
• The Lua script gets the data, builds and sends a curl command to the B4J server port address.
• The B4J server listens to the port, fetches the data and shows the data in the log.
Domoticz Lua Script
-~/domoticz/scripts/lua/script_device_windmeter_b4j.lua
----------------------------------------------------------------------------
- If the a value of the windmeter has changed, sent to B4J Server
- @project: haFuchsbau
- @author: Robert W.B. Linn
- @since: 20150719
- @version: 1.0
- @see: readme.txt
----------------------------------------------------------------------------
commandArray = {}
---------------------------------------------------------------------------------------------------------
- Domoticz IDX and names of the needed devices
---------------------------------------------------------------------------------------------------------
local WindmeterIdx = 28;
local WindmeterName = 'Windmeter';
local B4JServerIP = 'http://192.168.0.2:51042'
---------------------------------------------------------------------------------------------------------
- Get device changed information. This will capture information for any device
---------------------------------------------------------------------------------------------------------
tc=next(devicechanged);
v=tostring(tc);
---------------------------------------------------------------------------------------------------------
- Action for specific device idc the device with name Windmeter defined in a local WindmeterName
- IMPORTANT: After the device has been added first time ensure to REBOOT the Domoticz server first and then use this LUA script
---------------------------------------------------------------------------------------------------------
if (v == WindmeterName) then
time = os.date("*t")
ts = string.format("%02d:%02d:%02d", time.hour, time.min, time.sec);
print(string.format('Device %s changed @ %s', v, ts));
----------------------------------------------------------------------------------------------------------
-- Build the URL to send to the B4J Server
-- The curl parameter are added using -d parameter=value
----------------------------------------------------------------------------------------------------------
cmd = 'curl '
cmd = cmd .. B4JServerIP
cmd = cmd .. ' -d WindmeterAllValues="' .. otherdevices_svalues[WindmeterName] .. '"'
cmd = cmd .. ' -d Winddir=' .. otherdevices_winddir[WindmeterName]
cmd = cmd .. ' -d Windspeed=' .. otherdevices_windspeed[WindmeterName]
cmd = cmd .. ' -d Windgust=' .. otherdevices_windgust[WindmeterName]
print('Cmd:' .. cmd)
----------------------------------------------------------------------------------------------------------
-- Send command to B4J Server
----------------------------------------------------------------------------------------------------------
os.execute(cmd)
end
return commandArray
B4J Server Projectfile DomoticzActionHandler.b4j
Version=3.02
AppType=StandardJava
NumberOfModules=2
Module1=DomoticzActionHandler
Module2=WebUtils
Build1=Default,rwbl.domoticzactionhandler
NumberOfFiles=0
NumberOfLibraries=3
Library1=jcore
Library2=jserver
Library3=JavaObject
@EndOfDesignText@
'Non-UI application (console application)
'Project: Home Automation using Domoticz
'@author: Robert W.B. Linn
'@since: 20150624
'@version: 1.0
'@see: readme.txt
'
Region Project Attributes
#CommandLineArgs:
#MergeLibraries: false
End Region
`Sub Process_Globals
Public srvr As Server
'Define a settings map (if required)
'Public settings As Map
End Sub`
`Sub AppStart (Args() As String)
'Read the settings
'settings = File.ReadMap(File.DirApp, "settings.txt")
'Init the server and set the port
srvr.Initialize("srvr")
srvr.Port = 51042
srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
'Add the handler - see DomoticzActionHandler.bas
srvr.AddHandler("/*", "DomoticzActionHandler", False)
'Start the server. It will listen to port 51042
srvr.Start
Log("Server started")
'
StartMessageLoop
'
'For tests do not forget to kill the process
End Sub`
'B4J Handler DomoticzActionHandler.bas
Type=Class
Version=3.02
ModulesStructureVersion=1
B4J=true
@EndOfDesignText@
'Handler class
`Sub Class_Globals
End Sub`
`Public Sub Initialize
'Do nothing here
End Sub`
'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
LogR(DateTime.Time(DateTime.Now))
Log("FullRequestURI: " & req.FullRequestURI)
'Get the parameter map
Dim m As Map = req.ParameterMap
Log("Parameter: " & m.Size)
'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)
Next
Catch
File.WriteString(File.DirApp, "action.txt", DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now) & " :: Error " & LastException.Message)
End Try
End Sub`
'Write an entry in red text to the log
'Parameter: String to Log
`Sub LogR(s As String)
Dim joBA As JavaObject
joBA.InitializeStatic("java.lang.System")
Dim joBAF As JavaObject = joBA.GetFieldJO("err")
joBAF.RunMethod("println", Array(s))
End Sub`

Changed: 2018-06-07 09:11, Category: Domoticz Home Automation, File: T6C186M1348.htm, ID: 1348