Skip to content

Commit

Permalink
Merge pull request #30 from foxthefox/without-chalk,figlet
Browse files Browse the repository at this point in the history
1.0.1
  • Loading branch information
foxthefox authored Feb 5, 2023
2 parents ba16fa1 + 989f6a8 commit b265993
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 foxthefox
Copyright (c) 2022-2023 foxthefox

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,24 @@ const logout = await fritz.logout_SID();
see the example.js.

## API Calls
* todo for 1.0.1
* todo for 1.0.2

## Changelog
### 1.0.2
* (foxthefox) use of User-Agent
* (foxthefox) implementation of new commands from API version 1.57

### 1.0.1
* skipping usage of chalk, figlet

### 1.0.0
* (foxthefox) common js module with 2 named exports Fritz and FritzEmu

### 0.9.1
* (foxthefox) first release on npm as ESM

## License
Copyright (c) 2022 foxthefox <[email protected]>
Copyright (c) 2022-2023 foxthefox <[email protected]>

see [LICENSE.md]

Expand Down
108 changes: 59 additions & 49 deletions lib/fritz_mockserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const http = require('http');
const fs = require('fs');
const { parse } = require('querystring');
const parser = require('xml2json-light');
const figlet = require('figlet');
const chalk = require('chalk');
const crypto = require('crypto');

const path = require('path');
Expand Down Expand Up @@ -207,7 +205,7 @@ function homeautoswitchAnswer(
response.write(String(deviceinfos));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -230,7 +228,7 @@ function homeautoswitchAnswer(
response.write(template[0].id);
response.end();
} else {
console.log(' did not find the ain in templates ' + ain);
console.log('\x1b[31m', ' did not find the ain in templates ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand Down Expand Up @@ -341,7 +339,7 @@ function homeautoswitchAnswer(
response.write(JSON.stringify([ "'" + groupvalue + "'" ]));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -362,7 +360,7 @@ function homeautoswitchAnswer(
response.write(JSON.stringify([ "'" + getgroupstate + "'" ]));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -387,7 +385,7 @@ function homeautoswitchAnswer(
response.write(String(getgroupmeter));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
//verursacht StatusCode400
response = errorAnswer(response, 400);
}
Expand All @@ -413,7 +411,7 @@ function homeautoswitchAnswer(
response.write(String(getgrouphkrtemp));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -437,7 +435,7 @@ function homeautoswitchAnswer(
response.statusCode = 200;
response.end();
} else {
console.log('-> did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', '-> did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand Down Expand Up @@ -468,7 +466,7 @@ function homeautoswitchAnswer(
response.write(String(xmlPowerStats));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand Down Expand Up @@ -506,7 +504,7 @@ function homeautoswitchAnswer(
response.statusCode = 200;
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response);
}
return response;
Expand Down Expand Up @@ -558,7 +556,7 @@ function homeautoswitchAnswer(
response.statusCode = 200;
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand Down Expand Up @@ -586,7 +584,7 @@ function homeautoswitchAnswer(
response.statusCode = 200;
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -610,7 +608,7 @@ function homeautoswitchAnswer(
response.statusCode = 200;
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand Down Expand Up @@ -646,7 +644,7 @@ function homeautoswitchAnswer(
response.write(String(endtimestamp));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand Down Expand Up @@ -675,7 +673,7 @@ function homeautoswitchAnswer(
response.write(String(endtimestamp));
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -695,7 +693,7 @@ function homeautoswitchAnswer(
response.statusCode = 200;
response.end();
} else {
console.log(' did not find the ain in devices/groups ' + ain);
console.log('\x1b[31m', ' did not find the ain in devices/groups ' + ain);
response = errorAnswer(response, 400);
}
return response;
Expand All @@ -705,12 +703,24 @@ function homeautoswitchAnswer(
response.end();
return response;
break;
case 'setmetadata':
break;
case 'gettriggerlistinfo':
break;
case 'settriggeractive':
break;
case 'setmappedcolor':
break;
case 'setcolortemperature':
break;
case 'addcolorleveltemplate':
break;
case 'startulesubscription':
break;
case 'getsubscriptionstate':
break;
default:
console.log('switchcmd no case found ' + switchcmd);
console.log('\x1b[31m', 'switchcmd no case found ' + switchcmd);
response = errorAnswer(response, 400);
return response;
break;
Expand Down Expand Up @@ -746,29 +756,29 @@ class FritzEmu {
this.debugmode = debugmode;
this.deviceresponse = null;
}

// S7WebAPI
setupHttpServer(callback) {
console.log(
chalk.yellow(
figlet.textSync('FB AHA Emulation', {
font: 'Standard',
horizontalLayout: 'default',
verticalLayout: 'default',
width: 80,
whitespaceBreak: true
})
)
);
console.log('\x1b[33m', ' _____ ____ _ _ _ _ ');
console.log('\x1b[33m', ' | ___| __ ) / \\ | | | | / \\ ');
console.log('\x1b[33m', ' | |_ | _ \\ / _ \\ | |_| | / _ \\ ');
console.log('\x1b[33m', ' | _| | |_) | / ___ \\| _ |/ ___ \\ ');
console.log('\x1b[33m', ' |_| |____/ /_/ \\_\\_| |_/_/ \\_\\ ');
console.log('\x1b[32m', ' _____ _ _ _ ');
console.log('\x1b[32m', '| ____| _ __ ___ _ _| | __ _| |_(_) ___ _ __ ');
console.log('\x1b[32m', '| _| | `_ ` _ \\| | | | |/ _` | __| |/ _ \\| `_ \\ ');
console.log('\x1b[32m', '| |___ | | | | | | |_| | | (_| | |_| | (_) | | | |');
console.log('\x1b[32m', '|_____||_| |_| |_|\\__,_|_|\\__,_|\\__|_|\\___/|_| |_|');
console.log('\x1b[0m');
//We need a which handles requests and send response
//Create a server
server = http.createServer(this.handleHttpRequest);
//Lets start our server
server.listen(3333, function() {
//Callback triggered when server is successfully listening. Hurray!
console.log('MOCK HTTP-Server (Fritzbox Emulation) listening on: http://localhost:3333');
console.log('\x1b[34m', 'MOCK HTTP-Server (Fritzbox Emulation) listening on: http://localhost:3333');
console.log();
console.log('for testing, setup in iobroker for second instance admin:password');
console.log('-------------------------------------------------------------------');
console.log('\x1b[34m', 'for testing, setup in iobroker for second instance admin:password');
console.log('\x1b[0m', '-------------------------------------------------------------------');
callback();
});
}
Expand Down Expand Up @@ -802,65 +812,65 @@ class FritzEmu {
switch (commandsplit[0]) {
case 'sid':
sid = commandsplit[1];
console.log('-> sid : ', commandsplit[1]);
console.log('\x1b[36m', '-> sid : ', commandsplit[1]);
break;
case 'switchcmd':
switchcmd = commandsplit[1];
console.log('-> switchcmd : ', commandsplit[1]);
console.log('\x1b[36m', '-> switchcmd : ', commandsplit[1]);
break;
case 'ain':
ain = commandsplit[1];
console.log('-> ain : ', commandsplit[1]);
console.log('\x1b[36m', '-> ain : ', commandsplit[1]);
break;
case 'ain':
onoff = commandsplit[1];
console.log('-> onoff : ', commandsplit[1]);
console.log('\x1b[36m', '-> onoff : ', commandsplit[1]);
break;
case 'param':
onoff = commandsplit[1];
console.log('-> param : ', commandsplit[1]);
console.log('\x1b[36m', '-> param : ', commandsplit[1]);
break;
case 'endtimestamp':
onoff = commandsplit[1];
console.log('-> endtimestamp : ', commandsplit[1]);
console.log('\x1b[36m', '-> endtimestamp : ', commandsplit[1]);
break;
case 'hue':
colorcmd = 'hue';
hue = commandsplit[1];
console.log('-> hue : ', commandsplit[1]);
console.log('\x1b[36m', '-> hue : ', commandsplit[1]);
break;
case 'saturation':
colorcmd = 'saturation';
saturation = commandsplit[1];
console.log('-> saturation : ', commandsplit[1]);
console.log('\x1b[36m', '-> saturation : ', commandsplit[1]);
break;
case 'temperature':
temperature = commandsplit[1];
console.log('-> temperature : ', commandsplit[1]);
console.log('\x1b[36m', '-> temperature : ', commandsplit[1]);
break;
case 'duration':
temperature = commandsplit[1];
console.log('-> duration : ', commandsplit[1]);
console.log('\x1b[36m', '-> duration : ', commandsplit[1]);
break;
case 'target':
target = commandsplit[1];
console.log('-> target : ', commandsplit[1]);
console.log('\x1b[36m', '-> target : ', commandsplit[1]);
break;
case 'version':
version = commandsplit[1];
console.log('-> version : ', commandsplit[1]);
console.log('\x1b[36m', '-> version : ', commandsplit[1]);
break;
case 'username':
username = commandsplit[1];
console.log('-> username : ', commandsplit[1]);
console.log('\x1b[36m', '-> username : ', commandsplit[1]);
break;
case 'response':
response = commandsplit[1];
console.log('-> response : ', commandsplit[1]);
console.log('\x1b[36m', '-> response : ', commandsplit[1]);
break;
case 'logout':
logout = commandsplit[1];
console.log('-> logout : ', commandsplit[1]);
console.log('\x1b[36m', '-> logout : ', commandsplit[1]);
break;
default:
break;
Expand Down Expand Up @@ -942,7 +952,7 @@ class FritzEmu {
}
});
} else {
console.log('-> not supported call ' + request.method + ' ' + request.url);
console.log('\x1b[31m', '-> not supported call ' + request.method + ' ' + request.url);
response.statusCode = 403;
response.end();
}
Expand Down
Loading

0 comments on commit b265993

Please sign in to comment.