Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions public/js/doorbell.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var socket = io();
const socket = io();

//Change socket to the below when not running locally
//var socket = io.connect('http://example.com');
window.addEventListener("load", function () {
var buttonState = document.getElementById("button");
const buttonState = document.getElementById("button");
socket.emit("init");
button.addEventListener("click", function () {
socket.emit("check", Number(this.value));
});


var checkbox = document.getElementsByClassName("checkbox")[0];
const checkbox = document.getElementsByClassName("checkbox")[0];
checkbox.addEventListener("click", function () {
console.log("IN");
if(checkbox.checked) {
Expand Down
16 changes: 5 additions & 11 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
html {
font-size: 10px;
font-family: "Comic Sans MS", cursive, sans-serif;
background-color: #FFFFFF;
}

h1 {
font-size: 60px;
text-align: center;
margin: 0;
padding: 20px 0;
color: #00539F;
}

p, li {
font-size: 20px;
font-size: 20px;
line-height: 2;
letter-spacing: 1px;
}

html {
background-color: #FFFFFF;
}

body {
width: 600px;
height: auto;
Expand All @@ -27,12 +27,6 @@ body {
border: 5px solid black;
}

h1 {
margin: 0;
padding: 20px 0;
color: #00539F;
}

div {
font-size: 14px;
}
Expand Down
52 changes: 41 additions & 11 deletions webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
if(process.env.DOORBELL_PORT) {
console.log(`DOORBELL_PORT set from env variable: ${process.env.DOORBELL_PORT}`)
}
const port = process.env.DOORBELL_PORT || 8080;
let port = process.env.DOORBELL_PORT || 8080;

const { ArgumentParser } = require('argparse');
const { version } = require('./package.json');
Expand All @@ -29,41 +29,71 @@ var io = require('socket.io')(server);
var path = require('path');
var fs = require('fs')

// Configuration constants
const DOORBELL_STATES = {
AVAILABLE: 'green',
TRIGGERED: 'red'
};
const DOORBELL_TRIGGER_DURATION_MS = 10000;

const dirPath = path.join(__dirname, '/public');
app.use(express.static(dirPath));

console.log(`Server running on port ${port}`);

var state = "green";
var state = DOORBELL_STATES.AVAILABLE;

app.get('/', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});

io.on('connection', function (socket) {


socket.on('error', function(error) {
// Handle socket errors
});

socket.on('disconnect', function(reason) {
// Handle disconnections
});

socket.on('init', function(){
if(state==="red"){
if(state===DOORBELL_STATES.TRIGGERED){
io.emit('init');
socket.broadcast.emit('init');
}
})

socket.on('check', function(value) {
if (state === "green" && value === 1) {
// Validate input
if (typeof value !== 'number' || (value !== 0 && value !== 1)) {
console.warn('Invalid check event value:', value);
return;
}

if (state === DOORBELL_STATES.AVAILABLE && value === 1) {
//console.log("change:" + value);
state = "red";
state = DOORBELL_STATES.TRIGGERED;
fs.readdir(dirPath + "/audio", function(err, files){
if(err) {
console.error('Failed to read audio directory:', err);
state = DOORBELL_STATES.AVAILABLE;
return;
}

if(!files || files.length === 0) {
console.warn('No audio files found in audio directory');
state = DOORBELL_STATES.AVAILABLE;
return;
}

let randomSound = files[Math.floor(Math.random() * files.length)];
//console.log(randomSound);
io.emit('change', 0, randomSound);
socket.broadcast.emit('change', 0, randomSound);
setTimeout(function(){
state = "green";
state = DOORBELL_STATES.AVAILABLE;
//console.log(state);
io.emit('change', 1);
socket.broadcast.emit('change', 1);
}, 10000);
}, DOORBELL_TRIGGER_DURATION_MS);
});
}
});
Expand Down