Haris Razis
4 years ago
10 changed files with 1118 additions and 74 deletions
File diff suppressed because it is too large
@ -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; |
||||
|
} |
||||
|
} |
@ -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');
|
|
||||
} |
|
||||
} |
|
@ -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(); |
|
||||
} |
|
||||
} |
|
@ -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); |
||||
|
} |
||||
|
} |
@ -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(); |
||||
|
} |
||||
|
} |
@ -1,7 +1,21 @@ |
|||||
require('dotenv').config() |
require('dotenv').config(); |
||||
|
import express from 'express'; |
||||
import chalk from 'chalk'; |
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...')); |
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...')); |
||||
|
}); |
||||
|
@ -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…
Reference in new issue