Browse Source

Add Express

main
Haris Razis 4 years ago
parent
commit
0f971f5bed
No known key found for this signature in database GPG Key ID: 86A4D290ED03FAB4
  1. 975
      server/package-lock.json
  2. 3
      server/package.json
  3. 11
      server/src/AppRouter.ts
  4. 7
      server/src/class/Database.ts
  5. 16
      server/src/class/ServerService.ts
  6. 52
      server/src/class/ServerSocket.ts
  7. 21
      server/src/class/Service.ts
  8. 62
      server/src/class/Socket.ts
  9. 20
      server/src/index.ts
  10. 25
      server/src/routes/Routes.ts

975
server/package-lock.json

File diff suppressed because it is too large

3
server/package.json

@ -23,11 +23,14 @@
"homepage": "https://github.com/xrazis/Anchiale-pi#readme",
"dependencies": {
"@influxdata/influxdb-client": "^1.8.0",
"@types/express": "^4.17.9",
"@types/node": "^14.14.7",
"@types/socket.io": "^2.1.11",
"chalk": "^3.0.0",
"concurrently": "^5.3.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"nodemon": "^2.0.6",
"socket.io": "^3.0.1",
"typescript": "^4.0.5"

11
server/src/AppRouter.ts

@ -0,0 +1,11 @@
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;
}
}

7
server/src/class/Database.ts

@ -25,9 +25,10 @@ export class Database {
process.env.DB_BUCKET!
);
this.queryApi = this.client.getQueryApi(process.env.DB_ORG!);
console.log(chalk.yellow('Initialized Database connection...'));
}
write(uuid: string, measurement: number, pointName: string) {
write(uuid: string, measurement: number, pointName: string): void {
const point = new Point(pointName)
.tag('client', uuid)
.floatField('value', measurement);
@ -35,7 +36,7 @@ export class Database {
this.writeApi.flush();
}
closeWrite() {
closeWrite(): void {
this.writeApi
.close()
.then(() => {
@ -47,7 +48,7 @@ export class Database {
});
}
query(filter: string) {
query(filter: string): void {
const query = `from(bucket: "${process.env.DB_BUCKET}") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "${filter}")`;
this.queryApi.queryRows(query, {

16
server/src/class/ServerService.ts

@ -1,16 +0,0 @@
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');
}
}

52
server/src/class/ServerSocket.ts

@ -1,52 +0,0 @@
import chalk from 'chalk';
import { Server } from 'socket.io';
import { Database } from './Database';
interface packet {
uuid: string;
measurement: number;
pointName: string;
}
export class ServerSocket {
private io!: Server;
constructor(
private path: string,
private port: number,
private eventName: string,
private database: Database
) {
this.initSocket();
this.connStatus();
}
private initSocket(): void {
this.io = new Server(this.port, {
path: this.path,
transports: ['websocket'],
});
console.log(chalk.yellow('Initialized server...'));
}
private connStatus(): void {
this.io.on('connect', (socket) => {
console.log(chalk.green('Client connected!'));
socket.on('disconnect', () => {
console.log(chalk.red('Client disconected!'));
});
socket.on(this.eventName, (data: packet) => {
const { uuid, measurement, pointName } = data;
this.database.write(uuid, measurement, pointName);
});
});
}
closeSocket(): void {
console.log(chalk.red('Closing socket...'));
this.database.closeWrite();
this.io.close();
}
}

21
server/src/class/Service.ts

@ -0,0 +1,21 @@
import { Server } from 'http';
import { Database } from './Database';
import { Socket } from './Socket';
export class Service {
private database!: Database;
socket!: Socket<Express.Application>;
constructor(private http: Server) {
this.initDatabase();
this.initSocket();
}
private initDatabase() {
this.database = new Database();
}
private initSocket() {
this.socket = new Socket<Server>('temp', this.http, this.database);
}
}

62
server/src/class/Socket.ts

@ -0,0 +1,62 @@
import chalk from 'chalk';
interface database {
write(uuid: string, measurement: number, pointName: string): void;
closeWrite(): void;
}
interface packet {
uuid: string;
measurement: number;
pointName: string;
}
export class Socket<T> {
private io!: SocketIO.Server;
constructor(
private eventName: string,
private server: T,
private database: database
) {
this.initSocket();
this.initConn();
}
private initSocket(): void {
this.io = require('socket.io')(this.server);
console.log(chalk.yellow('Initialized Socket...'));
}
private initConn(): void {
this.io.on('connect', (socket: SocketIO.Socket) => {
console.log(chalk.green('Client connected!'));
socket.on('subscribe', (room) => {
socket.join(room);
console.log(
chalk.magenta(`Client ${socket.id} joined room "${room}". `)
);
});
socket.on('disconnect', () => {
console.log(chalk.red('Client disconected!'));
});
socket.on(this.eventName, (data: packet) => {
const { measurement, pointName } = data;
this.database.write(socket.id, measurement, pointName);
});
});
}
get rooms() {
return this.io.sockets.adapter.rooms;
}
closeSocket(): void {
console.log(chalk.red('Closing socket...'));
this.database.closeWrite();
this.io.close();
}
}

20
server/src/index.ts

@ -1,7 +1,21 @@
require('dotenv').config()
require('dotenv').config();
import express from 'express';
import chalk from 'chalk';
import { ServerService } from './class/ServerService';
import { Service } from './class/Service';
import { AppRouter } from './AppRouter';
import { Routes } from './routes/Routes';
console.log(chalk.cyan('Started Anchiale Server...'));
const service = new ServerService();
const app = express();
const http = require('http').Server(app);
const service = new Service(http);
app.use(AppRouter.getInstance());
const route = new Routes(app, service);
route.routes();
http.listen(3000, () => {
console.log(chalk.yellow('Listening on 3000...'));
});

25
server/src/routes/Routes.ts

@ -0,0 +1,25 @@
import * as express from 'express';
import { Service } from '../class/Service';
export class Routes {
constructor(public app: express.Application, public service: Service) {
this.app = app;
this.setStaticDir();
}
setStaticDir() {}
private devices(): void {
this.app.get('/devices', async (req, res) => {
const data = this.service.socket.rooms;
console.log(data);
const parsedOata = JSON.stringify(data);
res.header('Content-Type', 'application/json');
res.send(parsedOata);
});
}
public routes(): void {
this.devices();
}
}
Loading…
Cancel
Save