Fixed MQTT and added PM2.5 trait to google assistant
This commit is contained in:
parent
cfcebca5a0
commit
2d2a79470d
7 changed files with 119 additions and 28 deletions
|
|
@ -18,7 +18,7 @@ subprojects {
|
||||||
apply plugin: 'java-library'
|
apply plugin: 'java-library'
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'se.koc:zutil:1.0.312'
|
implementation 'se.koc:zutil:1.0.313'
|
||||||
//implementation 'se.koc:zutil:1.0.0-SNAPSHOT'
|
//implementation 'se.koc:zutil:1.0.0-SNAPSHOT'
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,9 @@ public class DeviceTraitFactory {
|
||||||
case "se.hal.struct.devicedata.OpenClosedEventData":
|
case "se.hal.struct.devicedata.OpenClosedEventData":
|
||||||
return new DeviceTrait[]{new OpenCloseTrait()};
|
return new DeviceTrait[]{new OpenCloseTrait()};
|
||||||
|
|
||||||
|
case "se.hal.struct.devicedata.ParticulateMatterSensorData":
|
||||||
|
return new DeviceTrait[]{new SensorStateTrait()};
|
||||||
|
|
||||||
case "se.hal.struct.devicedata.PowerConsumptionSensorData":
|
case "se.hal.struct.devicedata.PowerConsumptionSensorData":
|
||||||
case "se.hal.struct.devicedata.LightSensorData":
|
case "se.hal.struct.devicedata.LightSensorData":
|
||||||
return new DeviceTrait[]{new SensorStateTrait()};
|
return new DeviceTrait[]{new SensorStateTrait()};
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,14 @@ package se.hal.plugin.assistant.google.trait;
|
||||||
|
|
||||||
import se.hal.intf.HalDeviceConfig;
|
import se.hal.intf.HalDeviceConfig;
|
||||||
import se.hal.intf.HalDeviceData;
|
import se.hal.intf.HalDeviceData;
|
||||||
import se.hal.struct.devicedata.OpenClosedEventData;
|
import se.hal.struct.devicedata.ParticulateMatterSensorData;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://developers.google.com/assistant/smarthome/traits/openclose
|
* https://developers.home.google.com/cloud-to-cloud/traits/sensorstate
|
||||||
*/
|
*/
|
||||||
public class SensorStateTrait extends DeviceTrait {
|
public class SensorStateTrait extends DeviceTrait {
|
||||||
|
|
||||||
|
|
@ -48,10 +48,14 @@ public class SensorStateTrait extends DeviceTrait {
|
||||||
HashMap<String, Object> response = new HashMap<>();
|
HashMap<String, Object> response = new HashMap<>();
|
||||||
ArrayList<HashMap> sensorStatesSupported = new ArrayList<>();
|
ArrayList<HashMap> sensorStatesSupported = new ArrayList<>();
|
||||||
|
|
||||||
/*sensorStatesSupported.add(new HashMap<String, Object>() {{
|
sensorStatesSupported.add(new HashMap<String, Object>() {{
|
||||||
put("name", xxx);
|
if (config.getDeviceDataClass() == ParticulateMatterSensorData.class) {
|
||||||
put("maxThresholdCelsius", 60);
|
put("name", "PM2.5");
|
||||||
}});*/
|
put("numericCapabilities", new HashMap<String, Object>() {{
|
||||||
|
put("rawValueUnit", "MICROGRAMS_PER_CUBIC_METER");
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
}});
|
||||||
|
|
||||||
response.put("sensorStatesSupported", sensorStatesSupported);
|
response.put("sensorStatesSupported", sensorStatesSupported);
|
||||||
return response;
|
return response;
|
||||||
|
|
@ -62,11 +66,13 @@ public class SensorStateTrait extends DeviceTrait {
|
||||||
HashMap<String, Object> response = new HashMap<>();
|
HashMap<String, Object> response = new HashMap<>();
|
||||||
ArrayList<HashMap> currentSensorStateData = new ArrayList<>();
|
ArrayList<HashMap> currentSensorStateData = new ArrayList<>();
|
||||||
|
|
||||||
/*currentSensorStateData.add(new HashMap<String, Object>() {{
|
currentSensorStateData.add(new HashMap<String, Object>() {{
|
||||||
put("name", xxx);
|
if (data instanceof ParticulateMatterSensorData) {
|
||||||
put("currentSensorState", xxx);
|
put("name", "PM2.5");
|
||||||
put("rawValue", xxx);
|
//put("currentSensorState", xxx);
|
||||||
}});*/
|
put("rawValue", data.getData());
|
||||||
|
}
|
||||||
|
}});
|
||||||
|
|
||||||
response.put("currentSensorStateData", currentSensorStateData);
|
response.put("currentSensorStateData", currentSensorStateData);
|
||||||
return response;
|
return response;
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,18 @@ package se.hal.plugin.mqtt;
|
||||||
import se.hal.daemon.HalMulticastDnsDaemon;
|
import se.hal.daemon.HalMulticastDnsDaemon;
|
||||||
import se.hal.intf.*;
|
import se.hal.intf.*;
|
||||||
import se.hal.plugin.mqtt.device.HalMqttDeviceConfig;
|
import se.hal.plugin.mqtt.device.HalMqttDeviceConfig;
|
||||||
|
import se.hal.plugin.mqtt.device.HalMqttUnknownDeviceConfig;
|
||||||
import zutil.InetUtil;
|
import zutil.InetUtil;
|
||||||
|
import zutil.ObjectUtil;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.net.mqtt.MqttBroker;
|
import zutil.net.mqtt.MqttBroker;
|
||||||
import zutil.net.mqtt.MqttSubscriptionListener;
|
import zutil.net.mqtt.MqttSubscriptionListener;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
@ -96,9 +100,12 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dataPublished(String topic, byte[] data) {
|
public void dataPublished(String topic, byte[] data) {
|
||||||
List<HalMqttDeviceConfig> devices = topics.get(topic);
|
logger.finest("MQTT data published(topic: " + topic + "): " + new String(data, StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
List<HalMqttDeviceConfig> devices = topics.get(topic);
|
||||||
|
if (ObjectUtil.isEmpty(devices))
|
||||||
|
devices = Arrays.asList(new HalMqttUnknownDeviceConfig(topic));
|
||||||
|
|
||||||
if (devices != null && data.length > 0) {
|
|
||||||
for (HalMqttDeviceConfig deviceConfig : devices) {
|
for (HalMqttDeviceConfig deviceConfig : devices) {
|
||||||
HalDeviceData deviceData = deviceConfig.getDeviceData(data);
|
HalDeviceData deviceData = deviceConfig.getDeviceData(data);
|
||||||
|
|
||||||
|
|
@ -109,7 +116,6 @@ public class HalMqttController implements HalAutostartController, MqttSubscripti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public MqttBroker getBroker() {
|
public MqttBroker getBroker() {
|
||||||
return mqttBroker;
|
return mqttBroker;
|
||||||
|
|
|
||||||
|
|
@ -50,11 +50,12 @@ package se.hal.plugin.mqtt.device;
|
||||||
|
|
||||||
import se.hal.intf.HalDeviceData;
|
import se.hal.intf.HalDeviceData;
|
||||||
import se.hal.intf.HalEventController;
|
import se.hal.intf.HalEventController;
|
||||||
import se.hal.intf.HalEventData;
|
|
||||||
import se.hal.intf.HalSensorConfig;
|
import se.hal.intf.HalSensorConfig;
|
||||||
import se.hal.plugin.mqtt.HalMqttController;
|
import se.hal.plugin.mqtt.HalMqttController;
|
||||||
import zutil.ui.conf.Configurator;
|
import zutil.ui.conf.Configurator;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class HalMqttDeviceConfig implements HalSensorConfig {
|
public abstract class HalMqttDeviceConfig implements HalSensorConfig {
|
||||||
@Configurator.Configurable(value = "MQTT Topic")
|
@Configurator.Configurable(value = "MQTT Topic")
|
||||||
private String topic;
|
private String topic;
|
||||||
|
|
@ -97,10 +98,21 @@ public abstract class HalMqttDeviceConfig implements HalSensorConfig {
|
||||||
// --------------------------
|
// --------------------------
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object o) {
|
||||||
if (obj instanceof HalMqttDeviceConfig)
|
if (this == o) return true;
|
||||||
return topic.equals(((HalMqttDeviceConfig) obj).topic) && jsonPath.equals(((HalMqttDeviceConfig) obj).jsonPath);
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
return false;
|
|
||||||
|
HalMqttDeviceConfig that = (HalMqttDeviceConfig) o;
|
||||||
|
|
||||||
|
if (!Objects.equals(topic, that.topic)) return false;
|
||||||
|
return Objects.equals(jsonPath, that.jsonPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = topic != null ? topic.hashCode() : 0;
|
||||||
|
result = 31 * result + (jsonPath != null ? jsonPath.hashCode() : 0);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ package se.hal.plugin.mqtt.device;
|
||||||
import se.hal.intf.HalDeviceData;
|
import se.hal.intf.HalDeviceData;
|
||||||
import se.hal.intf.HalEventController;
|
import se.hal.intf.HalEventController;
|
||||||
import se.hal.plugin.mqtt.HalMqttController;
|
import se.hal.plugin.mqtt.HalMqttController;
|
||||||
import se.hal.struct.devicedata.TemperatureSensorData;
|
import se.hal.struct.devicedata.ParticulateMatterSensorData;
|
||||||
import zutil.ObjectUtil;
|
import zutil.ObjectUtil;
|
||||||
import zutil.converter.Converter;
|
import zutil.converter.Converter;
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
|
|
@ -73,7 +73,7 @@ public class HalMqttParticularMatterDeviceConfig extends HalMqttDeviceConfig {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<? extends HalDeviceData> getDeviceDataClass() {
|
public Class<? extends HalDeviceData> getDeviceDataClass() {
|
||||||
return TemperatureSensorData.class;
|
return ParticulateMatterSensorData.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -84,11 +84,11 @@ public class HalMqttParticularMatterDeviceConfig extends HalMqttDeviceConfig {
|
||||||
DataNode deviceDataValue = DataNodePath.search(getJsonPath(), json);
|
DataNode deviceDataValue = DataNodePath.search(getJsonPath(), json);
|
||||||
|
|
||||||
if (deviceDataValue != null)
|
if (deviceDataValue != null)
|
||||||
return new TemperatureSensorData(deviceDataValue.getDouble(), System.currentTimeMillis());
|
return new ParticulateMatterSensorData(deviceDataValue.getDouble(), System.currentTimeMillis());
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new TemperatureSensorData(Converter.toInt(data), System.currentTimeMillis());
|
return new ParticulateMatterSensorData(Converter.toInt(data), System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2023 Ziver Koc
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package se.hal.plugin.mqtt.device;
|
||||||
|
|
||||||
|
import se.hal.intf.HalDeviceData;
|
||||||
|
import se.hal.intf.HalEventController;
|
||||||
|
import se.hal.plugin.mqtt.HalMqttController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a unknown device data type
|
||||||
|
*/
|
||||||
|
public class HalMqttUnknownDeviceConfig extends HalMqttDeviceConfig {
|
||||||
|
|
||||||
|
|
||||||
|
public HalMqttUnknownDeviceConfig(String topic) {
|
||||||
|
setTopic(topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------
|
||||||
|
// Hal Methods
|
||||||
|
// --------------------------
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends HalEventController> getDeviceControllerClass() {
|
||||||
|
return HalMqttController.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends HalDeviceData> getDeviceDataClass() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HalDeviceData getDeviceData(byte[] data) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AggregationMethod getAggregationMethod() {
|
||||||
|
return AggregationMethod.AVERAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue