Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit bc544e0

Browse files
committed
refactorings
1 parent 69be132 commit bc544e0

File tree

12 files changed

+1038
-120
lines changed

12 files changed

+1038
-120
lines changed

package-lock.json

Lines changed: 989 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "smart-display-server",
3-
"version": "0.45.0",
3+
"version": "1.0.0",
44
"description": "Server which controlles the smart display.",
55
"repository": {
66
"type": "git",
@@ -22,21 +22,20 @@
2222
"dependencies": {
2323
"clear": "^0.1.0",
2424
"color": "^4.2.1",
25-
"commander": "^9.0.0",
26-
"dayjs": "^1.10.8",
25+
"dayjs": "^1.11.0",
2726
"dotenv": "^16.0.0",
28-
"exit-hook": "^3.0.0",
29-
"mqtt": "^4.3.6",
30-
"node-fetch": "^3.2.1",
27+
"mqtt": "^4.3.7",
28+
"node-fetch": "^3.2.3",
3129
"path": "^0.12.7"
3230
},
3331
"devDependencies": {
32+
"@types/clear": "^0.1.2",
3433
"@types/color": "^3.0.3",
3534
"@types/mqtt": "^2.5.0",
36-
"@types/node": "^17.0.21",
35+
"@types/node": "^17.0.23",
3736
"@types/node-fetch": "^2.6.1",
38-
"@types/ws": "^8.5.1",
39-
"ts-node": "^10.6.0",
40-
"typescript": "^4.6.2"
37+
"@types/ws": "^8.5.3",
38+
"ts-node": "^10.7.0",
39+
"typescript": "^4.6.3"
4140
}
4241
}

src/apps/city-weather/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import {
88
roundToFixed,
99
secondaryColor,
1010
} from '../../helper';
11-
import { OpenWeatherMapService } from './services';
11+
import { loadData } from './services/open-weather-map.service';
1212
import { CityWeatherData } from './models';
1313

1414
export class CityWeatherApp implements App {
1515
private readonly _data = new LastUpdated<CityWeatherData>();
16-
private readonly _service = new OpenWeatherMapService();
1716
private readonly _maxCacheAgeMinutes = parseInt(
1817
process.env.APP_CITY_WEATHER_MAX_CACHE_AGE || '0',
1918
10
@@ -81,7 +80,7 @@ export class CityWeatherApp implements App {
8180

8281
private async _refreshWeatherData(): Promise<void> {
8382
try {
84-
const data = await this._service.loadData();
83+
const data = await loadData();
8584
console.log('city weather', data);
8685

8786
this._data.value = data;

src/apps/city-weather/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './city-weather-data';
2+
export * from './open-weather-response';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface OpenWeatherResponse {
2+
main: { temp: number; humidity: number };
3+
wind: {
4+
speed: number;
5+
};
6+
}

src/apps/city-weather/services/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import fetch from 'node-fetch';
22

3-
import { CityWeatherData } from '../models';
3+
import { CityWeatherData, OpenWeatherResponse } from '../models';
44

5-
export class OpenWeatherMapService {
6-
async loadData(): Promise<CityWeatherData> {
7-
const cityId = process.env.APP_CITY_WEATHER_CITY_ID;
8-
const appId = process.env.APP_CITY_WEATHER_APP_ID;
9-
const units = process.env.APP_CITY_WEATHER_UNITS;
5+
const cityId = process.env.APP_CITY_WEATHER_CITY_ID!;
6+
const appId = process.env.APP_CITY_WEATHER_APP_ID!;
7+
const units = process.env.APP_CITY_WEATHER_UNITS!;
108

11-
const url = `https://api.openweathermap.org/data/2.5/weather?id=${cityId}&appid=${appId}&units=${units}`;
9+
export const loadData = async () => {
10+
const url = `https://api.openweathermap.org/data/2.5/weather?id=${cityId}&appid=${appId}&units=${units}`;
11+
const response = await fetch(url);
12+
const data = (await response.json()) as OpenWeatherResponse;
1213

13-
const response = await fetch(url);
14-
const data = await response.json();
14+
const result: CityWeatherData = {
15+
temperature: data.main.temp,
16+
humidity: data.main.humidity,
17+
windSpeed: data.wind.speed,
18+
};
1519

16-
const result: CityWeatherData = {
17-
temperature: data.main.temp,
18-
humidity: data.main.humidity,
19-
windSpeed: data.wind.speed,
20-
};
21-
22-
return result;
23-
}
24-
}
20+
return result;
21+
};

src/apps/date/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import dayjs from 'dayjs';
22

33
import { App } from '../app';
44
import { SmartDisplayController } from '../../smart-display-controller';
5-
import { DrawHelper } from '../../helper';
5+
import { primaryColor, renderWeekday } from '../../helper';
66

77
export class DateApp implements App {
88
readonly name = 'date';
@@ -13,14 +13,14 @@ export class DateApp implements App {
1313
render(): void {
1414
this._renderDate();
1515

16-
DrawHelper.renderWeekday(this._controller);
16+
renderWeekday(this._controller);
1717
}
1818

1919
private _renderDate(): void {
2020
const date = dayjs().format('DD.MM.');
2121

2222
this._controller.drawText({
23-
hexColor: DrawHelper.PrimaryColor,
23+
hexColor: primaryColor,
2424
text: date,
2525
position: { x: 7, y: 1 },
2626
});

src/apps/time/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import dayjs from 'dayjs';
22

33
import { App } from '../app';
44
import { SmartDisplayController } from '../../smart-display-controller';
5-
import { DrawHelper } from '../../helper';
5+
import { primaryColor, renderWeekday } from '../../helper';
66

77
export class TimeApp implements App {
88
private _showColon = true;
@@ -15,7 +15,7 @@ export class TimeApp implements App {
1515
render(): void {
1616
this._renderTime();
1717

18-
DrawHelper.renderWeekday(this._controller);
18+
renderWeekday(this._controller);
1919

2020
// toggle colon
2121
//this.showColon = !this.showColon;
@@ -26,7 +26,7 @@ export class TimeApp implements App {
2626
const time = dayjs().format(format);
2727

2828
this._controller.drawText({
29-
hexColor: DrawHelper.PrimaryColor,
29+
hexColor: primaryColor,
3030
text: time,
3131
position: { x: 7, y: 1 },
3232
});

src/index.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
#!/usr/bin/env ts-node
22

3-
const clear = require('clear');
4-
require('dotenv').config();
5-
6-
import process from 'process';
7-
import { program } from 'commander';
8-
import exitHook from 'exit-hook';
9-
10-
import packageJson from '../package.json';
3+
import clear from 'clear';
4+
//import process from 'process';
5+
//import exitHook from 'exit-hook';
6+
import dayjs from 'dayjs';
7+
import weekday from 'dayjs/plugin/weekday';
8+
import 'dotenv/config';
119

1210
import { Server } from './server';
1311

14-
program
15-
.description(packageJson.description)
16-
.version(packageJson.version)
17-
.parse(process.argv);
18-
1912
clear();
2013
console.log('SmartDisplay');
2114

@@ -28,16 +21,13 @@ let server: Server;
2821

2922
//process.stdin.resume(); // so the program will not close instantly
3023

31-
exitHook(() => {
24+
/*exitHook(() => {
3225
console.debug('Exiting');
3326
3427
if (server != null) {
3528
server.shutdown();
3629
}
37-
});
38-
39-
import dayjs from 'dayjs';
40-
import weekday from 'dayjs/plugin/weekday';
30+
});*/
4131

4232
const locale = process.env.LOCALE;
4333

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Server {
3333
};
3434

3535
this._client = mqtt
36-
.connect(process.env.MQTT_SERVER, clientOptions)
36+
.connect(process.env.MQTT_SERVER!, clientOptions)
3737
.subscribe('smartDisplay/server/in/#')
3838
.on('message', (topic, message) => {
3939
const command = getLastTopicPart(topic);

tsconfig.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
11
{
2-
"compilerOptions": {
3-
"baseUrl": "src",
4-
"outDir": "dist",
5-
"target": "ES6",
6-
"module": "commonjs",
7-
"lib": ["es6", "es2015", "DOM"],
8-
"declaration": true,
9-
"strict": true,
10-
"types": ["node"],
11-
"esModuleInterop": true,
12-
"resolveJsonModule": true
13-
},
14-
"exclude": ["node_modules", "dist"]
2+
"extends": "ts-node/node16/tsconfig.json"
153
}

0 commit comments

Comments
 (0)