Websockets
Einklappen
X
-
-
Ok, thanks for your response.
The burning question I have is how you handle the status updates?
Furthermore, anything you can already share about what your plan is?
Kommentar
-
In short the idea is the following:
- Connect to miniserver using websockets --> WORKS
- Use keep alive --> WORKS
- Register for status updates from miniserver --> WORKS
- Read loxone structure file --> WORKS (without parsing)
- Be able to modify active scene or modify individual lights --> Working sample, need to create gui for it
- Make a configurable list of UUID's of scenes and lights that we are interested in to control & extract data from loxone structure file --> TODO
I want to control my loxone miniserver using a simple web interface on a raspberry pi with 7" Pi TFT using AngularJS, i'm also integrating mopidy, my security camera system & ventilation&heating system.
-
-
For the parsing of the updates i've got 2 helper files from loxone support:
datastream.js - http://pastebin.com/yG5EahJu
binaryevent.js - http://pastebin.com/akM7gN9V
I have a callback method that gets executed when a websocket message is received. I hope this makes some things clear. If you have further questions please ask. The part where the type of message is a string is used for my keep alive. I send a request for the dev/sps/LoxAPPversion3, i'm using it for keep alive otherwise the miniserver closes the connection and i compare the last modified timestamp with the cached one. In case the timestamp is updated i reload the structure into the cache.
--------------------------------------------------
Code:[COLOR=#808080][I]// Websocket callback for parsing status messages and updating cached json tree [/I][/COLOR]$rootScope.[COLOR=#7a7a43]_processStatusMessage [/COLOR]= [COLOR=#000080][B]function[/B][/COLOR](message) { [COLOR=#000080][B]if [/B][/COLOR]([COLOR=#008000][B]"object" [/B][/COLOR]== [COLOR=#000080][B]typeof [/B][/COLOR]message.[COLOR=#660e7a][B]data[/B][/COLOR]) { [COLOR=#000080][B]var [/B][/COLOR][COLOR=#458383]arrayBuffer[/COLOR]; [COLOR=#000080][B]var [/B][/COLOR][COLOR=#458383]fileReader [/COLOR]= [COLOR=#000080][B]new [/B][/COLOR][COLOR=#660e7a][B]FileReader[/B][/COLOR](); [COLOR=#458383]fileReader[/COLOR].onload = [COLOR=#000080][B]function [/B][/COLOR]() { [COLOR=#458383]arrayBuffer [/COLOR]= [COLOR=#000080][B]this[/B][/COLOR].[COLOR=#660e7a][B]result[/B][/COLOR]; [COLOR=#000080][B]var [/B][/COLOR][COLOR=#458383]be [/COLOR]= [COLOR=#000080][B]new [/B][/COLOR][I]BinaryEvent[/I]([COLOR=#458383]arrayBuffer[/COLOR]); [COLOR=#000080][B]for [/B][/COLOR]([COLOR=#000080][B]var [/B][/COLOR][COLOR=#458383]i [/COLOR]= [COLOR=#0000ff]0[/COLOR]; [COLOR=#458383]i [/COLOR]< [COLOR=#458383]be[/COLOR].[COLOR=#660e7a][B]events[/B][/COLOR].[COLOR=#660e7a][B]length[/B][/COLOR]; [COLOR=#458383]i[/COLOR]++) { [COLOR=#808080][I]// [/I][/COLOR][COLOR=#0000ff][B][I]TODO update local cached json tree [/I][/B][/COLOR][COLOR=#0000ff][B][I] [/I][/B][/COLOR][COLOR=#660e7a][B]console[/B][/COLOR].[COLOR=#7a7a43]log[/COLOR]([COLOR=#458383]be[/COLOR].[COLOR=#660e7a][B]events[/B][/COLOR][[COLOR=#458383]i[/COLOR]].[COLOR=#7a7a43]toString[/COLOR]()); } }; [COLOR=#458383]fileReader[/COLOR].[COLOR=#7a7a43]readAsArrayBuffer[/COLOR](message.[COLOR=#660e7a][B]data[/B][/COLOR]); } [COLOR=#000080][B]else if [/B][/COLOR]([COLOR=#008000][B]"string" [/B][/COLOR]== [COLOR=#000080][B]typeof [/B][/COLOR]message.[COLOR=#660e7a][B]data[/B][/COLOR]) { [COLOR=#000080][B]try [/B][/COLOR]{ [COLOR=#000080][B]var [/B][/COLOR][COLOR=#458383]textMessage [/COLOR]= [COLOR=#660e7a][B]angular[/B][/COLOR].[I]fromJson[/I](message.[COLOR=#660e7a][B]data[/B][/COLOR]); [COLOR=#000080][B]if [/B][/COLOR]([COLOR=#458383]textMessage[/COLOR].LL.Code == [COLOR=#0000ff]200[/COLOR]) { [COLOR=#000080][B]if [/B][/COLOR]([COLOR=#458383]textMessage[/COLOR].LL.control == [COLOR=#008000][B]"dev/sps/LoxAPPversion3"[/B][/COLOR]) { [COLOR=#808080][I]// Received keep alive response, comparing last modified dates [/I][/COLOR][COLOR=#808080][I] [/I][/COLOR]$rootScope.[COLOR=#7a7a43]_compareLastModified[/COLOR]([COLOR=#660e7a][B]angular[/B][/COLOR].[I]fromJson[/I](message.[COLOR=#660e7a][B]data[/B][/COLOR])); } } [COLOR=#000080][B]else [/B][/COLOR]{ [COLOR=#660e7a][B]console[/B][/COLOR].[COLOR=#7a7a43]error[/COLOR]([COLOR=#458383]textMessage[/COLOR].LL.Code + [COLOR=#008000][B]' - ' [/B][/COLOR]+ [COLOR=#458383]textMessage[/COLOR].LL.control + [COLOR=#008000][B]' - ' [/B][/COLOR]+ [COLOR=#458383]textMessage[/COLOR].LL.[COLOR=#7a7a43]value[/COLOR]); } } [COLOR=#000080][B]catch [/B][/COLOR](h) { [COLOR=#660e7a][B]console[/B][/COLOR].[COLOR=#7a7a43]error[/COLOR](h); } } [COLOR=#000080][B]else [/B][/COLOR]{ [COLOR=#660e7a][B]console[/B][/COLOR].[COLOR=#7a7a43]error[/COLOR]([COLOR=#008000][B]"Invalid type of message received!"[/B][/COLOR]); [COLOR=#660e7a][B]console[/B][/COLOR].[COLOR=#7a7a43]error[/COLOR](message); $rootScope.[COLOR=#7a7a43]reset[/COLOR](); } };Zuletzt geändert von Gast; 08.12.2015, 19:07.Kommentar
-
Good info about websockets can be found in the old loxone forum:
Kommentar
-
In the meantime, anybody made any progress with the status update? Put the structure file in cache, compare structure file.
Main problem I have at this stage is that I can get the status of "state" but I don't how how to correlate the UUID of the state with the action-UUID.
If there is any Loxonaut that could give any insight in this issue, would be more then welcome.
The idea I have is to integrate the Logitech media server together with Loxone in some kind of dashboard on my tablet.
Offcourse this is something Loxone will never support, because they have their own music server.
Kommentar
-
Dear forum...
I have been struggling to get the web socket connection to work for the last couple of days. I have crawled through all available documentation available including the posts in the Loxone forum archive. I am using chrome browser with CORS blocking disabled with a plain HTML file. I am applying the code below but I am unable to establish the websocket connection (Error code 400). Anyone any idea if this is supposed to work or if it is still supported? Upgraded to the latest firmware last week...
Your help would be greatly appreciated.
function hexToString(d){for(var r="",m=(""+d).match(/../g),t;t=m.shift()
r+=String.fromCharCode("0x"+t);return r}
var getKey = new XMLHttpRequest();
getKey.open("GET","http://admin:admin@192.168.1.2/jdev/sys/getkey?" + Math.random(),!0);
getKey.send(null);
getKey.onreadystatechange=function()
{
if(4===getKey.readyState && 200===getKey.status)
{
var salt=JSON.parse(JSON.parse(getKey.responseText).LL .value);
var url = "ws://192.168.1.2/ws";
var hash = Crypto.HMAC(Crypto.SHA1, "admin:admin", hexToString(salt));
try
{
var d = new WebSocket(url, hash);
d.onopen = function ()
{
console.log("opened");
};
d.onclose = function ()
{
console.log("closed");
}
}
catch (h)
{
alert(h.message);
}
}
}
</script>
<body>
</body>
</html>
Kommentar
Kommentar