Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
68 lines (56 loc) · 2.46 KB
/
Copy pathMain.java
File metadata and controls
68 lines (56 loc) · 2.46 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
66
67
68
package com.beowulfe.hap.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import io.github.hapjava.server.impl.HomekitRoot;
import io.github.hapjava.server.impl.HomekitServer;
import io.github.hapjava.server.impl.crypto.HAPSetupCodeUtils;
public class Main {
private static final int PORT = 9123;
public static void main(String[] args) {
try {
File authFile = new File("auth-state.bin");
MockAuthInfo mockAuth;
if (authFile.exists()) {
FileInputStream fileInputStream = new FileInputStream(authFile);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
try {
System.out.println("Using persisted auth");
AuthState authState = (AuthState) objectInputStream.readObject();
mockAuth = new MockAuthInfo(authState);
} finally {
objectInputStream.close();
}
} else {
mockAuth = new MockAuthInfo();
}
HomekitServer homekit = new HomekitServer(PORT);
HomekitRoot bridge = homekit.createBridge(mockAuth, "Test Bridge", "TestBridge, Inc.", "G6", "111abe234", "1.1", "1.2");
String setupURI = HAPSetupCodeUtils.getSetupURI(mockAuth.getPin().replace("-",""), mockAuth.getSetupId(), 2);
QRtoConsole.printQR(setupURI);
mockAuth.onChange(state -> {
try {
System.out.println("State has changed! Writing");
FileOutputStream fileOutputStream = new FileOutputStream(authFile);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(state);
objectOutputStream.flush();
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});
bridge.addAccessory(new MockSwitch());
bridge.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("Stopping homekit server.");
homekit.stop();
}));
} catch (Exception e) {
e.printStackTrace();
}
}
}