NAS Synology shutdown: how to

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

NAS Synology shutdown: how to

Post by Number8 »

Hello,
Since most of the Synology NAS API are undocumented I thought it might be interesting to share how to shutdown Synology NAS using dzvents (currently running dzvents 3.0.9). Bare in mind that the user account used to log to the API has to be admin.
In order to have a general understanding of the Synology API, one might refer to https://global.download.synology.com/do ... eb_API.pdf

Code: Select all

return {
	active = true, -- optional
	on = { 
		devices = { 423 }, 
		httpResponses = { 'sid1', 'endOfAction' }
	},
	data = { 
		sid1 = { initial = "" },
		message = { initial = "" }
	},

	logging = {
		level = domoticz.LOG_INFO,
		marker = "syno script"
	},

	execute = function(dz, item)
		local _u = dz.utils
		local _h = dz.helpers
		local _d = dz.data
		local _ = dz.utils._

		local function messageAdd (msg, msgNew)
			if msg == "" then
				msg = msgNew
			else
				msg = msg .. "\r" .. msgNew								
			end
			return (msg)
		end

		if item.isDevice then
			_d.initialize("sid1")
			_d.initialize("message")
			if item.idx == 423 and item.state == "Off" then
				url= 'http://'.._h.saintPierreNasIP..'/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account='.._h.nasUser..'&passwd='.._h.nasPass..'&session=mySession&format=sid'
				dz.openURL({
           		url = url,
            	callback = 'sid1'
				})
			end
		elseif item.isHTTPResponse then
			if item.trigger == 'sid1' then
				if item.ok then
					if item.isJSON then
						if item.json.success then 
							_d.sid1 = item.json.data.sid
							-- send shutdown - for reboot replace shutdown by reboot
							url = 'http://' .. _h.saintPierreNasIP..'/webapi/entry.cgi?api=SYNO.Core.System&version=1&method=shutdown&_sid='.._d.sid1
							dz.openURL({
			           		url = url,
			            	callback = "endOfAction"
							})
					else
						dz.log ("Looging error, cannot get SID")
						dz.devices(423).toggleSwitch().silent()						
					end
					else
						-- TBD
						dz.log ("API JSON malformed, cannot get SID")
						dz.devices(423).toggleSwitch().silent()
					end
				else
					dz.log ("Can't connect to NAS - statusCode = " .. item.statusCode .. " | status - " .. item.statusText)
					dz.devices(423).toggleSwitch().silent()
				end
			elseif item.trigger == "endOfAction" then
				if item.ok then
					if item.isJSON then
						if item.json.success then 
							dz.log("NAS shutdown initiated")
						else 
							dz.log("NAS shutdown failed") 
							dz.devices(423).toggleSwitch().silent()
						end
					else
						dz.log("NAS shutdown failed") 
						dz.devices(423).toggleSwitch().silent()
					end
				else
					dz.log("NAS shutdown failed") 
					dz.devices(423).toggleSwitch().silent()
				end
			end
		end
	end
}

Debian buster on NUC and three RPi with buster.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest