Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Websocket Plugin

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Post by cfull1 »

What kind of smartphone do you have?
I don't think Android supports websockets yet.

I don't know much about cgi.
What types of files are you wanting to send?
I don't think websockets can do much of that yet.
Pictures can be done with Base64 encoding but I don't think the plugin or I am ready for that yet.

Sorry
Mannyman
Posts: 4
Joined: Sat Jul 09, 2011 7:38 am

Re: Websocket Plugin

Post by Mannyman »

I have HP webOS Pre2.

Currently I am using webserver plugin to send commands and OSD text from my Pre2 to PC running EG.

I'd like to send a picture from my phone and then use EG's Show Picture command to put it on my Monitor and things like that.
Also .wav files, .mp3, etc...

I am patient so no hurry, as it is fun learning all this stuff.

Thanks,
Manny
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Post by cfull1 »

Unfortunately webOS doesn't support websockets.
Since websockets are a fairly new protocol they are not yet supported many browsers.
Does Pako's email plugin allow for attachments?
Maybe you could use that or find another script that downloads certain email's attachments and saves them in a folder.
The script could then trigger an eg event through the cmd functions, triggering a macro to display the picture.

Just a thought
Mannyman
Posts: 4
Joined: Sat Jul 09, 2011 7:38 am

Re: Websocket Plugin

Post by Mannyman »

It looks like webSocket is supported in webOS 3.0 per the webOS development forum.

I've also been looking at http://socket.io/ for previous versions of webOS...another guy is using that to connect to a .NET WebSocket server...

I'll look into your other suggestions as well.

Thanks,
Manny
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Post by cfull1 »

Socket.io looks like good stuff. I've heard about it alot too. I'll look into it
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Websocket Plugin

Post by krambriw »

Hi there,

This works fine. At least with Crome. This technic would allow to build very efficient flat panels around the house to control & always have actual state updated on those

It's a pitty if android doesn't support websocket since those small flat panels are getting cheaper every day. Is socket.io supported on android?

If socket.io works on those, it might be a smarter choice than websocket (just thinking loud)

Best regards, Walter
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Post by cfull1 »

The problem with me is most of my stuff is controlled by IR which I'm getting really sick of lol. It's so limited.
But I've got a nice setup with iTunes through this.

I've been looking into socket.io.
I would mean i major code rewrite if not just starting from scratch.
I'm not sure how soon I would be able to do it. There are other projects I want to work on too.

But it does offer some advantages:
Heartbeats - To see whether the client is still connected, currently if the plugin runs into an error with the client it just removes it to prevent the whole thing from crapping out.
Multiple "onmessage" functions - You can setup custom functions so that everything doesn't have to go through one function.
Also it doesn't use just websockets it picks from the best choice. One of the methods is flash. This provides more compatibly with browsers, supporting IE, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, and webOS. But some of these connects are not true websockets, and I'm not sure how responsive they will be, given that one of the methods of transport being ajax polling.

I will continue to research.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Websocket Plugin

Post by krambriw »

Hi,

This topic is for sure very interesting and really appreciates all work done by everybody.

I have played with it a bit and I have some ideas how I see that this can be extremely useful. I think best is maybe to describe with example what I would like to achieve.

I use EG to control many devices and EG always know the actual status of the devices (ON, OFF etc)

Now I plan to put a number of flat touch panels around in the house to enable a visual view and control functions using buttons & status indicators on an html page.

The devices can, besides using EG, also be controlled by various remotes. Those signals (coming from remotes and others) are captured by EG too, just to have the actual status correctly represented

The websocket server works fine to control the status indicators, they are changing accordingly to show the actual status. So far so good!

The remaing parts I have to think about is

- I need to make the current statuses persistent to make it possible to restart EG and to recover the correct display on all connected web clients
- I need to make it possible so that when a new web client connects, it shall be synchronized with the current statuses
- When I touch a button on the touch screen to control a device, I need to make sure that the page is not fully refreshed to its default state, it should only update the changed status of the specific device

To save the current statuses I think I have to solve this by creating (a script or a plugin)
- that saves the status for each device when it changes using eg.PersistentData
- that sends out the current statuses to a web client when it detects that it is newly connected

Currently I have a problem to make a simple html page that has just two buttons, one ÔÇ£ONÔÇØ and one ÔÇ£OFFÔÇØ that I can change the background & text color on

- Default button background are lets say red and green
- Click/Touch the ÔÇ£ONÔÇØ button, it shall trigger an event in EG
- The event is then captured (by a script) used to trigger the sending from the websocket
- The event from the websocket is captured in the web client and a java script function is changing the background and text color of the buttons to reflect the actual statuses

Examples below

Any help to make this simple html page to work is very much appreciated. How much I tried, I did not manage to make the page not refresh back to the default state (red & green background)


Best regards, Walter

Java script function that receives events from websocket server and changes the properties of elements in the html page

Code: Select all

function StartUp()
{
	ws = new WebSocket("ws://127.0.0.1:1235");
	ws.onmessage = function (event) {

        if (event.data == "Heater ON")
          {
          document.getElementById("p07").style.backgroundColor="yellow";
          document.getElementById("p07").style.color ="black";
          document.getElementById("p08").style.backgroundColor="black";
          document.getElementById("p08").style.color ="white";
          }
        if (event.data == "Heater OFF")
          {
          document.getElementById("p07").style.backgroundColor="black";
          document.getElementById("p07").style.color ="white";
          document.getElementById("p08").style.backgroundColor="black";
          document.getElementById("p08").style.color ="white";
          }

	};
	ws.onclose = function() {
		ws = ''
		alert('Connection Closed');
	};
}
Sample html with two buttons

Code: Select all

<html lang="sv">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<title>My websocket test page</title>

</head>
<form name="example">
<body onLoad="StartUp()" bgcolor="#3A3C69" text=white link=white vlink=white alink=white />
<center>
<br>
<table border="1" width="25%">
  <tr><!-- Row 4 -->
        <td><button id="p07" onMouseDown="TriggerEvent('Heater ON')" style="background-color:green"><img src="lamp-on.png" ALIGN="right">&nbsp;Towel dryer</button></td>
        <td><button id="p08" onMouseDown="TriggerEvent('Heater OFF')" style="background-color:blue"><img src="lamp-off.png" ALIGN="right">&nbsp;Towel dryer</button></td>
     <td><!-- Col 3 -->
  </tr>
</table>
</center>
</form>
</body>
</html>
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Post by cfull1 »

I need to make the current statuses persistent to make it possible to restart EG and to recover the correct display on all connected web clients
I deal with this in eventghost. Whenever a states changes I write it to a file. When eventhgost starts it grab that file and saves the variable that way.
That may be easier than trying to use an html client.
I need to make it possible so that when a new web client connects, it shall be synchronized with the current statuses
Can you not use the WebSocket.ClientConnected event to trigger a macro to send that data?
When I touch a button on the touch screen to control a device, I need to make sure that the page is not fully refreshed to its default state, it should only update the changed status of the specific device
Does your webpage reload everytime you send a command?
I have mine setup using ajax...I think. I copied part of the code from someone here.

Code: Select all

function Request(eventname){
	var xmlHttp = null;
	var dateObject = new Date();
	try {
		// Firefox, Opera 8.0+, Safari, IE7
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		try
		{
			// IE 6
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			// IE 5
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	xmlHttp.open('GET', "empty?" + eventname + "&" + dateObject.getHours() + ":" + dateObject.getMinutes() + "&" + deviceName, true);		
	xmlHttp.send(null);
	return false;
}
Let me know how it goes,
Carson
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Websocket Plugin

Post by krambriw »

Thanks, I got everything working now :D
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Websocket Plugin

Post by krambriw »

Hi, I have seen that my web client is making a disconnect/reconnect to the websocket server everytime I click on something in the browser that sends an event to eg.

Is it possible to avoid the disconnection in any way?


Best regards, Walter
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Websocket Plugin

Post by Pako »

Dear Walter!
As I already wrote, I'm just deal with the development of my WebSocket plugin.
It forced me to make a demonstration web client as well.
I met with javascript so far only very marginally.
Now I know about it much more.
Therefore, I can advise you:
Your problem is occurs because the BUTTON tag is enclosed in the FORM tag.
If you remove the FORM tag, it will work properly.

Best regards, Pako.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Websocket Plugin

Post by krambriw »

Dear Pako,

This is so exciting, I have a rapid update of my button text and background indicating actual status of devices in EG, I will try to find how to move the buttons as you suggested.

Thanks for the hint,

Very best regards, Walter


And yes, yes, it works super!!!!

Thanks a lot Pako!
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Post by cfull1 »

Pako,

Thanks for your input, I appreciate your help!
Are you currently working on your websocket plugin?
Have you looked into socket.io?
They just updated to 0.7, unfortunately all the python code I can find is for the old version.
Maybe this is something you can address being a more experienced programmer than I?

I'm excited to see where all this goes!
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Websocket Plugin

Post by krambriw »

So I finally have made a simple html page from where I can control lamps and other power consumers. I have also added some information coming from a number of temperature & humidity sensors.

Everything is instantly updated using the websocket server plugin, it really works nice.

I also made buttons as toggle button so they actually will work as pushbuttons once I get my tablets.

Most of the lights and other power consumers are controlled by EG using schedulers or suntracker plugins. They can be operated with
- standard rf remotes
- by EG using schedulers or suntrackers
- from touch screen using tablets

Whatever, the status will always be shown correctly and thats the key point.

For browsers, I have tried various onces but I believe Opera will be my choice because Opera supports websockets on all platforms including android and symbian. I even tried this wih my symbian Nokia mobile and it worked perfect even if the display of course is too small.

Now the fun part starts, it's time to extend with even more controls...

Text is in Swedish but I think you get the idea

Best regards, Walter
Image4.jpg
Last edited by krambriw on Sun Sep 04, 2011 3:40 pm, edited 3 times in total.
Post Reply