From c6f90c42af382ada94eab00009ec6ff24227ed3f Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sat, 5 Dec 2020 00:07:41 +0100 Subject: [PATCH] Some more assistant progress, SYNC is now working but not yet returning devices --- .../assistant/google/SmartHomeDaemon.java | 1 - .../assistant/google/SmartHomeImpl.java | 63 +++++++++++-------- .../google/{endpoint => }/SmartHomePage.java | 2 +- .../google/data/SmartHomeDeviceTrait.java | 33 ++++++++++ .../google/data/SmartHomeDeviceType.java | 22 ++++++- 5 files changed, 93 insertions(+), 28 deletions(-) rename plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/{endpoint => }/SmartHomePage.java (98%) 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 d0d76733..8e9dcfdc 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 @@ -26,7 +26,6 @@ package se.hal.plugin.assistant.google; import se.hal.HalContext; import se.hal.intf.HalDaemon; -import se.hal.plugin.assistant.google.endpoint.SmartHomePage; import zutil.log.LogUtil; import zutil.net.http.HttpServer; import zutil.net.http.page.oauth.OAuth2AuthorizationPage; 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 ecd205ef..59028eab 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 @@ -16,6 +16,7 @@ package se.hal.plugin.assistant.google; +import java.sql.SQLException; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -23,13 +24,19 @@ import java.util.logging.Logger; import com.google.actions.api.smarthome.*; import com.google.auth.oauth2.AccessToken; 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.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 { private static final Logger logger = LogUtil.getLogger(); - + private static final String AGENT_USER_ID = "Hal-" + (int)(Math.random()*10000); public SmartHomeImpl() { } @@ -57,30 +64,39 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList res.setRequestId(syncRequest.requestId); res.setPayload(new SyncResponse.Payload()); - int numOfDevices = 0; - res.payload.devices = new SyncResponse.Payload.Device[numOfDevices]; -/* for (int i = 0; i < numOfDevices; i++) { + List sensors = Collections.EMPTY_LIST; + + 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 = new SyncResponse.Payload.Device.Builder() - .setId(device.getId()) - .setType((String) device.get("type")) - .setTraits((List) device.get("traits")) + .setId("Sensor-" + sensor.getId()) + .setType(SmartHomeDeviceType.getType(sensor).toString()) + .setTraits(SmartHomeDeviceTrait.getTraitIds(sensor)) .setName( DeviceProto.DeviceNames.newBuilder() - .addAllDefaultNames((List) device.get("defaultNames")) - .setName((String) device.get("name")) - .addAllNicknames((List) device.get("nicknames")) + .setName(sensor.getName()) .build()) - .setWillReportState((Boolean) device.get("willReportState")) - .setRoomHint((String) device.get("roomHint")) + .setWillReportState(true) + //.setRoomHint(sensor.getRoom().getName()) .setDeviceInfo( DeviceProto.DeviceInfo.newBuilder() - .setManufacturer((String) device.get("manufacturer")) - .setModel((String) device.get("model")) - .setHwVersion((String) device.get("hwVersion")) - .setSwVersion((String) device.get("swVersion")) + //.setManufacturer((String) device.get("manufacturer")) + //.setModel((String) device.get("model")) + //.setHwVersion((String) device.get("hwVersion")) + //.setSwVersion((String) device.get("swVersion")) .build()); - if (device.contains("attributes")) { + /*if (device.contains("attributes")) { Map attributes = new HashMap<>(); attributes.putAll((Map) device.get("attributes")); String attributesJson = new Gson().toJson(attributes); @@ -91,20 +107,17 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList logger.error("FAILED TO BUILD"); } deviceBuilder.setAttributes(attributeBuilder.build()); - } - if (device.contains("customData")) { + }*/ + /*if (device.contains("customData")) { Map customData = new HashMap<>(); customData.putAll((Map) device.get("customData")); String customDataJson = new Gson().toJson(customData); deviceBuilder.setCustomData(customDataJson); - } - if (device.contains("otherDeviceIds")) { - deviceBuilder.setOtherDeviceIds((List) device.get("otherDeviceIds")); - } + }*/ res.payload.devices[i] = deviceBuilder.build(); } -*/ + return res; } 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/SmartHomePage.java similarity index 98% rename from plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/endpoint/SmartHomePage.java rename to plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomePage.java index 466f992c..2510fe8c 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/SmartHomePage.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package se.hal.plugin.assistant.google.endpoint; +package se.hal.plugin.assistant.google; import java.io.IOException; import java.io.InputStream; 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 f9ee6c00..9cdf8781 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 @@ -48,6 +48,11 @@ 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 */ @@ -102,4 +107,32 @@ public enum SmartHomeDeviceTrait { public String getString() { 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 getTraitIds(Sensor sensor) { + List list = new ArrayList<>(); + + for (SmartHomeDeviceTrait trait : getTraits(sensor)) { + list.add(trait.toString()); + } + + return list; + } } 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 430060f1..01b656c4 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 @@ -48,6 +48,8 @@ package se.hal.plugin.assistant.google.data; +import se.hal.struct.Sensor; + /** * Enum for https://developers.google.com/assistant/smarthome/types */ @@ -71,7 +73,7 @@ public enum SmartHomeDeviceType { CURTAIN("action.devices.types.CURTAIN"), DEHUMIDIFIER("action.devices.types.DEHUMIDIFIER"), DEHYDRATOR("action.devices.types.DEHYDRATOR"), - DISHWASHERv("action.devices.types.DISHWASHER"), + DISHWASHER("action.devices.types.DISHWASHER"), DOOR("action.devices.types.DOOR"), DRAWER("action.devices.types.DRAWER"), DRYER("action.devices.types.DRYER"), @@ -142,4 +144,22 @@ public enum SmartHomeDeviceType { public String getString() { 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()); + } + } }