]> git.basschouten.com Git - openhab-addons.git/blob
a410e2bbc9c7adba0bd4c073f48687515c3fc054
[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.voice.pollytts.internal.cloudapi;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 /**
21  * This class implements the PollyTTS configuration.
22  *
23  * @author Robert Hillman - Initial contribution
24  */
25 public class PollyTTSConfig {
26
27     private static final String ACCESS_KEY = "accessKey";
28     private static final String SECRET_KEY = "secretKey";
29     private static final String SERVICE_REGION = "serviceRegion";
30     private static final String AUDIO_FORMAT = "audioFormat";
31     private static final String CACHE_EXPIRATION = "cacheExpiration";
32
33     private String accessKey = "";
34     private String secretKey = "";
35     private String serviceRegion = "eu-west-1";
36     private int expireDate = 0;
37     private String audioFormat = "default";
38     private long lastDelete;
39
40     public PollyTTSConfig(Map<String, Object> config) {
41         assertValidConfig(config);
42
43         accessKey = config.getOrDefault(ACCESS_KEY, accessKey).toString();
44         secretKey = config.getOrDefault(SECRET_KEY, secretKey).toString();
45         serviceRegion = config.getOrDefault(SERVICE_REGION, serviceRegion).toString();
46         audioFormat = config.getOrDefault(AUDIO_FORMAT, audioFormat).toString();
47         expireDate = (int) Double
48                 .parseDouble(config.getOrDefault(CACHE_EXPIRATION, Double.toString(expireDate)).toString());
49     }
50
51     private void assertValidConfig(Map<String, Object> config) {
52         List<String> emptyParams = Stream.of(ACCESS_KEY, SECRET_KEY, SERVICE_REGION)
53                 .filter(param -> config.get(param) == null || config.get(param).toString().isEmpty())
54                 .collect(Collectors.toList());
55
56         if (!emptyParams.isEmpty()) {
57             throw new IllegalArgumentException(
58                     "Missing configuration parameters: " + emptyParams.stream().collect(Collectors.joining(", ")));
59         }
60     }
61
62     public String getAccessKey() {
63         return accessKey;
64     }
65
66     public String getSecretKey() {
67         return secretKey;
68     }
69
70     public String getServiceRegion() {
71         return serviceRegion;
72     }
73
74     /**
75      * get the life time for cache files
76      */
77     public int getExpireDate() {
78         return expireDate;
79     }
80
81     /**
82      * returns audio format specified for audio
83      */
84     public String getAudioFormat() {
85         return audioFormat;
86     }
87
88     /**
89      * get the date when cache was cleaned last
90      */
91     public long getLastDelete() {
92         return lastDelete;
93     }
94
95     /**
96      * set the date when cache was cleaned last
97      */
98     public void setLastDelete(long lastDelete) {
99         this.lastDelete = lastDelete;
100     }
101
102     @Override
103     public String toString() {
104         StringBuilder builder = new StringBuilder();
105         builder.append("PollyTTSConfig [accessKey=").append(accessKey).append(", secretKey=").append(secretKey)
106                 .append(", serviceRegion=").append(serviceRegion).append(", expireDate=").append(expireDate)
107                 .append(", audioFormat=").append(audioFormat).append(", lastDelete=").append(lastDelete).append("]");
108         return builder.toString();
109     }
110 }