Skip to content

Commit 9494ddc

Browse files
authored
[pihole] Discrepencies between dashboard and channels (#20394)
* Solving some v6 API issues Signed-off-by: gael@lhopital.org <gael@lhopital.org>
1 parent 6ddbebf commit 9494ddc

6 files changed

Lines changed: 31 additions & 28 deletions

File tree

bundles/org.openhab.binding.pihole/src/main/java/org/openhab/binding/pihole/internal/PiHoleActions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public static void disableBlocking(@Nullable ThingActions actions, long time, @N
6767

6868
@RuleAction(label = "@text/action.disable.label", description = "@text/action.disable.description")
6969
public void disableBlocking(
70-
@ActionInput(name = "time", label = "@text/action.disable.timeLabel", description = "@text/action.disable.timeDescription") long time)
70+
@ActionInput(name = "time", label = "@text/action.disable.timeLabel", description = "@text/action.disable.timeDescription") Number time)
7171
throws PiHoleException {
72-
disableBlocking(time, null);
72+
disableBlocking(time.longValue(), null);
7373
}
7474

7575
public static void disableBlocking(@Nullable ThingActions actions, long time) throws PiHoleException {

bundles/org.openhab.binding.pihole/src/main/java/org/openhab/binding/pihole/internal/rest/JettyAdminServiceV6.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class JettyAdminServiceV6 extends AdminService {
6565

6666
private @Nullable String sid;
6767
private @Nullable Instant sessionValidity;
68-
private @Nullable Instant midnightUtc;
6968

7069
public JettyAdminServiceV6(String token, URI baseUrl, HttpClient client, Gson gson) {
7170
super(client, gson);
@@ -110,18 +109,6 @@ private String getSid() throws PiHoleException {
110109
return updateSid();
111110
}
112111

113-
private long getTodayMidnight() {
114-
Instant now = Instant.now();
115-
Instant local = midnightUtc;
116-
117-
if (local == null || now.minus(1, ChronoUnit.DAYS).isAfter(local)) {
118-
local = now.truncatedTo(ChronoUnit.DAYS);
119-
midnightUtc = local;
120-
}
121-
122-
return local.getEpochSecond();
123-
}
124-
125112
@Override
126113
public Optional<DnsStatistics> summary() throws PiHoleException {
127114
logger.debug("Building the as if it was a v5 API");
@@ -132,9 +119,12 @@ public Optional<DnsStatistics> summary() throws PiHoleException {
132119

133120
DnsBlockingAnswer blockingAnswer = get(dnsBlockingURI, DnsBlockingAnswer.class);
134121

135-
long todayMidnight = getTodayMidnight();
136-
StatDatabaseSummary statDatabase = get(databaseSummaryURI, StatDatabaseSummary.class, "from",
137-
Long.toString(todayMidnight), "until", Long.toString(todayMidnight + 24 * 60 * 60));
122+
Instant now = Instant.now();
123+
String oneDayAgo = Long.toString(now.minus(24, ChronoUnit.HOURS).getEpochSecond());
124+
String toNow = Long.toString(now.getEpochSecond());
125+
126+
StatDatabaseSummary statDatabase = get(databaseSummaryURI, StatDatabaseSummary.class, "from", oneDayAgo,
127+
"until", toNow);
138128

139129
HistoryClients historyClients = get(historyClientsURI, HistoryClients.class, "N", "0");
140130
ConfigAnswer configAnswer = get(configURI, ConfigAnswer.class);
@@ -143,12 +133,13 @@ public Optional<DnsStatistics> summary() throws PiHoleException {
143133

144134
DnsStatistics translated = new DnsStatistics(gravity.domainsBeingBlocked(), statDatabase.sumQueries(),
145135
statDatabase.sumBlocked(), statDatabase.percentBlocked(), statQueries.uniqueDomains(),
146-
statQueries.forwarded(), statQueries.cached(), historyClients.clients().size(), null,
147-
statQueries.types().all(), replies.unknown(), replies.nodata(), replies.nxdomain(), replies.cname(),
148-
replies.ip(), replies.domain(), replies.rrname(), replies.servfail(), replies.refused(),
149-
replies.notimp(), replies.other(), replies.dnssec(), replies.none(), replies.blob(), replies.all(),
150-
configAnswer.config().misc().privacylevel(), blockingAnswer.blocking(), new GravityLastUpdated(
151-
configAnswer.config().files().gravity() != null, gravity.lastUpdate(), relative));
136+
statQueries.forwarded(), statQueries.cached(), historyClients.clients().size(),
137+
statAnswer.clients().active(), statQueries.types().all(), replies.unknown(), replies.nodata(),
138+
replies.nxdomain(), replies.cname(), replies.ip(), replies.domain(), replies.rrname(),
139+
replies.servfail(), replies.refused(), replies.notimp(), replies.other(), replies.dnssec(),
140+
replies.none(), replies.blob(), replies.all(), configAnswer.config().misc().privacylevel(),
141+
blockingAnswer.blocking(), new GravityLastUpdated(configAnswer.config().files().gravity() != null,
142+
gravity.lastUpdate(), relative));
152143

153144
return Optional.of(translated);
154145
}

bundles/org.openhab.binding.pihole/src/main/java/org/openhab/binding/pihole/internal/rest/model/v6/StatAnswer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* @author Gaël L'hopital - Initial contribution
2323
*/
2424
@NonNullByDefault
25-
public record StatAnswer(Queries queries, Clients client, Gravity gravity, double took) {
26-
record Clients(int active, int total) {
25+
public record StatAnswer(Queries queries, Clients clients, Gravity gravity, double took) {
26+
public record Clients(int active, int total) {
2727

2828
}
2929

bundles/org.openhab.binding.pihole/src/main/resources/OH-INF/addon/addon.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<type>binding</type>
77
<name>Pi-hole Binding</name>
88
<description>This is the binding for Pi-hole.</description>
9-
<connection>cloud</connection>
9+
<connection>local</connection>
1010

1111
</addon:addon>

bundles/org.openhab.binding.pihole/src/main/resources/OH-INF/thing/thing-types.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@
115115
</channel>
116116
<channel id="enabled" typeId="enabled-channel"/>
117117
<channel id="disable-enable" typeId="disable-enable-channel"/>
118+
<channel id="gravity-file-exists" typeId="gravity-file-exists-channel"/>
119+
<channel id="gravity-last-update" typeId="gravity-last-update-channel"/>
118120
</channels>
119121
<properties>
120-
<property name="thingTypeVersion">2</property>
122+
<property name="thingTypeVersion">3</property>
121123
</properties>
122124

123125
<config-description>

bundles/org.openhab.binding.pihole/src/main/resources/OH-INF/update/update.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@
2020
</update-channel>
2121
</instruction-set>
2222
</thing-type>
23+
<thing-type uid="pihole:server">
24+
<instruction-set targetVersion="3">
25+
<add-channel id="gravity-file-exists">
26+
<type>pihole:gravity-file-exists-channel</type>
27+
</add-channel>
28+
<add-channel id="gravity-last-update">
29+
<type>pihole:gravity-last-update-channel</type>
30+
</add-channel>
31+
</instruction-set>
32+
</thing-type>
2333
</update:update-descriptions>

0 commit comments

Comments
 (0)