]> git.basschouten.com Git - openhab-addons.git/blob
cf4f495e59efe55b8fda642d2c8f6cf2aaae2065
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.linky.internal.console;
14
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;
21
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;
38
39 /**
40  * The {@link LinkyCommandExtension} is responsible for handling console commands
41  *
42  * @author Laurent Garnier - Initial contribution
43  */
44
45 @NonNullByDefault
46 @Component(service = ConsoleCommandExtension.class)
47 public class LinkyCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
48
49     private static final String REPORT = "report";
50     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(REPORT), false);
51
52     private final ThingRegistry thingRegistry;
53
54     @Activate
55     public LinkyCommandExtension(final @Reference ThingRegistry thingRegistry) {
56         super(LinkyBindingConstants.BINDING_ID, "Interact with the Linky binding.");
57         this.thingRegistry = thingRegistry;
58     }
59
60     @Override
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;
66             if (thing != null) {
67                 thingHandler = thing.getHandler();
68                 if (thingHandler instanceof LinkyHandler) {
69                     handler = (LinkyHandler) thingHandler;
70                 }
71             }
72             if (thing == null) {
73                 console.println(String.format("Bad thing id '%s'", args[0]));
74                 printUsage(console);
75             } else if (thingHandler == null) {
76                 console.println(String.format("No handler initialized for the thing id '%s'", args[0]));
77                 printUsage(console);
78             } else if (handler == null) {
79                 console.println(String.format("'%s' is not a Linky thing id", args[0]));
80                 printUsage(console);
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) {
87                     try {
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]));
92                         printUsage(console);
93                         return;
94                     }
95                 }
96                 if (args.length >= 4) {
97                     try {
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",
101                                 args[3]));
102                         printUsage(console);
103                         return;
104                     }
105                 }
106                 if (start.isAfter(yesterday) || start.isAfter(end)) {
107                     console.println("Start day must be in the past and before the end day");
108                     printUsage(console);
109                     return;
110                 }
111                 if (end.isAfter(yesterday)) {
112                     end = yesterday;
113                 }
114                 if (args.length >= 5) {
115                     separator = args[4];
116                 }
117                 handler.reportValues(start, end, separator).forEach(console::println);
118             } else {
119                 printUsage(console);
120             }
121         } else {
122             printUsage(console);
123         }
124     }
125
126     @Override
127     public List<String> getUsages() {
128         return Arrays
129                 .asList(buildCommandUsage(String.format("<thingUID> %s <start day> <end day> [<separator>]", REPORT),
130                         "report daily consumptions between two dates"));
131     }
132
133     @Override
134     public @Nullable ConsoleCommandCompleter getCompleter() {
135         return this;
136     }
137
138     @Override
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).complete(args,
144                             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);
149             }
150         }
151         return false;
152     }
153
154     private @Nullable Thing getThing(String uid) {
155         Thing thing = null;
156         try {
157             ThingUID thingUID = new ThingUID(uid);
158             thing = thingRegistry.get(thingUID);
159         } catch (IllegalArgumentException e) {
160             thing = null;
161         }
162         return thing;
163     }
164 }