How to Setup Jython

This will work, although my python libraries are under /usr/lib/python2.7/site-packages. However, this will not work for @Tomibeck. Attempting to use requests will return this…

TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str in <script> at line number 14

A full stack trace shows…

2018-10-18 12:47:44.301 [ERROR] [org.eclipse.smarthome.automation.scriptException] - JSR223: test: Exception: [Error when calling the metaclass bases
    function() argument 1 must be code, not str]: [Traceback (most recent call last):
  File "<script>", line 15, in <module>
  File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 93, in <module>
    from .api import request, get, head, post, patch, put, delete, options
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 13, in <module>
    from . import sessions
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 28, in <module>
    from .adapters import HTTPAdapter
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 41, in <module>
    from urllib3.contrib.socks import SOCKSProxyManager
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 41, in <module>
    from urllib3.contrib.socks import SOCKSProxyManager
  File "/usr/lib/python2.7/site-packages/urllib3/contrib/socks.py", line 27, in <module>
    import socks
  File "/usr/lib/python2.7/site-packages/urllib3/contrib/socks.py", line 27, in <module>
    import socks
  File "/usr/lib/python2.7/site-packages/socks.py", line 267, in <module>
    class _BaseSocket(socket.socket):
TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str
]

Jython cannot use CPython extension modules written in C, so if you see a .pyc file, chances are it will not work (there is a socks.pyc). An alternative would be to use a Java library, or you could also use executeCommandLine and run anything in Jython that you can in a shell (even Python3), just as you could in the Rules DSL…

from org.slf4j import Logger, LoggerFactory
from org.eclipse.smarthome.model.script.actions.Exec import executeCommandLine
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")

result = executeCommandLine("/bin/sh@@-c@@/usr/bin/python -c \"import requests; print(requests.get('http://localhost:8080').status_code)\"",5000)
log.info("JSR223: test requests [{}]".format(result))

This returns…

2018-10-18 14:22:20.269 [INFO ] [org.eclipse.smarthome.model.script.Rules] - JSR223: test requests [200]

But don’t forget we are in OH! By far the easiest solution for you is to simply use one of the OH HTTP Actions.

from openhab.rules import rule
from openhab.triggers import when
from org.slf4j import Logger, LoggerFactory
from org.eclipse.smarthome.model.script.actions.HTTP import sendHttpPutRequest

log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")
url = "http://172.16.0.15/api/GBL1Hvu8d4kbK7G1iCb3BtsRQiPXFabP9RpKnjng/"

@rule("Hallway Light ON")
@when("Hallway_Motion changed to ON")
def testFunction(event):
    payload = "{\n\t\"on\": true\n}\n"
    sendHttpPutRequest(url, "text/html", payload)
    log.info("JSR223: Hallway Light ON")