From c162eb5586499df872897ccd000db139cdfc0989 Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Tue, 10 Nov 2020 16:15:19 +0200 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=91=95=20Change=20file=20structure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- broker/src/index.ts | 6 ------ {sensor => client}/package-lock.json | 0 {sensor => client}/package.json | 6 ++++-- client/src/class/ClientService.ts | 21 +++++++++++++++++++ .../src/class}/ClientSocket.ts | 15 +++++++++++-- client/src/class/Sensor.ts | 11 ++++++++++ client/src/index.ts | 3 +++ {sensor => client}/tsconfig.json | 4 ++-- sensor/src/index.ts | 9 -------- {broker => server}/package-lock.json | 11 ++++++++++ {broker => server}/package.json | 7 +++++-- server/src/class/Database.ts | 3 +++ .../src/class}/ServerSocket.ts | 20 +++++++++++++++--- server/src/index.ts | 3 +++ {broker => server}/tsconfig.json | 8 +++---- 15 files changed, 97 insertions(+), 30 deletions(-) delete mode 100644 broker/src/index.ts rename {sensor => client}/package-lock.json (100%) rename {sensor => client}/package.json (79%) create mode 100644 client/src/class/ClientService.ts rename {sensor/src/controllers => client/src/class}/ClientSocket.ts (66%) create mode 100644 client/src/class/Sensor.ts create mode 100644 client/src/index.ts rename {sensor => client}/tsconfig.json (95%) delete mode 100644 sensor/src/index.ts rename {broker => server}/package-lock.json (99%) rename {broker => server}/package.json (75%) create mode 100644 server/src/class/Database.ts rename {broker/src/controllers => server/src/class}/ServerSocket.ts (52%) create mode 100644 server/src/index.ts rename {broker => server}/tsconfig.json (91%) diff --git a/broker/src/index.ts b/broker/src/index.ts deleted file mode 100644 index 08d081f..0000000 --- a/broker/src/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -import chalk from 'chalk'; -import { ServerSocket } from './controllers/ServerSocket'; - -console.log(chalk.cyan('Started Anchiale broker...')); - -const socket = new ServerSocket('/', 3000); diff --git a/sensor/package-lock.json b/client/package-lock.json similarity index 100% rename from sensor/package-lock.json rename to client/package-lock.json diff --git a/sensor/package.json b/client/package.json similarity index 79% rename from sensor/package.json rename to client/package.json index 00d1226..046e2d9 100644 --- a/sensor/package.json +++ b/client/package.json @@ -5,8 +5,10 @@ "main": "index.js", "scripts": { "start:build": "tsc -w", - "start:run": "nodemon build/index.js", - "start": "concurrently npm:start:*" + "start:run": "node build/index.js", + "dev": "nodemon build/index.js", + "test": "concurrently npm:start:build npm:dev", + "start": "concurrently start:*" }, "repository": { "type": "git", diff --git a/client/src/class/ClientService.ts b/client/src/class/ClientService.ts new file mode 100644 index 0000000..5568423 --- /dev/null +++ b/client/src/class/ClientService.ts @@ -0,0 +1,21 @@ +import { ClientSocket } from './ClientSocket'; +import { Sensor } from './Sensor'; + +export class ClientService { + clientSocket: ClientSocket = new ClientSocket(3000, '/'); + sensor: Sensor = new Sensor(); + + constructor() {} + + tempService() { + setInterval(() => { + this.clientSocket.sendTemp(this.sensor.takeMeasure); + }, 1000); + } + + tempTestService() { + setInterval(() => { + this.clientSocket.sendTemp(this.sensor.takeTestMeasure); + }, 1000); + } +} diff --git a/sensor/src/controllers/ClientSocket.ts b/client/src/class/ClientSocket.ts similarity index 66% rename from sensor/src/controllers/ClientSocket.ts rename to client/src/class/ClientSocket.ts index f88cc94..b62e02a 100644 --- a/sensor/src/controllers/ClientSocket.ts +++ b/client/src/class/ClientSocket.ts @@ -4,7 +4,7 @@ import io from 'socket.io-client'; export class ClientSocket { private socket!: SocketIOClient.Socket; - constructor(private path: string, private port: number) { + constructor(private port: number, private path: string) { this.initSocket(); this.connStatus(); } @@ -16,7 +16,7 @@ export class ClientSocket { private connStatus() { this.socket.on('connect', () => { - console.log(chalk.magenta('Connected to server!')); + console.log(chalk.green('Connected to server!')); }); this.socket.on('disconnect', (reason: string) => { @@ -29,4 +29,15 @@ export class ClientSocket { console.log(chalk.yellow('Reconecting...')); }); } + + closeConn() { + console.log(chalk.yellow('Closing socket...')); + this.socket.disconnect(); + } + + sendTemp(temp: number) { + this.socket.emit('temp', temp, (data: string) => { + console.log(data); + }); + } } diff --git a/client/src/class/Sensor.ts b/client/src/class/Sensor.ts new file mode 100644 index 0000000..96319e5 --- /dev/null +++ b/client/src/class/Sensor.ts @@ -0,0 +1,11 @@ +export class Sensor { + constructor() {} + + get takeMeasure(): number { + return 0; + } + + get takeTestMeasure(): number { + return Math.floor(Math.random() * 50); + } +} diff --git a/client/src/index.ts b/client/src/index.ts new file mode 100644 index 0000000..9fe0e03 --- /dev/null +++ b/client/src/index.ts @@ -0,0 +1,3 @@ +import chalk from 'chalk'; + +console.log(chalk.cyan('Started Anchiale Client...')); diff --git a/sensor/tsconfig.json b/client/tsconfig.json similarity index 95% rename from sensor/tsconfig.json rename to client/tsconfig.json index 25286ac..2ac0118 100644 --- a/sensor/tsconfig.json +++ b/client/tsconfig.json @@ -14,8 +14,8 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./build" /* Redirect output structure to the directory. */, - "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + // "outDir": "./", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */ diff --git a/sensor/src/index.ts b/sensor/src/index.ts deleted file mode 100644 index 6ecf1f6..0000000 --- a/sensor/src/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import chalk from 'chalk'; -import { ClientSocket } from './controllers/ClientSocket'; -import { Sensor } from './controllers/Sensor'; - -console.log(chalk.cyan('Started Anchiale measure...')); - -const socket = new ClientSocket('/', 3000); - -const sensor = new Sensor(); diff --git a/broker/package-lock.json b/server/package-lock.json similarity index 99% rename from broker/package-lock.json rename to server/package-lock.json index 7c4cf5a..8d7228c 100644 --- a/broker/package-lock.json +++ b/server/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@influxdata/influxdb-client": "^1.8.0", "@types/node": "^14.14.7", "@types/socket.io": "^2.1.11", "chalk": "^3.0.0", @@ -16,6 +17,11 @@ "socket.io": "^3.0.1" } }, + "node_modules/@influxdata/influxdb-client": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/@influxdata/influxdb-client/-/influxdb-client-1.8.0.tgz", + "integrity": "sha512-fygvCHBUrcuduMRZf1oGhQzvIJG6gEA+0JX4ffxq5cxAeHKUGCYt30GafBc23QyrnCc50zc0IsPSG2Tw47aw+w==" + }, "node_modules/@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -1385,6 +1391,11 @@ } }, "dependencies": { + "@influxdata/influxdb-client": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/@influxdata/influxdb-client/-/influxdb-client-1.8.0.tgz", + "integrity": "sha512-fygvCHBUrcuduMRZf1oGhQzvIJG6gEA+0JX4ffxq5cxAeHKUGCYt30GafBc23QyrnCc50zc0IsPSG2Tw47aw+w==" + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", diff --git a/broker/package.json b/server/package.json similarity index 75% rename from broker/package.json rename to server/package.json index 289a3cd..3e20214 100644 --- a/broker/package.json +++ b/server/package.json @@ -5,8 +5,10 @@ "main": "index.js", "scripts": { "start:build": "tsc -w", - "start:run": "nodemon build/index.js", - "start": "concurrently npm:start:*" + "start:run": "node build/index.js", + "dev": "nodemon build/index.js", + "test": "concurrently npm:start:build npm:dev", + "start": "concurrently start:*" }, "repository": { "type": "git", @@ -20,6 +22,7 @@ }, "homepage": "https://github.com/xrazis/Anchiale-pi#readme", "dependencies": { + "@influxdata/influxdb-client": "^1.8.0", "@types/node": "^14.14.7", "@types/socket.io": "^2.1.11", "chalk": "^3.0.0", diff --git a/server/src/class/Database.ts b/server/src/class/Database.ts new file mode 100644 index 0000000..97e3c85 --- /dev/null +++ b/server/src/class/Database.ts @@ -0,0 +1,3 @@ +export class localdb { + constructor() {} +} diff --git a/broker/src/controllers/ServerSocket.ts b/server/src/class/ServerSocket.ts similarity index 52% rename from broker/src/controllers/ServerSocket.ts rename to server/src/class/ServerSocket.ts index 138477b..0da834f 100644 --- a/broker/src/controllers/ServerSocket.ts +++ b/server/src/class/ServerSocket.ts @@ -1,10 +1,14 @@ import chalk from 'chalk'; import { Server } from 'socket.io'; -export class ServerSocket { +interface DatabaseHas { + push(temp: number): void; +} + +export class ServerSocket { private io!: Server; - constructor(private path: string, private port: number) { + constructor(private path: string, private port: number, private database: T) { this.initSocket(); this.connStatus(); } @@ -18,11 +22,21 @@ export class ServerSocket { private connStatus() { this.io.on('connect', (socket) => { - console.log(chalk.magenta('Client connected!')); + console.log(chalk.green('Client connected!')); socket.on('disconnect', () => { console.log(chalk.red('Client disconected!')); }); + + socket.on('temp', (temp: number) => { + this.database.push(temp); + }); + }); + } + + closeSocket() { + this.io.close(() => { + console.log(chalk.red('Closing socket...')); }); } } diff --git a/server/src/index.ts b/server/src/index.ts new file mode 100644 index 0000000..0481f01 --- /dev/null +++ b/server/src/index.ts @@ -0,0 +1,3 @@ +import chalk from 'chalk'; + +console.log(chalk.cyan('Started Anchiale Server...')); diff --git a/broker/tsconfig.json b/server/tsconfig.json similarity index 91% rename from broker/tsconfig.json rename to server/tsconfig.json index 4717be4..2ac0118 100644 --- a/broker/tsconfig.json +++ b/server/tsconfig.json @@ -4,7 +4,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, + "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ @@ -14,8 +14,8 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./build" /* Redirect output structure to the directory. */, - "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + // "outDir": "./", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */ @@ -47,7 +47,7 @@ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ - "allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */, + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */