Created
February 7, 2018 20:28
-
-
Save thomxc/d2ec9850e442a42dbf799cf82101c1a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() | |
local sonos_name = 'Draagbaar' | |
local sonos=assert(io.popen('curl http://localhost:5005/'..sonos_name..'/state')) | |
local status = sonos:read('*all') | |
sonos:close() | |
local jsonStatus = json:decode(status) | |
local tv_woonkamer_status = 'TV Woonkamer System Alive' | |
--print("Eerste JSON stappen met LUA") | |
--print(status) | |
print(jsonStatus) | |
playbackState = jsonStatus['playbackState'] | |
artist = jsonStatus['currentTrack']['artist'] | |
title = jsonStatus['currentTrack']['title'] | |
album = jsonStatus['currentTrack']['album'] | |
type = jsonStatus['currentTrack']['type'] | |
--streaminfo = jsonStatus['currentTrack']['streamInfo'] | |
commandArray = {} | |
if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK" or playbackState == "PLAYING") then | |
if (playbackState == "PLAYING") then | |
if type == "radio" then | |
print(sonos_name .. ' Sonos status: ' ..playbackState.. ' ' ..type.. ' Titel: '..title) | |
else | |
if artist ~= nil then | |
print(sonos_name .. ' Sonos status: ' ..playbackState.. ' Artiest: ' ..artist.. ' Titel: '..title) | |
else | |
print(sonos_name .. ' Sonos status: ' ..playbackState.. ' Titel: '..title) | |
end | |
end | |
if (uservariables["SonosStatus"] == "PAUSED") then | |
commandArray['Variable:SonosStatus']='PLAYING' | |
print("Uservar SonosWoonkamer updated to PLAYING") | |
end | |
end | |
if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK") then | |
print(sonos_name .. ' Sonos Status: ' ..playbackState) | |
if (uservariables["SonosStatus"] == "PLAYING") then | |
commandArray['Variable:SonosStatus']='PAUSED' | |
print("Uservar SonosSatus updated to PAUSED") | |
end | |
end | |
else | |
--print("2") | |
print('<font color="red">'.. sonos_name ..': SONOS-HTTP-API is off-line</font>') | |
end | |
--check script if pauze -> should be pauze | |
if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK") then | |
switch_status_sonos = otherdevices[sonos_name] | |
print(sonos_name .. ' Switch Status: ' ..switch_status_sonos) | |
if otherdevices[sonos_name] == 'Pauze' then | |
print(sonos_name .. ' Switch Status is correct, should be PAUZE: ' ..switch_status_sonos) | |
else | |
print(sonos_name .. ' Switch Status not correct, should be PAUZE: ' ..switch_status_sonos) | |
commandArray['UpdateDevice'] = 'idx|20|20' | |
end | |
end | |
--check script if play -> should be play | |
if (playbackState == "PLAYING") then | |
switch_status_sonos = otherdevices[sonos_name] | |
print(sonos_name .. ' Switch Status: ' ..switch_status_sonos) | |
if otherdevices[sonos_name] == 'Play' then | |
print(sonos_name .. ' Switch Status is correct, should be PLAY: ' ..switch_status_sonos) | |
else | |
print(sonos_name .. ' Switch Status not correct, should be PLAY: ' ..switch_status_sonos) | |
commandArray['UpdateDevice'] = 'idx|10|10' | |
end | |
end | |
--extra optional if tv (if applicable) is on -> stop sonos | |
if devicechanged[tv_woonkamer_status] == 'On' then | |
print("TV Woonkamer is net aangezet, status Sonos onderzoeken") | |
if (playerstate == "PLAYING" or playbackState == "PLAYING") then | |
print("Sonos heeft status PLAYING, Sonos wordt uitgeschakeld") | |
commandArray[sonos_name]='Set Level 20' | |
end | |
end | |
return commandArray |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment