### Python runs B4J Non-UI program

Raspberry Pi example of running a B4J Non-UI program triggered from a Python script.
This example makes use of Domoticz Home Automation ( http://www.domoticz.com ).
Steps:
• Domoticz triggers the Python script runb4jjar.py.
• The Python script starts the B4J program as a subprocess by running java -jar b4jtest.jar.
• The B4J program creates a log file b4jtest.log located in /home/pi/domoticz.
• The Python scripts wait a second to then close.
Domoticz Trigger
Switch Device Action Command:
script:///home/pi/domoticz/scripts/python/runb4jjar.py
Corresponding Domoticz Log Entry:
Executing script: /home/pi/domoticz/scripts/python/espledswitch.py
Python Script
Note:
The full path to the B4J jar must be provided. The B4J program is running once.
If Java is not on the path, then the full path must be given as well.
#!/usr/bin/python
## @name runb4jjar.py
## @brief Example running B4J Non-UI program as a Python Subprocess triggered by a Device Switch Action Command.
## The B4J program writes datetime to a log file b4jtest.log, located (by default) in /home/pi/domoticz. This file is owned by root.
## Change owner: sudo chown pi /home/pi/domoticz/b4jtest.log. Delete the file: rm /home/pi/domoticz/b4jtest.log
## The switch device action command is script:///home/pi/domoticz/scripts/python/runb4jjar.py
## Ensure runb4jjar.py is made executable by sudo chmod +x runb4jjar.py. Test using python3 runb4jjar.py.
## @author Robert W.B. Linn
## @version 20170605
import os
import subprocess
import time
subp = subprocess.Popen(
'java -jar /home/pi/domoticz/scripts/python/b4jtest.jar',
shell=True,
preexec_fn=os.setsid)
time.sleep(1)
exit(0)
#For Test Only
#subprocess.call(['java', '-jar', '/home/pi/domoticz/scripts/python/b4jtest.jar'])
B4J Non-UI Application
`Sub Process_Globals
Private LogFile As String = "b4jtest.log"
End Sub`
`Sub AppStart (Args() As String)
Dim s As String = $"${DateTime.Time(DateTime.now)} updated log"$
File.WriteString(File.dirapp, LogFile, s)
ExitApplication
End Sub`

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