3 changed files with 79 additions and 0 deletions
@ -0,0 +1,6 @@ |
|||||
|
FROM node:latest |
||||
|
WORKDIR /app |
||||
|
COPY package.json /app |
||||
|
RUN npm install |
||||
|
COPY server.js /app |
||||
|
CMD ["npm","start"] |
@ -0,0 +1,18 @@ |
|||||
|
{ |
||||
|
"name": "myserver", |
||||
|
"version": "1.0.0", |
||||
|
"description": "", |
||||
|
"main": "server.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1", |
||||
|
"start": "node server.js" |
||||
|
}, |
||||
|
"author": "", |
||||
|
"license": "ISC", |
||||
|
"dependencies": { |
||||
|
"express": "^4.17.1", |
||||
|
"fs": "0.0.1-security", |
||||
|
"nodemon": "^2.0.7", |
||||
|
"socket.io": "^3.0.5" |
||||
|
} |
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
//Required modules
|
||||
|
const express = require('express'); |
||||
|
const fs = require('fs'); |
||||
|
|
||||
|
//App setup
|
||||
|
|
||||
|
//creating an instance of express server
|
||||
|
const app = express(); |
||||
|
|
||||
|
//server port
|
||||
|
const port = 4000; |
||||
|
|
||||
|
//http.createServer() method turns your computer into an HTTP server.
|
||||
|
const http = require('http').createServer(); |
||||
|
|
||||
|
//(http.Server) the server bind to...
|
||||
|
const io = require('socket.io')(http); |
||||
|
|
||||
|
//my rooms
|
||||
|
const measurementRooms = ["temp_Values", "humidity_Values", "airspeed_Values"]; |
||||
|
|
||||
|
//Server listen to...
|
||||
|
http.listen(port, () => { |
||||
|
console.log("Server is listening on localhost: " + port); |
||||
|
}); |
||||
|
|
||||
|
//Waitting for connections...
|
||||
|
io.of("/atticaRegion").on("connection", (socket) => { |
||||
|
|
||||
|
//Trigger the "join_Temp_room" event to join a room
|
||||
|
socket.on("join_Temp_room", (room) => { |
||||
|
|
||||
|
//Checking the room integrity..
|
||||
|
if (measurementRooms.includes(room)) { |
||||
|
socket.join(room); |
||||
|
return socket.emit("success", "You have successfuly joined this room!\n"); |
||||
|
} else { |
||||
|
return socket.emit("err", "Error:No room named " + room); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
//Trigger the "handleData" event to manipulate the recieved data
|
||||
|
socket.on("handleData", (data) => { |
||||
|
|
||||
|
//Convert from JSON to javascript object
|
||||
|
var sensordata = JSON.parse(data); |
||||
|
console.log(`The sensor ${sensordata.sensorName} sends the value ${sensordata.temperature}`); |
||||
|
|
||||
|
//Append the recieved data into a .txt file...
|
||||
|
fs.appendFile('tempValues.txt', `Sensor name: ${sensordata.sensorName}, Sensor value:${sensordata.temperature}\n`, function(err) { |
||||
|
if (err) return console.log(err); |
||||
|
console.log('Success write to file tempValues.txt\n'); |
||||
|
}); |
||||
|
}) |
||||
|
}); |
Loading…
Reference in new issue