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

Commit 5b68700

Browse files
committed
move settings to .env file; refactoring
1 parent 6c212ee commit 5b68700

14 files changed

Lines changed: 96 additions & 165 deletions

File tree

.env

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
MQTT_SERVER=mqtt://my-server.com:1883
2+
MQTT_USERNAME=smart-display
3+
MQTT_PASSWORD=*******
4+
5+
APP_ITERATIONS=10
6+
7+
LOCALE=de
8+
9+
APP_CITY_WEATHER_CITY_ID=your-city-id
10+
APP_CITY_WEATHER_APP_ID=your-app-id
11+
APP_CITY_WEATHER_UNITS=metric
12+
APP_CITY_WEATHER_MAX_CACHE_AGE=30
13+
APP_CITY_WEATHER_PUBLISH_DATA=false

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"color": "^3.1.2",
2525
"commander": "^6.2.0",
2626
"dayjs": "^1.10.4",
27+
"dotenv": "^8.2.0",
2728
"exit-hook": "^2.2.1",
2829
"mqtt": "^4.2.6",
2930
"node-fetch": "^2.6.1",

settings.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/apps/city-weather/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import { LastUpdated } from '../../models';
66
import { SmartDisplayController } from '../../smart-display-controller';
77
import { StringHelper, DrawHelper } from '../../helper';
88
import { OpenWeatherMapService } from './services';
9-
import { CityWeatherData, CityWeatherSetting } from './models';
9+
import { CityWeatherData } from './models';
1010

1111
export class CityWeatherApp implements App {
1212
private readonly _data = new LastUpdated<CityWeatherData>();
13-
private readonly _service = new OpenWeatherMapService(this.setting);
13+
private readonly _service = new OpenWeatherMapService();
14+
private readonly _maxCacheAgeMinutes = parseInt(
15+
process.env.APP_CITY_WEATHER_MAX_CACHE_AGE || '0',
16+
10
17+
);
18+
private readonly _publishWeatherData =
19+
process.env.APP_CITY_WEATHER_PUBLISH_DATA?.toLowerCase() === 'true';
1420

1521
readonly name = 'city-weather';
1622
readonly renderOnlyOneTime = true;
@@ -26,13 +32,12 @@ export class CityWeatherApp implements App {
2632
return true;
2733
}
2834

29-
return cacheMinutesAge >= this.setting.maxCacheAgeMinutes;
35+
return cacheMinutesAge >= this._maxCacheAgeMinutes;
3036
}
3137

3238
constructor(
3339
private controller: SmartDisplayController,
34-
private client: MqttClient,
35-
private setting: CityWeatherSetting
40+
private client: MqttClient
3641
) {}
3742

3843
init(): void {
@@ -51,7 +56,7 @@ export class CityWeatherApp implements App {
5156
DrawHelper.renderPixelProgress(
5257
this.controller,
5358
this.calcCacheMinutesAge(),
54-
this.setting.maxCacheAgeMinutes
59+
this._maxCacheAgeMinutes
5560
);
5661
}
5762

@@ -63,7 +68,7 @@ export class CityWeatherApp implements App {
6368
this.controller.drawText({
6469
hexColor: '#4CFF00',
6570
text: `${temperature}°`,
66-
position: { x: 7, y: 1 }
71+
position: { x: 7, y: 1 },
6772
});
6873
}
6974

@@ -86,7 +91,7 @@ export class CityWeatherApp implements App {
8691

8792
this._data.value = data;
8893

89-
if (this.setting.publishWeatherData) {
94+
if (this._publishWeatherData) {
9095
this.client.publish(
9196
'smartDisplay/server/out/cityWeather',
9297
JSON.stringify(data)

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

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from './city-weather-data';
2-
export * from './city-weather-setting';

src/apps/city-weather/services/open-weather-map.service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import fetch from 'node-fetch';
22

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

55
export class OpenWeatherMapService {
6-
constructor(private settings: CityWeatherSetting) {}
7-
86
async loadData(): Promise<CityWeatherData> {
9-
const { cityId, appId, units } = this.settings;
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;
10+
1011
const url = `http://api.openweathermap.org/data/2.5/weather?id=${cityId}&appid=${appId}&units=${units}`;
1112

1213
const response = await fetch(url);
@@ -15,7 +16,7 @@ export class OpenWeatherMapService {
1516
const result: CityWeatherData = {
1617
temperature: data.main.temp,
1718
humidity: data.main.humidity,
18-
windSpeed: data.wind.speed
19+
windSpeed: data.wind.speed,
1920
};
2021

2122
return result;

src/helper/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './draw-helper';
22
export * from './mqtt-helper';
3-
export * from './settings-helper';
43
export * from './string-helper';

src/helper/settings-helper.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)