[netatmo] Adding a request counter (#13494)

* Adding a request counter channel to Api Bridge thing.

Signed-off-by: clinique <gael@lhopital.org>
pull/13506/head
Gaël L'hopital 2022-10-07 11:48:41 +02:00 committed by GitHub
parent 31820ad740
commit cc6e4fad61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 79 additions and 4 deletions

View 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.

View 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";
}

View 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),

View File

@ -12,12 +12,17 @@
*/
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());

View File

@ -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);
}
}

View 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

View File

@ -85,6 +85,13 @@
<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>

View 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>