Repository navigation
Expand file tree
/
Copy pathMainActivity.java
More file actions
65 lines (52 loc) · 1.83 KB
/
Copy pathMainActivity.java
File metadata and controls
65 lines (52 loc) · 1.83 KB
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
package atownsend.androidthingsfirebase;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import atownsend.androidthingsfirebase.peripheral.SimpleLed;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String RED_PIN = "BCM4";
private static final String BLUE_PIN = "BCM6";
private static final String GREEN_PIN = "BCM16";
private SimpleLed red;
private SimpleLed blue;
private SimpleLed green;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
red = new SimpleLed(RED_PIN, SimpleLed.InitialState.OFF);
blue = new SimpleLed(BLUE_PIN, SimpleLed.InitialState.OFF);
green = new SimpleLed(GREEN_PIN, SimpleLed.InitialState.OFF);
} catch (Exception e) {
Log.e(TAG, "Error opening LEDs", e);
}
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("lighting_system");
DatabaseReference redRef = ref.child("red_on");
DatabaseReference blueRef = ref.child("blue_on");
DatabaseReference greenRef = ref.child("green_on");
redRef.addValueEventListener(new LedValueEventListener(red));
blueRef.addValueEventListener(new LedValueEventListener(blue));
greenRef.addValueEventListener(new LedValueEventListener(green));
}
@Override protected void onDestroy() {
super.onDestroy();
try {
red.close();
} catch (Exception ignored) {
red = null;
}
try {
blue.close();
} catch (Exception ignored) {
blue = null;
}
try {
green.close();
} catch (Exception ignored) {
green = null;
}
}
}