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
|
@Override
|
||||||
public SyncResponse onSync(SyncRequest syncRequest, Map<?, ?> headers) {
|
public SyncResponse onSync(SyncRequest syncRequest, Map<?, ?> headers) {
|
||||||
logger.fine("Received sync request.");
|
logger.fine("Received sync request.");
|
||||||
|
|
@ -121,30 +125,62 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://developers.google.com/assistant/smarthome/reference/intent/query
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public QueryResponse onQuery(QueryRequest queryRequest, Map<?, ?> headers) {
|
public QueryResponse onQuery(QueryRequest queryRequest, Map<?, ?> headers) {
|
||||||
logger.fine("Received query request.");
|
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();
|
QueryResponse res = new QueryResponse();
|
||||||
res.setRequestId(queryRequest.requestId);
|
res.setRequestId(queryRequest.requestId);
|
||||||
res.setPayload(new QueryResponse.Payload());
|
res.setPayload(new QueryResponse.Payload());
|
||||||
/*
|
|
||||||
Map<String, Map<String, Object>> deviceStates = new HashMap<>();
|
Map<String, Map<String, Object>> deviceStates = new HashMap<>();
|
||||||
for (QueryRequest.Inputs.Payload.Device device : devices) {
|
|
||||||
try {
|
for (SmartHomeRequest.RequestInputs input : queryRequest.getInputs()) {
|
||||||
Map<String, Object> deviceState = database.getState(userId, device.id);
|
if (!"action.devices.QUERY".equals(input.intent))
|
||||||
deviceState.put("status", "SUCCESS");
|
continue;
|
||||||
deviceStates.put(device.id, deviceState);
|
|
||||||
} catch (Exception e) {
|
for (QueryRequest.Inputs.Payload.Device device : ((QueryRequest.Inputs) input).payload.devices) {
|
||||||
logger.error("QUERY FAILED: {}", e);
|
try {
|
||||||
Map<String, Object> failedDevice = new HashMap<>();
|
if (!device.getId().startsWith("Sensor-"))
|
||||||
failedDevice.put("status", "ERROR");
|
throw new IllegalArgumentException("Invalid device ID supplied: " + device.getId());
|
||||||
failedDevice.put("errorCode", "deviceOffline");
|
|
||||||
deviceStates.put(device.id, failedDevice);
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,15 +66,15 @@ public class SmartHomePage implements HttpPage {
|
||||||
contentLength = Integer.parseInt(headers.getHeader(HttpHeader.HEADER_CONTENT_LENGTH));
|
contentLength = Integer.parseInt(headers.getHeader(HttpHeader.HEADER_CONTENT_LENGTH));
|
||||||
|
|
||||||
String body = IOUtil.readContentAsString(headers.getInputStream(), contentLength);
|
String body = IOUtil.readContentAsString(headers.getInputStream(), contentLength);
|
||||||
logger.fine("doPost, body = " + body);
|
logger.finest("Request body = " + body);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String response = smartHome.handleRequest(body, request).get();
|
String response = smartHome.handleRequest(body, request).get();
|
||||||
|
logger.finest("Response body = " + response);
|
||||||
|
|
||||||
out.setHeader("Content-Type", "application/json");
|
out.setHeader("Content-Type", "application/json");
|
||||||
out.setHeader("Access-Control-Allow-Origin", "*");
|
out.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
out.setHeader("Pragma", "no-cache");
|
out.setHeader("Pragma", "no-cache");
|
||||||
logger.fine("doPost, response body = " + response);
|
|
||||||
out.println(response);
|
out.println(response);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.SEVERE, "Was unable to handle SmartHome request.", 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;
|
return typeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,11 +116,13 @@ public enum SmartHomeDeviceTrait {
|
||||||
return new SmartHomeDeviceTrait[]{OnOff};
|
return new SmartHomeDeviceTrait[]{OnOff};
|
||||||
|
|
||||||
case "se.hal.struct.devicedata.PowerConsumptionSensorData":
|
case "se.hal.struct.devicedata.PowerConsumptionSensorData":
|
||||||
case "se.hal.struct.devicedata.TemperatureSensorData":
|
|
||||||
case "se.hal.struct.devicedata.HumiditySensorData":
|
case "se.hal.struct.devicedata.HumiditySensorData":
|
||||||
case "se.hal.struct.devicedata.LightSensorData":
|
case "se.hal.struct.devicedata.LightSensorData":
|
||||||
return new SmartHomeDeviceTrait[]{SensorState};
|
return new SmartHomeDeviceTrait[]{SensorState};
|
||||||
|
|
||||||
|
case "se.hal.struct.devicedata.TemperatureSensorData":
|
||||||
|
return new SmartHomeDeviceTrait[]{TemperatureSetting};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unregistered Sensor device data: " + sensor.getDeviceData());
|
throw new IllegalArgumentException("Unregistered Sensor device data: " + sensor.getDeviceData());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ public enum SmartHomeDeviceType {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getString() {
|
public String toString() {
|
||||||
return typeId;
|
return typeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue