forked from oweitman/ioBroker.rssfeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
101 lines (92 loc) · 2.87 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const RssFeedRequire = require('./lib/rssFeedServer');
let rssFeedServer;
class RssFeed extends utils.Adapter {
/**
* @param [options] adapter options
*/
constructor(options) {
super({
...options,
name: 'rssfeed',
});
this.on('ready', this.onReady.bind(this));
this.on('objectChange', this.onObjectChange.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.log.debug('main onReady start');
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
// Initialize your adapter here
if (!rssFeedServer) {
this.log.debug('main onReady open rssfeed');
rssFeedServer = new RssFeedRequire(this);
this.subscribeStates('*');
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
*
* @param callback callback to call
*/
onUnload(callback) {
try {
this.log.debug('main onUnload try');
rssFeedServer.closeConnections();
this.log.info('cleaned everything up...');
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
callback();
} catch {
this.log.debug('main onUnload error');
callback();
}
}
/**
* Is called if a subscribed object changes
*
* @param id objct id
* @param obj object
*/
onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.silly(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
this.log.silly(`object ${id} deleted`);
}
}
onStateChange(id, state) {
if (state) {
// The state was changed
this.log.silly(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
rssFeedServer.stateChange(id, state);
} else {
// The state was deleted
this.log.silly(`state ${id} deleted`);
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param [options] adapter options
*/
module.exports = options => new RssFeed(options);
} else {
// otherwise start the instance directly
new RssFeed();
}