]> git.basschouten.com Git - openhab-addons.git/blob
f4c627f28783986d867c88073020ade70e99c2ba
[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.shelly.internal.manager;
14
15 import static org.openhab.binding.shelly.internal.manager.ShellyManagerConstants.*;
16
17 import java.util.LinkedHashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.http.HttpStatus;
23 import org.openhab.binding.shelly.internal.ShellyHandlerFactory;
24 import org.openhab.binding.shelly.internal.api.ShellyApiException;
25 import org.openhab.binding.shelly.internal.manager.ShellyManagerPage.ShellyMgrResponse;
26 import org.openhab.binding.shelly.internal.provider.ShellyTranslationProvider;
27 import org.osgi.service.cm.ConfigurationAdmin;
28
29 /**
30  * {@link ShellyManager} implements the Shelly Manager
31  *
32  * @author Markus Michels - Initial contribution
33  */
34 @NonNullByDefault
35 public class ShellyManager {
36     private final Map<String, ShellyManagerPage> pages = new LinkedHashMap<>();
37
38     public ShellyManager(ConfigurationAdmin configurationAdmin, ShellyTranslationProvider translationProvider,
39             HttpClient httpClient, String localIp, int localPort, ShellyHandlerFactory handlerFactory) {
40         pages.put(SHELLY_MGR_OVERVIEW_URI, new ShellyManagerOverviewPage(configurationAdmin, translationProvider,
41                 httpClient, localIp, localPort, handlerFactory));
42         pages.put(SHELLY_MGR_ACTION_URI, new ShellyManagerActionPage(configurationAdmin, translationProvider,
43                 httpClient, localIp, localPort, handlerFactory));
44         pages.put(SHELLY_MGR_FWUPDATE_URI, new ShellyManagerOtaPage(configurationAdmin, translationProvider, httpClient,
45                 localIp, localPort, handlerFactory));
46         pages.put(SHELLY_MGR_OTA_URI, new ShellyManagerOtaPage(configurationAdmin, translationProvider, httpClient,
47                 localIp, localPort, handlerFactory));
48         pages.put(SHELLY_MGR_IMAGES_URI, new ShellyManagerImageLoader(configurationAdmin, translationProvider,
49                 httpClient, localIp, localPort, handlerFactory));
50         pages.put(SHELLY_MANAGER_URI, new ShellyManagerOverviewPage(configurationAdmin, translationProvider, httpClient,
51                 localIp, localPort, handlerFactory));
52     }
53
54     public ShellyMgrResponse generateContent(String path, Map<String, String[]> parameters) throws ShellyApiException {
55         for (Map.Entry<String, ShellyManagerPage> page : pages.entrySet()) {
56             if (path.toLowerCase().startsWith(page.getKey())) {
57                 ShellyManagerPage p = page.getValue();
58                 return p.generateContent(path, parameters);
59             }
60         }
61         return new ShellyMgrResponse("Invalid URL or syntax", HttpStatus.BAD_REQUEST_400);
62     }
63 }