diff --git a/.gitignore b/.gitignore index e08672c..c909884 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ build/ -creds.ts # Logs logs diff --git a/client/src/.env.template b/client/src/.env.template new file mode 100644 index 0000000..fadb003 --- /dev/null +++ b/client/src/.env.template @@ -0,0 +1 @@ +SERVER_URL = XXXX \ No newline at end of file diff --git a/client/src/class/ClientService.ts b/client/src/class/ClientService.ts index 634c789..0703c1c 100644 --- a/client/src/class/ClientService.ts +++ b/client/src/class/ClientService.ts @@ -3,7 +3,7 @@ import { Sensor } from './Sensor'; export class ClientService { private clientSocket: ClientSocket = new ClientSocket( - 'http://localhost:3000/', + process.env.SERVER_URL!, 'temp' ); private sensor: Sensor = new Sensor(); @@ -12,13 +12,21 @@ export class ClientService { tempService(): void { setInterval(() => { - this.clientSocket.sendTemp(this.uuid, this.sensor.takeMeasure); - }, 1000); + this.clientSocket.sendTemp( + this.uuid, + this.sensor.takeMeasure, + 'temperature' + ); + }, 3000); } tempTestService(): void { setInterval(() => { - this.clientSocket.sendTemp(this.uuid, this.sensor.takeTestMeasure); + this.clientSocket.sendTemp( + this.uuid, + this.sensor.takeTestMeasure, + 'temperature' + ); }, 3000); } } diff --git a/client/src/class/ClientSocket.ts b/client/src/class/ClientSocket.ts index 272119e..9687461 100644 --- a/client/src/class/ClientSocket.ts +++ b/client/src/class/ClientSocket.ts @@ -10,7 +10,11 @@ export class ClientSocket { } private initSocket(): void { - this.socket = io(this.path, { transports: ['websocket'] }); + this.socket = io(this.path, { + timeout: 10000, + reconnectionAttempts: 200, + transports: ['websocket'], + }); console.log(chalk.yellow('Initialized socket...')); } @@ -35,7 +39,7 @@ export class ClientSocket { this.socket.disconnect(); } - sendTemp(uuid: string, temp: number): void { - this.socket.emit(this.eventName, { uuid, temp }); + sendTemp(uuid: string, measurement: number, pointName: string): void { + this.socket.emit(this.eventName, { uuid, measurement, pointName }); } } diff --git a/server/src/.env.template b/server/src/.env.template new file mode 100644 index 0000000..baae769 --- /dev/null +++ b/server/src/.env.template @@ -0,0 +1,4 @@ +DB_URL = XXXX; +DB_TOKEN = XXXX; +DB_ORG = XXXX; +DB_BUCKET = XXXX; \ No newline at end of file diff --git a/server/src/class/Database.ts b/server/src/class/Database.ts index 0a83efd..bb04a71 100644 --- a/server/src/class/Database.ts +++ b/server/src/class/Database.ts @@ -5,7 +5,6 @@ import { WriteApi, } from '@influxdata/influxdb-client'; import chalk from 'chalk'; -import { url, token, org, bucket } from '../config/creds'; export class Database { private client!: InfluxDB; @@ -17,16 +16,22 @@ export class Database { } private initDatabase(): void { - this.client = new InfluxDB({ url: url, token: token }); - this.writeApi = this.client.getWriteApi(org, bucket); + this.client = new InfluxDB({ + url: process.env.DB_URL!, + token: process.env.DB_TOKEN!, + }); + this.writeApi = this.client.getWriteApi( + process.env.DB_ORG!, + process.env.DB_BUCKET! + ); this.writeApi.useDefaultTags({ host: 'local' }); - this.queryApi = this.client.getQueryApi(org); + this.queryApi = this.client.getQueryApi(process.env.DB_ORG!); } - write(uuid: string, temp: number) { - const point = new Point('temperature') + write(uuid: string, value: number, pointName: string) { + const point = new Point(pointName) .tag('client', uuid) - .floatField('value', temp); + .floatField('value', value); this.writeApi.writePoint(point); this.writeApi.flush(); } @@ -44,7 +49,7 @@ export class Database { } query(filter: string) { - const query = `from(bucket: "${bucket}") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "${filter}")`; + const query = `from(bucket: "${process.env.DB_BUCKET}") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "${filter}")`; this.queryApi.queryRows(query, { next(row, tableMeta) { diff --git a/server/src/class/ServerSocket.ts b/server/src/class/ServerSocket.ts index 5a04d71..8fb8d14 100644 --- a/server/src/class/ServerSocket.ts +++ b/server/src/class/ServerSocket.ts @@ -4,7 +4,8 @@ import { Database } from './Database'; interface packet { uuid: string; - temp: number; + measurement: number; + pointName: string; } export class ServerSocket { @@ -37,8 +38,8 @@ export class ServerSocket { }); socket.on(this.eventName, (data: packet) => { - const { uuid, temp } = data; - this.database.write(uuid, temp); + const { uuid, measurement, pointName } = data; + this.database.write(uuid, measurement, pointName); }); }); } diff --git a/server/src/index.ts b/server/src/index.ts index 0e21d3d..7935e1f 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -4,5 +4,3 @@ import { ServerService } from './class/ServerService'; console.log(chalk.cyan('Started Anchiale Server...')); const service = new ServerService(); - -// service.;