Haris Razis
4 years ago
11 changed files with 166 additions and 37 deletions
@ -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(); |
||||
|
@ -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')); |
||||
|
}, |
||||
|
}); |
||||
|
} |
||||
} |
} |
||||
|
@ -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');
|
||||
|
} |
||||
|
} |
@ -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.;
|
||||
|
Loading…
Reference in new issue