30308e2c1615dc6c17fd531a0b6af1dccdb864a3
[kaka/cakelight.git] / src / kaka / cakelight / Configuration.java
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         public CropConfiguration crop;
64
65         private VideoConfiguration(Properties prop) {
66             device = get(prop, "video.device", "/dev/video0");
67             width = Integer.parseInt(get(prop, "video.width", "720"));
68             height = Integer.parseInt(get(prop, "video.height", "576"));
69             bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
70             crop = new CropConfiguration(prop);
71         }
72
73         public class CropConfiguration {
74             public int left, right, top, bottom;
75
76             private CropConfiguration(Properties prop) {
77                 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
78                 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
79                 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
80                 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
81             }
82         }
83     }
84
85     public class LedConfiguration {
86         public int cols;
87         public int rows;
88
89         private LedConfiguration(Properties prop) {
90             cols = Integer.parseInt(get(prop, "leds.cols"));
91             rows = Integer.parseInt(get(prop, "leds.rows"));
92         }
93     }
94 }