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 59028eab..8df37963 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 @@ -55,7 +55,11 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList } } - + /** + * https://developers.google.com/assistant/smarthome/reference/intent/sync + * + * TODO: https://developers.google.com/assistant/smarthome/traits/temperaturesetting + */ @Override public SyncResponse onSync(SyncRequest syncRequest, Map headers) { logger.fine("Received sync request."); @@ -121,30 +125,62 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList return res; } + /** + * https://developers.google.com/assistant/smarthome/reference/intent/query + */ @Override public QueryResponse onQuery(QueryRequest queryRequest, Map headers) { logger.fine("Received query request."); - QueryRequest.Inputs.Payload.Device[] devices = ((QueryRequest.Inputs) queryRequest.getInputs()[0]).payload.devices; + DBConnection db = HalContext.getDB(); + QueryResponse res = new QueryResponse(); res.setRequestId(queryRequest.requestId); res.setPayload(new QueryResponse.Payload()); -/* Map> deviceStates = new HashMap<>(); - for (QueryRequest.Inputs.Payload.Device device : devices) { - try { - Map deviceState = database.getState(userId, device.id); - deviceState.put("status", "SUCCESS"); - deviceStates.put(device.id, deviceState); - } catch (Exception e) { - logger.error("QUERY FAILED: {}", e); - Map failedDevice = new HashMap<>(); - failedDevice.put("status", "ERROR"); - failedDevice.put("errorCode", "deviceOffline"); - deviceStates.put(device.id, failedDevice); + + for (SmartHomeRequest.RequestInputs input : queryRequest.getInputs()) { + if (!"action.devices.QUERY".equals(input.intent)) + continue; + + for (QueryRequest.Inputs.Payload.Device device : ((QueryRequest.Inputs) input).payload.devices) { + try { + if (!device.getId().startsWith("Sensor-")) + throw new IllegalArgumentException("Invalid device ID supplied: " + device.getId()); + + long sensorId = Long.parseLong(device.getId().substring(7)); // Get the number in the id "Sensor-" + Sensor sensor = Sensor.getSensor(db, sensorId); + + Map deviceState = new HashMap<>(); + + switch (sensor.getDeviceData().getClass().getName()) { + case "se.hal.struct.devicedata.HumiditySensorData": + deviceState.put("humidity", sensor.getDeviceData().getData()); + break; + case "se.hal.struct.devicedata.LightSensorData": + deviceState.put("light", sensor.getDeviceData().getData()); + break; + case "se.hal.struct.devicedata.PowerConsumptionSensorData": + deviceState.put("power", sensor.getDeviceData().getData()); + break; + case "se.hal.struct.devicedata.TemperatureSensorData": + deviceState.put("temperature", sensor.getDeviceData().getData()); + break; + } + + deviceState.put("status", "SUCCESS"); + deviceStates.put(device.id, deviceState); + } catch (Exception e) { + logger.log(Level.SEVERE, "Query request failed for sensor: " + device.getId(), e); + Map failedDevice = new HashMap<>(); + failedDevice.put("status", "ERROR"); + failedDevice.put("errorCode", "deviceOffline"); + deviceStates.put(device.id, failedDevice); + } } } - res.payload.setDevices(deviceStates);*/ + + res.payload.setDevices(deviceStates); return res; } diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomePage.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomePage.java index 2510fe8c..b2e1c6bb 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomePage.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomePage.java @@ -66,15 +66,15 @@ public class SmartHomePage implements HttpPage { contentLength = Integer.parseInt(headers.getHeader(HttpHeader.HEADER_CONTENT_LENGTH)); String body = IOUtil.readContentAsString(headers.getInputStream(), contentLength); - logger.fine("doPost, body = " + body); + logger.finest("Request body = " + body); try { String response = smartHome.handleRequest(body, request).get(); + logger.finest("Response body = " + response); out.setHeader("Content-Type", "application/json"); out.setHeader("Access-Control-Allow-Origin", "*"); out.setHeader("Pragma", "no-cache"); - logger.fine("doPost, response body = " + response); out.println(response); } catch (Exception e) { logger.log(Level.SEVERE, "Was unable to handle SmartHome request.", e); diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceTrait.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceTrait.java index 9cdf8781..5652687a 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceTrait.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceTrait.java @@ -104,7 +104,7 @@ public enum SmartHomeDeviceTrait { } - public String getString() { + public String toString() { return typeId; } @@ -116,11 +116,13 @@ public enum SmartHomeDeviceTrait { return new SmartHomeDeviceTrait[]{OnOff}; case "se.hal.struct.devicedata.PowerConsumptionSensorData": - case "se.hal.struct.devicedata.TemperatureSensorData": case "se.hal.struct.devicedata.HumiditySensorData": case "se.hal.struct.devicedata.LightSensorData": return new SmartHomeDeviceTrait[]{SensorState}; + case "se.hal.struct.devicedata.TemperatureSensorData": + return new SmartHomeDeviceTrait[]{TemperatureSetting}; + default: throw new IllegalArgumentException("Unregistered Sensor device data: " + sensor.getDeviceData()); } diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceType.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceType.java index 01b656c4..0c7622cc 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceType.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/data/SmartHomeDeviceType.java @@ -141,7 +141,7 @@ public enum SmartHomeDeviceType { } - public String getString() { + public String toString() { return typeId; }