Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- %% properties
- %% autostart
- %% globals
- --]]
- -- DIESE DATEN ANPASSEN
- local client_id = 'xxx'
- local client_secret = 'xxx'
- local username = 'xxx'
- local password = 'xxx'
- local refresh = 300
- local debug = 1
- local debug_developer = true
- local vd_ID = 178
- -- AB HIER NICHTS MEHR ANPASSEN
- local token = ''
- local request_body = ''
- local rains = {hour = -1000, day = -1000, week = -1000, month = -1000}
- local sourceTrigger = fibaro:getSourceTrigger()
- Debug = function ( color, message )
- fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span"))
- end
- DebugError = function ( color, message )
- fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span"))
- end
- Debug( 'orange', 'netatmo Zusatzmodul Integration v.1.3b gestartet.' )
- Debug( 'white', 'Daten werden alle ' ..(refresh/60).. ' min aktualisiert. Bei debug = 0 wird nichts debuggt' )
- function oAuth()
- local request_body = 'grant_type=password&client_id=' .. client_id .. '&client_secret=' .. client_secret .. '&username=' .. username .. '&password=' .. password .. '&scope=read_station'
- getResponseData('https://api.netatmo.net/oauth2/token', request_body,
- function(data)
- if (data.access_token ~= nil) then
- token = data.access_token
- if (debug == 1) then
- Debug( 'green', 'oAuth 2.0 durchgeführt.' )
- end
- getDevices()
- fibaro:call(vd_ID, 'pressButton', '9')
- else
- DebugError( 'red', 'oAuth 2.0 konnte nicht durchgeführt werden! Bitte die Anmeldedaten überprüfen')
- end
- end
- )
- setTimeout(oAuth, refresh*1000);
- end
- function getResponseData(url, body, func)
- local http = net.HTTPClient()
- http:request(url, {
- options = {
- method = 'POST',
- headers = {
- ['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
- },
- data = body
- },
- success = function(response)
- if (debug_developer) then
- Debug( 'blue', '_________DEV-INFO_____________')
- fibaro:debug('ERKANNTER DATENTYP: ' ..type(response.data))
- fibaro:debug(response.data)
- Debug( 'blue', '_______DEV-INFO-END___________')
- end
- func(json.decode(response.data))
- end
- })
- end
- function getDevices()
- getResponseData('https://api.netatmo.net/api/devicelist','access_token='..token,
- function(data)
- if (data.body ~= nil) then
- if (debug == 1) then
- Debug( 'green', 'netatmo Sensoren werden gesucht...' );
- end
- for _, v in pairs(data.body.modules) do
- --fibaro:debug('Tabelle auslesen')
- if (v.data_type[1] == 'Rain') then
- rain_id = v._id
- if (debug == 1) then
- Debug( 'green', 'Regensensor ' ..rain_id.. ' erkannt.' );
- end
- if rain_id ~= nil then
- getSumRain(60 * 60, 'hour')
- getSumRain(60 * 60 * 24, 'day')
- getSumRain(60 * 60 * 24 * 7, 'week')
- getSumRain(60 * 60 * 24 * 30, 'month')
- end
- elseif (v.data_type[1] == 'Wind') then
- wind_id = v._id
- if (debug == 1) then
- Debug( 'green', 'Windmesser ' ..wind_id.. ' erkannt.' );
- end
- if wind_id ~= nil then
- getmeasureWind()
- end
- end
- int_id = data.body.devices[1]._id
- end
- else
- DebugError( 'red', 'device-list konnte nicht abgefragt werden! Bitte nächsten Durchlauf abwarten')
- end
- end
- )
- end
- function getmeasureWind()
- request_body_wind = 'access_token='..token..'&device_id='..int_id..'&module_id='..wind_id..'&scale=max&type=WindStrength,WindAngle,GustStrength,GustAngle&date_end=last'
- getResponseData('https://api.netatmo.net/api/getmeasure', request_body_wind,
- function(getData)
- if (getData.body ~= nil) then
- if (debug == 1) then
- Debug( 'green', 'Windmesser wird ausgelesen...' );
- end
- WindStrength = getData.body[1].value[1][1]
- WindAngle = getData.body[1].value[1][2]
- GustStrength = getData.body[1].value[1][3]
- GustAngle= getData.body[1].value[1][4]
- if (debug == 1) then
- Debug( 'green', 'Windgeschwindigkeit: ' .. WindStrength .. ' km/h' );
- end
- if fibaro:getGlobalValue('windstaerke') ~= nil then
- fibaro:setGlobal('windstaerke', WindStrength)
- else
- DebugError( 'red', 'Varible windstaerke nicht gefunden. Bitte anlegen')
- end
- if fibaro:getGlobalValue('windrichtung') ~= nil then
- fibaro:setGlobal('windrichtung', WindAngle)
- else
- DebugError( 'red', 'Varible windrichtung nicht gefunden. Bitte anlegen')
- end
- if fibaro:getGlobalValue('boenstaerke') ~= nil then
- fibaro:setGlobal('boenstaerke', GustStrength)
- else
- DebugError( 'red', 'Varible boenstaerke nicht gefunden. Bitte anlegen')
- end
- if fibaro:getGlobalValue('boenrichtung') ~= nil then
- fibaro:setGlobal('boenrichtung', GustAngle)
- else
- DebugError( 'red', 'Varible boenrichtung nicht gefunden. Bitte anlegen')
- end
- if (debug == 1) then
- Debug( 'green', 'Windmesser auslesen beendet.' );
- end
- else
- DebugError( 'red', 'API-Call konnte nicht durchgeführt werden! API nicht erreichbar! Bitte nächsten Durchlauf abwarten.')
- end
- end
- )
- end
- function getSumRain(dauer, variable)
- local now = os.time();
- getResponseData('https://api.netatmo.net/api/getmeasure','access_token='..token..'&device_id='..int_id..'&module_id='..rain_id..'&scale=1hour&type=sum_rain&real_time=true&date_begin='..os.date('!%c', (now - dauer)),
- function(data)
- local sum_rain = 0
- for k, v in pairs(data.body) do
- for l, w in pairs(v.value) do
- sum_rain = sum_rain + w[1]
- end
- end
- if fibaro:getGlobalValue('rain_' ..variable) ~= nil then
- fibaro:setGlobal('rain_' ..variable, sum_rain)
- else
- DebugError( 'red', 'Varible rain_' ..variable.. ' nicht gefunden. Bitte anlegen')
- end
- if (debug == 1) then
- fibaro:debug('Regenmenge: ' ..sum_rain.. ' mm2 (' .. variable .. ')')
- end
- end
- )
- end
- if (sourceTrigger['type'] == 'autostart') then
- oAuth();
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement