Skip to content

Commit cd42b8f

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

File tree

5 files changed

+337
-122
lines changed

5 files changed

+337
-122
lines changed

topics/sw_concepts/code/abstractions.py

-120
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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({"PMD.CognitiveComplexity", "PMD.CyclomaticComplexity", "PMD.SystemPrintln", "PMD.AvoidPrintStackTrace"})
37+
public static void main(String[] args) throws IOException {
38+
if (args.length != 3) {
39+
throw new IllegalArgumentException("Wrong number of arguments");
40+
}
41+
42+
String path = args[0];
43+
String inputPath = args[1];
44+
String outputPath = args[2];
45+
boolean opt3001 = Files.isSymbolicLink(Paths.get(inputPath)) &&
46+
Files.readSymbolicLink(Paths.get(inputPath)).toString().contains("in_illuminance_input");
47+
48+
int inputMax = INPUT_MAX_TSL2550;
49+
int inputLastValue = INPUT_MIN - INPUT_THRESHOLD;
50+
Integer outputLastValue = null;
51+
52+
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
53+
int outputMax = Integer.parseInt(reader.readLine().trim());
54+
int outputMin = (int) Math.ceil(outputMax * OUTPUT_MIN_FACTOR);
55+
int outputChangeMax = (int) Math.ceil(outputMax * OUTPUT_CHANGE_MAX_FACTOR);
56+
57+
while (true) {
58+
try {
59+
int inputValue;
60+
if (opt3001) {
61+
try (BufferedReader inputReader = new BufferedReader(new FileReader(inputPath))) {
62+
inputValue = (int) Double.parseDouble(inputReader.readLine().trim());
63+
} catch (IOException e) {
64+
inputValue = INPUT_MIN;
65+
}
66+
} else {
67+
try (BufferedReader inputReader = new BufferedReader(new FileReader(inputPath))) {
68+
inputValue = Integer.parseInt(inputReader.readLine().trim());
69+
}
70+
if (0 <= inputValue && inputValue < SENSOR_VALUE_LUX_APPROX_MAP.length) {
71+
inputValue = SENSOR_VALUE_LUX_APPROX_MAP[inputValue];
72+
} else {
73+
inputValue = inputLastValue;
74+
}
75+
}
76+
77+
inputValue = Math.min(inputValue, inputMax);
78+
79+
if (Math.abs(inputValue - inputLastValue) < INPUT_THRESHOLD) {
80+
inputValue = inputLastValue;
81+
}
82+
83+
double a = (inputValue - INPUT_MIN) / (double) (inputMax - INPUT_MIN);
84+
int value1 = (int) (a * (outputMax - outputMin) + outputMin);
85+
int outputValue = Math.min(value1, outputMax);
86+
87+
if (outputLastValue == null) {
88+
outputValue = outputValue;
89+
} else if (outputValue >= outputLastValue) {
90+
outputValue = Math.min(outputValue, outputLastValue + outputChangeMax);
91+
} else {
92+
outputValue = Math.max(outputValue, outputLastValue - outputChangeMax);
93+
}
94+
int dimmedValue = outputValue;
95+
96+
if (!Objects.equals(outputValue, outputLastValue)) {
97+
System.out.printf("input: %4d (%4.1f%%), output: %4d (%4.1f%%), dimmed: %4d (%4.1f%%)%n",
98+
inputValue, 100 * (inputValue - INPUT_MIN) / (double) (inputMax - INPUT_MIN),
99+
outputValue, 100 * (outputValue - outputMin) / (double) (outputMax - outputMin),
100+
dimmedValue, 100 * (dimmedValue - outputMin) / (double) (outputMax - outputMin));
101+
System.out.flush();
102+
}
103+
104+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
105+
outputLastValue = outputValue;
106+
writer.write(String.valueOf(outputValue));
107+
}
108+
109+
inputLastValue = inputValue;
110+
} catch (IOException e) {
111+
if (!(e instanceof FileNotFoundException)) {
112+
throw e;
113+
}
114+
}
115+
116+
TimeUnit.MILLISECONDS.sleep(10);
117+
}
118+
} catch (IOException | InterruptedException e) {
119+
e.printStackTrace();
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)