-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartHouse.java
132 lines (117 loc) · 4.6 KB
/
SmartHouse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.Arrays;
/**
* Created by matthew on 3/26/17.
*/
public class SmartHouse {
private static Neural neural;
private static boolean light1 = false;
private static boolean light2 = false;
private static boolean auto = false;
private static boolean servo = false;
public static SimpleRead ardunio;
private static long lastTime = System.nanoTime();
private static long startTime = System.nanoTime();
private static double timeMin = 0;
private static int day = 0;
private static final double timeScale = 2*60*60;
private static double timeOn = 0;
private static double timeOnC = 0;
private static double timeOff = 0;
private static double timeOffC = 0;
public static void read(byte b) {
// System.out.println((char)b);
// write((byte)('b'));
}
public static void write(byte b) {
ardunio.write(b);
}
public static void process(String s) {
// System.out.println("Got " + s);
if(s.contains("on1")) {
light1 = true;
System.out.println("Turning on light 1");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
timeOn = (timeOn*timeOnC+timeMin)/(++timeOnC);
} else if(s.contains("off1")) {
light1 = false;
System.out.println("Turning off light 1");
//neural.addData(new double[]{timeMin, day}, new double[]{0});
timeOff = (timeOff*timeOffC+timeMin)/(++timeOffC);
} else if(s.contains("on2")) {
light2 = true;
System.out.println("Turning on light 2");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
} else if(s.contains("off2")) {
light2 = false;
System.out.println("Turning off light 2");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
} else if(s.contains("onA")) {
auto = true;
System.out.println("Turning on Auto");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
} else if(s.contains("offA")) {
auto = false;
light1 = false;
System.out.println("Turning off Auto");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
} else if(s.contains("onS")) {
servo = true;
System.out.println("Turning on Servo");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
} else if(s.contains("offS")) {
servo = false;
System.out.println("Turning off Servo");
//neural.addData(new double[]{timeMin, day}, new double[]{1});
}
}
public static synchronized void start() {
//Clock c = new Clock(500, 500);
neural = new Neural(2, 1);
System.out.println("Starting smart house");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int b = 0;
while(true) {
startTime = System.nanoTime();
long deltaTime = startTime - lastTime;
timeMin += (deltaTime/1000000000d/60d)*timeScale;
if(timeMin > 24*60) {
timeMin=0;
day++;
}
if(day > 6) {
day = 0;
}
if(b++ > 1000000) {
b = 0;
// c.run((int)timeMin);
System.out.println(Math.round(timeMin/60) + ":" + Math.round(timeMin%60)); //+ " " + timeMin + " " + timeOn + " " + timeOff);
}
if (light1) {
//System.out.println("asd");
write((byte) ('b'));
}
if(auto) {
// double[] out = neural.calculate(new double[]{timeMin, day});
//System.out.println(Arrays.toString(out));
if(timeMin > timeOn && timeMin < timeOff) {
light1 = true;
} else {
light1 = false;
}
}
if (light2) {
write((byte) ('d'));
}
if(servo) {
write((byte)('f'));
}
Thread.yield();
lastTime = startTime;
}
}
});
t.start();
}
}