Skip to content

Commit

Permalink
Minor changes during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyrode committed May 30, 2019
1 parent cb04fe2 commit 106a1f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
8 changes: 2 additions & 6 deletions nodejsserver/html/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ function onOpen(evt) {
};

function onError(evt) {
content.html($('<p>', {
text: 'Sorry, but there\'s some problem with your connection or the server is down.'
}));
statust.innerHTML = "Connection Error";
};

function onMessage(message) {
Expand All @@ -68,7 +66,7 @@ function onMessage(message) {
statust.innerHTML = "Invalid Message";
return;
}
if (json.on) {
if (json.on == true) {
statust.innerHTML = "System On";
}
else {
Expand Down Expand Up @@ -163,11 +161,9 @@ function onMessage(message) {
function clickFun(channel,command){
if (connection.readyState == 1) {
connection.send(channel + ':' + command);
statust.innerHTML = "Sent";
}
else {
console.log('Got Click, connection not ready');
statust.innerHTML = 'Connection not ready';
start();
}
};
40 changes: 21 additions & 19 deletions nodejsserver/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ status.volume = [0,0,0,0,0,0];
status.mute = [0,0,0,0,0,0];
status.input = [0,0,0,0,0,0];
status.on = false;
var last_update = 0;
var last_update = 0; //Timestamp of last serial update
const old_data = Buffer.alloc(35); //Serial buffer to compair for changes
var clients = [ ]; //Who's connected
var onStatusCheck; //Interval on check
var onStatusCheck; //Interval that checks if system is still on
// HTTP server
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
Expand All @@ -43,32 +43,31 @@ wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
// we need to know client index to remove them on 'close' event
var index = clients.push(connection) - 1;
console.log((new Date()) + ' Connection accepted.');
/* console.log((new Date()) + ' Connection accepted.');
console.log('Client List: ');
for (var i = 0; i < clients.length; i++) {
console.log(clients[i].remoteAddress)
}
} */
connection.sendUTF(JSON.stringify(status)); //Send the current status to new clients
connection.on('message', function(message) {
if (message.type === 'utf8') {
var commnds = message.utf8Data.split(":");
var commnds = message.utf8Data.split(":"); //Yes, I should have used JSON
var command = parseInt(commnds[0]);
var channel = parseInt(commnds[1]);
addon.send_zpad_command_napi(channel,command);
addon.send_zpad_command_napi(channel,command); //Calls my C bitbanger
}
else {
console.log('Got non utf8 message');
}
});
// user disconnected (this doesn't get all disconnects)
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
// console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
// remove user from the list of connected clients
clients.splice(index, 1);
});
});


const port = new SerialPort('/dev/ttyAMA0', {
baudRate: 19200
})
Expand All @@ -80,10 +79,11 @@ function extractData(sdata) {
cstatus.volume = [0,0,0,0,0,0];
cstatus.mute = [0,0,0,0,0,0];
cstatus.input = [0,0,0,0,0,0];
cstatus.on = true; //We're on if we're extrcting data
for (var i = 0; i < 6; i++) {
cstatus.volume[i] = 48 - sdata[i*6+2] & 0b00111111; //Volume is sent as a 6-bit attenuation value in LSBs
cstatus.mute[i] = (sdata[i*6] & 0b00010000) >>> 4; // Mute is just a bit
cstatus.input[i] = sdata[i*6] & 0b00000111; // Mute is just a bit
cstatus.input[i] = sdata[i*6] & 0b00000111; // Status is 3 LSBs
}
return cstatus;
}
Expand All @@ -100,42 +100,44 @@ function diffData(data1,data2) {

function onDiffData(sdata) {
last_update = Date.now(); //We are getting serial data, note the time
if (!status.on) {
status.on = true; //We're on now!
if (status.on == false) { //If we have differing data, we turned on!
status.on = true; // We're on now!
onStatusCheck = setInterval(isOn,2000); // Check to see if we're off every 2s
}
if (old_data.compare(sdata) != 0) { //If the serial data changes (doesn't mean status has changed)
var new_data = extractData(sdata); //Get the new status
if (diffData(new_data,status)) { //If new status is different
if (old_data.compare(sdata) != 0) { // If the serial data changes (doesn't mean status has changed)
var new_data = extractData(sdata); // Get the new status
if (diffData(new_data,status)) { // If new status is different
status = new_data; //Store new status
updateClients();
}
sdata.copy(old_data); // Save the serial data for compairson
}
}

setInterval(updateClients,10000); // Force an update every 10s, will also help cull clients

function updateClients() {
var json = JSON.stringify(status); // encode new status
for (var i=0; i < clients.length; i++) { //Send the status to clients
if (clients[i].connected) { //only if client is connected
clients[i].sendUTF(json);
}
else {
console.log((new Date()) + " Peer " + clients[i].remoteAddress + " culled");
//console.log((new Date()) + " Peer " + clients[i].remoteAddress + " culled");
clients.splice(i, 1); // Remove the client
console.log('Client List: ');
/*console.log('Client List: ');
for (var i = 0; i < clients.length; i++) {
console.log(clients[i].remoteAddress);
}
} */
}
}
}

parser.on('data', function(data) {onDiffData(data);});

function isOn() {
if (Date.now() - last_update > 2000) { //No serial for 2s, we off
console.log('System is Off');
if (Date.now() - last_update > 2000) { //If no serial data for 2s, we off
//console.log('System is Off');
status.volume = [0,0,0,0,0,0];
status.mute = [0,0,0,0,0,0];
status.input = [0,0,0,0,0,0];
Expand Down

0 comments on commit 106a1f8

Please sign in to comment.