Bonjour jojo,
Impossible de vous envoyer un message, mais je me suis dit que cela serait pas trop grave de mettre le code ici.
Je rappelle que je n'ai pas la paternité du VD, il fut d'abord créé par fdp2.
Voici le code pour les différents boutons du VD NAS Synology :
Redémarrer :
--------------------------------------------------
-- Synology DSM
-- Reboot the disk station
-- janvier 2015
--------------------------------------------------
-- User configurable variables
local login = "aa";
local password = "bb";
-- 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 = {
[0] = "Sans erreur.",
[100] = "Erreur inconnue.",
[101] = "Compte non spécifié.",
[400] = "Mot de passe invalide.",
[401] = "Compte désactivé.",
[402] = "Permission refusée.",
[403] = "Unique mot de passe non spécifié.",
[404] = "Autentification erronée."
};
local API_RECORD_ERROR_CODE = {
[400] = "Exécution ratée.",
};
-- Discover available APIs and corresponding information
payload = "/webapi/query.cgi?api=SYNO.API.Info&method=Query&version=1&query=SYNO.API.Auth";
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
jsonTable = json.decode(response);
local version = jsonTable.data["SYNO.API.Auth"].maxVersion;
if version >= 2 then
fibaro:debug("Version API Synology OK");
pathAuth = jsonTable.data["SYNO.API.Auth"].path;
fibaro:debug("Chemin API Synology = "..pathAuth);
cgiUsed="/entry.cgi";
apiUsed="SYNO.Core.System";
if version < 6 then
cgiUsed="/dsm/system.cgi";
apiUsed="SYNO.DSM.System";
end
-- Create new login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Login&version=2&account="..login.."&passwd="..password.."&session=DSM&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("SID API Synology = ["..SID.."]");
-- Reboot the disk station
payload = "/webapi"..cgiUsed.."?api="..apiUsed.."&version=1&method=reboot&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
fibaro:debug("Response ["..response.."]");
jsonTable = json.decode(response);
if jsonTable.success == true then
fibaro:log("Redémarrage OK");
fibaro:debug('<span style="color:green;">Redémarrage Disk Station Synology OK</span>');
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Disk Station Synology n\'a pas redémarré</span>');
end
--
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(errorCode)]..'</span>');
--fibaro:debug('<span style="color:red;">Erreur : '..errorCode..'</span>');
end
-- Destroy current login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Logout&version=2&session=DSM&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
fibaro:debug(status);
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(jsonTable.error.code)]..'</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Authentification API ratée</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Version API Synology trop vieille : <b>DSM 4.0-2251</b> est requise</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Impossible de se connecter au Disk Station Synology</span>');
end
Arrêter :
--------------------------------------------------
-- Synology DSM
-- Shutdown the disk station
-- janvier 2015
--------------------------------------------------
-- User configurable variables
local login = "aa";
local password = "bb";
-- 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] = "Erreur inconnue.",
[101] = "Compte non spécifié.",
[400] = "Mot de passe invalide.",
[401] = "Compte désactivé.",
[402] = "Permission refusée.",
[403] = "Unique mot de passe non spécifié.",
[404] = "Autentification erronée."
};
local API_RECORD_ERROR_CODE = {
[400] = "Exécution ratée.",
};
-- Discover available APIs and corresponding information
payload = "/webapi/query.cgi?api=SYNO.API.Info&method=Query&version=1&query=SYNO.API.Auth";
response, status, errorCode = Synology:GET(payload);
--fibaro:debug("response [" .. response .. "] status [" .. status .."] error [" .. errorCode .. "]")
if tonumber(status) == 200 then
jsonTable = json.decode(response);
local version = jsonTable.data["SYNO.API.Auth"].maxVersion;
if version >= 2 then
fibaro:debug("Version API Synology OK");
pathAuth = jsonTable.data["SYNO.API.Auth"].path;
fibaro:debug("Chemin API Synology = "..pathAuth);
cgiUsed="/entry.cgi";
apiUsed="SYNO.Core.System";
if version < 6 then
cgiUsed="/dsm/system.cgi";
apiUsed="SYNO.DSM.System";
end
-- Create new login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Login&version=2&account="..login.."&passwd="..password.."&session=DSM&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("SID API Synology = "..SID);
-- ShutDown the disk station
payload = "/webapi"..cgiUsed.."?api="..apiUsed.."&version=1&method=shutdown&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
fibaro:debug("response [" .. response .. "] status [" .. status .."] error [" .. errorCode .. "]")
jsonTable = json.decode(response);
if jsonTable.success == true then
fibaro:log("Arrêt OK");
fibaro:debug('<span style="color:green;">Arrêt du Disk Station Synology OK</span>');
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Le Disk Station ne s\'est pas arrêté</span>');
end
--
-- Destroy current login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Logout&version=2&session=DSM&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
fibaro:debug(status);
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(jsonTable.error.code)]..'</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Authentification API ratée</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Version API Synology trop vieille : <b>DSM 4.0-2251</b> est requise</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Impossible de se connecter au Disk Station Synology</span>');
end
État :
--------------------------------------------------
-- Synology DSM
-- Reboot the disk station
-- janvier 2015
--------------------------------------------------
-- User configurable variables
local login = "aa";
local password = "bb";
-- System variables
local selfID = fibaro:getSelfId();
local ip = fibaro:get(selfID, 'IPAddress');
local port = fibaro:get(selfID, 'TCPPort');
local nameNAS = fibaro:getName(selfID);
local Synology = Net.FHttp(ip, tonumber(port));
local API_AUTH_ERROR_CODE = {
[0] = "Sans erreur.",
[100] = "Erreur inconnue.",
[101] = "Compte non spécifié.",
[400] = "Mot de passe invalide.",
[401] = "Compte désactivé.",
[402] = "Permission refusée.",
[403] = "Unique mot de passe non spécifié.",
[404] = "Autentification erronée."
};
local API_RECORD_ERROR_CODE = {
[400] = "Exécution ratée.",
};
local OK_connected = false;
-- Test connection
tcpSocket = Net.FTcpSocket(ip, tonumber(port));
bytes, errorCode = tcpSocket:write("Test");
fibaro:log("Détection");
if errorCode == 0 then
OK_connected = true;
fibaro:log(nameNAS.." ON");
fibaro:call(selfID, "setProperty", "currentIcon", "52");
--fibaro:call(175, "sendDefinedPushNotification", "Ping ReseauHome OK");
else
fibaro:log(nameNAS.." OFF");
fibaro:call(selfID, "setProperty", "currentIcon", "53");
--fibaro:call(175, "sendDefinedPushNotification", "Ping ReseauHome OFF");
end
tcpSocket:disconnect();
-- Round function to display percentages
round = function(num)
local a = math.floor(num*100 + 0.5)/ 100;
return a,string.format("%.2f",a);
end
if OK_connected then
-- Discover available APIs and corresponding information
payload = "/webapi/query.cgi?api=SYNO.API.Info&method=Query&version=1&query=SYNO.API.Auth";
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
jsonTable = json.decode(response);
version = jsonTable.data["SYNO.API.Auth"].maxVersion;
if version >= 2 then
fibaro:debug("Version API Synology OK");
pathAuth = jsonTable.data["SYNO.API.Auth"].path;
fibaro:debug("Chemin API Synology = "..pathAuth);
cgiUsed_cpu_mem = "/entry.cgi";
cgiUsed_temp = cgiUsed_cpu_mem;
cgiUsed_vols = cgiUsed_cpu_mem;
apiUsed_cpu_mem = "SYNO.Core.System.Utilization";
apiUsed_temp_system = "SYNO.Core.System";
apiUsed_temp_disk = apiUsed_temp_system;
apiUsed_vols = apiUsed_temp_system;
apiMethod_cpu_mem="get";
apiMethod_temp = "info";
apiMethod_vols = apiMethod_temp;
if version < 6 then
cgiUsed_cpu_mem = "/dsm/system_loading.cgi";
cgiUsed_temp = "/dsm/info.cgi";
cgiUsed_vols = "/dsm/volume.cgi";
apiUsed_cpu_mem = "SYNO.DSM.SystemLoading";
apiUsed_temp_system = "SYNO.DSM.Info";
apiUsed_temp_disk = "";
apiUsed_vols = "SYNO.DSM.Volume";
apiMethod_cpu_mem = "getinfo";
apiMethod_temp = apiMethod_cpu_mem;
apiMethod_vols = "list";
end
-- Create new login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Login&version=2&account="..login.."&passwd="..password.."&session=DSM&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("SID API Synology = ["..SID.."]");
-- Prepare string
local info_cpu_memory = "";
-- CPU info from the disk station
payload = "/webapi"..cgiUsed_cpu_mem.."?api="..apiUsed_cpu_mem.."&version=1&method="..apiMethod_cpu_mem.."&type=current&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
jsonTable = json.decode(response);
if jsonTable.success == true then
info_cpu = jsonTable.data.cpu;
load_mem = jsonTable.data.memory.real_usage;
fibaro:debug("Memory: "..load_mem.."%");
info_cpu_memory = "Memory: "..load_mem.."% ";
load_cpu = 0;
if (version < 6) then
load_cpu = info_cpu.user*100;
else
load_cpu = info_cpu.other_load+info_cpu.system_load+info_cpu.user_load;
end
fibaro:debug("CPU: "..load_cpu.."%");
info_cpu_memory = info_cpu_memory.."CPU: "..load_cpu.."%";
fibaro:call(selfID, "setProperty", "ui.CPULoad.value", info_cpu_memory);
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(errorCode)]..'</span>');
end
-- Temp info from the disk station
payload = "/webapi"..cgiUsed_temp.."?api="..apiUsed_temp_system.."&version=1&method="..apiMethod_temp.."&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
jsonTable = json.decode(response);
if jsonTable.success == true then
if jsonTable.data.sys_temp ~= nil then
temp_system = "System: "..jsonTable.data.sys_temp.."°C";
else
temp_system = "System: N/A";
end
fibaro:debug("Temperature "..temp_system);
fibaro:call(selfID, "setProperty", "ui.SYSTEMTemp.value", temp_system);
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(errorCode)]..'</span>');
end
-- Prepare string
local temp_disks = "";
-- Temp info from the disk station
payload = "/webapi"..cgiUsed_temp.."?api="..apiUsed_temp_disk.."&version=1&method="..apiMethod_temp.."&type=storage&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
jsonTable = json.decode(response);
if jsonTable.success == true then
local disk_tab = jsonTable.data.hdd_info;
for nb = 1, #disk_tab do
temp_disks = temp_disks..disk_tab[nb].diskno..": "..disk_tab[nb].temp.."°C ";
end
fibaro:debug("Disks temperature: "..temp_disks);
fibaro:call(selfID, "setProperty", "ui.DISKSTemp.value", temp_disks);
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(errorCode)]..'</span>');
end
-- Prepare string
local vols_load = "";
-- Temp info from the disk station
payload = "/webapi"..cgiUsed_vols.."?api="..apiUsed_vols.."&version=1&method="..apiMethod_vols.."&type=storage&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
if tonumber(status) == 200 then
jsonTable = json.decode(response);
if jsonTable.success == true then
local vols_tab = jsonTable.data.vol_info;
for nb = #vols_tab, 1, -1 do
used_size_vol = vols_tab[nb].used_size;
total_size_vol = vols_tab[nb].total_size;
occupied_size = round(used_size_vol / total_size_vol * 100);
vols_load = vols_load..vols_tab[nb].name..": "..occupied_size.."% ";
end
fibaro:debug("Volumes Load: "..vols_load);
fibaro:call(selfID, "setProperty", "ui.VOLSLoad.value", vols_load);
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(errorCode)]..'</span>');
end
-- Destroy current login session
payload = "/webapi/"..pathAuth.."?api=SYNO.API.Auth&method=Logout&version=2&session=DSM&_sid="..SID;
response, status, errorCode = Synology:GET(payload);
fibaro:debug("Déconnexion: "..status);
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : '..API_AUTH_ERROR_CODE[tonumber(jsonTable.error.code)]..'</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Authentification API ratée</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Version API Synology trop vieille : <b>DSM 4.0-2251</b> est requise</span>');
end
else
fibaro:log("Erreur");
fibaro:debug('<span style="color:red;">Erreur : Impossible de se connecter au Disk Station Synology</span>');
end
else
fibaro:call(selfID, "setProperty", "ui.CPULoad.value", "Memory: N/A % CPU: N/A %");
fibaro:call(selfID, "setProperty", "ui.SYSTEMTemp.value", "System: N/A °C");
fibaro:call(selfID, "setProperty", "ui.DISKSTemp.value", "Disk: N/A °C");
end
Réveiller :
-- convert MAC adress, every 2 Chars (7-bit ASCII), to one Byte Char (8-bits)
function convertMacAddress(address)
local s = string.gsub(address, ":", "");
local x = ""; -- will contain converted MAC
for i=1, 12, 2 do
x = x .. string.char(tonumber(string.sub(s, i, i+1), 16));
end
return x;
end
fibaro:log("Réveiller");
-- MAC adress
local _macAddress = convertMacAddress("AA:BB:CC:DD:EE:FF");
-- Create Magic Packet 6 x FF
local _magicPacket = string.char(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
-- Broadcast Address
local _broadcastAddress = "192.168.X.255";
-- Default port used
local _wakeOnLanPort = 9;
fibaro:sleep(750);
for i = 1, 16 do
_magicPacket = _magicPacket .. _macAddress;
end
fibaro:log("Magic packet créé");
fibaro:sleep(1000);
socket = Net.FUdpSocket();
socket:setBroadcast(true);
local bytes, errorCode = socket:write(_magicPacket, _broadcastAddress, _wakeOnLanPort);
--check for error
if errorCode == 0 then
fibaro:log("Envoyé");
else
fibaro:log("Envoi raté");
end
-- clean up memory
socket = nil;
fibaro:log("Attendre le réveil.");
Et le lien vers la page qui m'a servie à faire cela :
api documentation for homebridge-synology (v0.2.2)
Voilà ...