I feel like I’m missing something important, but how do I use my existing socket.io connection to call an action on a device? For instance how do I:
GET /api/device/my-switch/turnOn
with socket.io?
@dgmltn You can do so by using the “/api/execute-action” POST method. Here is an example for turning a switch on. The “actionString” syntax is what you have as part of rules. So, you can use id or name to specify the device. I think the REST-API needs better documentation with more examples.
EDIT: Sorry, I didn’t read your post carefully. Using a web socket the following code snippet is doing the same as the HTTP-POST above.
var io = require('socket.io-client');
var host = 'raspi2.fritz.box';
var port = 80;
var u = encodeURIComponent('XXXXXXXXX');
var p = encodeURIComponent('XXXXXXXXX');
var socket = io('http://' + host + ':' + port + '/?username=' + u + '&password=' + p, {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax: 3000,
timeout: 20000,
forceNew: true
});
socket.on('connect', function() {
console.log('connected');
socket.emit('call', {
id: 'executeAction-1',
action: 'executeAction',
params: {
actionString: 'switch sispm3 on'
}
});
});
socket.on('callResult', function(msg){
if(msg.id === 'executeAction-1') {
console.log(msg.result);
}
});
EDIT2: … and I did not realize that something like GET /api/device/sispm3/turnOn also works! It is also a learning exercise for me.
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
I agree, the API documentation could use some work. I am currently making an external frontend for my pimatic setup with no config options, as the end user will be my parents.
I’d like to use socket.io as the main tool, but I haven’t gotten to the part where I get the switch statuses hooked to front-end components. Well, it’s a learning process overall. …
If someone has already done a second front-end of somekind, it would be great if it was shared as an example.
@kitsunen Sounds great. It may also be somewhat helpful to have a look at https://github.com/pimatic/pimatic-peer which implements a kind of proxy device for a remote pimatic device using the socket API. Note, however, the project is just a proof-of-concept implementation.
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
Thanks for the help. It seems like executeAction should work.
EDIT2: … and I did not realize that something like GET /api/device/sispm3/turnOn also works! It is also a learning exercise for me.
FWIW, this is what the pimatic web ui uses, when I analyze it using chrome developer tools.