Synology surveillance center script

Posted on
Tue Mar 24, 2015 3:03 am
hakanos offline
Posts: 6
Joined: Mar 17, 2015

Synology surveillance center script

Hi guys,

With my Fibaro HC2 I could start/stop my ip cam recordings on my synology nas with a script. Is there a way I could do this with indigo? I am not that good with programming, I would be great If someone could convert this to Python.

Here is the script I used with my HC2:

start recording:


-- User configurable variables
local login = "admin"
local password = "xxxxxxx"
local cameras = {1,3}
local action = "start"

-- System variables
local selfID = fibaro:getSelfId()
local ip = fibaro:get(selfID, 'IPAddress')
local port = fibaro:get(selfID, 'TCPPort')
local Synology = Net.FHttp(ip, tonumber(port))
local API_AUTH_ERROR_CODE = {
[100] = "Unknown error.",
[101] = "The account parameter is not specified.",
[400] = "Invalid password.",
[401] = "Guest or disabled account.",
[402] = "Permission denied.",
[403] = "One time password not specified.",
[404] = "One time password authenticate failed."
}
local API_RECORD_ERROR_CODE = {
[400] = "Execution failed.",
[401] = "Parameter invalid.",
[402] = "Camera disabled."
}

-- Discover available APIs and corresponding information
payload = "/webapi/query.cgi?api=SYNO.API.Info&method=Query&version=1&query=SYNO.API.Auth,SYNO.SurveillanceStation.ExternalRecording"
response, status, errorCode = Synology:GET(payload)
if tonumber(status) == 200 then
jsonTable = json.decode(response);
if jsonTable.data["SYNO.API.Auth"].maxVersion >= 2 and jsonTable.data["SYNO.SurveillanceStation.ExternalRecording"].maxVersion >= 2 then
fibaro:debug("Synology API version OK")
pathAuth = jsonTable.data["SYNO.API.Auth"].path
pathRecord = jsonTable.data["SYNO.SurveillanceStation.ExternalRecording"].path
fibaro:debug("Synology API Auth path = "..pathAuth)
fibaro:debug("Synology API Surveillance Station Record path = "..pathRecord)

-- Create new login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Login&version=2&account="..login.."&passwd="..password.."&session=SurveillanceStation&format=sid"
response, status, errorCode = Synology:GET(payload)
if tonumber(status) == 200 then
jsonTable = json.decode(response);
if jsonTable.success == true then
SID = jsonTable.data.sid
fibaro:debug("Synology API Auth SID = "..SID)

for i = 1, #cameras do
-- Start or stop external recording of a camera
payload = "/webapi/"..pathRecord.."?api=SYNO.SurveillanceStation.ExternalRecording&method=Record&version=2&cameraId="..cameras[i].."&action="..action.."&_sid="..SID
response, status, errorCode = Synology:GET(payload)
jsonTable = json.decode(response);
if jsonTable.success == true then
fibaro:log("OK")
fibaro:debug('<span style="color:green;">Synology Surveillance Station '..action..' recording for camera "'..tostring(cameras[i])..'" OK</span>')
else
fibaro:log("Erreur")
fibaro:debug('<span style="color:red;">Synology Surveillance Station '..action..' recording for camera "'..tostring(cameras[i])..'" FAILED : '..API_RECORD_ERROR_CODE[tonumber(jsonTable.error.code)]..'</span>')
end
end

-- Destroy current login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Logout&version=2&session=SurveillanceStation&_sid="..SID
response, status, errorCode = Synology:GET(payload)
else
fibaro:log("Erreur")
fibaro:debug('<span style="color:red;">Error : '..API_AUTH_ERROR_CODE[tonumber(jsonTable.error.code)]..'</span>')
end
else
fibaro:log("Erreur")
fibaro:debug('<span style="color:red;">Error : API Authentication failure</span>')
end
else
fibaro:log("Erreur")
fibaro:debug('<span style="color:red;">Error : Synology API version is too old : <b>DSM 4.0-2251</b> and <b>Surveillance Station 6.1</b> are required</span>')
end
else
fibaro:log("Erreur")
fibaro:debug('<span style="color:red;">Error : Can not connect to Synology server</span>')
end

Posted on
Tue Mar 24, 2015 9:21 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Synology surveillance center script

I don't pretend to understand all of that script, but it looks like it could be done in Python. I'd recommend looking to see if you can find any Python examples for controlling the Synology.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Apr 02, 2015 4:19 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Synology surveillance center script

jay (support) wrote:
I don't pretend to understand all of that script

Surprised at that, Jay - its a pretty straight forward script for anyone familiar with any coding language, even if not the specific language it's written in. ;-)

Hakanos, I'm not versed in Python myself (though I'm learning), but that script is very simple for anyone who does know Python. It just needs HTTP and JSON handlers, which I'm sure will be native to Python 2.5 that's currently supported by Indigo.

I'm sure someone will pop along to give you a hand. I'll keep an eye on the thread as well.

Peter

Posted on
Fri Apr 03, 2015 1:50 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Synology surveillance center script

Just got nosy and decided to google this - it looks like the Python 'Requests' module is what you want:

http://www.python-requests.org/en/lates ... quickstart

Deals with the HTTP calls and the JSON parsing in one.

Peter

Posted on
Thu Jan 05, 2017 2:44 am
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

Re: Synology surveillance center script

Hi

Have anyone of you got this script to work and are you using it in a newer version of Surveillance station?

RAID5 "A happy Swede addicted to automation"

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests