Browse Source

Add influx-db

main
Haris Razis 4 years ago
parent
commit
841a262be8
No known key found for this signature in database GPG Key ID: 86A4D290ED03FAB4
  1. 27
      client/package-lock.json
  2. 6
      client/package.json
  3. 17
      client/src/class/ClientService.ts
  4. 16
      client/src/class/ClientSocket.ts
  5. 8
      client/src/index.ts
  6. 4
      client/tsconfig.json
  7. 68
      server/src/class/Database.ts
  8. 16
      server/src/class/ServerService.ts
  9. 32
      server/src/class/ServerSocket.ts
  10. 5
      server/src/index.ts
  11. 4
      server/tsconfig.json

27
client/package-lock.json

@ -11,9 +11,11 @@
"dependencies": { "dependencies": {
"@types/node": "^14.14.7", "@types/node": "^14.14.7",
"@types/socket.io-client": "^1.4.34", "@types/socket.io-client": "^1.4.34",
"@types/uuid": "^8.3.0",
"chalk": "^3.0.0", "chalk": "^3.0.0",
"nodemon": "^2.0.6", "nodemon": "^2.0.6",
"socket.io-client": "^3.0.0" "socket.io-client": "^3.0.0",
"uuid": "^8.3.1"
} }
}, },
"node_modules/@sindresorhus/is": { "node_modules/@sindresorhus/is": {
@ -50,6 +52,11 @@
"resolved": "https://registry.npmjs.org/@types/socket.io-client/-/socket.io-client-1.4.34.tgz", "resolved": "https://registry.npmjs.org/@types/socket.io-client/-/socket.io-client-1.4.34.tgz",
"integrity": "sha512-Lzia5OTQFJZJ5R4HsEEldywiiqT9+W2rDbyHJiiTGqOcju89sCsQ8aUXDljY6Ls33wKZZGC0bfMhr/VpOyjtXg==" "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": { "node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -1283,6 +1290,14 @@
"node": ">=4" "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": { "node_modules/widest-line": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/widest-line/-/widest-line-3.1.0.tgz", "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", "resolved": "https://registry.npmjs.org/@types/socket.io-client/-/socket.io-client-1.4.34.tgz",
"integrity": "sha512-Lzia5OTQFJZJ5R4HsEEldywiiqT9+W2rDbyHJiiTGqOcju89sCsQ8aUXDljY6Ls33wKZZGC0bfMhr/VpOyjtXg==" "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": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -2331,6 +2351,11 @@
"prepend-http": "^2.0.0" "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": { "widest-line": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/widest-line/-/widest-line-3.1.0.tgz", "resolved": "https://registry.npmjs.org/widest-line/-/widest-line-3.1.0.tgz",

6
client/package.json

@ -8,7 +8,7 @@
"start:run": "node build/index.js", "start:run": "node build/index.js",
"dev": "nodemon build/index.js", "dev": "nodemon build/index.js",
"test": "concurrently npm:start:build npm:dev", "test": "concurrently npm:start:build npm:dev",
"start": "concurrently start:*" "start": "concurrently npm:start:*"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -24,8 +24,10 @@
"dependencies": { "dependencies": {
"@types/node": "^14.14.7", "@types/node": "^14.14.7",
"@types/socket.io-client": "^1.4.34", "@types/socket.io-client": "^1.4.34",
"@types/uuid": "^8.3.0",
"chalk": "^3.0.0", "chalk": "^3.0.0",
"nodemon": "^2.0.6", "nodemon": "^2.0.6",
"socket.io-client": "^3.0.0" "socket.io-client": "^3.0.0",
"uuid": "^8.3.1"
} }
} }

17
client/src/class/ClientService.ts

@ -2,20 +2,23 @@ import { ClientSocket } from './ClientSocket';
import { Sensor } from './Sensor'; import { Sensor } from './Sensor';
export class ClientService { export class ClientService {
clientSocket: ClientSocket = new ClientSocket(3000, '/'); private clientSocket: ClientSocket = new ClientSocket(
sensor: Sensor = new Sensor(); 'http://localhost:3000/',
'temp'
);
private sensor: Sensor = new Sensor();
constructor() {} constructor(public uuid: string) {}
tempService() { tempService(): void {
setInterval(() => { setInterval(() => {
this.clientSocket.sendTemp(this.sensor.takeMeasure); this.clientSocket.sendTemp(this.uuid, this.sensor.takeMeasure);
}, 1000); }, 1000);
} }
tempTestService() { tempTestService(): void {
setInterval(() => { setInterval(() => {
this.clientSocket.sendTemp(this.sensor.takeTestMeasure); this.clientSocket.sendTemp(this.uuid, this.sensor.takeTestMeasure);
}, 1000); }, 1000);
} }
} }

16
client/src/class/ClientSocket.ts

@ -4,17 +4,17 @@ import io from 'socket.io-client';
export class ClientSocket { export class ClientSocket {
private socket!: SocketIOClient.Socket; private socket!: SocketIOClient.Socket;
constructor(private port: number, private path: string) { constructor(private path: string, private eventName: string) {
this.initSocket(); this.initSocket();
this.connStatus(); this.connStatus();
} }
private initSocket() { private initSocket(): void {
this.socket = io(`http://localhost:${this.port}${this.path}`); this.socket = io(this.path);
console.log(chalk.yellow('Initialized socket...')); console.log(chalk.yellow('Initialized socket...'));
} }
private connStatus() { private connStatus(): void {
this.socket.on('connect', () => { this.socket.on('connect', () => {
console.log(chalk.green('Connected to server!')); console.log(chalk.green('Connected to server!'));
}); });
@ -30,14 +30,12 @@ export class ClientSocket {
}); });
} }
closeConn() { closeConn(): void {
console.log(chalk.yellow('Closing socket...')); console.log(chalk.yellow('Closing socket...'));
this.socket.disconnect(); this.socket.disconnect();
} }
sendTemp(temp: number) { sendTemp(uuid: string, temp: number): void {
this.socket.emit('temp', temp, (data: string) => { this.socket.emit(this.eventName, { uuid, temp });
console.log(data);
});
} }
} }

8
client/src/index.ts

@ -1,3 +1,11 @@
import chalk from 'chalk'; import chalk from 'chalk';
import { ClientService } from './class/ClientService';
const { v4: uuidv4 } = require('uuid');
console.log(chalk.cyan('Started Anchiale Client...')); console.log(chalk.cyan('Started Anchiale Client...'));
const uuid = uuidv4();
const service = new ClientService(uuid);
service.tempTestService();

4
client/tsconfig.json

@ -14,8 +14,8 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */ // "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */ "outDir": "./build" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */ // "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */ // "removeComments": true, /* Do not emit comments to output. */

68
server/src/class/Database.ts

@ -1,3 +1,67 @@
export class localdb { import {
constructor() {} 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'));
},
});
}
} }

16
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');
}
}

32
server/src/class/ServerSocket.ts

@ -1,26 +1,33 @@
import chalk from 'chalk'; import chalk from 'chalk';
import { Server } from 'socket.io'; import { Server } from 'socket.io';
import { Database } from './Database';
interface DatabaseHas { interface packet {
push(temp: number): void; uuid: string;
temp: number;
} }
export class ServerSocket<T extends DatabaseHas> { export class ServerSocket {
private io!: Server; 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.initSocket();
this.connStatus(); this.connStatus();
} }
private initSocket() { private initSocket(): void {
this.io = new Server(this.port, { this.io = new Server(this.port, {
path: this.path, path: this.path,
}); });
console.log(chalk.yellow('Initialized server...')); console.log(chalk.yellow('Initialized server...'));
} }
private connStatus() { private connStatus(): void {
this.io.on('connect', (socket) => { this.io.on('connect', (socket) => {
console.log(chalk.green('Client connected!')); console.log(chalk.green('Client connected!'));
@ -28,15 +35,16 @@ export class ServerSocket<T extends DatabaseHas> {
console.log(chalk.red('Client disconected!')); console.log(chalk.red('Client disconected!'));
}); });
socket.on('temp', (temp: number) => { socket.on(this.eventName, (data: packet) => {
this.database.push(temp); const { uuid, temp } = data;
this.database.write(uuid, temp);
}); });
}); });
} }
closeSocket() { closeSocket(): void {
this.io.close(() => { console.log(chalk.red('Closing socket...'));
console.log(chalk.red('Closing socket...')); this.database.closeWrite();
}); this.io.close();
} }
} }

5
server/src/index.ts

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

4
server/tsconfig.json

@ -14,8 +14,8 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */ // "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */ "outDir": "./build" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */ // "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */ // "removeComments": true, /* Do not emit comments to output. */

Loading…
Cancel
Save