Commit | Line | Data |
---|---|---|
e59e98fc TW |
1 | package kaka.cakelight; |
2 | ||
3 | import java.io.FileInputStream; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.util.*; | |
7 | import java.util.stream.Collectors; | |
8 | ||
9 | public class Configuration { | |
10 | private List<Map.Entry<String, String>> settings = new ArrayList<>(); | |
11 | public VideoConfiguration video; | |
12 | public LedConfiguration leds; | |
13 | ||
14 | private Configuration(Properties prop) { | |
15 | video = new VideoConfiguration(prop); | |
16 | leds = new LedConfiguration(prop); | |
17 | } | |
18 | ||
19 | public static Configuration from(String propertiesFile) { | |
20 | InputStream input = null; | |
21 | try { | |
22 | input = new FileInputStream(propertiesFile); | |
23 | Properties prop = new Properties(); | |
24 | prop.load(input); | |
25 | return new Configuration(prop); | |
26 | } catch (IOException ex) { | |
27 | ex.printStackTrace(); | |
28 | } finally { | |
29 | if (input != null) { | |
30 | try { | |
31 | input.close(); | |
32 | } catch (IOException e) { | |
33 | e.printStackTrace(); | |
34 | } | |
35 | } | |
36 | } | |
37 | return null; | |
38 | } | |
39 | ||
40 | private String get(Properties prop, String name) { | |
41 | return addSetting(name, prop.getProperty(name)); | |
42 | } | |
43 | ||
44 | private String get(Properties prop, String name, String dflt) { | |
45 | return addSetting(name, prop.getProperty(name, dflt)); | |
46 | } | |
47 | ||
48 | private String addSetting(String name, String value) { | |
49 | settings.add(new AbstractMap.SimpleEntry<>(name, value)); | |
50 | return value; | |
51 | } | |
52 | ||
53 | @Override | |
54 | public String toString() { | |
55 | return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n")); | |
56 | } | |
57 | ||
58 | public class VideoConfiguration { | |
59 | public String device; | |
60 | public int width; | |
61 | public int height; | |
62 | public int bpp; | |
63 | ||
64 | private VideoConfiguration(Properties prop) { | |
65 | device = get(prop, "video.device", "/dev/video0"); | |
66 | width = Integer.parseInt(get(prop, "video.width", "720")); | |
67 | height = Integer.parseInt(get(prop, "video.height", "576")); | |
68 | bpp = Integer.parseInt(get(prop, "video.bpp", "2")); | |
69 | } | |
70 | } | |
71 | ||
72 | public class LedConfiguration { | |
73 | public int cols; | |
74 | public int rows; | |
75 | ||
76 | private LedConfiguration(Properties prop) { | |
77 | cols = Integer.parseInt(get(prop, "leds.cols")); | |
78 | rows = Integer.parseInt(get(prop, "leds.rows")); | |
79 | } | |
80 | } | |
81 | } |