Added a device listener
[kaka/cakelight.git] / src / kaka / cakelight / Configuration.java
CommitLineData
e59e98fc
TW
1package kaka.cakelight;
2
3import java.io.FileInputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.*;
7import java.util.stream.Collectors;
8
9public 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 {
e59e98fc
TW
59 public int width;
60 public int height;
61 public int bpp;
100b82fe 62 public CropConfiguration crop;
e59e98fc
TW
63
64 private VideoConfiguration(Properties prop) {
e59e98fc
TW
65 width = Integer.parseInt(get(prop, "video.width", "720"));
66 height = Integer.parseInt(get(prop, "video.height", "576"));
67 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
100b82fe
TW
68 crop = new CropConfiguration(prop);
69 }
70
71 public class CropConfiguration {
72 public int left, right, top, bottom;
73
74 private CropConfiguration(Properties prop) {
75 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
76 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
77 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
78 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
79 }
e59e98fc
TW
80 }
81 }
82
83 public class LedConfiguration {
84 public int cols;
85 public int rows;
86
87 private LedConfiguration(Properties prop) {
88 cols = Integer.parseInt(get(prop, "leds.cols"));
89 rows = Integer.parseInt(get(prop, "leds.rows"));
90 }
91 }
92}