Question about OH3 Rules

Hello,

i am using a bit more complex rule in OpenHAB 2.x (see example below). Now I’m not sure how to get them into the new OpenHAB 3 world? Do I have to use ECMAScript or DSL Rule?
The core is the HTTP request with authentication:

connection.setRequestProperty('Authorization', 'Bearer ' + 'XXXXXXXXXXXXXXXXXXXXXXXXXX')
connection.setRequestProperty("Content-Type", "application/json");

Does anyone have an idea how I can best continue? Here is an extract of the code:

import java.net.URL
import javax.net.ssl.HttpsURLConnection
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import org.eclipse.xtext.xbase.lib.Functions

val Functions$Function1<String, String> queryGroheAPI = [ dummy |
   val endpoint = new URL('https://XXXXXXXXXXXXXXXXXXXXXXX')
   val HttpsURLConnection connection = endpoint.openConnection() as HttpsURLConnection
   connection.setRequestProperty('Authorization', 'Bearer ' + 'XXXXXXXXXXXXXXXXXXXXXXXXXX')
   connection.setRequestProperty("Content-Type", "application/json");
   connection.requestMethod = "GET";
   connection.setDoOutput(true);
   val BufferedReader reader = new BufferedReader(new 
   InputStreamReader(connection.getInputStream()));
   val String jsonString = org.apache.commons.io.IOUtils.toString(reader);
  jsonString
]

rule "GET Grohe Data"
when
   Item Grohe_GET received command ON
then  
   var String jsonString = queryGroheAPI.apply('dummy')    
   Grohe_LastUpdate.postUpdate(transform("JSONPATH", "$.locations[0].rooms[0].appliances[0].tdt", jsonString))
   Grohe_Name.postUpdate(transform("JSONPATH", "$.locations[0].rooms[0].appliances[0].name", jsonString))
    ...
end

You have different possibilities here.

OH3 uses the next gen rule engine that was introduced in OH2. Therefore you can use DSL and ECMA scripts. If you prefere to use the Webinterface to write the rules you will need to rewrite the rule in ECMA because vals outside a rule are not possible when creating a rule in the GUI.

But OH3 supports still the old rules. So you can give it a try and place your rule in the rules folder. If you do this you can use variables for serval rules in the same file. And in best case your rule is still working because you use http from a java class and not the http binding which was replaced with OH3 :wink:

For this purpose I would advise an ECMAScript rule since ECMAScript is very good at dealing with JSON [JavaScript Object Notation], who knew…

I have several like this to get stuff as JSON (I adapted quickly to your case):

var client = java.net.http.HttpClient.newBuilder().build();
function httpGet(uri) {
  try {
    var request = java.net.http.HttpRequest.newBuilder()
      .GET()
      .uri(java.net.URI.create(uri))
      .header("Authorization", "Bearer XXX")
      .header("Content-Type", "application/json")
      .build();
    var response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());
    // print(response.body());
    return response;
  } catch (e) {
    print(e);
  }
}

var result = JSON.parse(httpGet("https://XXX").body());
events.postUpdate("Grohe_LastUpdate", result.locations[0].rooms[0].appliances[0].tdt);
events.postUpdate("Grohe_Name", result.locations[0].rooms[0].appliances[0].name);

But of course you can also try the new HTTP binding, and define channels with the JSONPATH transformation then link your items.

Thanks, this is exactly what I was looking for!

Hello @ysc
While i suppose i could use the HTTP binding for this, but still wanted to check how to handle encoded (gzip) response body? i was trying to fiddle with GZIPInputStream but couldnt do it.
Thanks in advance

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.