hoffmation-base 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hoffmation-base",
3
3
  "description": "Base Libraries and functions for own Hoffmation projects",
4
- "version": "0.0.9",
4
+ "version": "0.1.0",
5
5
  "main": "index.js",
6
6
  "repository": {
7
7
  "type": "git",
@@ -0,0 +1,83 @@
1
+ import { OwnSonosDevice, SonosService } from "/server/config/private/server/services/Sonos/sonos-service";
2
+ import { ServerLogService } from "/server/config/private/server/services/log-service";
3
+ import { LogLevel } from "/server/config/private/models/logLevel";
4
+ import { TelegramService } from "/server/config/private/server/services/Telegram/telegram-service";
5
+
6
+ export class MuellTonne {
7
+ public static oneDay: number = 1000 * 60 * 60 * 24;
8
+ public nextDate: Date | undefined = undefined;
9
+ public dates: Date[] = [];
10
+
11
+ public constructor(public name: string, public ownSonosDevice?: OwnSonosDevice) {
12
+ }
13
+
14
+ public sortDates(): void {
15
+ this.dates = this.dates.sort((a, b) => a.getTime() - b.getTime());
16
+ this.removePassedDates();
17
+ ServerLogService.writeLog(
18
+ LogLevel.Info,
19
+ `Die "${this.name}" ist das nächste mal am ${this.nextDate.toLocaleDateString('de-DE')} zu leeren`,
20
+ );
21
+ }
22
+
23
+ public removePassedDates(): void {
24
+ const todayMidnight: number = new Date().setHours(0, 0, 0, 0);
25
+ while (this.dates.length > 0 && this.dates[0].getTime() < todayMidnight) {
26
+ this.dates.shift();
27
+ }
28
+ this.nextDate = this.dates[0];
29
+ }
30
+
31
+ public check(): void {
32
+ this.removePassedDates();
33
+ if (this.nextDate === undefined) {
34
+ ServerLogService.writeLog(LogLevel.Alert, `Die Mülltonne mit dem Namen ${this.name} hat keine nächste Abholung!`);
35
+ return;
36
+ }
37
+ const todayMidnight: number = new Date().setHours(0, 0, 0, 0);
38
+ const tomorowMidnight: number = todayMidnight + MuellTonne.oneDay;
39
+ const tomorowAfterMidnight: number = tomorowMidnight + MuellTonne.oneDay;
40
+ const nextTimestamp: number = this.nextDate.getTime();
41
+
42
+ const daysTilNextEvent: number = (nextTimestamp - todayMidnight) / MuellTonne.oneDay;
43
+ ServerLogService.writeLog(
44
+ LogLevel.Info,
45
+ `Die Mülltonne mit dem Namen ${this.name} wird in ${daysTilNextEvent} Tagen das nächste Mal abgeholt.`,
46
+ );
47
+
48
+ if (nextTimestamp >= tomorowAfterMidnight) {
49
+ ServerLogService.writeLog(
50
+ LogLevel.Trace,
51
+ `Die Mülltonne mit dem Namen ${this.name} wird erst nach Übermorgen abgeholt`,
52
+ );
53
+ return; // Es ist noch lange hin
54
+ }
55
+
56
+ if (nextTimestamp >= tomorowMidnight) {
57
+ const message = `Die Mülltonne mit dem Namen ${this.name} wird morgen abgeholt!`;
58
+ TelegramService.inform(message);
59
+
60
+ if (this.ownSonosDevice) {
61
+ SonosService.speakOnDevice(message, this.ownSonosDevice, 30);
62
+ }
63
+ return;
64
+ }
65
+
66
+ if (nextTimestamp >= todayMidnight) {
67
+ if (new Date().getHours() > 10) {
68
+ const message = `Die Mülltonne mit dem Namen ${this.name} wurde heute abgeholt, Mülltonne zurückstellen!`;
69
+ TelegramService.inform(message);
70
+ if (this.ownSonosDevice) {
71
+ SonosService.speakOnDevice(message, this.ownSonosDevice, 30);
72
+ }
73
+ } else {
74
+ const message = `Die Mülltonne mit dem Namen ${this.name} wird heute abgeholt!`;
75
+ TelegramService.inform(message);
76
+ if (this.ownSonosDevice) {
77
+ SonosService.speakOnDevice(message, this.ownSonosDevice, 30);
78
+ }
79
+ }
80
+ return;
81
+ }
82
+ }
83
+ }
@@ -1,91 +1,12 @@
1
1
  import { async, VEvent } from 'node-ical';
2
2
  import { ServerLogService } from '../log-service';
3
- import { OwnSonosDevice, SonosService } from '../Sonos/sonos-service';
4
- import { TelegramService } from '../Telegram/telegram-service';
5
3
  import { TimeCallbackService } from '../time-callback-service';
6
4
  import { LogLevel } from '../../../models/logLevel';
7
5
  import { TimeCallback, TimeCallbackType } from '../../../models/timeCallback';
8
6
  import { iMuellSettings } from '../../config/iConfig';
9
7
  import { Utils } from '../utils/utils';
10
-
11
- export class MuellTonne {
12
- public static oneDay: number = 1000 * 60 * 60 * 24;
13
- public nextDate: Date | undefined = undefined;
14
- public dates: Date[] = [];
15
-
16
- public constructor(public name: string, public ownSonosDevice?: OwnSonosDevice) {
17
- }
18
-
19
- public sortDates(): void {
20
- this.dates = this.dates.sort((a, b) => a.getTime() - b.getTime());
21
- this.removePassedDates();
22
- ServerLogService.writeLog(
23
- LogLevel.Info,
24
- `Die "${this.name}" ist das nächste mal am ${this.nextDate.toLocaleDateString('de-DE')} zu leeren`,
25
- );
26
- }
27
-
28
- public removePassedDates(): void {
29
- const todayMidnight: number = new Date().setHours(0, 0, 0, 0);
30
- while (this.dates.length > 0 && this.dates[0].getTime() < todayMidnight) {
31
- this.dates.shift();
32
- }
33
- this.nextDate = this.dates[0];
34
- }
35
-
36
- public check(): void {
37
- this.removePassedDates();
38
- if (this.nextDate === undefined) {
39
- ServerLogService.writeLog(LogLevel.Alert, `Die Mülltonne mit dem Namen ${this.name} hat keine nächste Abholung!`);
40
- return;
41
- }
42
- const todayMidnight: number = new Date().setHours(0, 0, 0, 0);
43
- const tomorowMidnight: number = todayMidnight + MuellTonne.oneDay;
44
- const tomorowAfterMidnight: number = tomorowMidnight + MuellTonne.oneDay;
45
- const nextTimestamp: number = this.nextDate.getTime();
46
-
47
- const daysTilNextEvent: number = (nextTimestamp - todayMidnight) / MuellTonne.oneDay;
48
- ServerLogService.writeLog(
49
- LogLevel.Info,
50
- `Die Mülltonne mit dem Namen ${this.name} wird in ${daysTilNextEvent} Tagen das nächste Mal abgeholt.`,
51
- );
52
-
53
- if (nextTimestamp >= tomorowAfterMidnight) {
54
- ServerLogService.writeLog(
55
- LogLevel.Trace,
56
- `Die Mülltonne mit dem Namen ${this.name} wird erst nach Übermorgen abgeholt`,
57
- );
58
- return; // Es ist noch lange hin
59
- }
60
-
61
- if (nextTimestamp >= tomorowMidnight) {
62
- const message = `Die Mülltonne mit dem Namen ${this.name} wird morgen abgeholt!`;
63
- TelegramService.inform(message);
64
-
65
- if (this.ownSonosDevice) {
66
- SonosService.speakOnDevice(message, this.ownSonosDevice, 30);
67
- }
68
- return;
69
- }
70
-
71
- if (nextTimestamp >= todayMidnight) {
72
- if (new Date().getHours() > 10) {
73
- const message = `Die Mülltonne mit dem Namen ${this.name} wurde heute abgeholt, Mülltonne zurückstellen!`;
74
- TelegramService.inform(message);
75
- if (this.ownSonosDevice) {
76
- SonosService.speakOnDevice(message, this.ownSonosDevice, 30);
77
- }
78
- } else {
79
- const message = `Die Mülltonne mit dem Namen ${this.name} wird heute abgeholt!`;
80
- TelegramService.inform(message);
81
- if (this.ownSonosDevice) {
82
- SonosService.speakOnDevice(message, this.ownSonosDevice, 30);
83
- }
84
- }
85
- return;
86
- }
87
- }
88
- }
8
+ import { MuellTonne } from "./muell-tonne";
9
+ import { OwnSonosDevice } from "/server/config/private/server/services/Sonos/sonos-service";
89
10
 
90
11
  export class MuellService {
91
12
  public static alleTonnen: Array<{ name: string; date: Date }> = [];
@@ -98,10 +19,11 @@ export class MuellService {
98
19
  private static lastCheck: Date = new Date(0);
99
20
  private static _calendarURL: string;
100
21
  private static _active: boolean = false;
22
+ private static defaultSonosDevice: OwnSonosDevice | undefined = undefined;
101
23
 
102
24
  public static months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
103
25
 
104
- public static intialize(config: iMuellSettings): void {
26
+ public static intialize(config: iMuellSettings, defaultSonosDevice: OwnSonosDevice | undefined): void {
105
27
  this._active = true;
106
28
  this._calendarURL = config.calendarURL;
107
29
  this.updateTimeCallback = new TimeCallback(
@@ -134,10 +56,10 @@ export class MuellService {
134
56
  public static updateCalendar(checkAfterwards: boolean = true): void {
135
57
  ServerLogService.writeLog(LogLevel.Debug, `Muell Service wird nun initialisiert`);
136
58
  async.fromURL(this._calendarURL).then((data) => {
137
- this.gelbeTonne = new MuellTonne('Gelbe Tonne');
138
- this.graueTonne = new MuellTonne('Graue Tonne');
139
- this.blaueTonne = new MuellTonne('Blaue Tonne');
140
- this.brauneTonne = new MuellTonne('Braune Tonne');
59
+ this.gelbeTonne = new MuellTonne('Gelbe Tonne', this.defaultSonosDevice);
60
+ this.graueTonne = new MuellTonne('Graue Tonne', this.defaultSonosDevice);
61
+ this.blaueTonne = new MuellTonne('Blaue Tonne', this.defaultSonosDevice);
62
+ this.brauneTonne = new MuellTonne('Braune Tonne', this.defaultSonosDevice);
141
63
  this.alleTonnen = [];
142
64
  const todayMidnight: number = new Date().setHours(0, 0, 0, 0);
143
65
  for (const k in data) {