Browse Source

💚 Small code improvements

main
Haris Razis 4 years ago
parent
commit
c97e155a15
No known key found for this signature in database GPG Key ID: 86A4D290ED03FAB4
  1. 6
      client/src/class/ClientService.ts
  2. 6
      client/src/class/ClientSocket.ts
  3. 11
      server/src/AppRouter.ts
  4. 2
      server/src/class/Service.ts
  5. 3
      server/src/index.ts
  6. 2
      server/src/routes/Routes.ts
  7. 5
      web/.eslintrc.json

6
client/src/class/ClientService.ts

@ -4,7 +4,7 @@ import { Sensor } from './Sensor';
export class ClientService { export class ClientService {
private clientSocket: ClientSocket = new ClientSocket( private clientSocket: ClientSocket = new ClientSocket(
process.env.SERVER_URL!, process.env.SERVER_URL!,
'temp' 'measurement'
); );
private sensor: Sensor = new Sensor(); private sensor: Sensor = new Sensor();
@ -12,13 +12,13 @@ export class ClientService {
tempService(): void { tempService(): void {
setInterval(() => { setInterval(() => {
this.clientSocket.sendTemp(this.sensor.takeMeasure, 'temperature'); this.clientSocket.sendMeasure(this.sensor.takeMeasure, 'temperature');
}, 5 * 60000); }, 5 * 60000);
} }
tempTestService(): void { tempTestService(): void {
setInterval(() => { setInterval(() => {
this.clientSocket.sendTemp(this.sensor.takeTestMeasure, 'temperature'); this.clientSocket.sendMeasure(this.sensor.takeTestMeasure, 'temperature');
}, 5 * 60000); }, 5 * 60000);
} }
} }

6
client/src/class/ClientSocket.ts

@ -1,5 +1,5 @@
import chalk from 'chalk';
import { exit } from 'process'; import { exit } from 'process';
import chalk from 'chalk';
import io from 'socket.io-client'; import io from 'socket.io-client';
export class ClientSocket { export class ClientSocket {
@ -31,7 +31,7 @@ export class ClientSocket {
if (reason === 'io server disconnect') this.socket.connect(); if (reason === 'io server disconnect') this.socket.connect();
if (reason === 'io client disconnect') { if (reason === 'io client disconnect') {
console.log(chalk.red('Server kicked you out!')); console.log(chalk.red('Server kicked you!'));
exit(1); exit(1);
} }
@ -48,7 +48,7 @@ export class ClientSocket {
this.socket.disconnect(); this.socket.disconnect();
} }
sendTemp(measurement: number, pointName: string): void { sendMeasure(measurement: number, pointName: string): void {
this.socket.emit(this.eventName, { measurement, pointName }); this.socket.emit(this.eventName, { measurement, pointName });
} }
} }

11
server/src/AppRouter.ts

@ -1,11 +0,0 @@
import express from 'express';
export class AppRouter {
private static instance: express.Router;
static getInstance(): express.Router {
if (!AppRouter.instance) AppRouter.instance = express.Router();
return AppRouter.instance;
}
}

2
server/src/class/Service.ts

@ -16,6 +16,6 @@ export class Service {
} }
private initSocket() { private initSocket() {
this.socket = new Socket<Server>('temp', this.http, this.database); this.socket = new Socket<Server>('measurement', this.http, this.database);
} }
} }

3
server/src/index.ts

@ -3,7 +3,7 @@ import express from 'express';
import chalk from 'chalk'; import chalk from 'chalk';
var cors = require('cors'); var cors = require('cors');
import { Service } from './class/Service'; import { Service } from './class/Service';
import { AppRouter } from './AppRouter'; // import { AppRouter } from './AppRouter';
import { Routes } from './routes/Routes'; import { Routes } from './routes/Routes';
console.log(chalk.cyan('Started Anchiale Server...')); console.log(chalk.cyan('Started Anchiale Server...'));
@ -13,7 +13,6 @@ const http = require('http').Server(app);
const service = new Service(http); const service = new Service(http);
app.use(cors()); app.use(cors());
app.use(AppRouter.getInstance());
const route = new Routes(app, service); const route = new Routes(app, service);
route.routes(); route.routes();

2
server/src/routes/Routes.ts

@ -38,7 +38,7 @@ export class Routes {
private measurements(): void { private measurements(): void {
this.app.get('/measurements', async (req, res) => { this.app.get('/measurements', async (req, res) => {
const points = await this.service.database.query('temperature', '1h'); const points = await this.service.database.query('temperature', '1h'); // make temperature to something more abstract
res.header('Content-Type', 'application/json'); res.header('Content-Type', 'application/json');
if (points) { if (points) {

5
web/.eslintrc.json

@ -0,0 +1,5 @@
{
"rules": {
"quotes": ["error", "single"]
}
}
Loading…
Cancel
Save