Dives now registered in Google Assistant but no data is displayed
This commit is contained in:
parent
7f22b2dee2
commit
f4515f86c8
4 changed files with 58 additions and 20 deletions
|
|
@ -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<String, Map<String, Object>> deviceStates = new HashMap<>();
|
||||
for (QueryRequest.Inputs.Payload.Device device : devices) {
|
||||
try {
|
||||
Map<String, Object> deviceState = database.getState(userId, device.id);
|
||||
deviceState.put("status", "SUCCESS");
|
||||
deviceStates.put(device.id, deviceState);
|
||||
} catch (Exception e) {
|
||||
logger.error("QUERY FAILED: {}", e);
|
||||
Map<String, Object> 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-<number>"
|
||||
Sensor sensor = Sensor.getSensor(db, sensorId);
|
||||
|
||||
Map<String, Object> 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<String, Object> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ public enum SmartHomeDeviceType {
|
|||
}
|
||||
|
||||
|
||||
public String getString() {
|
||||
public String toString() {
|
||||
return typeId;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue