Some more assistant progress, SYNC is now working but not yet returning devices
This commit is contained in:
parent
1b83d4180b
commit
c6f90c42af
5 changed files with 93 additions and 28 deletions
|
|
@ -26,7 +26,6 @@ package se.hal.plugin.assistant.google;
|
||||||
|
|
||||||
import se.hal.HalContext;
|
import se.hal.HalContext;
|
||||||
import se.hal.intf.HalDaemon;
|
import se.hal.intf.HalDaemon;
|
||||||
import se.hal.plugin.assistant.google.endpoint.SmartHomePage;
|
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.net.http.HttpServer;
|
import zutil.net.http.HttpServer;
|
||||||
import zutil.net.http.page.oauth.OAuth2AuthorizationPage;
|
import zutil.net.http.page.oauth.OAuth2AuthorizationPage;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package se.hal.plugin.assistant.google;
|
package se.hal.plugin.assistant.google;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
@ -23,13 +24,19 @@ import java.util.logging.Logger;
|
||||||
import com.google.actions.api.smarthome.*;
|
import com.google.actions.api.smarthome.*;
|
||||||
import com.google.auth.oauth2.AccessToken;
|
import com.google.auth.oauth2.AccessToken;
|
||||||
import com.google.auth.oauth2.GoogleCredentials;
|
import com.google.auth.oauth2.GoogleCredentials;
|
||||||
|
import com.google.home.graph.v1.DeviceProto;
|
||||||
|
import se.hal.HalContext;
|
||||||
|
import se.hal.plugin.assistant.google.data.SmartHomeDeviceType;
|
||||||
|
import se.hal.struct.Sensor;
|
||||||
|
import zutil.db.DBConnection;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.net.http.page.oauth.OAuth2Registry.TokenRegistrationListener;
|
import zutil.net.http.page.oauth.OAuth2Registry.TokenRegistrationListener;
|
||||||
|
import se.hal.plugin.assistant.google.data.SmartHomeDeviceTrait;
|
||||||
|
import se.hal.plugin.assistant.google.data.SmartHomeDeviceType;
|
||||||
|
|
||||||
public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationListener {
|
public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationListener {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
private static final String AGENT_USER_ID = "Hal-" + (int)(Math.random()*10000);
|
||||||
|
|
||||||
public SmartHomeImpl() { }
|
public SmartHomeImpl() { }
|
||||||
|
|
||||||
|
|
@ -57,30 +64,39 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList
|
||||||
res.setRequestId(syncRequest.requestId);
|
res.setRequestId(syncRequest.requestId);
|
||||||
res.setPayload(new SyncResponse.Payload());
|
res.setPayload(new SyncResponse.Payload());
|
||||||
|
|
||||||
int numOfDevices = 0;
|
List<Sensor> sensors = Collections.EMPTY_LIST;
|
||||||
res.payload.devices = new SyncResponse.Payload.Device[numOfDevices];
|
|
||||||
/* for (int i = 0; i < numOfDevices; i++) {
|
try {
|
||||||
|
DBConnection db = HalContext.getDB();
|
||||||
|
sensors = Sensor.getLocalSensors(db);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.log(Level.WARNING, "Unable to retrieve sensor list.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.payload.agentUserId = AGENT_USER_ID;
|
||||||
|
res.payload.devices = new SyncResponse.Payload.Device[sensors.size()];
|
||||||
|
for (int i = 0; i < res.payload.devices.length; i++) {
|
||||||
|
Sensor sensor = sensors.get(i);
|
||||||
|
|
||||||
SyncResponse.Payload.Device.Builder deviceBuilder =
|
SyncResponse.Payload.Device.Builder deviceBuilder =
|
||||||
new SyncResponse.Payload.Device.Builder()
|
new SyncResponse.Payload.Device.Builder()
|
||||||
.setId(device.getId())
|
.setId("Sensor-" + sensor.getId())
|
||||||
.setType((String) device.get("type"))
|
.setType(SmartHomeDeviceType.getType(sensor).toString())
|
||||||
.setTraits((List<String>) device.get("traits"))
|
.setTraits(SmartHomeDeviceTrait.getTraitIds(sensor))
|
||||||
.setName(
|
.setName(
|
||||||
DeviceProto.DeviceNames.newBuilder()
|
DeviceProto.DeviceNames.newBuilder()
|
||||||
.addAllDefaultNames((List<String>) device.get("defaultNames"))
|
.setName(sensor.getName())
|
||||||
.setName((String) device.get("name"))
|
|
||||||
.addAllNicknames((List<String>) device.get("nicknames"))
|
|
||||||
.build())
|
.build())
|
||||||
.setWillReportState((Boolean) device.get("willReportState"))
|
.setWillReportState(true)
|
||||||
.setRoomHint((String) device.get("roomHint"))
|
//.setRoomHint(sensor.getRoom().getName())
|
||||||
.setDeviceInfo(
|
.setDeviceInfo(
|
||||||
DeviceProto.DeviceInfo.newBuilder()
|
DeviceProto.DeviceInfo.newBuilder()
|
||||||
.setManufacturer((String) device.get("manufacturer"))
|
//.setManufacturer((String) device.get("manufacturer"))
|
||||||
.setModel((String) device.get("model"))
|
//.setModel((String) device.get("model"))
|
||||||
.setHwVersion((String) device.get("hwVersion"))
|
//.setHwVersion((String) device.get("hwVersion"))
|
||||||
.setSwVersion((String) device.get("swVersion"))
|
//.setSwVersion((String) device.get("swVersion"))
|
||||||
.build());
|
.build());
|
||||||
if (device.contains("attributes")) {
|
/*if (device.contains("attributes")) {
|
||||||
Map<String, Object> attributes = new HashMap<>();
|
Map<String, Object> attributes = new HashMap<>();
|
||||||
attributes.putAll((Map<String, Object>) device.get("attributes"));
|
attributes.putAll((Map<String, Object>) device.get("attributes"));
|
||||||
String attributesJson = new Gson().toJson(attributes);
|
String attributesJson = new Gson().toJson(attributes);
|
||||||
|
|
@ -91,20 +107,17 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList
|
||||||
logger.error("FAILED TO BUILD");
|
logger.error("FAILED TO BUILD");
|
||||||
}
|
}
|
||||||
deviceBuilder.setAttributes(attributeBuilder.build());
|
deviceBuilder.setAttributes(attributeBuilder.build());
|
||||||
}
|
}*/
|
||||||
if (device.contains("customData")) {
|
/*if (device.contains("customData")) {
|
||||||
Map<String, Object> customData = new HashMap<>();
|
Map<String, Object> customData = new HashMap<>();
|
||||||
customData.putAll((Map<String, Object>) device.get("customData"));
|
customData.putAll((Map<String, Object>) device.get("customData"));
|
||||||
|
|
||||||
String customDataJson = new Gson().toJson(customData);
|
String customDataJson = new Gson().toJson(customData);
|
||||||
deviceBuilder.setCustomData(customDataJson);
|
deviceBuilder.setCustomData(customDataJson);
|
||||||
}
|
}*/
|
||||||
if (device.contains("otherDeviceIds")) {
|
|
||||||
deviceBuilder.setOtherDeviceIds((List) device.get("otherDeviceIds"));
|
|
||||||
}
|
|
||||||
res.payload.devices[i] = deviceBuilder.build();
|
res.payload.devices[i] = deviceBuilder.build();
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.hal.plugin.assistant.google.endpoint;
|
package se.hal.plugin.assistant.google;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
@ -48,6 +48,11 @@
|
||||||
|
|
||||||
package se.hal.plugin.assistant.google.data;
|
package se.hal.plugin.assistant.google.data;
|
||||||
|
|
||||||
|
import se.hal.struct.Sensor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum for https://developers.google.com/assistant/smarthome/traits
|
* Enum for https://developers.google.com/assistant/smarthome/traits
|
||||||
*/
|
*/
|
||||||
|
|
@ -102,4 +107,32 @@ public enum SmartHomeDeviceTrait {
|
||||||
public String getString() {
|
public String getString() {
|
||||||
return typeId;
|
return typeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static SmartHomeDeviceTrait[] getTraits(Sensor sensor) {
|
||||||
|
switch (sensor.getDeviceData().getClass().getName()) {
|
||||||
|
case "se.hal.struct.devicedata.DimmerEventData":
|
||||||
|
case "se.hal.struct.devicedata.SwitchEventData":
|
||||||
|
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};
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unregistered Sensor device data: " + sensor.getDeviceData());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getTraitIds(Sensor sensor) {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
|
||||||
|
for (SmartHomeDeviceTrait trait : getTraits(sensor)) {
|
||||||
|
list.add(trait.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,8 @@
|
||||||
|
|
||||||
package se.hal.plugin.assistant.google.data;
|
package se.hal.plugin.assistant.google.data;
|
||||||
|
|
||||||
|
import se.hal.struct.Sensor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum for https://developers.google.com/assistant/smarthome/types
|
* Enum for https://developers.google.com/assistant/smarthome/types
|
||||||
*/
|
*/
|
||||||
|
|
@ -71,7 +73,7 @@ public enum SmartHomeDeviceType {
|
||||||
CURTAIN("action.devices.types.CURTAIN"),
|
CURTAIN("action.devices.types.CURTAIN"),
|
||||||
DEHUMIDIFIER("action.devices.types.DEHUMIDIFIER"),
|
DEHUMIDIFIER("action.devices.types.DEHUMIDIFIER"),
|
||||||
DEHYDRATOR("action.devices.types.DEHYDRATOR"),
|
DEHYDRATOR("action.devices.types.DEHYDRATOR"),
|
||||||
DISHWASHERv("action.devices.types.DISHWASHER"),
|
DISHWASHER("action.devices.types.DISHWASHER"),
|
||||||
DOOR("action.devices.types.DOOR"),
|
DOOR("action.devices.types.DOOR"),
|
||||||
DRAWER("action.devices.types.DRAWER"),
|
DRAWER("action.devices.types.DRAWER"),
|
||||||
DRYER("action.devices.types.DRYER"),
|
DRYER("action.devices.types.DRYER"),
|
||||||
|
|
@ -142,4 +144,22 @@ public enum SmartHomeDeviceType {
|
||||||
public String getString() {
|
public String getString() {
|
||||||
return typeId;
|
return typeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static SmartHomeDeviceType getType(Sensor sensor) {
|
||||||
|
switch (sensor.getDeviceData().getClass().getName()) {
|
||||||
|
case "se.hal.struct.devicedata.DimmerEventData":
|
||||||
|
case "se.hal.struct.devicedata.SwitchEventData":
|
||||||
|
return LIGHT;
|
||||||
|
|
||||||
|
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 SENSOR;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unregistered Sensor device data: " + sensor.getDeviceData());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue