wtorek, 19 czerwca 2018

Coś dla fanów piłki nożnej - skrypt dzVents

Dzisiaj coś dla fanów piłki nożnej. Ja osobiście nim nie jestem, ale nie w tym rzecz. Poniższy skrypt (w dwóch wersjach, dla wersji dzVents z ostatniej wersji stabilnej Domoticz - czyli 2.2 - oraz dla najnowszej w wersji Beta) jest wspaniałym przykładem tego, co można zrobić z danymi pobieranymi ze stron www. Pobrać, przemielić, wyświetlić.

Nie jestem autorem kodu, autorem jest elmortero, kod można znaleźć na forum Domoticz.

Dla przypomnienia, kod wstawiamy tutaj:




local scorefile = '/tmp/score.json'
local fetchIntervalMins = 3     -- interval to check the info (don't set to lower than 3)
return {
active = true,
on = {
timer = { 'every minute' },
},
execute = function(domoticz, device)
local callUrl = false
if (os.date('*t').min % fetchIntervalMins) == 0 then
callUrl = true
elseif ((os.date('*t').min -1) % fetchIntervalMins) ~= 0 then
return
end
local board = domoticz.devices('Scoreboard')
local prevboard = tostring(board.text)
local linker = tostring('http://worldcup.sfg.io/matches/today')
if callUrl then
os.execute('curl -s "'..linker..'" > '..scorefile..'&')
print('json written to file')
return -- Nothing more to do for now, we'll be back in a minute to read the data!
end
local function readLuaFromJsonFile(fileName)
local file = io.open(fileName, 'r')
if file then
package.path = './scripts/lua/?.lua;'..package.path
local jsonParser = require('JSON')
local _json = file:read('*a')
local json = jsonParser:decode(_json)
io.close(file)
return json
end
return nil
end
local scoreData = readLuaFromJsonFile(scorefile)

if not scoreData then
print('** Scoreboard// Could not read scoreData from file: '.. scorefile)
return
end
--
tl = #scoreData
if tl > 0  then
tc = 1
repeat
local status = tostring(scoreData[tc].status)
local date = tostring(scoreData[tc].datetime)
local home = tostring(scoreData[tc].home_team.country)
local away = tostring(scoreData[tc].away_team.country)
local hgoal = tostring(scoreData[tc].home_team.goals)
local agoal = tostring(scoreData[tc].away_team.goals)

local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)"
local year, month, day, hour, minute, seconds = date:match(pattern)
hour = hour + 2

show = (home..' '..hgoal..' - '..agoal..' '..away)

if status == 'in progress' then tc = tl + 1 end
if status == 'completed' then
local score = (hgoal..' - '..agoal)
local winner = tostring(scoreData[tc].winner)
if winner == 'Draw' then 
   show = ('Draw '..home..' '..hgoal..' - '..agoal..' '..away)
   else 
   show = ('Match won by '..winner..' with '..score)
end
tc = tc + 1
end
if status == 'future' then 
    show = (show..' KO '..hour..':'..minute)
    tc = tl + 1 
end
until tc > tl
if show ~= prevboard then
    print ('result '..show)
board.updateText(show)
end

    end
end

}

Poniżej wersja dla dzVents w wersji przynajmniej 2.4.

return {
on = {
timer = { 'every 2 minutes' },
httpResponses = { 'scoreboard2' } -- matches callback string below
},

execute = function(domoticz, triggerItem)
    local board = domoticz.devices('Scoreboard2')    --a virtual text sensor
    local prevboard = tostring(board.text)          --get current value of text sensor

if (triggerItem.isTimer) then
domoticz.openURL({
url = 'http://worldcup.sfg.io/matches/today',
method = 'GET',
callback = 'scoreboard2'
})

elseif (triggerItem.isHTTPResponse) then

local response = triggerItem
if (response.ok and response.isJSON) then
tl = #response.json
tc = 1
repeat
local status = tostring(response.json[tc].status)
local home = tostring(response.json[tc].home_team.country)
local away = tostring(response.json[tc].away_team.country)
local hgoal = tostring(response.json[tc].home_team.goals)
local agoal = tostring(response.json[tc].away_team.goals)
if away == 'Spain' or away == 'Belgium' or home == 'Spain' or home == 'Belgium' then
sender = true
end
show = (home..' '..hgoal..' - '..agoal..' '..away)

if status == 'in progress' then tc = tl + 1 end
if status == 'completed' then
local score = (hgoal..' - '..agoal)
local winner = tostring(response.json[tc].winner)
show = ('Match won by '..winner..' with '..score)
tc = tc + 1
end
if status == 'future' then tc = tl + 1 end
until tc > tl
if show ~= prevboard then
    print ('result '..show)
board.updateText(show)
if sender then
--put your notification method here (mine is telegram)
end
end
else
print('**scoreboard2 failed to fetch info')
end
end
end

}

Dodajemy Virtual switch typu text:


I w sumie gotowe :)


Trzymamy kciuki aby wygrali!

1 komentarz: