diff --git a/client/package-lock.json b/client/package-lock.json index bfeea1c..e1a617f 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -11,9 +11,11 @@ "dependencies": { "@types/node": "^14.14.7", "@types/socket.io-client": "^1.4.34", + "@types/uuid": "^8.3.0", "chalk": "^3.0.0", "nodemon": "^2.0.6", - "socket.io-client": "^3.0.0" + "socket.io-client": "^3.0.0", + "uuid": "^8.3.1" } }, "node_modules/@sindresorhus/is": { @@ -50,6 +52,11 @@ "resolved": "https://registry.npmjs.org/@types/socket.io-client/-/socket.io-client-1.4.34.tgz", "integrity": "sha512-Lzia5OTQFJZJ5R4HsEEldywiiqT9+W2rDbyHJiiTGqOcju89sCsQ8aUXDljY6Ls33wKZZGC0bfMhr/VpOyjtXg==" }, + "node_modules/@types/uuid": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.0.tgz", + "integrity": "sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==" + }, "node_modules/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -1283,6 +1290,14 @@ "node": ">=4" } }, + "node_modules/uuid": { + "version": "8.3.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.1.tgz", + "integrity": "sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/widest-line": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/widest-line/-/widest-line-3.1.0.tgz", @@ -1381,6 +1396,11 @@ "resolved": "https://registry.npmjs.org/@types/socket.io-client/-/socket.io-client-1.4.34.tgz", "integrity": "sha512-Lzia5OTQFJZJ5R4HsEEldywiiqT9+W2rDbyHJiiTGqOcju89sCsQ8aUXDljY6Ls33wKZZGC0bfMhr/VpOyjtXg==" }, + "@types/uuid": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.0.tgz", + "integrity": "sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==" + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -2331,6 +2351,11 @@ "prepend-http": "^2.0.0" } }, + "uuid": { + "version": "8.3.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.1.tgz", + "integrity": "sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg==" + }, "widest-line": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/widest-line/-/widest-line-3.1.0.tgz", diff --git a/client/package.json b/client/package.json index 046e2d9..e998441 100644 --- a/client/package.json +++ b/client/package.json @@ -8,7 +8,7 @@ "start:run": "node build/index.js", "dev": "nodemon build/index.js", "test": "concurrently npm:start:build npm:dev", - "start": "concurrently start:*" + "start": "concurrently npm:start:*" }, "repository": { "type": "git", @@ -24,8 +24,10 @@ "dependencies": { "@types/node": "^14.14.7", "@types/socket.io-client": "^1.4.34", + "@types/uuid": "^8.3.0", "chalk": "^3.0.0", "nodemon": "^2.0.6", - "socket.io-client": "^3.0.0" + "socket.io-client": "^3.0.0", + "uuid": "^8.3.1" } } diff --git a/client/src/class/ClientService.ts b/client/src/class/ClientService.ts index 5568423..1838fd2 100644 --- a/client/src/class/ClientService.ts +++ b/client/src/class/ClientService.ts @@ -2,20 +2,23 @@ import { ClientSocket } from './ClientSocket'; import { Sensor } from './Sensor'; export class ClientService { - clientSocket: ClientSocket = new ClientSocket(3000, '/'); - sensor: Sensor = new Sensor(); + private clientSocket: ClientSocket = new ClientSocket( + 'http://localhost:3000/', + 'temp' + ); + private sensor: Sensor = new Sensor(); - constructor() {} + constructor(public uuid: string) {} - tempService() { + tempService(): void { setInterval(() => { - this.clientSocket.sendTemp(this.sensor.takeMeasure); + this.clientSocket.sendTemp(this.uuid, this.sensor.takeMeasure); }, 1000); } - tempTestService() { + tempTestService(): void { setInterval(() => { - this.clientSocket.sendTemp(this.sensor.takeTestMeasure); + this.clientSocket.sendTemp(this.uuid, this.sensor.takeTestMeasure); }, 1000); } } diff --git a/client/src/class/ClientSocket.ts b/client/src/class/ClientSocket.ts index b62e02a..23f233e 100644 --- a/client/src/class/ClientSocket.ts +++ b/client/src/class/ClientSocket.ts @@ -4,17 +4,17 @@ import io from 'socket.io-client'; export class ClientSocket { private socket!: SocketIOClient.Socket; - constructor(private port: number, private path: string) { + constructor(private path: string, private eventName: string) { this.initSocket(); this.connStatus(); } - private initSocket() { - this.socket = io(`http://localhost:${this.port}${this.path}`); + private initSocket(): void { + this.socket = io(this.path); console.log(chalk.yellow('Initialized socket...')); } - private connStatus() { + private connStatus(): void { this.socket.on('connect', () => { console.log(chalk.green('Connected to server!')); }); @@ -30,14 +30,12 @@ export class ClientSocket { }); } - closeConn() { + closeConn(): void { console.log(chalk.yellow('Closing socket...')); this.socket.disconnect(); } - sendTemp(temp: number) { - this.socket.emit('temp', temp, (data: string) => { - console.log(data); - }); + sendTemp(uuid: string, temp: number): void { + this.socket.emit(this.eventName, { uuid, temp }); } } diff --git a/client/src/index.ts b/client/src/index.ts index 9fe0e03..c7537fc 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -1,3 +1,11 @@ import chalk from 'chalk'; +import { ClientService } from './class/ClientService'; +const { v4: uuidv4 } = require('uuid'); console.log(chalk.cyan('Started Anchiale Client...')); + +const uuid = uuidv4(); + +const service = new ClientService(uuid); + +service.tempTestService(); diff --git a/client/tsconfig.json b/client/tsconfig.json index 2ac0118..25286ac 100644 --- a/client/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": "./", /* Redirect output structure to the directory. */ - // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "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. */, // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */ diff --git a/server/src/class/Database.ts b/server/src/class/Database.ts index 97e3c85..0a83efd 100644 --- a/server/src/class/Database.ts +++ b/server/src/class/Database.ts @@ -1,3 +1,67 @@ -export class localdb { - constructor() {} +import { + InfluxDB, + Point, + QueryApi, + WriteApi, +} from '@influxdata/influxdb-client'; +import chalk from 'chalk'; +import { url, token, org, bucket } from '../config/creds'; + +export class Database { + private client!: InfluxDB; + private writeApi!: WriteApi; + private queryApi!: QueryApi; + + constructor() { + this.initDatabase(); + } + + private initDatabase(): void { + this.client = new InfluxDB({ url: url, token: token }); + this.writeApi = this.client.getWriteApi(org, bucket); + this.writeApi.useDefaultTags({ host: 'local' }); + this.queryApi = this.client.getQueryApi(org); + } + + write(uuid: string, temp: number) { + const point = new Point('temperature') + .tag('client', uuid) + .floatField('value', temp); + this.writeApi.writePoint(point); + this.writeApi.flush(); + } + + closeWrite() { + this.writeApi + .close() + .then(() => { + console.log(chalk.magenta('Write finished')); + }) + .catch((e) => { + console.error(e); + console.log(chalk.red('Write ERROR')); + }); + } + + query(filter: string) { + const query = `from(bucket: "${bucket}") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "${filter}")`; + + this.queryApi.queryRows(query, { + next(row, tableMeta) { + const o = tableMeta.toObject(row); + console.log( + chalk.cyan( + `On ${o._time} took ${o._measurement} of ${o._field}=${o._value}` + ) + ); + }, + error(error) { + console.error(error); + console.log(chalk.red('Query ERROR')); + }, + complete() { + console.log(chalk.magenta('Query finished')); + }, + }); + } } diff --git a/server/src/class/ServerService.ts b/server/src/class/ServerService.ts new file mode 100644 index 0000000..986719c --- /dev/null +++ b/server/src/class/ServerService.ts @@ -0,0 +1,16 @@ +import { Database } from './Database'; +import { ServerSocket } from './ServerSocket'; + +export class ServerService { + private database: Database = new Database(); + private serverSocket: ServerSocket = new ServerSocket( + '/', + 3000, + 'temp', + this.database + ); + + constructor() { + // this.database.query('temperature'); + } +} diff --git a/server/src/class/ServerSocket.ts b/server/src/class/ServerSocket.ts index 0da834f..b1fd355 100644 --- a/server/src/class/ServerSocket.ts +++ b/server/src/class/ServerSocket.ts @@ -1,26 +1,33 @@ import chalk from 'chalk'; import { Server } from 'socket.io'; +import { Database } from './Database'; -interface DatabaseHas { - push(temp: number): void; +interface packet { + uuid: string; + temp: number; } -export class ServerSocket { +export class ServerSocket { private io!: Server; - constructor(private path: string, private port: number, private database: T) { + constructor( + private path: string, + private port: number, + private eventName: string, + private database: Database + ) { this.initSocket(); this.connStatus(); } - private initSocket() { + private initSocket(): void { this.io = new Server(this.port, { path: this.path, }); console.log(chalk.yellow('Initialized server...')); } - private connStatus() { + private connStatus(): void { this.io.on('connect', (socket) => { console.log(chalk.green('Client connected!')); @@ -28,15 +35,16 @@ export class ServerSocket { console.log(chalk.red('Client disconected!')); }); - socket.on('temp', (temp: number) => { - this.database.push(temp); + socket.on(this.eventName, (data: packet) => { + const { uuid, temp } = data; + this.database.write(uuid, temp); }); }); } - closeSocket() { - this.io.close(() => { - console.log(chalk.red('Closing socket...')); - }); + closeSocket(): void { + console.log(chalk.red('Closing socket...')); + this.database.closeWrite(); + this.io.close(); } } diff --git a/server/src/index.ts b/server/src/index.ts index 0481f01..0e21d3d 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,3 +1,8 @@ import chalk from 'chalk'; +import { ServerService } from './class/ServerService'; console.log(chalk.cyan('Started Anchiale Server...')); + +const service = new ServerService(); + +// service.; diff --git a/server/tsconfig.json b/server/tsconfig.json index 2ac0118..25286ac 100644 --- a/server/tsconfig.json +++ b/server/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": "./", /* Redirect output structure to the directory. */ - // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "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. */, // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */