Browse Source

Add .env.template

Pull hard-coded strings into .env file
main
Haris Razis 4 years ago
parent
commit
2c7ead4f22
No known key found for this signature in database GPG Key ID: 86A4D290ED03FAB4
  1. 1
      .gitignore
  2. 1
      client/src/.env.template
  3. 16
      client/src/class/ClientService.ts
  4. 10
      client/src/class/ClientSocket.ts
  5. 4
      server/src/.env.template
  6. 21
      server/src/class/Database.ts
  7. 7
      server/src/class/ServerSocket.ts
  8. 2
      server/src/index.ts

1
.gitignore

@ -1,5 +1,4 @@
build/ build/
creds.ts
# Logs # Logs
logs logs

1
client/src/.env.template

@ -0,0 +1 @@
SERVER_URL = XXXX

16
client/src/class/ClientService.ts

@ -3,7 +3,7 @@ import { Sensor } from './Sensor';
export class ClientService { export class ClientService {
private clientSocket: ClientSocket = new ClientSocket( private clientSocket: ClientSocket = new ClientSocket(
'http://localhost:3000/', process.env.SERVER_URL!,
'temp' 'temp'
); );
private sensor: Sensor = new Sensor(); private sensor: Sensor = new Sensor();
@ -12,13 +12,21 @@ export class ClientService {
tempService(): void { tempService(): void {
setInterval(() => { setInterval(() => {
this.clientSocket.sendTemp(this.uuid, this.sensor.takeMeasure); this.clientSocket.sendTemp(
}, 1000); this.uuid,
this.sensor.takeMeasure,
'temperature'
);
}, 3000);
} }
tempTestService(): void { tempTestService(): void {
setInterval(() => { setInterval(() => {
this.clientSocket.sendTemp(this.uuid, this.sensor.takeTestMeasure); this.clientSocket.sendTemp(
this.uuid,
this.sensor.takeTestMeasure,
'temperature'
);
}, 3000); }, 3000);
} }
} }

10
client/src/class/ClientSocket.ts

@ -10,7 +10,11 @@ export class ClientSocket {
} }
private initSocket(): void { 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...')); console.log(chalk.yellow('Initialized socket...'));
} }
@ -35,7 +39,7 @@ export class ClientSocket {
this.socket.disconnect(); this.socket.disconnect();
} }
sendTemp(uuid: string, temp: number): void { sendTemp(uuid: string, measurement: number, pointName: string): void {
this.socket.emit(this.eventName, { uuid, temp }); this.socket.emit(this.eventName, { uuid, measurement, pointName });
} }
} }

4
server/src/.env.template

@ -0,0 +1,4 @@
DB_URL = XXXX;
DB_TOKEN = XXXX;
DB_ORG = XXXX;
DB_BUCKET = XXXX;

21
server/src/class/Database.ts

@ -5,7 +5,6 @@ import {
WriteApi, WriteApi,
} from '@influxdata/influxdb-client'; } from '@influxdata/influxdb-client';
import chalk from 'chalk'; import chalk from 'chalk';
import { url, token, org, bucket } from '../config/creds';
export class Database { export class Database {
private client!: InfluxDB; private client!: InfluxDB;
@ -17,16 +16,22 @@ export class Database {
} }
private initDatabase(): void { private initDatabase(): void {
this.client = new InfluxDB({ url: url, token: token }); this.client = new InfluxDB({
this.writeApi = this.client.getWriteApi(org, bucket); 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.writeApi.useDefaultTags({ host: 'local' });
this.queryApi = this.client.getQueryApi(org); this.queryApi = this.client.getQueryApi(process.env.DB_ORG!);
} }
write(uuid: string, temp: number) { write(uuid: string, value: number, pointName: string) {
const point = new Point('temperature') const point = new Point(pointName)
.tag('client', uuid) .tag('client', uuid)
.floatField('value', temp); .floatField('value', value);
this.writeApi.writePoint(point); this.writeApi.writePoint(point);
this.writeApi.flush(); this.writeApi.flush();
} }
@ -44,7 +49,7 @@ export class Database {
} }
query(filter: string) { 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, { this.queryApi.queryRows(query, {
next(row, tableMeta) { next(row, tableMeta) {

7
server/src/class/ServerSocket.ts

@ -4,7 +4,8 @@ import { Database } from './Database';
interface packet { interface packet {
uuid: string; uuid: string;
temp: number; measurement: number;
pointName: string;
} }
export class ServerSocket { export class ServerSocket {
@ -37,8 +38,8 @@ export class ServerSocket {
}); });
socket.on(this.eventName, (data: packet) => { socket.on(this.eventName, (data: packet) => {
const { uuid, temp } = data; const { uuid, measurement, pointName } = data;
this.database.write(uuid, temp); this.database.write(uuid, measurement, pointName);
}); });
}); });
} }

2
server/src/index.ts

@ -4,5 +4,3 @@ import { ServerService } from './class/ServerService';
console.log(chalk.cyan('Started Anchiale Server...')); console.log(chalk.cyan('Started Anchiale Server...'));
const service = new ServerService(); const service = new ServerService();
// service.;

Loading…
Cancel
Save