Compare commits
No commits in common. "90dafc2b8396ea567909f115a44b4f9364c81ece" and "6bb99d58b647134d388caf15c49cae25a25737d3" have entirely different histories.
90dafc2b83
...
6bb99d58b6
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
/Server/scripts/
|
|
@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"noImplicitAny": false,
|
|
||||||
"noEmitOnError": true,
|
|
||||||
"removeComments": false,
|
|
||||||
"sourceMap": false,
|
|
||||||
"target": "ES5",
|
|
||||||
"outDir": "../html/scripts"
|
|
||||||
},
|
|
||||||
"include": [
|
|
||||||
"client/**/*"
|
|
||||||
]
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
export interface Config {
|
|
||||||
abfall: Abfall;
|
|
||||||
abfall_after: Abfall;
|
|
||||||
heizmodus: Heizprogramm;
|
|
||||||
fensterzf: Fensterstatus;
|
|
||||||
aussenklima: Aussenklima;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Abfall {
|
|
||||||
uri: string;
|
|
||||||
text: string;
|
|
||||||
date: string;
|
|
||||||
in_days: string;
|
|
||||||
type: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Heizprogramm {
|
|
||||||
uri: string;
|
|
||||||
state: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Fensterstatus {
|
|
||||||
uri: string;
|
|
||||||
state: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Aussenklima {
|
|
||||||
uri: string;
|
|
||||||
temp: string;
|
|
||||||
feuchte: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const cfg: Config = {
|
|
||||||
"abfall": {
|
|
||||||
"uri": "/abfall",
|
|
||||||
"text": "Nächste Abholung",
|
|
||||||
"date": "trashschedule.0.next.dateFormat",
|
|
||||||
"in_days": "trashschedule.0.next.daysLeft",
|
|
||||||
"type": "trashschedule.0.next.typesText"
|
|
||||||
},
|
|
||||||
"abfall_after": {
|
|
||||||
"uri": "/danachabfall",
|
|
||||||
"text": "Kommende Abholung",
|
|
||||||
"date": "trashschedule.0.nextAfter.dateFormat",
|
|
||||||
"in_days": "trashschedule.0.nextAfter.daysLeft",
|
|
||||||
"type": "trashschedule.0.nextAfter.typesText"
|
|
||||||
},
|
|
||||||
"heizmodus": {
|
|
||||||
"uri": "/heizmodus",
|
|
||||||
"state": "hm-rega.0.3897"
|
|
||||||
},
|
|
||||||
"fensterzf": {
|
|
||||||
"uri": "/fensterzf",
|
|
||||||
"state": "0_userdata.0.OPEN_WINDOWS.COUNT"
|
|
||||||
},
|
|
||||||
"aussenklima": {
|
|
||||||
"uri": "/aussenklima",
|
|
||||||
"temp": "hm-rpc.0.000EDD89B3A1C4.1.ACTUAL_TEMPERATURE",
|
|
||||||
"feuchte": "hm-rpc.0.000EDD89B3A1C4.1.HUMIDITY"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
import { cfg } from "./Config";
|
|
||||||
import { Database } from "./LocalDatabase";
|
|
||||||
|
|
||||||
export class DataHandler {
|
|
||||||
private db: &Database;
|
|
||||||
|
|
||||||
constructor(db: &Database) {
|
|
||||||
this.db = db;
|
|
||||||
}
|
|
||||||
|
|
||||||
valid_uri(uri: string) {
|
|
||||||
let found: boolean = false;
|
|
||||||
for(const key of Object.keys(cfg)) {
|
|
||||||
if(cfg[key].uri == uri) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
get_data_json(uri: string): string {
|
|
||||||
let found: boolean = false;
|
|
||||||
let data: string = '{';
|
|
||||||
let dp;
|
|
||||||
for(const key of Object.keys(cfg)) {
|
|
||||||
if(cfg[key].uri == uri) {
|
|
||||||
found = true;
|
|
||||||
dp = cfg[key];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let add_comma: boolean = false;
|
|
||||||
let o = Object.keys(dp);
|
|
||||||
o.forEach(d => {
|
|
||||||
let s = this.db.get(dp[d]);
|
|
||||||
if(s != null) {
|
|
||||||
if(add_comma) {
|
|
||||||
data += ","
|
|
||||||
}
|
|
||||||
data += '"' + d + '":"' + s + '"';
|
|
||||||
add_comma = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
data += '}';
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
import { IncomingMessage, request } from "http";
|
|
||||||
import { Database } from "./LocalDatabase";
|
|
||||||
|
|
||||||
export function request_data(id: string, self: &Database): void {
|
|
||||||
let url: string = 'http://192.168.178.34:8082/getBulk/' + id;
|
|
||||||
let data: string = "";
|
|
||||||
|
|
||||||
let req = request(url, (res: IncomingMessage) => {
|
|
||||||
res.setEncoding('utf8');
|
|
||||||
|
|
||||||
res.on('data', (chunk) => {
|
|
||||||
data += chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', () => {
|
|
||||||
let d = JSON.parse(data);
|
|
||||||
d.forEach(element => {
|
|
||||||
self.add(element.id, element.val)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
req.on('error', (e) => {
|
|
||||||
console.error(`problem with request: ${e.message}`);
|
|
||||||
});
|
|
||||||
req.end();
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
import { existsSync, readFileSync } from 'node:fs';
|
|
||||||
|
|
||||||
interface Mapping {
|
|
||||||
ending: string;
|
|
||||||
encoding: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class File {
|
|
||||||
type: string;
|
|
||||||
content: Buffer;
|
|
||||||
|
|
||||||
constructor(type: string, content: Buffer) {
|
|
||||||
this.content = content;
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class FileHandler {
|
|
||||||
|
|
||||||
private www_folder: string = '/home/carsten/Dokumente/SmartHomeDashboard/html/';
|
|
||||||
private index_alias: string = 'index.html';
|
|
||||||
|
|
||||||
private mappings: Mapping[] = [
|
|
||||||
{ ending: 'html', encoding: 'text/html' },
|
|
||||||
{ ending: 'js', encoding: 'text/javascript' },
|
|
||||||
{ ending: 'css', encoding: 'text/css' },
|
|
||||||
{ ending: 'json', encoding: 'application/json' },
|
|
||||||
{ ending: 'ico', encoding: 'image/x-icon' },
|
|
||||||
{ ending: 'svg', encoding: 'image/svg+xml' },
|
|
||||||
{ ending: 'txt', encoding: 'text/plain' },
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
file_allowed(url: string): boolean {
|
|
||||||
let allowed: boolean = false;
|
|
||||||
let m = url.match('\\.\\.');
|
|
||||||
if (m == null) {
|
|
||||||
allowed = true;
|
|
||||||
}
|
|
||||||
return allowed;
|
|
||||||
}
|
|
||||||
|
|
||||||
file_exists(url: string): boolean {
|
|
||||||
if(url == '/') {
|
|
||||||
url = this.index_alias;
|
|
||||||
}
|
|
||||||
return existsSync(this.www_folder + url);
|
|
||||||
}
|
|
||||||
|
|
||||||
file_get(url: string): File {
|
|
||||||
if(url == '/') {
|
|
||||||
url = this.index_alias;
|
|
||||||
}
|
|
||||||
return new File(this.get_type(url), readFileSync(this.www_folder + url));
|
|
||||||
}
|
|
||||||
|
|
||||||
private get_type(url: string): string {
|
|
||||||
if(url == '/') {
|
|
||||||
url = this.index_alias;
|
|
||||||
}
|
|
||||||
let type: string = 'application/octet-stream';
|
|
||||||
let m: Mapping = this.mappings.find((e) => e.ending == url.split('.').pop());
|
|
||||||
if(m != undefined) {
|
|
||||||
type = m.encoding;
|
|
||||||
}
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,63 +0,0 @@
|
|||||||
import { Config, cfg } from "./Config";
|
|
||||||
import { request_data } from "./DataRequester";
|
|
||||||
|
|
||||||
export class Database {
|
|
||||||
private db: Datapoint[] = [];
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
let groups = Object.keys(cfg);
|
|
||||||
groups.forEach(group => {
|
|
||||||
let members = Object.keys(cfg[group]);
|
|
||||||
members.forEach(member => {
|
|
||||||
if((member != "text") && (member != "uri")) {
|
|
||||||
this.add(cfg[group][member])
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
add(id: string, value?: string, type?: string): void {
|
|
||||||
let found: boolean = false;
|
|
||||||
this.db.forEach(element => {
|
|
||||||
if(element.id == id) {
|
|
||||||
element.value = value;
|
|
||||||
element.type = type
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if(!found) {
|
|
||||||
this.db.push(new Datapoint(id, value, type));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get(id: string): string {
|
|
||||||
for(const el of this.db) {
|
|
||||||
if(el.id == id) {
|
|
||||||
return el.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
start(): void {
|
|
||||||
setInterval(this.cyclic_requets, 1000, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
cyclic_requets(self: &Database): void {
|
|
||||||
self.db.forEach(element => {
|
|
||||||
request_data(element.id, self)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Datapoint {
|
|
||||||
id: string;
|
|
||||||
value: string;
|
|
||||||
type: string;
|
|
||||||
|
|
||||||
constructor(id: string, value: string, type: string) {
|
|
||||||
this.id = id;
|
|
||||||
this.type = type;
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
import { IncomingMessage, ServerResponse } from "node:http";
|
|
||||||
import { FileHandler, File } from "./FileHandler";
|
|
||||||
import { DataHandler } from "./DataHandler";
|
|
||||||
import { Database } from "./LocalDatabase";
|
|
||||||
|
|
||||||
export class RequestHandler {
|
|
||||||
static dh: DataHandler;
|
|
||||||
static fh: FileHandler;
|
|
||||||
|
|
||||||
constructor(db: &Database) {
|
|
||||||
RequestHandler.dh = new DataHandler(db);
|
|
||||||
RequestHandler.fh = new FileHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
new_request(req: IncomingMessage, res: ServerResponse): void {
|
|
||||||
if(RequestHandler.dh.valid_uri(req.url)) {
|
|
||||||
res.statusCode = 200;
|
|
||||||
res.setHeader('Content-Type', 'application/json');
|
|
||||||
res.end(RequestHandler.dh.get_data_json(req.url));
|
|
||||||
}
|
|
||||||
else if(RequestHandler.fh.file_allowed(req.url)) {
|
|
||||||
if(RequestHandler.fh.file_exists(req.url)) {
|
|
||||||
res.statusCode = 200;
|
|
||||||
let f: File = RequestHandler.fh.file_get(req.url)
|
|
||||||
res.setHeader('Content-Type', f.type);
|
|
||||||
res.end(f.content);
|
|
||||||
} else {
|
|
||||||
res.statusCode = 404;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
res.statusCode = 403;
|
|
||||||
}
|
|
||||||
console.log(req.url, res.statusCode);
|
|
||||||
res.end();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
import { createServer } from 'node:http'
|
|
||||||
import { RequestHandler } from './RequestHandler';
|
|
||||||
import { Database } from './LocalDatabase';
|
|
||||||
|
|
||||||
export class Webserver {
|
|
||||||
private port: number = 8080;
|
|
||||||
private hostname: string = '0.0.0.0';
|
|
||||||
private rh: RequestHandler;
|
|
||||||
|
|
||||||
constructor(db: &Database) {
|
|
||||||
this.rh = new RequestHandler(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
start_server(): void {
|
|
||||||
|
|
||||||
const server = createServer(this.rh.new_request);
|
|
||||||
|
|
||||||
server.listen(this.port, this.hostname, () => {
|
|
||||||
console.log('Server running at http://' + this.hostname + ':' + this.port);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
import { Webserver } from "./Webserver";
|
|
||||||
import { Database } from "./LocalDatabase";
|
|
||||||
|
|
||||||
let db = new Database();
|
|
||||||
let ws = new Webserver(db);
|
|
||||||
|
|
||||||
ws.start_server();
|
|
||||||
console.log("Server started");
|
|
||||||
|
|
||||||
db.start();
|
|
||||||
console.log("DB started");
|
|
@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"noImplicitAny": false,
|
|
||||||
"noEmitOnError": true,
|
|
||||||
"removeComments": false,
|
|
||||||
"sourceMap": false,
|
|
||||||
"target": "ES5",
|
|
||||||
"outDir": "scripts"
|
|
||||||
},
|
|
||||||
"include": [
|
|
||||||
"server/**/*"
|
|
||||||
]
|
|
||||||
}
|
|
BIN
html/favicon.ico
BIN
html/favicon.ico
Binary file not shown.
Before Width: | Height: | Size: 6.1 KiB |
@ -1,5 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<script src="/scripts/main.js"></script>
|
|
||||||
Hat geklappt
|
|
||||||
</html>
|
|
Loading…
x
Reference in New Issue
Block a user