local composer = require( "composer" ) local json = require("json") local widget = require("widget") local AskiRepository = require("askirepository") local scene = composer.newScene() -- Globale Variablen für Datenabruf local yesterdayDate = nil local selectedDate = nil -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- Hilfsfunktion: Hole Datum minus 7 Tage Datum local function getCurrentAndLastDate(isCurrentDate) local currentTime = os.time() --print("Aktuelle Zeit (Timestamp): " .. currentTime) local lastSevenDayTime = currentTime - (24 * 60 * 60 * 7) -- 7 Tage zurück --print("Vor 7 Tagen Zeit (Timestamp): " .. lastSevenDayTime) if isCurrentDate then return os.date("*t", currentTime - (24 * 60 * 60)) -- minus 1 Tag für gestern else return os.date("*t", lastSevenDayTime - (24 * 60 * 60)) -- nochmal minus 1 Tag für vor 7 Tage end end -- Hilfsfunktion: Formatiere Datum für API local function formatDateForAPI(dateTable, isEndOfDay) if isEndOfDay then return string.format("%04d-%02d-%02dT23:59:59.999Z", dateTable.year, dateTable.month, dateTable.day) else return string.format("%04d-%02d-%02dT00:00:00.000Z", dateTable.year, dateTable.month, dateTable.day) end end -- Go to Day scene02 local function gotoDay() composer.gotoScene("scenen.scene01", { effect = "fade", time = 500 }) end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- Hintergrund local bg = display.newRect(sceneGroup, display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight) bg:setFillColor(0.95, 0.95, 0.95) -- aktuelles Tagesdatum -1 Tag für gestern yesterdayDate = getCurrentAndLastDate(true) -- Setze Standard-Datum auf vor 8 Tagen selectedDate = getCurrentAndLastDate(false) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Hole die übergebenen Daten local devices = event.params.devices local currentCookie = event.params.cookie -- Formatiere die Daten für die API local fromDate = formatDateForAPI(selectedDate, false) local toDate = formatDateForAPI(yesterdayDate, true) local completedRequests = 0 local totalRequests = #devices local currentDataSets = {} -- Ausgabe der übergebenen Daten für Debugging print("Scene02 ist jetzt sichtbar - Zeige Daten an") print("Entspricht einer Woche von Gestern 7 Tage zurück") print("Formatiertes Start-Datum für API: " .. fromDate) print("Formatiertes End-Datum für API: " .. toDate) print("Übergebene Geräte: " .. (devices and #devices or "Keine")) print("Cookie:" .. tostring(currentCookie)) -- Funktion zum Neuladen der Daten local function reloadDataFromSevenDays() if not currentCookie or not devices or not selectedDate then print("Fehler: Cookie, Devices oder Datum fehlt") return end print("Lade Daten von " .. string.format("%04d-%02d-%02d", selectedDate.year, selectedDate.month, selectedDate.day) .. " bis " .. string.format("%04d-%02d-%02d", yesterdayDate.year, yesterdayDate.month, yesterdayDate.day) .. " für " .. #devices .. " Geräte") -- Lade-Anzeige local loadingOverlay = display.newRect(sceneGroup, display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight) loadingOverlay:setFillColor(0, 0, 0, 0.5) loadingOverlay._isDataDisplay = true local loadingText = display.newText({ parent = sceneGroup, text = "Lade Daten...", x = display.contentCenterX, y = display.contentCenterY, fontSize = 20, font = native.systemFontBold }) loadingText:setFillColor(1, 1, 1) loadingText._isDataDisplay = true -- Callback für jeden Device local function onDataComplete(deviceName, success, dataSet, response) completedRequests = completedRequests + 1 if success then currentDataSets[deviceName] = dataSet else currentDataSets[deviceName] = { error = tostring(response) } end if completedRequests >= totalRequests then -- Entferne Lade-Anzeige loadingOverlay:removeSelf() loadingText:removeSelf() -- Ausgabe der kompletten newDeviceDataSets Table print("\n========== ALLE GELADENEN DATEN (newDeviceDataSets) ==========") print("JSON-Format:") print(json.prettify(currentDataSets)) print("===============================================================\n") -- Zeige neue Daten print("✓ Daten erfolgreich geladen") end end -- Lade Daten für alle Devices for i, device in ipairs(devices) do local function deviceCallback(success, dataSet, response) onDataComplete(device.name, success, dataSet, response) end AskiRepository.getPostHistoricData( "https://api.portal.aski.at/historical_values/power", device.deviceId, fromDate, toDate, currentCookie, deviceCallback ) end end -- Code here runs when the scene is entirely on screen reloadDataFromSevenDays() -- Lade die Daten direkt beim Anzeigen der Scene02 -- Inhalt Diagramm anzeigen -- Button für zurück zu Scene01 -- Design für Tagesansicht Button local dayBtn = widget.newButton({ label = "Vortagesansicht", emboss = false, shape = "roundedRect", width = 200, height = 40, cornerRadius = 8, fillColor = { default = { 0.3, 0.5, 0.8, 1 }, over = { 0.2, 0.4, 0.7, 1 } }, strokeColor = { default = { 0.2, 0.4, 0.2, 1 }, over = { 0.1, 0.3, 0.1, 1 } }, strokeWidth = 2, labelColor = { default = { 1, 1, 1 }, over = { 1, 1, 1 } }, fontSize = 16, x = display.contentCenterX, y = 380 }) -- Event-Listener für Tagesansicht Button sceneGroup:insert(dayBtn) dayBtn:addEventListener("tap", gotoDay) end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view print("Scene02 zerstört") end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene