2 * Copyright (c) 2010-2020 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.handler.LinkyHandler;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
25 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingRegistry;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link LinkyCommandExtension} is responsible for handling console commands
37 * @author Laurent Garnier - Initial contribution
41 @Component(service = ConsoleCommandExtension.class)
42 public class LinkyCommandExtension extends AbstractConsoleCommandExtension {
44 private static final String REPORT = "report";
46 private final ThingRegistry thingRegistry;
49 public LinkyCommandExtension(final @Reference ThingRegistry thingRegistry) {
50 super("linky", "Interact with the Linky binding.");
51 this.thingRegistry = thingRegistry;
55 public void execute(String[] args, Console console) {
56 if (args.length >= 2) {
59 ThingUID thingUID = new ThingUID(args[0]);
60 thing = thingRegistry.get(thingUID);
61 } catch (IllegalArgumentException e) {
64 ThingHandler thingHandler = null;
65 LinkyHandler handler = null;
67 thingHandler = thing.getHandler();
68 if (thingHandler instanceof LinkyHandler) {
69 handler = (LinkyHandler) thingHandler;
73 console.println("Bad thing id '" + args[0] + "'");
75 } else if (thingHandler == null) {
76 console.println("No handler initialized for the thing id '" + args[0] + "'");
78 } else if (handler == null) {
79 console.println("'" + args[0] + "' is not a Linky thing id");
81 } else if (REPORT.equals(args[1])) {
82 LocalDate now = LocalDate.now();
83 LocalDate start = now.minusDays(7);
84 LocalDate end = now.minusDays(1);
85 String separator = " ";
86 if (args.length >= 3) {
88 start = LocalDate.parse(args[2], DateTimeFormatter.ISO_LOCAL_DATE);
89 } catch (DateTimeParseException e) {
91 "Invalid format for start day '" + args[2] + "'; expected format is YYYY-MM-DD");
96 if (args.length >= 4) {
98 end = LocalDate.parse(args[3], DateTimeFormatter.ISO_LOCAL_DATE);
99 } catch (DateTimeParseException e) {
100 console.println("Invalid format for end day '" + args[3] + "'; expected format is YYYY-MM-DD");
105 if (!start.isBefore(now) || start.isAfter(end)) {
106 console.println("Start day must be in the past and before the end day");
110 if (end.isAfter(now.minusDays(1))) {
111 end = now.minusDays(1);
113 if (args.length >= 5) {
116 handler.reportValues(start, end, separator).forEach(console::println);
126 public List<String> getUsages() {
127 return Arrays.asList(buildCommandUsage("<thingUID> " + REPORT + " <start day> <end day> [<separator>]",
128 "report daily consumptions between two dates"));