[JSR223] [JYTHON] Jython with requests successful?

Hmm, what am I doing differently? You have a completely stock OH 2.5.2 other than the one add-on you mention?

Does this match your jython engine?

203 x Active x  80 x 2.5.0.202001221001      x openHAB Core :: Bundles :: Jython ScriptEngineFactory
-rw-r--r-- 1 openhab openhab 37403148 Mar 22 16:26 /usr/share/openhab2/addons/org.openhab.core.automation.module.script.scriptenginefactory.jython-2.5.0-SNAPSHOT.jar

What bundle is your change in? Since 2.5.0 came out before your change was checked in, it must be in an add-on patch bundle? Core bundles are not updated in the 2.5.x patches.

The script is below. Yours is just like this?

"""
TankUtility Jython Rule
"""

from core.rules import rule
from core.triggers import when
from core.actions import HTTP

@rule("TankUtility", description="Picks up propane tank values from Tank Utility servers", tags=[])
@when("Time cron 0 4 9 * * ?")
def tankUtilityDataCollection(event):
    tankUtilityDataCollection.log.info("TankUtility script is running")

    # leaving commented lines as blank so line numbers match the log below
    # 
    # 

    # 
    headers="{Authorization=Basic XXXXYYYY}"
    TokenResponse = HTTP.sendHttpGetRequest("https://data.tankutility.com/api/getToken", headers, 10000)

The log:

2020-04-01 09:04:00.312 [INFO ] [jsr223.jython.TankUtility           ] - TankUtility script is running
2020-04-01 09:04:00.318 [ERROR] [jsr223.jython.TankUtility           ] - Traceback (most recent call last):
  File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
    return fn(*args, **kwargs)
  File "<script>", line 20, in tankUtilityDataCollection
TypeError: sendHttpGetRequest(): expected 1-2 args; got 3

2020-04-01 09:04:00.320 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule '9d3c7542-2a9f-4ea8-8dc8-9ab9ac820829': Fail to execute action: 1
2020-04-01 09:04:00.320 [DEBUG] [e.automation.internal.RuleEngineImpl] -
java.lang.RuntimeException: Fail to execute action: 1
        at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1197) ~[bundleFile:?]

Thanks for your help.

org.openhab.core.model.script

This change was in OHC and came out after 2.5.0.

This is not completely correct, since there were a few OHC PRs included in 2.5.1. Try upgrading to the latest release (2.5.4).

2.5.4 isn’t released yet - at least I don’t see it for an apt upgrade. I’m running 2.5.3 and this doesn’t appear to be there. Is it one of these bundles or something else?

Also, how is it that @JimT can use it on 2.5.2?

172 x Active x  80 x 2.5.0                   x openHAB Core :: Bundles :: Model Script
173 x Active x  80 x 2.5.0                   x openHAB Core :: Bundles :: Model Script IDE
174 x Active x  80 x 2.5.0                   x openHAB Core :: Bundles :: Model Script Runtime

Yes, sorry. There are 2.5.4 snapshot releases, but the latest stable release is 2.5.3.

But it looks like I stuck my foot into something here and now I need to clean it up! I just tried using sendHttpGetRequest with headers on 2.5.4 S79 and I get the same error. @JimT, since you wrote and presumably tested the PR, maybe you are running a custom bundle?

It should be:

headers = {'Authorization': 'Basic XXXXX'}
openhab> bundle:list | grep Jython
203 │ Active  │  80 │ 2.5.0.201912061703      │ openHAB Core :: Bundles :: Jython ScriptEngineFactory

I haven’t updated my jython scriptenginefactory - don’t know how to (@5iver?) and haven’t been bothered to update.

This might explain why it works on mine.

openhab> bundle:list | grep "Model Script"
172 │ Active  │  80 │ 2.5.0.201912191214      │ openHAB Core :: Bundles :: Model Script
173 │ Active  │  80 │ 2.5.0                   │ openHAB Core :: Bundles :: Model Script IDE
174 │ Active  │  80 │ 2.5.0                   │ openHAB Core :: Bundles :: Model Script Runtime

My bad, after further investigation I found that I am indeed using my own patched version!

This is my docker-compose.yaml:

$ cat docker-compose.yaml
version: "3"
services:
  openhab:
    container_name: openhab
    image: openhab/openhab:milestone
    restart: always
    network_mode: host
    volumes:
      - /home/openhab/tmp/org.openhab.core.model.script-2.5.0-SNAPSHOT.jar:/openhab/runtime/system/org/openhab/core/bundles/org.openhab.core.model.script/2.5.0/org.openhab.core.model.script-2.5.0.jar
 ...

Sorry to have sent you on a wild goose chase! I had compiled my own org.openhab.core.model.script-2.5.0-SNAPSHOT.jar and mounted it into my docker to replace the OHC jar.

I must defer to a more knowledgeable person who knows when my PR actually got included into OHC.

There has not been an update recently, but there is one coming soon. But again, this issue has nothing to do with Jython, the helper libraries, or the beta Jython bundle.

Mystery solved! OHC is frozen in OH 2.5, so your update won’t be available until the OH 3.0 stable release, although it’ll be in the snapshots too. However, I predict that the core actions will be moved to another OHC package or incorporated into an addon.

Any chance you could make that available for others to use? Thanks.

https://github.com/jimtng/openhab-core/raw/http-actions/org.openhab.core.model.script-2.5.0-SNAPSHOT.jar

2 Likes

Thanks for posting the jar. Unfortunately, I get this issue when I try to use it:

2020-03-22 19:38:29.620 [WARN ] [org.apache.felix.fileinstall        ] - Error while starting bundle: file:/usr/share/openhab2/addons/org.openhab.core.automation.module.script.scriptenginefactory.jython-2.5.0-SNAPSHOT.jar
org.osgi.framework.BundleException: Could not resolve module: org.openhab.core.automation.module.script.scriptenginefactory.jython [203]
  Unresolved requirement: Import-Package: org.openhab.core.automation.module.script; version="[2.5.0,3.0.0)"

@5iver, could it be that the latest jython script bundle doesn’t think that this matches 2.5.0 to 3.0.0?

204 x Active    x  80 x 2.5.0.201912191214      x openHAB Core :: Bundles :: Model Script

I’m using this bundle:

203 x Installed x  80 x 2.5.0.202001221001      x openHAB Core :: Bundles :: Jython ScriptEngineFactory

These two bundles have nothing in common. Use the console to view the installed bundles. This will filter them so that you can see Jim’s…

bundle:list -s | grep model

You probably have two org.openhab.core.model.script-2.5.0-SNAPSHOT.jar and need to uninstall one of them. My guess is that you just copied it to addons. You can also replace the file in runtime, but you need to clear the cache to pick up the new one.

Other than these log messages, what is not working?

openhab> bundle:list -s | grep module
203 │ Active  │  80 │ 2.5.0.201912061703      │ org.openhab.core.automation.module.script.scriptenginefactory.jython
264 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.automation.module.media
265 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.automation.module.script
266 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.automation.module.script.rulesupport
openhab> bundle:list -s | grep model
161 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.core
162 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.item
163 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.item.ide
164 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.item.runtime
165 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.lsp
166 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.persistence
167 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.persistence.ide
168 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.persistence.runtime
169 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.rule
170 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.rule.ide
171 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.rule.runtime
172 │ Active  │  80 │ 2.5.0.201912191214      │ org.openhab.core.model.script
173 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.script.ide
174 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.script.runtime
175 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.sitemap
176 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.sitemap.ide
177 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.sitemap.runtime
178 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.thing
179 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.thing.ide
180 │ Active  │  80 │ 2.5.0                   │ org.openhab.core.model.thing.runtime

model vs module

Assuming you’ve replaced the core model jar and not just adding it to the addons folder, try restarting openhab again.

@5iver, @JimT: thank you both. I have this working now. I clearly don’t understand all the caching that goes on with the jar files. More to learn…

Anyway, this recipe worked for me:

  1. go into karaf and uninstall the current org.openhab.core.model.script
  2. copy JimT’s replacement jar to the addons directory
  3. bundle will be “Waiting” now
  4. restart openHAB

Now I can see it as Active and it is functional. Thanks again.

3 Likes

Guys, i didn’t get it. I exchanged the jar file and did not get any error messages about wrong number of parameters or anything else.

But the function with headers does not work for me. If i try

response = HTTP.sendHttpGetRequest("https://api.eiswarnung.de/?lat=53.73338496170717&lng=9.66561413531053&key=xxxxx", 10000)

i get the desired response from the server, but if i use a function with headers

    headers = {"key" : "xxxxxxx", "lat" : "53.73338496170717", "lng" : "9.66561413531053"}
    response = HTTP.sendHttpGetRequest("https://api.eiswarnung.de/", headers, 10000)

i only get an error. The error is that the api key was missing. The request itself is OK.

Has someone an idea about this?

Is it allowed to have a space before the :? I’ve only seen this written without a space, but I’m no expert on the syntax:

headers = {'key': 'xxxxxxx',

Just a guess, no more.

Tried it, but as expected no scucess. headers is a dictionary comparable to a java map so the space is not a part of an string.

Now that I reread your post, I am wondering why you are trying to do this. Is there a reason to try to convert the parameters into headers? I found some info while searching this. Possibly you may need to encode when using as a header. But I’m back to wondering why you want to do this if your parameters are working? What does it solve? (Not saying you wouldn’t want to do this - I am curious about the reason.)

2 Likes

You made the point. Parameters are not headers. Too much easter eggs in my Brain. Coming from Python requests where parameters are stored in a dictionary (Map) going to openHAB2 Action which is java i only saw the datatype and not the purpose.

Thanks for the hint.

This is probably redundant but a while ago I spent quite a bit of time trying to get a spotify api script running in jython. One of the big problems was requests. There may be better solutions with updated jython bundles and stuff but here are my notes on the issues.

Jython is old and only supports http, requests only supports https. Downgrade requests to a version that supports http for spotify api to run on it
pip install requests==2.5.3

The https problem with requests is still there. Instead of requests it should be possible to use openHAB http actions like sendHttpGetRequest.