2 * Copyright (c) 2010-2021 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;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.linky.internal.LinkyBindingConstants;
23 import org.openhab.binding.linky.internal.handler.LinkyHandler;
24 import org.openhab.core.io.console.Console;
25 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
26 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingRegistry;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
36 * The {@link LinkyCommandExtension} is responsible for handling console commands
38 * @author Laurent Garnier - Initial contribution
42 @Component(service = ConsoleCommandExtension.class)
43 public class LinkyCommandExtension extends AbstractConsoleCommandExtension {
45 private static final String REPORT = "report";
47 private final ThingRegistry thingRegistry;
50 public LinkyCommandExtension(final @Reference ThingRegistry thingRegistry) {
51 super(LinkyBindingConstants.BINDING_ID, "Interact with the Linky binding.");
52 this.thingRegistry = thingRegistry;
56 public void execute(String[] args, Console console) {
57 if (args.length >= 2) {
60 ThingUID thingUID = new ThingUID(args[0]);
61 thing = thingRegistry.get(thingUID);
62 } catch (IllegalArgumentException e) {
65 ThingHandler thingHandler = null;
66 LinkyHandler handler = null;
68 thingHandler = thing.getHandler();
69 if (thingHandler instanceof LinkyHandler) {
70 handler = (LinkyHandler) thingHandler;
74 console.println(String.format("Bad thing id '%s'", args[0]));
76 } else if (thingHandler == null) {
77 console.println(String.format("No handler initialized for the thing id '%s'", args[0]));
79 } else if (handler == null) {
80 console.println(String.format("'%s' is not a Linky thing id", args[0]));
82 } else if (REPORT.equals(args[1])) {
83 LocalDate yesterday = LocalDate.now().minusDays(1);
84 LocalDate start = yesterday.minusDays(6);
85 LocalDate end = yesterday;
86 String separator = " ";
87 if (args.length >= 3) {
89 start = LocalDate.parse(args[2], DateTimeFormatter.ISO_LOCAL_DATE);
90 } catch (DateTimeParseException e) {
91 console.println(String
92 .format("Invalid format for start day '%s'; expected format is YYYY-MM-DD", args[2]));
97 if (args.length >= 4) {
99 end = LocalDate.parse(args[3], DateTimeFormatter.ISO_LOCAL_DATE);
100 } catch (DateTimeParseException e) {
101 console.println(String.format("Invalid format for end day '%s'; expected format is YYYY-MM-DD",
107 if (start.isAfter(yesterday) || start.isAfter(end)) {
108 console.println("Start day must be in the past and before the end day");
112 if (end.isAfter(yesterday)) {
115 if (args.length >= 5) {
118 handler.reportValues(start, end, separator).forEach(console::println);
128 public List<String> getUsages() {
130 .asList(buildCommandUsage(String.format("<thingUID> %s <start day> <end day> [<separator>]", REPORT),
131 "report daily consumptions between two dates"));