Logic Machine Forum
Control Camera Notification Status - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Control Camera Notification Status (/showthread.php?tid=71)



Control Camera Notification Status - jetsetter - 28.08.2015

Hi to all.




I am looking for some help on how to create a library in order to call some functions provided by the Synology Surveillance Station API found here.

[url=http://global.download.synology.com/download/Document/DeveloperGuide/Surveillance_Station_Web_API_v2.0.pdf][/url]

The idea is to be able to turn on/off the notification setting of this recorder, so for example, if I arm my alarm system, the KNX bus knows, so through a relative LM script I can then enable the camera recorder to start sending me notification emails (i.e if movement detection), and if I disarm the alarm, the script will disable the notifications.

In the recorder API documentation I understand that it support control through JSON commands and more specifically, I need to modify each time the boolean parameter "mailEnable" of the "SetSetting" Method (page 243) like this:



GET /webapi/entry.cgi?

mailEnable=1 (or 0 accordingly)



Also as I understand from the NetATMO scripting presented at another thread, this must be similar with what I am looking for, but since I am a very new at programming (and LUA and LM in general) can somebody help me with some basic guidelines to start a little faster?



Thank you all in advance.


RE: Control Camera Notification Status - admin - 31.08.2015

You can use LuaSocket to do HTTP requests:
http://w3.impa.br/~diego/software/luasocket/http.html#request

Your code should look like this (replace 1.2.3.4 with your Synology device IP):
Code:
require('socket.http')
socket.http.TIMEOUT = 5
data = socket.http.request('http://1.2.3.4/webapi/entry.cgi?mailEnable=1')



RE: Control Camera Notification Status - jetsetter - 31.08.2015

Thank you for your reply.
I tried this but it didn't work because as I saw in the API documentation, you have first to login to the synology like this:

GET /webapi/auth.cgi?

api=SYNO.API.Auth&method=Login&version=2&account=USERNAME&passwd=PASSWORD&session=SurveillanceStation&format=sid

or in LUA according to your example, it will be something like this:


Code:
data = socket.http.request('http://1.2.3.4/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&
account=USERNAME&passwd=PASSWORD&session=SurveillanceStation&format=sid')

Then you have to get the sid that will be returned as a response to the above request (GET) command and use it to all other calls you made like this:
Code:
data = socket.http.request('http://1.2.3.4/webapi/entry.cgi?mailEnable=1_sid=Jn5dZ9aS95wh2')

Now in order to insert in the string above the sid response that I would have previously get as data, should I try something like this?:

local request_body = "http://1.2.3.4/webapi/entry.cgi?mailEnable=1_sid=".. data
and then send this?:

data = socket.http.request(request_body)

??


RE: Control Camera Notification Status - admin - 31.08.2015

The easiest way is to do auth request in your browser and check response format. If you get just raw sid value then next request is simple:
Code:
url = "http://1.2.3.4/webapi/entry.cgi?mailEnable=1&_sid=".. data
data = socket.http.request(request_body)

Otherwise, post response here and we will help you with parsing it Wink


RE: Control Camera Notification Status - jetsetter - 31.08.2015

I just tried in the browser and I got this:
{"data":{"sid":"IuGC4XNPXTzfkB3J4N01003"},"success":true}
Smile
So how can i get only the sid part out of the response??


RE: Control Camera Notification Status - Pawel - 31.08.2015

It looks like JSON type string, so the easiest way is to use already existed library, so:


Code:
local json = require "json"



stringfromsynology = [[{"data":{"sid":"IuGC4XNPXTzfkB3J4N01003"},"success":true}]]
local t = json.decode( stringfromsynology)

log(t) -- here you can see what json library done
if t.success == true then
 sid_number = t.data.sid
end
log (sid_number)



RE: Control Camera Notification Status - jetsetter - 18.09.2015

(31.08.2015, 19:04)Pawel Wrote: It looks like JSON type string, so the easiest way is to use already existed library, so:


Code:
local json = require "json"



stringfromsynology = [[{"data":{"sid":"IuGC4XNPXTzfkB3J4N01003"},"success":true}]]
local t = json.decode( stringfromsynology)

log(t) -- here you can see what json library done
if t.success == true then
 sid_number = t.data.sid
end
log (sid_number)

Thanks a ton Pawel. I will give it a try this weekend and report back results!