]> git.basschouten.com Git - openhab-addons.git/commitdiff
[netatmo] Adding a request counter (#13494)
authorGaël L'hopital <gael@lhopital.org>
Fri, 7 Oct 2022 09:48:41 +0000 (11:48 +0200)
committerGitHub <noreply@github.com>
Fri, 7 Oct 2022 09:48:41 +0000 (11:48 +0200)
* Adding a request counter channel to Api Bridge thing.

Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.netatmo/README.md
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoBindingConstants.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/ModuleType.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/ApiBridgeChannelHelper.java [new file with mode: 0644]
bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties
bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/channels.xml
bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/common.xml

index f7356d2582fa051aa987d9a8ba88f02acd9b6296..adcb2bc596a5c713c9afd89dca7879e6f427bab5 100644 (file)
@@ -52,6 +52,13 @@ The Account bridge has the following configuration elements:
 
 (*) Strictly said this parameter is not mandatory at first run, until you grant your binding on Netatmo Connect. Once present, you'll not have to grant again.
 
+**Supported channels for the Account bridge thing:**
+
+| Channel Group | Channel Id    | Item Type | Description                                                       |
+|---------------|---------------|-----------|-------------------------------------------------------------------|
+| monitoring    | request-count | Number    | Number of request transmitted to Netatmo API during the last hour |
+
+
 ### Configure the Bridge
 
 1. Complete the Netatmo Application Registration if you have not already done so, see above.
index a1d7258fa545355005d53ac1642199f98ed5768e..4165bb6bae235c1eb11b80e90e4d42c8898b9839 100644 (file)
@@ -58,6 +58,7 @@ public class NetatmoBindingConstants {
     public static final String GROUP_PROPERTIES = "properties";
     public static final String GROUP_SETPOINT = "setpoint";
     public static final String GROUP_LOCATION = "location";
+    public static final String GROUP_MONITORING = "monitoring";
 
     // Alternative extended groups
     public static final String OPTION_EXTENDED = "-extended";
@@ -153,4 +154,5 @@ public class NetatmoBindingConstants {
     public static final String CHANNEL_HOME_EVENT = "home-event";
     public static final String CHANNEL_SETPOINT_DURATION = "setpoint-duration";
     public static final String CHANNEL_FLOODLIGHT = "floodlight";
+    public static final String CHANNEL_REQUEST_COUNT = "request-count";
 }
index f999e6a1e1684c80760f34b7147b94249efea282..fc313af17f5198d7518f8b285b0220a0003b90fc 100644 (file)
@@ -39,6 +39,7 @@ import org.openhab.binding.netatmo.internal.handler.capability.RoomCapability;
 import org.openhab.binding.netatmo.internal.handler.capability.SmokeCapability;
 import org.openhab.binding.netatmo.internal.handler.capability.WeatherCapability;
 import org.openhab.binding.netatmo.internal.handler.channelhelper.AirQualityChannelHelper;
+import org.openhab.binding.netatmo.internal.handler.channelhelper.ApiBridgeChannelHelper;
 import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper;
 import org.openhab.binding.netatmo.internal.handler.channelhelper.EnergyChannelHelper;
 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventChannelHelper;
@@ -64,7 +65,7 @@ import org.openhab.core.thing.ThingTypeUID;
 @NonNullByDefault
 public enum ModuleType {
     UNKNOWN(FeatureArea.NONE, "", null, Set.of()),
-    ACCOUNT(FeatureArea.NONE, "", null, Set.of()),
+    ACCOUNT(FeatureArea.NONE, "", null, Set.of(), new ChannelGroup(ApiBridgeChannelHelper.class, GROUP_MONITORING)),
 
     HOME(FeatureArea.NONE, "NAHome", ACCOUNT,
             Set.of(DeviceCapability.class, HomeCapability.class, ChannelHelperCapability.class),
index 702c7820cfcf9c1e067cef476650af69ef8a9d39..bdbccab760c3e4755d8c1b3401c1733d9a80b19a 100644 (file)
  */
 package org.openhab.binding.netatmo.internal.handler;
 
+import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
+
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.lang.reflect.Constructor;
 import java.net.URI;
 import java.nio.charset.StandardCharsets;
+import java.time.LocalDateTime;
+import java.util.ArrayDeque;
 import java.util.Collection;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -55,6 +60,7 @@ import org.openhab.binding.netatmo.internal.discovery.NetatmoDiscoveryService;
 import org.openhab.binding.netatmo.internal.servlet.GrantServlet;
 import org.openhab.binding.netatmo.internal.servlet.WebhookServlet;
 import org.openhab.core.config.core.Configuration;
+import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.thing.Bridge;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
@@ -88,6 +94,8 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
     private Map<Class<? extends RestManager>, RestManager> managers = new HashMap<>();
     private @Nullable WebhookServlet webHookServlet;
     private @Nullable GrantServlet grantServlet;
+    private Deque<LocalDateTime> requestsTimestamps;
+    private final ChannelUID requestCountChannelUID;
 
     public ApiBridgeHandler(Bridge bridge, HttpClient httpClient, NADeserializer deserializer,
             BindingConfiguration configuration, HttpService httpService) {
@@ -97,6 +105,8 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
         this.httpClient = httpClient;
         this.deserializer = deserializer;
         this.httpService = httpService;
+        this.requestsTimestamps = new ArrayDeque<>(200);
+        this.requestCountChannelUID = new ChannelUID(getThing().getUID(), GROUP_MONITORING, CHANNEL_REQUEST_COUNT);
     }
 
     @Override
@@ -105,7 +115,7 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
         updateStatus(ThingStatus.UNKNOWN);
         GrantServlet servlet = new GrantServlet(this, httpService);
         servlet.startListening();
-        this.grantServlet = servlet;
+        grantServlet = servlet;
         scheduler.execute(() -> openConnection(null, null));
     }
 
@@ -235,6 +245,15 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
                 }
             }
 
+            if (isLinked(requestCountChannelUID)) {
+                LocalDateTime now = LocalDateTime.now();
+                LocalDateTime oneHourAgo = now.minusHours(1);
+                requestsTimestamps.addLast(now);
+                while (requestsTimestamps.getFirst().isBefore(oneHourAgo)) {
+                    requestsTimestamps.removeFirst();
+                }
+                updateState(requestCountChannelUID, new DecimalType(requestsTimestamps.size()));
+            }
             ContentResponse response = request.send();
 
             Code statusCode = HttpStatus.getCode(response.getStatus());
diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/ApiBridgeChannelHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/ApiBridgeChannelHelper.java
new file mode 100644 (file)
index 0000000..a24d3af
--- /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.netatmo.internal.handler.channelhelper;
+
+import java.util.Set;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * The {@link ApiBridgeChannelHelper} handle specifics channels the Netatmo Bridge
+ *
+ * @author Gaël L'hopital - Initial contribution
+ *
+ */
+@NonNullByDefault
+public class ApiBridgeChannelHelper extends ChannelHelper {
+
+    public ApiBridgeChannelHelper(Set<String> providedGroups) {
+        super(providedGroups);
+    }
+}
index 51f17f9b90e42ffb4a6128d546528d7963d979a3..a716315f36b216e27b657468ca701b1ead04906e 100644 (file)
@@ -9,7 +9,7 @@ binding.config.netatmo.readFriends.label = Access Guests
 binding.config.netatmo.readFriends.description = For Weather Stations: A friend gave you access to their Netatmo Weather Station.
 
 # channel group types
-
+channel-group-type.netatmo.monitoring.label = API Monitoring
 channel-group-type.netatmo.airquality-extended.label = Air Quality
 channel-group-type.netatmo.airquality.label = Air Quality
 channel-group-type.netatmo.battery-extended.label = Battery
@@ -117,7 +117,8 @@ channel-group-type.netatmo.wind.channel.max-strength-date.label = Date Max Wind
 channel-group-type.netatmo.wind.channel.max-strength-date.description = Moment when max wind strength was recorded.
 
 # channel types
-
+channel-type.netatmo.request-count.label = Request Count
+channel-type.netatmo.request-count.description = Number of request transmitted to Netatmo API during the last hour.
 channel-type.netatmo.absolute-pressure.label = Absolute Pressure
 channel-type.netatmo.absolute-pressure.description = Pressure measured relative to a full vacuum.
 channel-type.netatmo.alim-status.label = Alim State
index 5b60f29de173fedf7e647c967a48fdf8adbf5023..d91a77d0e3656428c94c1860149a1c01f468ffa0 100644 (file)
                <state readOnly="false" pattern="%s"/>
        </channel-type>
 
+       <channel-type id="request-count" advanced="true">
+               <item-type>Number</item-type>
+               <label>Request Count</label>
+               <description>Number of request transmitted to Netatmo API during the last hour.</description>
+               <state readOnly="true" pattern="%d"/>
+       </channel-type>
+
        <channel-type id="person-count">
                <item-type>Number</item-type>
                <label>Person Count</label>
index 1fd7424fa7046e616b84a4903de90ffc03f6d2f8..5f45b72661c0b690541095dfff8bae891e63b666 100644 (file)
@@ -4,6 +4,13 @@
        xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0"
        xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">
 
+       <channel-group-type id="monitoring">
+               <label>API Monitoring</label>
+               <channels>
+                       <channel id="request-count" typeId="request-count"/>
+               </channels>
+       </channel-group-type>
+
        <channel-group-type id="signal">
                <label>Signal</label>
                <channels>