]> git.basschouten.com Git - openhab-addons.git/blob
618eaf959e18222dedc8020a181ee9379a30a6d3
[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.dbquery.action;
14
15 import java.time.Duration;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.dbquery.internal.DatabaseBridgeHandler;
23 import org.openhab.binding.dbquery.internal.QueryHandler;
24 import org.openhab.binding.dbquery.internal.domain.ExecuteNonConfiguredQuery;
25 import org.openhab.binding.dbquery.internal.domain.QueryResult;
26 import org.openhab.binding.dbquery.internal.domain.ResultRow;
27 import org.openhab.binding.dbquery.internal.error.UnnexpectedCondition;
28 import org.openhab.core.automation.annotation.ActionInput;
29 import org.openhab.core.automation.annotation.RuleAction;
30 import org.openhab.core.thing.binding.ThingActions;
31 import org.openhab.core.thing.binding.ThingActionsScope;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author Joan Pujol - Initial contribution
38  */
39 @ThingActionsScope(name = "dbquery")
40 @NonNullByDefault
41 public class DBQueryActions implements IDBQueryActions, ThingActions {
42     private final Logger logger = LoggerFactory.getLogger(DBQueryActions.class);
43
44     private @Nullable QueryHandler queryHandler;
45     private @Nullable DatabaseBridgeHandler databaseBridgeHandler;
46
47     @Override
48     @RuleAction(label = "Execute query", description = "Execute query synchronously (use with care)")
49     public ActionQueryResult executeQuery(String query, Map<String, @Nullable Object> parameters,
50             int timeoutInSeconds) {
51         logger.debug("executeQuery from action {} params={}", query, parameters);
52         var currentDatabaseBridgeHandler = databaseBridgeHandler;
53         if (currentDatabaseBridgeHandler != null) {
54             QueryResult queryResult = new ExecuteNonConfiguredQuery(currentDatabaseBridgeHandler.getDatabase())
55                     .executeSynchronously(query, parameters, Duration.ofSeconds(timeoutInSeconds));
56             logger.debug("executeQuery from action result {}", queryResult);
57             return queryResult2ActionQueryResult(queryResult);
58         } else {
59             logger.warn("Execute queried ignored as databaseBridgeHandler is null");
60             return new ActionQueryResult(false, null);
61         }
62     }
63
64     private ActionQueryResult queryResult2ActionQueryResult(QueryResult queryResult) {
65         return new ActionQueryResult(queryResult.isCorrect(),
66                 queryResult.getData().stream().map(DBQueryActions::resultRow2Map).collect(Collectors.toList()));
67     }
68
69     private static Map<String, @Nullable Object> resultRow2Map(ResultRow resultRow) {
70         Map<String, @Nullable Object> map = new HashMap<>();
71         for (String column : resultRow.getColumnNames()) {
72             map.put(column, resultRow.getValue(column));
73         }
74         return map;
75     }
76
77     @Override
78     @RuleAction(label = "Set query parameters", description = "Set query parameters for a query")
79     public void setQueryParameters(@ActionInput(name = "parameters") Map<String, @Nullable Object> parameters) {
80         logger.debug("setQueryParameters {}", parameters);
81         var thingHandler = getThingHandler();
82         if (thingHandler instanceof QueryHandler queryHandler) {
83             queryHandler.setParameters(parameters);
84         } else {
85             logger.warn("setQueryParameters called on wrong Thing, it must be a Query Thing");
86         }
87     }
88
89     @Override
90     @RuleAction(label = "Get last query result", description = "Get last result from a query")
91     public ActionQueryResult getLastQueryResult() {
92         var currentQueryHandler = queryHandler;
93         if (currentQueryHandler != null) {
94             return queryResult2ActionQueryResult(queryHandler.getLastQueryResult());
95         } else {
96             logger.warn("getLastQueryResult ignored as queryHandler is null");
97             return new ActionQueryResult(false, null);
98         }
99     }
100
101     @Override
102     public void setThingHandler(ThingHandler thingHandler) {
103         if (thingHandler instanceof QueryHandler queryHandler) {
104             this.queryHandler = queryHandler;
105         } else if (thingHandler instanceof DatabaseBridgeHandler databaseBridgeHandler) {
106             this.databaseBridgeHandler = databaseBridgeHandler;
107         } else {
108             throw new UnnexpectedCondition("Not expected thing handler " + thingHandler);
109         }
110     }
111
112     @Override
113     public @Nullable ThingHandler getThingHandler() {
114         return queryHandler != null ? queryHandler : databaseBridgeHandler;
115     }
116 }