|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Microsoft has released an Android application for automation using javascript which is a pretty cool idea.
I just tried it out and wanted to share my simple idea to start my Sonos player as soon as I get WiFi connection (I know there was a question regarding starting it when you get home before on the forum). This is a simple script that would just try to start all players that you add to the script manually (into the players array): Code:
// Players that you want to autostart
// Players that are grouped, or contains no queue or paused item will not be affected
var players = [
"192.168.1.151",
"192.168.1.152",
"192.168.1.154"
];
// Between which hours on the day we should try to autostart the players
// Example below, if time is between 17:00-19:59
var startHour = 17;
var endHour = 20;
// SSID of your home network. This is case sensitive!
// Since 0.60 or something, we now get SSID qouted in the string which is a breaking change.
// This might change again.
var ssid = "'\"JS 5G\"";
////////// DO NOT MODIFY BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
function invokePlay() {
for (var i in players) {
var ip = players[i];
device.ajax({
url: 'http://'+ip+':1400/MediaRenderer/AVTransport/Control',
type: 'POST',
headers: {
'CONTENT-TYPE': 'text/xml; charset="utf-8"',
'CONNECTION': 'close',
'SOAPACTION': '"urn:schemas-upnp-org:service:AVTransport:1#Play"'
},
data: '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></u:Play></s:Body></s:Envelope>'
});
}
}
device.network.on("wifiOn", function (ns) {
var now = new Date();
var hour = now.getHours();
var lastStarted = device.localStorage.getItem('lastStarted') || 0;
lastStartedDate = new Date(lastStarted);
var lastStartedSince = (now.valueOf() - lastStarted) / 1000;
console.log(now.toUTCString(), lastStartedDate.toUTCString(), lastStartedSince);
if (ns.ssid != ssid) {
console.log("won't try to start since not on home network", ssid, ns.ssid);
return;
}
// If it's between defined hours, and last time we started it was more than 20 hours ago
if (hour < startHour || hour >= endHour || lastStartedSince < 72000){
console.log("Not applicable to start players because of time interval", now.toUTCString(), hour, lastStartedSince);
return;
}
device.notifications.createNotification('Starting players...').show();
console.log('invoking wifiOn');
// To prevent another autostart in the next 20 hours
device.localStorage.setItem('lastStarted', now.valueOf());
invokePlay();
});
Since version 0.55 of on{X}, they now include the SSID of your network when notifying about wifi connection. This means that you can limit it to your home network. I updated the script to reflect that. Have any other suggestions for automation? Let's share ideas! Cheers, Jimmy Last edited by jishi; Feb 7th, 2013 at 02:13 AM. Reason: since version 0.60 you get SSID quoted for some reason. |
|
#2
|
|||
|
|||
|
jishi,
Great idea. Rather than scare regular users, we should keep this sort of discussion in The Unsupported Area. And, for the less than hard core hacker, I'll mention that this scheme is best coupled with DHCP IP address reservation in the router. |
|
#3
|
|||
|
|||
|
This is exactly what I was looking for, thank you!
will try this out tonight.. my original idea was to use NFC tags to start sonos playback when I get home, but with this you don't even have to scan the tag, it should happen automatically as soon as you connect to your home SSID. I think I saw some recipes on the on{x] where people are checking the SSID you have connected to, so it looks like it should be possible Goran |
|
#4
|
|||
|
|||
|
I updated the script with the following:
Time limitation (only try to start players between 17-20 / 5pm and 8pm), this is easily changeable. Prevent it from trying to autostart several times during the evening (in the event of signal loss). It will only try once every 20h, which basically means once every day (when you get home from work!) |
|
#5
|
|||
|
|||
|
hm, I tried updating the title for the thread, but it only updated the title for the first post.
Guess it has to be done by a moderator? Can anyone assist me with that? |
|
#6
|
|||
|
|||
|
Done.
|
|
#7
|
|||
|
|||
|
Since the last update, they now give you which SSID you have connected to, this gives you better control of when to actually try to start the players (only on your home network).
I updated the script to include this. |
|
#8
|
|||
|
|||
|
Made a mistake in the SSID-check. Updated the script.
|
|
#9
|
|||
|
|||
|
I'm not an Android user but I love the idea of this little script.
So little but so cool! Gött jobbat agron
__________________
Son's room: Philips SLA-5520 (audio) Zyxel (video) Kitchen: Logitech Squeezebox Radio Livingroom: Sonos Connect:amp Philips SLM-5500 (video), eeebox server New Part: Sonos Play 3, Sonos Dock |
|
#10
|
|||
|
|||
|
I realized that my check for previous attempts to start players wasn't working, since you can't store objects in the localStorage.
It should work better with the updated version. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|