Sorry about the formatting, but this should help you figure out how to control your Synology Surveillance Station.
var numEnableAttempts = 0; // used to let us retry a Camera Enable a few times in case it's failing on initial attempt
var camTimeoutID = 0; // stores the timerTimeout ID for the 3 hour auto-resume of camera recording
var DiskStation_Addr = '192.168.x.x:5000'; // where to find the Synology Disk Station on our network
var SS_Admin_Password = 'Put_Password_Here'; // Surveillance Station password - must match the password DiskStation has for userID "admin"
// This function gets called internally when the setTimeout timer expires. We fire off the setTimeout timer whenever the Suspend Cams button processing
// occurs. This allows us to resume the camera recording at a fixed time after the user suspends it (default is 3 hours). We also use the setTimeout
// timer (set at 5 seconds) to handle retries of our Resume operation when the initial attempt fails.
function resumeCameras() {
if (numEnableAttempts == 0) { // if we're not here because the 5 second retry time expired...
helper.log(controller,'Cameras being resumed via 3 hour Suspend timer expiring');
}
camTimeoutID = 0; // reset - we're no longer within the 3 hour timeout window
controller.buttonWithName('Resume Cams').activate();
}
function onButtonActivate(button) {
switch (button.name) {
case 'Suspend Cams': // User is asking to suspend back yard camera recording for 3 hours,
if (camTimeoutID != 0) { // check to see if we're already suspended
helper.log(controller,"Suspend Cameras requested, but they're already suspended.");
break; // break out of the switch since we're not going to do anything, otherwise fall thru to the Resume Cams case
}
case 'Resume Cams': // or they're asking to resume them now (or the 3 hour timer expired and an auto-resume is in progress)
if (button.name == 'Suspend Cams') {
var op = 'Disable'
} else {
var op = 'Enable'
}
// This code is designed to work with Surveillance Station 7.x (ver 2.0 of the API) Jan 2016
var cameraIDs = '14,15,19,20'; // List of camera ID #s to suspend recording
// 14=Patios / 15=Back Yard West / 19=Back Yard PTZ / 20=MBR PTZ
var sid = ''; // Holds the SID we get back from the Surveillance Station login call
var sid_pos = ''; // Holds the position of the SID in the GET response
var req = new XMLHttpRequest();
req.onload = xmlLoaded1;
req.open('GET','http://' + DiskStation_Addr + '/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account=admin&passwd=' + SS_Admin_Password + '&Session=SurveillanceStation&format=sid');
req.send();
function xmlLoaded1() { // called when Login GET returns
helper.log(controller,'Login: ' + req.responseText);
sid_pos = req.responseText.indexOf('"sid":')
if (sid_pos > -1) { // Should be true unless login failed
sid = req.responseText.slice(sid_pos + 7); // copy everything starting 7 chars after the sid label
sid = sid.slice(0,sid.indexOf('"')); // copy everything up to the trailing " after the sid
helper.log(controller,'sid="' + sid + '"');
req.onload = xmlLoaded2;
req.open('GET','http://' + DiskStation_Addr + '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera&method=' + op + '&version=3&cameraIds="' + cameraIDs +'"&_sid=' + sid);
req.send();
} else {
helper.sendNotification(controller,'Failed to log in to Surveillance Station. Camera recording not suspended.');
}
} // xmlLoaded1()
function xmlLoaded2() { // called when Disable/Enable GET returns
if (req.responseText.indexOf('"success":true') > -1) {
numEnableAttempts = 0; // reset our retry counter every time we succeed
helper.log(controller,op + ': Success'); // a success response can be quite lengthy, so just cut to the chase
if (op == 'Disable') {
controller.messageWithName('Cams Suspended').log();
helper.sendNotification(controller,'Back yard camera recording is being suspended for 3 hours.');
camTimeoutID = setTimeout(resumeCameras, 3*60*60*1000); // set the timer for 3 hours that will re-enable them
} else {
helper.sendNotification(controller,'Back yard camera recording resumed.');
controller.messageWithName('Cams Resumed').log();
clearTimeout(camTimeoutID); // clear the 3 hour timeout timer in case this Resume was from a manual user request
camTimeoutID = 0; // reset - we're no longer within the 3 hour timeout window
}
} else { // for some reason the attempt failed…
helper.log(controller,op + ': ' + req.responseText);
if (op == 'Enable') { // if this is an attempt to Enable, it's important to try again a couple of times
if (numEnableAttempts < 3) { // because we don't want to leave the cameras disabled
numEnableAttempts += 1;
helper.log(controller, 'Back yard camera recording Resume operation failed. Trying again…');
setTimeout(resumeCameras, 5000); // wait 5 seconds and try the Surveillance Station API commands again
} else { // Even after 3 attempts we still failed to resume recording
controller.messageWithName('Cam Resume Fail').log();
helper.log(controller, 'Back yard camera recording Resume operation FAILED. No retries left to try again.');
helper.sendNotification(controller,'Back yard camera recording FAILED to resume!');
}
}
}
req.onload = xmlLoaded3;
req.open('GET','http://' + DiskStation_Addr + '/webapi/auth.cgi?api=SYNO.API.Auth&method=Logout&version=2&_sid=' + sid);
req.send();
} // xmlLoaded2()
function xmlLoaded3() { // called when Logout GET returns
helper.log(controller,'Logout: ' + req.responseText);
} // xmlLoaded3()
break; // done with Suspend Cams / Resume Cams
} // switch
} // onButtonActivate()
Edited by 2MuchTech, 05 January 2017 - 10:13 PM.