Skip to content

Commit 6a48c19

Browse files
committed
sw_concepts: rewrite abstractions.py to java
1 parent e088b5b commit 6a48c19

File tree

5 files changed

+361
-122
lines changed

5 files changed

+361
-122
lines changed

topics/sw_concepts/code/abstractions.py

Lines changed: 0 additions & 120 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package ch.scs.jumpstart.pattern.examples;
2+
3+
import java.io.*;
4+
import java.nio.file.*;
5+
import java.util.*;
6+
import java.util.concurrent.TimeUnit;
7+
8+
public class Abstractions {
9+
10+
private static final int INPUT_MIN = 0;
11+
private static final int INPUT_MAX_TSL2550 = 544;
12+
private static final int INPUT_THRESHOLD = 11;
13+
14+
private static final double OUTPUT_MIN_FACTOR = 0.2;
15+
private static final double OUTPUT_CHANGE_MAX_FACTOR = 0.005;
16+
17+
private static final int[] SENSOR_VALUE_LUX_APPROX_MAP = {
18+
0, 1, 2, 3, 4, 5, 6, 7,
19+
8, 9, 10, 11, 12, 13, 14, 15,
20+
16, 18, 20, 22, 24, 26, 28, 30,
21+
32, 34, 36, 38, 40, 42, 44, 46,
22+
49, 53, 57, 61, 65, 69, 73, 77,
23+
81, 85, 89, 93, 97, 101, 105, 109,
24+
115, 123, 131, 139, 147, 155, 163, 171,
25+
179, 187, 195, 203, 211, 219, 227, 235,
26+
247, 263, 279, 295, 311, 327, 343, 359,
27+
375, 391, 407, 423, 439, 455, 471, 487,
28+
511, 543, 575, 607, 639, 671, 703, 735,
29+
767, 799, 831, 863, 895, 927, 959, 991,
30+
1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
31+
1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
32+
2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
33+
3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015
34+
};
35+
36+
@SuppressWarnings({
37+
"PMD.CognitiveComplexity",
38+
"PMD.CyclomaticComplexity",
39+
"PMD.SystemPrintln",
40+
"PMD.AvoidPrintStackTrace"
41+
})
42+
public static void main(String[] args) throws IOException {
43+
if (args.length != 3) {
44+
throw new IllegalArgumentException("Wrong number of arguments");
45+
}
46+
47+
String path = args[0];
48+
String inputPath = args[1];
49+
String outputPath = args[2];
50+
boolean opt3001 =
51+
Files.isSymbolicLink(Paths.get(inputPath))
52+
&& Files.readSymbolicLink(Paths.get(inputPath))
53+
.toString()
54+
.contains("in_illuminance_input");
55+
56+
int inputMax = INPUT_MAX_TSL2550;
57+
int inputLastValue = INPUT_MIN - INPUT_THRESHOLD;
58+
Integer outputLastValue = null;
59+
60+
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
61+
int outputMax = Integer.parseInt(reader.readLine().trim());
62+
int outputMin = (int) Math.ceil(outputMax * OUTPUT_MIN_FACTOR);
63+
int outputChangeMax = (int) Math.ceil(outputMax * OUTPUT_CHANGE_MAX_FACTOR);
64+
65+
while (true) {
66+
try {
67+
int inputValue;
68+
if (opt3001) {
69+
try (BufferedReader inputReader = new BufferedReader(new FileReader(inputPath))) {
70+
inputValue = (int) Double.parseDouble(inputReader.readLine().trim());
71+
} catch (IOException e) {
72+
inputValue = INPUT_MIN;
73+
}
74+
} else {
75+
try (BufferedReader inputReader = new BufferedReader(new FileReader(inputPath))) {
76+
inputValue = Integer.parseInt(inputReader.readLine().trim());
77+
}
78+
if (0 <= inputValue && inputValue < SENSOR_VALUE_LUX_APPROX_MAP.length) {
79+
inputValue = SENSOR_VALUE_LUX_APPROX_MAP[inputValue];
80+
} else {
81+
inputValue = inputLastValue;
82+
}
83+
}
84+
85+
inputValue = Math.min(inputValue, inputMax);
86+
87+
if (Math.abs(inputValue - inputLastValue) < INPUT_THRESHOLD) {
88+
inputValue = inputLastValue;
89+
}
90+
91+
double a = (inputValue - INPUT_MIN) / (double) (inputMax - INPUT_MIN);
92+
int value1 = (int) (a * (outputMax - outputMin) + outputMin);
93+
int outputValue = Math.min(value1, outputMax);
94+
95+
if (outputLastValue == null) {
96+
outputValue = outputValue;
97+
} else if (outputValue >= outputLastValue) {
98+
outputValue = Math.min(outputValue, outputLastValue + outputChangeMax);
99+
} else {
100+
outputValue = Math.max(outputValue, outputLastValue - outputChangeMax);
101+
}
102+
int dimmedValue = outputValue;
103+
104+
if (!Objects.equals(outputValue, outputLastValue)) {
105+
System.out.printf(
106+
"input: %4d (%4.1f%%), output: %4d (%4.1f%%), dimmed: %4d (%4.1f%%)%n",
107+
inputValue,
108+
100 * (inputValue - INPUT_MIN) / (double) (inputMax - INPUT_MIN),
109+
outputValue,
110+
100 * (outputValue - outputMin) / (double) (outputMax - outputMin),
111+
dimmedValue,
112+
100 * (dimmedValue - outputMin) / (double) (outputMax - outputMin));
113+
System.out.flush();
114+
}
115+
116+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
117+
outputLastValue = outputValue;
118+
writer.write(String.valueOf(outputValue));
119+
}
120+
121+
inputLastValue = inputValue;
122+
} catch (IOException e) {
123+
if (!(e instanceof FileNotFoundException)) {
124+
throw e;
125+
}
126+
}
127+
128+
TimeUnit.MILLISECONDS.sleep(10);
129+
}
130+
} catch (IOException | InterruptedException e) {
131+
e.printStackTrace();
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)