From 15a80cc3d4b0c25c2dddf587b42f76eab6547617 Mon Sep 17 00:00:00 2001 From: PurpleRose Date: Sun, 17 Jan 2021 22:52:34 +0000 Subject: [PATCH] Upload files to 'MyServer' --- MyServer/Dockerfile | 6 +++++ MyServer/package.json | 18 ++++++++++++++ MyServer/server.js | 55 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 MyServer/Dockerfile create mode 100644 MyServer/package.json create mode 100644 MyServer/server.js diff --git a/MyServer/Dockerfile b/MyServer/Dockerfile new file mode 100644 index 0000000..86635c5 --- /dev/null +++ b/MyServer/Dockerfile @@ -0,0 +1,6 @@ +FROM node:latest +WORKDIR /app +COPY package.json /app +RUN npm install +COPY server.js /app +CMD ["npm","start"] \ No newline at end of file diff --git a/MyServer/package.json b/MyServer/package.json new file mode 100644 index 0000000..edc86d5 --- /dev/null +++ b/MyServer/package.json @@ -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" + } +} \ No newline at end of file diff --git a/MyServer/server.js b/MyServer/server.js new file mode 100644 index 0000000..4a07e46 --- /dev/null +++ b/MyServer/server.js @@ -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'); + }); + }) + }); \ No newline at end of file