-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
152 lines (138 loc) · 3.59 KB
/
main.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const express = require("express");
const path = require("path");
const app = express();
app.use(express.static(__dirname + "/build"));
app.get("*", function (request, response) {
response.sendFile(path.resolve(__dirname, "build/index.html"));
});
app.listen("80");
console.log("working on 80");
const server = require("http").createServer();
const io = require("socket.io")(server, { cors: { origin: "*" } });
io.on("connection", (socket) => {
console.log("Client has connected.");
socket.on("event", (data, callback) => {
const payload = JSON.parse(data);
console.log("payload", payload);
const res = processMsg(payload, socket);
callback(JSON.stringify(res));
});
socket.on("disconnect", () => {
console.log("Client has disconnected.");
});
});
server.listen(9898, () => {
console.log("started");
});
const Gpio = require("onoff").Gpio;
const processMsg = async (msg, socket) => {
let res = {
...msg,
message: { success: false, error: "" },
};
console.log("processMsg", msg);
if (msg?.command === "auth") {
if (msg?.action === "login") {
const output = await login(msg.message);
if (output.success) {
res.message.success = true;
}
}
} else if (msg?.command === "gpio") {
if (msg?.action === "setup") {
const { gpioEdgeDetectDirection, gpioPin } = msg.message;
const interrupt = new Gpio(gpioPin, "in", gpioEdgeDetectDirection);
try {
interrupt.watch((err, value) => {
if (err) {
throw err;
}
const marker = `gpio-${gpioEdgeDetectDirection}-${value}`;
console.log("marker", marker);
crown.addMarker(marker);
console.log("added marker");
});
} catch (e) {
console.log(e.message);
}
}
} else if (msg?.command === "websock") {
if (msg?.action === "end") {
if (crown) {
crown.logout();
crown = null;
}
}
}
return res;
};
const { Notion } = require("@neurosity/notion");
let crown = null;
const login = async (creds) => {
const { email, password } = creds;
const verification = verifyEnvs(email, password);
if (verification === null) {
console.log(email, password);
crown = new Notion({
timesync: true,
});
await crown
.login({
email,
password,
})
.catch((error) => {
console.log(error);
throw new Error(error);
});
console.log("Logged in");
crown.getInfo().then(console.log);
return {
success: true,
error: "",
};
}
return {
success: false,
error: verification,
};
};
const verifyEnvs = (email, password) => {
const invalidEnv = (env) => {
return env === "" || env === 0;
};
if (invalidEnv(email) || invalidEnv(password)) {
return "Please verify email and password";
}
return null;
};
// const gpioSetup = (config) => {
// const { gpioEdgeDetectDirection, gpioPin } = config;
// const interrupt = new Gpio(gpioPin, "in", gpioEdgeDetectDirection);
// // const interrupt = new Gpio(2, "in", "falling");
// try {
// interrupt.watch((err, value) => {
// if (err) {
// throw err;
// }
// const marker = `gpio-${gpioEdgeDetectDirection}-${value}`;
// crown.addMarker(marker);
// console.log("marker", marker);
// });
// return {
// success: true,
// error: "",
// };
// } catch (error) {
// return {
// success: false,
// error: error.msg,
// };
// }
// };
process.on("SIGTERM", () => {
if (crown) {
crown.logout();
console.log("Killed crown");
}
});