Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
fix: Remove URL parsing
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
zachowj committed Jul 13, 2021
1 parent 6c9fa13 commit a155ef4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { fileURLToPath } from 'url';
import { Config } from './config.js';
import { mqtt as MQTT } from './mqtt.js';
import { createServer } from './server.js';
import { xfinityUsage } from './xfinity.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -21,6 +22,7 @@ try {
process.exit(1);
}

export let usage: xfinityUsage | undefined;
const { xfinity: xfinityConfig, mqtt: mqttConfig, imap: imapConfig } = config.getConfig();
const intervalMs = xfinityConfig.interval * 60000;
const fetch = () => {
Expand All @@ -34,6 +36,7 @@ const fetch = () => {
break;
case 'usage':
console.log('Usage updated');
usage = data.usage as xfinityUsage;
eventBus.emit(DATA_UPDATED, data.usage);
break;
case 'error':
Expand All @@ -42,6 +45,7 @@ const fetch = () => {
break;
}
if (['usage', 'error'].includes(type)) {
xfinity.kill();
console.log(`Next fetch in ${xfinityConfig.interval} minutes @ ${nextAt}`);
}
});
Expand All @@ -51,7 +55,7 @@ fetch();
setInterval(fetch, intervalMs);

if (config.useHttp) {
createServer(eventBus);
createServer();
}

if (config.useMqtt && mqttConfig) {
Expand Down
21 changes: 5 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import EventEmitter from 'events';
import Http from 'http';
import { URL } from 'url';

import { DATA_UPDATED } from './app.js';
import { xfinityUsage } from './xfinity.js';
import { usage } from './app.js';

let usage: xfinityUsage | undefined;

export const createServer = (eventBus: EventEmitter): void => {
export const createServer = (): void => {
Http.createServer((req, res) => {
const url = new URL(req.url ?? '');
const path = url.pathname;
const homeassistant = path === '/homeassistant';
const homeassistant = req.url === '/homeassistant';

console.log(`HTTP request: ${path}`);
console.log(`HTTP request: ${req.url}`);

if (path !== '/' && !homeassistant) {
if (req.url !== '/' && !homeassistant) {
res.writeHead(404);
res.end();
return;
Expand Down Expand Up @@ -49,8 +42,4 @@ export const createServer = (eventBus: EventEmitter): void => {
res.end();
}).listen(7878);
console.log('http server started');

eventBus.on(DATA_UPDATED, (data: xfinityUsage) => {
usage = data;
});
};

0 comments on commit a155ef4

Please sign in to comment.