diff --git a/logging.properties b/logging.properties index 8a356b01..f5db9d3b 100755 --- a/logging.properties +++ b/logging.properties @@ -12,4 +12,4 @@ se.hal.level = ALL zutil.level = ALL zutil.db.bean.level = INFO -zutil.net.http.page.level = INFO \ No newline at end of file +zutil.net.http.page.HttpFilePage.level = INFO \ No newline at end of file diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java index 170ecee4..d0d76733 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java @@ -33,7 +33,6 @@ import zutil.net.http.page.oauth.OAuth2AuthorizationPage; import zutil.net.http.page.oauth.OAuth2Registry; import zutil.net.http.page.oauth.OAuth2TokenPage; -import java.util.Date; import java.util.concurrent.ScheduledExecutorService; import java.util.logging.Logger; @@ -41,8 +40,9 @@ import java.util.logging.Logger; public class SmartHomeDaemon implements HalDaemon { private static final Logger logger = LogUtil.getLogger(); - public static final String ENDPOINT_AUTH = "api/assistant/google/auth/token"; - public static final String ENDPOINT_TOKEN = "api/assistant/google/auth/authorize"; + public static final String ENDPOINT_AUTH = "api/assistant/google/auth/authorize"; + public static final String ENDPOINT_TOKEN = "api/assistant/google/auth/token"; + public static final String ENDPOINT_SMARTHOME = "api/assistant/google/smarthome"; private static final String PARAM_PORT = "assistant.google.port"; private static final String PARAM_CLIENT_ID = "assistant.google.client_id"; @@ -60,15 +60,16 @@ public class SmartHomeDaemon implements HalDaemon { return; } - smartHome = new SmartHomeImpl("token", new Date(System.currentTimeMillis() + 24*60*60*1000)); + smartHome = new SmartHomeImpl(); oAuth2Registry = new OAuth2Registry(); oAuth2Registry.addWhitelist(HalContext.getStringProperty(PARAM_CLIENT_ID)); + oAuth2Registry.setTokenListener(smartHome); httpServer = new HttpServer(HalContext.getIntegerProperty(PARAM_PORT)); httpServer.setPage(ENDPOINT_AUTH, new OAuth2AuthorizationPage(oAuth2Registry)); httpServer.setPage(ENDPOINT_TOKEN, new OAuth2TokenPage(oAuth2Registry)); - httpServer.setPage(SmartHomePage.ENDPOINT_URL, new SmartHomePage(smartHome)); + httpServer.setPage(ENDPOINT_SMARTHOME, new SmartHomePage(smartHome)); httpServer.start(); } } diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeImpl.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeImpl.java index b1b3a80e..ecd205ef 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeImpl.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeImpl.java @@ -17,26 +17,35 @@ package se.hal.plugin.assistant.google; import java.util.*; +import java.util.logging.Level; import java.util.logging.Logger; import com.google.actions.api.smarthome.*; import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.GoogleCredentials; import zutil.log.LogUtil; +import zutil.net.http.page.oauth.OAuth2Registry.TokenRegistrationListener; -public class SmartHomeImpl extends SmartHomeApp { +public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationListener { private static final Logger logger = LogUtil.getLogger(); - public SmartHomeImpl(String accessToken, Date accessExpirationTime) { -/* try { - GoogleCredentials credentials = GoogleCredentials.create(new AccessToken(accessToken, accessExpirationTime)); + public SmartHomeImpl() { } + + + @Override + public void onTokenRegistration(String clientId, String token, long timeoutMillis) { + try { + GoogleCredentials credentials = GoogleCredentials.create(new AccessToken( + token, + new Date(System.currentTimeMillis() + timeoutMillis) + )); this.setCredentials(credentials); + logger.fine("New OAuth2 token registered."); } catch (Exception e) { - logger.severe("Could not load google credentials"); - throw new RuntimeException(e); - }*/ + logger.log(Level.SEVERE, "Could not load google credentials", e); + } } diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/endpoint/SmartHomePage.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/endpoint/SmartHomePage.java index c7e7d1d3..315f0670 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/endpoint/SmartHomePage.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/endpoint/SmartHomePage.java @@ -61,7 +61,6 @@ import zutil.net.http.HttpPrintStream; */ public class SmartHomePage implements HttpPage { private static final Logger logger = LogUtil.getLogger(); - public static final String ENDPOINT_URL = "api/assistant/google/smarthome"; private SmartHomeImpl smartHome;