]> git.basschouten.com Git - openhab-addons.git/commitdiff
[plugwiseha] Fix set the scene for a zone (#13832)
authorlsiepel <leosiepel@gmail.com>
Fri, 9 Dec 2022 18:08:41 +0000 (19:08 +0100)
committerGitHub <noreply@github.com>
Fri, 9 Dec 2022 18:08:41 +0000 (19:08 +0100)
* Fix request body

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAController.java
bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/PlugwiseHAControllerRequest.java
bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/Location.java
bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/LocationsArray.java [new file with mode: 0644]
bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/PlugwiseBaseModel.java
bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/xml/PlugwiseHAXStream.java

index c46f0a8f09149eb75e3e0061cf7873cd718430ea..e1739c7953d3fc3657af069920f661db4cf54a21 100644 (file)
@@ -37,6 +37,7 @@ import org.openhab.binding.plugwiseha.internal.api.model.dto.DomainObjects;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.GatewayInfo;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Location;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Locations;
+import org.openhab.binding.plugwiseha.internal.api.model.dto.LocationsArray;
 import org.openhab.binding.plugwiseha.internal.api.xml.PlugwiseHAXStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -395,7 +396,15 @@ public class PlugwiseHAController {
 
         request.setPath("/core/locations");
         request.addPathParameter("id", String.format("%s", location.getId()));
-        request.setBodyParameter(new Location(state));
+
+        Location locationWithChangesOnly = new Location();
+        locationWithChangesOnly.setPreset(state);
+        locationWithChangesOnly.setId(location.getId());
+
+        LocationsArray locations = new LocationsArray();
+        locations.items = new Location[] { locationWithChangesOnly };
+
+        request.setBodyParameter(locations);
 
         executeRequest(request);
     }
index b559f64cd0d55f89a281c7172233d02d75c39e7c..f921e3a3994d6e793ea9025c33445ea11507dcfc 100644 (file)
@@ -17,9 +17,11 @@ import java.io.StringWriter;
 import java.net.ConnectException;
 import java.net.SocketTimeoutException;
 import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ExecutionException;
@@ -225,6 +227,22 @@ public class PlugwiseHAControllerRequest<T> {
 
         this.logger.debug("Performing API request: {} {}", request.getMethod(), request.getURI());
 
+        if (logger.isTraceEnabled()) {
+            String content = "";
+            final ContentProvider provider = request.getContent();
+            if (provider != null) {
+                final Iterator<ByteBuffer> it = provider.iterator();
+                while (it.hasNext()) {
+                    final ByteBuffer next = it.next();
+                    final byte[] bytes = new byte[next.capacity()];
+                    next.get(bytes);
+                    content += String.format("{}\n", new String(bytes, StandardCharsets.UTF_8));
+                }
+            }
+
+            logger.trace(">> \n{}", content);
+        }
+
         try {
             response = request.send();
         } catch (InterruptedException e) {
index 6e6673e1f53bb2b68d8b80319ec8b5e796b3ee47..b2bbf92c9482db82faebcb0dcc1fa0503e468c12 100644 (file)
@@ -47,10 +47,6 @@ public class Location extends PlugwiseBaseModel implements PlugwiseComparableDat
     @XStreamImplicit(itemFieldName = "actuator_functionality", keyFieldName = "type")
     private ActuatorFunctionalities actuatorFunctionalities;
 
-    public Location(String presetScene) {
-        this.preset = presetScene;
-    }
-
     public String getName() {
         return name;
     }
@@ -116,6 +112,10 @@ public class Location extends PlugwiseBaseModel implements PlugwiseComparableDat
         return null;
     }
 
+    public void setPreset(String preset) {
+        this.preset = preset;
+    }
+
     public int applianceCount() {
         if (this.locationAppliances == null) {
             return 0;
diff --git a/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/LocationsArray.java b/bundles/org.openhab.binding.plugwiseha/src/main/java/org/openhab/binding/plugwiseha/internal/api/model/dto/LocationsArray.java
new file mode 100644 (file)
index 0000000..bf44ca3
--- /dev/null
@@ -0,0 +1,31 @@
+/**
+ * Copyright (c) 2010-2022 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.plugwiseha.internal.api.model.dto;
+
+import com.thoughtworks.xstream.annotations.XStreamAlias;
+import com.thoughtworks.xstream.annotations.XStreamImplicit;
+
+/**
+ * The {@link LocationsArray} class is an object model class that mirrors the XML
+ * structure provided by the Plugwise Home Automation controller for the
+ * collection of Plugwise locations/zones.
+ * 
+ * @author L. Siepel - Initial contribution
+ */
+
+@XStreamAlias("locations")
+public class LocationsArray {
+
+    @XStreamImplicit
+    public Location[] items;
+}
index a4638f1a2c008b5aa6ba18744c272d871db53c46..a90327a0b9711160c04e374e187d0ccd30fbaf0a 100644 (file)
@@ -15,6 +15,7 @@ package org.openhab.binding.plugwiseha.internal.api.model.dto;
 import java.time.ZonedDateTime;
 
 import com.thoughtworks.xstream.annotations.XStreamAlias;
+import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
 
 /**
  * The {@link PlugwiseBaseModel} abstract class contains
@@ -24,6 +25,7 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
  */
 public abstract class PlugwiseBaseModel {
 
+    @XStreamAsAttribute
     private String id;
 
     @XStreamAlias("created_date")
@@ -57,4 +59,8 @@ public abstract class PlugwiseBaseModel {
     public ZonedDateTime getDeletedDate() {
         return deletedDate;
     }
+
+    public void setId(String id) {
+        this.id = id;
+    }
 }
index d8c16ab9857303b960c0ab4445a831a8c1b83efa..51f6461237dccf0a7b8e3352387f7c29fd2a3d57 100644 (file)
@@ -29,6 +29,7 @@ import org.openhab.binding.plugwiseha.internal.api.model.dto.GatewayEnvironment;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.GatewayInfo;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Location;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Locations;
+import org.openhab.binding.plugwiseha.internal.api.model.dto.LocationsArray;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Log;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Logs;
 import org.openhab.binding.plugwiseha.internal.api.model.dto.Module;
@@ -87,6 +88,7 @@ public class PlugwiseHAXStream extends XStream {
         this.allowClass(Appliance.class);
         this.allowClass(Modules.class);
         this.allowClass(Module.class);
+        this.allowClass(LocationsArray.class);
         this.allowClass(Locations.class);
         this.allowClass(Location.class);
         this.allowClass(Logs.class);