+package kaka.cakelight;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class Configuration {
+ private List<Map.Entry<String, String>> settings = new ArrayList<>();
+ public VideoConfiguration video;
+ public LedConfiguration leds;
+
+ private Configuration(Properties prop) {
+ video = new VideoConfiguration(prop);
+ leds = new LedConfiguration(prop);
+ }
+
+ public static Configuration from(String propertiesFile) {
+ InputStream input = null;
+ try {
+ input = new FileInputStream(propertiesFile);
+ Properties prop = new Properties();
+ prop.load(input);
+ return new Configuration(prop);
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ } finally {
+ if (input != null) {
+ try {
+ input.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return null;
+ }
+
+ private String get(Properties prop, String name) {
+ return addSetting(name, prop.getProperty(name));
+ }
+
+ private String get(Properties prop, String name, String dflt) {
+ return addSetting(name, prop.getProperty(name, dflt));
+ }
+
+ private String addSetting(String name, String value) {
+ settings.add(new AbstractMap.SimpleEntry<>(name, value));
+ return value;
+ }
+
+ @Override
+ public String toString() {
+ return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
+ }
+
+ public class VideoConfiguration {
+ public String device;
+ public int width;
+ public int height;
+ public int bpp;
+
+ private VideoConfiguration(Properties prop) {
+ device = get(prop, "video.device", "/dev/video0");
+ width = Integer.parseInt(get(prop, "video.width", "720"));
+ height = Integer.parseInt(get(prop, "video.height", "576"));
+ bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
+ }
+ }
+
+ public class LedConfiguration {
+ public int cols;
+ public int rows;
+
+ private LedConfiguration(Properties prop) {
+ cols = Integer.parseInt(get(prop, "leds.cols"));
+ rows = Integer.parseInt(get(prop, "leds.rows"));
+ }
+ }
+}