Skip to content

Commit

Permalink
[netatmo] Adding a request counter (#13494)
Browse files Browse the repository at this point in the history
* Adding a request counter channel to Api Bridge thing.

Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique authored Oct 7, 2022
1 parent 31820ad commit cc6e4fa
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 4 deletions.
7 changes: 7 additions & 0 deletions bundles/org.openhab.binding.netatmo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand All @@ -64,7 +65,7 @@
@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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +60,7 @@
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;
Expand Down Expand Up @@ -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) {
Expand All @@ -97,6 +105,8 @@ public ApiBridgeHandler(Bridge bridge, HttpClient httpClient, NADeserializer des
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
Expand All @@ -105,7 +115,7 @@ public void initialize() {
updateStatus(ThingStatus.UNKNOWN);
GrantServlet servlet = new GrantServlet(this, httpService);
servlet.startListening();
this.grantServlet = servlet;
grantServlet = servlet;
scheduler.execute(() -> openConnection(null, null));
}

Expand Down Expand Up @@ -235,6 +245,15 @@ public synchronized <T> T executeUri(URI uri, HttpMethod method, Class<T> clazz,
}
}

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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit cc6e4fa

Please sign in to comment.