Question

I have a module which requests status(technically a JSON object) from an FHEM server(home automation) using a simple HTTP post.

How can i poll the requestState method so as to continuously listen (like every 125ms) on the URL for changes in status?

[note: i am not using any server, this is a plain Java program called from a Main function.]

To give an illustration:

public static boolean requestState(){
HttpPost post = new HttpPost("http://localhost:8083/fhem?cmd=jsonlist+device1");
try{
        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader( 
                   new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
                    //response is a JSONObject
        JSONObject o = new JSONObject(result.toString());

                    //get the status from the JSON object
        System.out.println(o.getJSONObject("Result").get("STATE"));
        String STATE = o.getJSONObject("Result").get("STATE");
                    if(STATE.equals("OPEN"))
                       return false;
                    else
                       return true;
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

What i would like to achieve is:

keep listening to URL
  parse response to judge the status
  if status == OPEN
    soundAlarm();

Thanks in advance!

Was it helpful?

Solution

You could keep both posting a request and reading the result in a loop and use Thread.sleep to put pauses if needed between each poll and the other. I'm not familiar with FHEM but maybe there is a more efficient way that you could use where FHEM pushes the status change to your app rather than having to poll it continually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top