-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
172 lines (165 loc) · 6.27 KB
/
server.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const uWS = require('uWebSockets.js');
const abconv = require('arraybuffer-to-string');
const axios = require('axios')
const five = require('johnny-five');
const { Servo } = require('johnny-five');
const myBoard = new five.Board({ port: 'COM4' });
const delay = ms => new Promise(res => setTimeout(res, ms));
const websocketURL = ''; // Websocket URL (example: wss://websocketurl/ws)
const port = 3000; // Port
const discordWebhook = '' // Discord Webhook URL (optional)
function getTimeStamp() {
let date = new Date();
let year = date.getFullYear();
let month0 = date.getMonth();
let month1 = month0 + 1;
let day = date.getDate();
let timeLocal = date.toLocaleTimeString();
return 'TIME: ' + timeLocal + ' • DATE: ' + month1 + '/' + day + '/' + year;
}
function sendWebook() {
let embed = [
{
title: '[Gate Opened] - ' + getTimeStamp(),
}
];
let data = JSON.stringify({ embeds: embed });
var config = {
method: 'POST',
url: discordWebhook,
headers: { 'Content-Type': 'application/json' },
data: data,
};
axios(config)
.catch((error) => {
console.log(error);
return error;
});
}
myBoard.on('ready', function () {
const app = uWS.App().ws('/ws', {
compression: 0,
maxPayloadLength: 16 * 1024 * 1024,
idleTimeout: 60,
message: (ws, data) => {
let msg = abconv(data);
if (msg == 'open') {
async function openGate() {
await console.log('Opening gate...');
const servo = new Servo(8);
await servo.to(15);
await delay(1500);
await servo.to(80);
}
openGate();
if (discordWebhook != '') {
sendWebook();
}
}
}
}).any('/*', (res, req) => {
res.end(`
<!DOCTYPE html>
<html lang="en">
<head>
<title>Gate Opener</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700" rel="stylesheet">
<style type="text/css">
body{
display: flex;
height: 75vh;
justify-content: center;
align-items: center;
background-color: #1F639C;
background-image: url() no-repeat;
font-family: Open Sans, sans-serif;
}
.button{
display: inline-block;
width: 350px;
height: 100px;
border: 2px solid #fff;
color: #fff;
font-size: 20px;
font-weight: bold;
text-transform: uppercase;
text-align: center;
text-decoration: none;
line-height: 50px;
box-sizing: border-box;
border-radius: 25px;
background-color: transparent;
outline: none;
transition: all ease 0.1s;
}
.active{
font-size: 0;
width: 100px;
height: 100px;
border-radius: 50%;
border-bottom-color: transparent;
border-left-color: transparent;
border-right-color: transparent;
animation: rotate 1s ease 0.1s infinite;
}
@keyframes rotate{
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}
.footer{
text-align: center;
color: #fff;
position: absolute;
bottom: 0;
width: 100%;
height: 2.5rem;
font-size: 10px;
}
.hr{
color: #fff;
width: 200px;
height: .005px;
}
</style>
</head>
<body>
<button class="button">Press to open gate</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".button").click(function() {
websocketMessage();
$(this).addClass("active");
setTimeout(function() {
$(".button").removeClass("active");
}, 3000);
});
});
function websocketMessage() {
if ("WebSocket" in window) {
let ws = new WebSocket("${websocketURL}");
ws.onopen = function() {
ws.send("open");
}
}
}
</script>
<footer class="footer">
<hr class="hr">
<p>Developed by: kpabellan</p>
</footer>
</body>
</html>
`);
}).listen(port, token => {
token ? console.log(`Listening to port: ${port}`) : console.log(`Failed to listen to port: ${port}`);
});
});