2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.linky.internal.console;
15 import java.time.LocalDate;
16 import java.time.format.DateTimeFormatter;
17 import java.time.format.DateTimeParseException;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.linky.internal.LinkyBindingConstants;
25 import org.openhab.binding.linky.internal.handler.LinkyHandler;
26 import org.openhab.core.io.console.Console;
27 import org.openhab.core.io.console.ConsoleCommandCompleter;
28 import org.openhab.core.io.console.StringsCompleter;
29 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
30 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingRegistry;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
40 * The {@link LinkyCommandExtension} is responsible for handling console commands
42 * @author Laurent Garnier - Initial contribution
46 @Component(service = ConsoleCommandExtension.class)
47 public class LinkyCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
49 private static final String REPORT = "report";
50 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(REPORT), false);
52 private final ThingRegistry thingRegistry;
55 public LinkyCommandExtension(final @Reference ThingRegistry thingRegistry) {
56 super(LinkyBindingConstants.BINDING_ID, "Interact with the Linky binding.");
57 this.thingRegistry = thingRegistry;
61 public void execute(String[] args, Console console) {
62 if (args.length >= 2) {
63 Thing thing = getThing(args[0]);
64 ThingHandler thingHandler = null;
65 LinkyHandler handler = null;
67 thingHandler = thing.getHandler();
68 if (thingHandler instanceof LinkyHandler linkyHandler) {
69 handler = linkyHandler;
73 console.println(String.format("Bad thing id '%s'", args[0]));
75 } else if (thingHandler == null) {
76 console.println(String.format("No handler initialized for the thing id '%s'", args[0]));
78 } else if (handler == null) {
79 console.println(String.format("'%s' is not a Linky thing id", args[0]));
81 } else if (REPORT.equals(args[1])) {
82 LocalDate yesterday = LocalDate.now().minusDays(1);
83 LocalDate start = yesterday.minusDays(6);
84 LocalDate end = yesterday;
85 String separator = " ";
86 if (args.length >= 3) {
88 start = LocalDate.parse(args[2], DateTimeFormatter.ISO_LOCAL_DATE);
89 } catch (DateTimeParseException e) {
90 console.println(String
91 .format("Invalid format for start day '%s'; expected format is YYYY-MM-DD", args[2]));
96 if (args.length >= 4) {
98 end = LocalDate.parse(args[3], DateTimeFormatter.ISO_LOCAL_DATE);
99 } catch (DateTimeParseException e) {
100 console.println(String.format("Invalid format for end day '%s'; expected format is YYYY-MM-DD",
106 if (start.isAfter(yesterday) || start.isAfter(end)) {
107 console.println("Start day must be in the past and before the end day");
111 if (end.isAfter(yesterday)) {
114 if (args.length >= 5) {
117 handler.reportValues(start, end, separator).forEach(console::println);
127 public List<String> getUsages() {
129 .asList(buildCommandUsage(String.format("<thingUID> %s <start day> <end day> [<separator>]", REPORT),
130 "report daily consumptions between two dates"));
134 public @Nullable ConsoleCommandCompleter getCompleter() {
139 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
140 if (cursorArgumentIndex <= 0) {
141 return new StringsCompleter(thingRegistry.getAll().stream()
142 .filter(t -> LinkyBindingConstants.THING_TYPE_LINKY.equals(t.getThingTypeUID()))
143 .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true)
144 .complete(args, cursorArgumentIndex, cursorPosition, candidates);
145 } else if (cursorArgumentIndex == 1) {
146 Thing thing = getThing(args[0]);
147 if (thing != null && LinkyBindingConstants.THING_TYPE_LINKY.equals(thing.getThingTypeUID())) {
148 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
154 private @Nullable Thing getThing(String uid) {
157 ThingUID thingUID = new ThingUID(uid);
158 thing = thingRegistry.get(thingUID);
159 } catch (IllegalArgumentException e) {