@enyo-energy/energy-app-sdk 0.0.38 → 0.0.39
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/dist/cjs/implementations/appliances/appliance-manager.d.cts +1 -1
- package/dist/cjs/implementations/appliances/in-memory-appliance-manager.cjs +281 -0
- package/dist/cjs/implementations/appliances/in-memory-appliance-manager.d.cts +119 -0
- package/dist/cjs/implementations/data-bus/demo-data-bus.cjs +246 -0
- package/dist/cjs/implementations/data-bus/demo-data-bus.d.cts +111 -0
- package/dist/cjs/types/enyo-data-bus-value.d.cts +2 -1
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/implementations/appliances/appliance-manager.d.ts +1 -1
- package/dist/implementations/appliances/demo-appliance-manager.d.ts +118 -0
- package/dist/implementations/appliances/demo-appliance-manager.js +277 -0
- package/dist/implementations/appliances/in-memory-appliance-manager.d.ts +119 -0
- package/dist/implementations/appliances/in-memory-appliance-manager.js +277 -0
- package/dist/implementations/data-bus/demo-data-bus.d.ts +111 -0
- package/dist/implementations/data-bus/demo-data-bus.js +242 -0
- package/dist/types/enyo-data-bus-value.d.ts +2 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { EnyoApplianceConnectionType, EnyoApplianceStateEnum } from "../../types/enyo-appliance.js";
|
|
2
|
+
import { ApplianceManager } from "./appliance-manager.js";
|
|
3
|
+
/**
|
|
4
|
+
* Demo implementation of ApplianceManager that stores all data in memory.
|
|
5
|
+
* This class provides the same interface as ApplianceManager but doesn't
|
|
6
|
+
* use energyApp.useAppliances() for persistence.
|
|
7
|
+
*/
|
|
8
|
+
export class InMemoryApplianceManager extends ApplianceManager {
|
|
9
|
+
memoryStore = new Map();
|
|
10
|
+
nextId = 1;
|
|
11
|
+
networkDevicesStore = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new DemoApplianceManager instance.
|
|
14
|
+
* @param energyApp The EnergyApp instance (not used for persistence in demo)
|
|
15
|
+
* @param config Configuration options for the manager
|
|
16
|
+
*/
|
|
17
|
+
constructor(energyApp, config) {
|
|
18
|
+
super(energyApp, config);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates or updates an appliance in memory.
|
|
22
|
+
* @param config The appliance configuration
|
|
23
|
+
* @param existingApplianceId Optional ID of an existing appliance to update
|
|
24
|
+
* @returns The ID of the created or updated appliance
|
|
25
|
+
*/
|
|
26
|
+
async createOrUpdateAppliance(config, existingApplianceId) {
|
|
27
|
+
const applianceId = existingApplianceId || `appliance_${this.nextId++}`;
|
|
28
|
+
// Build network device IDs list
|
|
29
|
+
const networkDeviceIds = config.networkDevices?.map(d => d.id) ?? [];
|
|
30
|
+
// Get existing metadata if updating
|
|
31
|
+
const existingAppliance = existingApplianceId ? this.memoryStore.get(existingApplianceId) : null;
|
|
32
|
+
// Merge metadata with defaults and existing values
|
|
33
|
+
const metadata = {
|
|
34
|
+
connectionType: config.metadata?.connectionType ||
|
|
35
|
+
existingAppliance?.metadata?.connectionType ||
|
|
36
|
+
EnyoApplianceConnectionType.Connector,
|
|
37
|
+
state: config.metadata?.state ||
|
|
38
|
+
existingAppliance?.metadata?.state ||
|
|
39
|
+
EnyoApplianceStateEnum.Connected,
|
|
40
|
+
...config.metadata
|
|
41
|
+
};
|
|
42
|
+
// Build appliance data
|
|
43
|
+
const applianceData = {
|
|
44
|
+
id: applianceId,
|
|
45
|
+
name: config.name,
|
|
46
|
+
type: config.type,
|
|
47
|
+
networkDeviceIds,
|
|
48
|
+
metadata,
|
|
49
|
+
...(config.topology && { topology: config.topology })
|
|
50
|
+
};
|
|
51
|
+
// Add type-specific metadata
|
|
52
|
+
if (config.typeMetadata) {
|
|
53
|
+
Object.assign(applianceData, config.typeMetadata);
|
|
54
|
+
}
|
|
55
|
+
// Save to memory store
|
|
56
|
+
this.memoryStore.set(applianceId, applianceData);
|
|
57
|
+
// Store network devices if provided
|
|
58
|
+
if (config.networkDevices) {
|
|
59
|
+
this.networkDevicesStore.set(applianceId, config.networkDevices);
|
|
60
|
+
}
|
|
61
|
+
// Update cache
|
|
62
|
+
this.updateCache(applianceData, config.networkDevices?.[0]);
|
|
63
|
+
console.log(`[DEMO] ${existingApplianceId ? 'Updated' : 'Created'} appliance ${applianceId} of type ${config.type}`);
|
|
64
|
+
return applianceId;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Gets an appliance by ID from memory.
|
|
68
|
+
* @param applianceId The ID of the appliance
|
|
69
|
+
* @returns The appliance or null if not found
|
|
70
|
+
*/
|
|
71
|
+
async getApplianceById(applianceId) {
|
|
72
|
+
return this.memoryStore.get(applianceId) || null;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Gets all appliances from memory.
|
|
76
|
+
* @returns Array of all appliances
|
|
77
|
+
*/
|
|
78
|
+
async getAllAppliances() {
|
|
79
|
+
return Array.from(this.memoryStore.values());
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Gets network devices associated with an appliance.
|
|
83
|
+
* @param appliance The appliance
|
|
84
|
+
* @returns Array of network devices
|
|
85
|
+
*/
|
|
86
|
+
async getNetworkDevicesForApplianceDemo(appliance) {
|
|
87
|
+
return this.networkDevicesStore.get(appliance.id) || [];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Finds appliances by their identifier using the configured strategy.
|
|
91
|
+
* @param identifier The identifier to search for
|
|
92
|
+
* @returns Array of matching appliances
|
|
93
|
+
*/
|
|
94
|
+
async findByIdentifier(identifier) {
|
|
95
|
+
const matches = [];
|
|
96
|
+
const strategy = this.getIdentifierStrategy();
|
|
97
|
+
for (const appliance of this.memoryStore.values()) {
|
|
98
|
+
const networkDevices = await this.getNetworkDevicesForApplianceDemo(appliance);
|
|
99
|
+
const extractedId = strategy.extract(appliance, networkDevices[0]);
|
|
100
|
+
if (extractedId === identifier) {
|
|
101
|
+
matches.push(appliance);
|
|
102
|
+
this.updateCache(appliance, networkDevices[0]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return matches;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Finds an appliance using multiple strategies.
|
|
109
|
+
* @param searchValue The value to search for
|
|
110
|
+
* @param strategies Array of strategies to try
|
|
111
|
+
* @returns The first matching result or undefined
|
|
112
|
+
*/
|
|
113
|
+
async findWithStrategies(searchValue, strategies) {
|
|
114
|
+
for (const strategy of strategies) {
|
|
115
|
+
for (const appliance of this.memoryStore.values()) {
|
|
116
|
+
const networkDevices = await this.getNetworkDevicesForApplianceDemo(appliance);
|
|
117
|
+
const identifier = strategy.extract(appliance, networkDevices[0]);
|
|
118
|
+
if (identifier === searchValue) {
|
|
119
|
+
return {
|
|
120
|
+
appliance,
|
|
121
|
+
identifier,
|
|
122
|
+
strategy: strategy.name
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets all appliances of a specific type.
|
|
131
|
+
* @param type The appliance type to filter by
|
|
132
|
+
* @returns Array of appliances of the specified type
|
|
133
|
+
*/
|
|
134
|
+
async getAppliancesByType(type) {
|
|
135
|
+
return Array.from(this.memoryStore.values()).filter(a => a.type === type);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Updates the state of an appliance in memory.
|
|
139
|
+
* @param applianceId The ID of the appliance to update
|
|
140
|
+
* @param connectionType The new connection type
|
|
141
|
+
* @param state The new state
|
|
142
|
+
*/
|
|
143
|
+
async updateApplianceState(applianceId, connectionType, state) {
|
|
144
|
+
const appliance = this.memoryStore.get(applianceId);
|
|
145
|
+
if (appliance) {
|
|
146
|
+
appliance.metadata = {
|
|
147
|
+
...appliance.metadata,
|
|
148
|
+
connectionType,
|
|
149
|
+
state
|
|
150
|
+
};
|
|
151
|
+
this.memoryStore.set(applianceId, appliance);
|
|
152
|
+
this.updateCache(appliance);
|
|
153
|
+
console.log(`[DEMO] Updated appliance ${applianceId} state to ${state}`);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
throw new Error(`Appliance ${applianceId} not found`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Updates metadata for an appliance in memory.
|
|
161
|
+
* @param applianceId The ID of the appliance
|
|
162
|
+
* @param metadata The metadata to update
|
|
163
|
+
*/
|
|
164
|
+
async updateApplianceMetadata(applianceId, metadata) {
|
|
165
|
+
const appliance = this.memoryStore.get(applianceId);
|
|
166
|
+
if (appliance) {
|
|
167
|
+
appliance.metadata = {
|
|
168
|
+
...appliance.metadata,
|
|
169
|
+
...metadata
|
|
170
|
+
};
|
|
171
|
+
this.memoryStore.set(applianceId, appliance);
|
|
172
|
+
this.updateCache(appliance);
|
|
173
|
+
console.log(`[DEMO] Updated metadata for appliance ${applianceId}`);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
throw new Error(`Appliance ${applianceId} not found`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Removes an appliance from memory.
|
|
181
|
+
* @param applianceId The ID of the appliance to remove
|
|
182
|
+
*/
|
|
183
|
+
async removeAppliance(applianceId) {
|
|
184
|
+
if (this.memoryStore.has(applianceId)) {
|
|
185
|
+
this.memoryStore.delete(applianceId);
|
|
186
|
+
this.networkDevicesStore.delete(applianceId);
|
|
187
|
+
// Clean up cache (from parent class)
|
|
188
|
+
this.clearCache();
|
|
189
|
+
console.log(`[DEMO] Removed appliance ${applianceId}`);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
throw new Error(`Appliance ${applianceId} not found`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Performs a bulk update on multiple appliances in memory.
|
|
197
|
+
* @param updates Array of updates to perform
|
|
198
|
+
* @returns Results of the bulk update operation
|
|
199
|
+
*/
|
|
200
|
+
async bulkUpdate(updates) {
|
|
201
|
+
const succeeded = [];
|
|
202
|
+
const failed = [];
|
|
203
|
+
for (const update of updates) {
|
|
204
|
+
try {
|
|
205
|
+
const appliance = this.memoryStore.get(update.applianceId);
|
|
206
|
+
if (appliance) {
|
|
207
|
+
const updatedAppliance = {
|
|
208
|
+
...appliance,
|
|
209
|
+
...update.data,
|
|
210
|
+
id: appliance.id // Ensure ID is preserved
|
|
211
|
+
};
|
|
212
|
+
this.memoryStore.set(update.applianceId, updatedAppliance);
|
|
213
|
+
this.updateCache(updatedAppliance);
|
|
214
|
+
succeeded.push(update.applianceId);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
failed.push(update.applianceId);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
console.error(`[DEMO] Bulk update failed for ${update.applianceId}: ${error}`);
|
|
222
|
+
failed.push(update.applianceId);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
console.log(`[DEMO] Bulk update completed: ${succeeded.length} succeeded, ${failed.length} failed`);
|
|
226
|
+
return { succeeded, failed };
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Gets statistics about the managed appliances in memory.
|
|
230
|
+
* @returns Statistics object
|
|
231
|
+
*/
|
|
232
|
+
async getStatistics() {
|
|
233
|
+
const appliances = Array.from(this.memoryStore.values());
|
|
234
|
+
const byType = {};
|
|
235
|
+
const byState = {};
|
|
236
|
+
for (const appliance of appliances) {
|
|
237
|
+
// Count by type
|
|
238
|
+
byType[appliance.type] = (byType[appliance.type] ?? 0) + 1;
|
|
239
|
+
// Count by state
|
|
240
|
+
const state = appliance.metadata?.state ?? 'unknown';
|
|
241
|
+
byState[state] = (byState[state] ?? 0) + 1;
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
total: appliances.length,
|
|
245
|
+
byType,
|
|
246
|
+
byState,
|
|
247
|
+
cached: this.memoryStore.size
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Refreshes the cache with all appliances from memory.
|
|
252
|
+
*/
|
|
253
|
+
async refreshCache() {
|
|
254
|
+
this.clearCache();
|
|
255
|
+
for (const appliance of this.memoryStore.values()) {
|
|
256
|
+
const networkDevices = await this.getNetworkDevicesForApplianceDemo(appliance);
|
|
257
|
+
this.updateCache(appliance, networkDevices[0]);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Clears all data from memory (for testing purposes).
|
|
262
|
+
*/
|
|
263
|
+
clearAllData() {
|
|
264
|
+
this.memoryStore.clear();
|
|
265
|
+
this.networkDevicesStore.clear();
|
|
266
|
+
this.clearCache();
|
|
267
|
+
this.nextId = 1;
|
|
268
|
+
console.log('[DEMO] All data cleared from memory');
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Gets the current size of the memory store.
|
|
272
|
+
* @returns The number of appliances in memory
|
|
273
|
+
*/
|
|
274
|
+
getMemoryStoreSize() {
|
|
275
|
+
return this.memoryStore.size;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { EnergyAppDataBus, EnergyAppDataBusSendDataOptions } from "../../packages/energy-app-data-bus.js";
|
|
2
|
+
import { EnyoDataBusMessage, EnyoDataBusMessageAnswer, EnyoDataBusMessageEnum } from "../../types/enyo-data-bus-value.js";
|
|
3
|
+
/**
|
|
4
|
+
* Demo implementation of EnergyAppDataBus that logs messages to console
|
|
5
|
+
* and maintains an in-memory message history and listener registry.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DemoDataBus implements EnergyAppDataBus {
|
|
8
|
+
private enableConsoleLogging;
|
|
9
|
+
private messageHistory;
|
|
10
|
+
private listeners;
|
|
11
|
+
private nextListenerId;
|
|
12
|
+
private maxHistorySize;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new DemoDataBus instance.
|
|
15
|
+
* @param enableConsoleLogging Whether to log messages to console (default: true)
|
|
16
|
+
* @param maxHistorySize Maximum number of messages to keep in history (default: 1000)
|
|
17
|
+
*/
|
|
18
|
+
constructor(enableConsoleLogging?: boolean, maxHistorySize?: number);
|
|
19
|
+
/**
|
|
20
|
+
* Sends messages to the data bus (logs to console and notifies listeners).
|
|
21
|
+
* @param messages Array of messages to send
|
|
22
|
+
* @param options Optional send options (not used in demo)
|
|
23
|
+
*/
|
|
24
|
+
sendMessage(messages: EnyoDataBusMessage[], options?: EnergyAppDataBusSendDataOptions): void;
|
|
25
|
+
/**
|
|
26
|
+
* Sends an answer message to the data bus.
|
|
27
|
+
* @param answer The answer message to send
|
|
28
|
+
* @param options Optional send options (not used in demo)
|
|
29
|
+
*/
|
|
30
|
+
sendAnswer(answer: EnyoDataBusMessageAnswer, options?: EnergyAppDataBusSendDataOptions): void;
|
|
31
|
+
/**
|
|
32
|
+
* Registers a listener for specific message types.
|
|
33
|
+
* @param types Array of message types to listen for
|
|
34
|
+
* @param listener Callback function to invoke when matching messages are received
|
|
35
|
+
* @returns Listener ID that can be used to unsubscribe
|
|
36
|
+
*/
|
|
37
|
+
listenForMessages(types: EnyoDataBusMessageEnum[], listener: (entry: EnyoDataBusMessage) => void): string;
|
|
38
|
+
/**
|
|
39
|
+
* Unsubscribes a listener from the data bus.
|
|
40
|
+
* @param listenerId The ID of the listener to unsubscribe
|
|
41
|
+
*/
|
|
42
|
+
unsubscribe(listenerId: string): void;
|
|
43
|
+
/**
|
|
44
|
+
* Adds a message to the history, maintaining the maximum size limit.
|
|
45
|
+
* @param message The message to add to history
|
|
46
|
+
*/
|
|
47
|
+
private addToHistory;
|
|
48
|
+
/**
|
|
49
|
+
* Notifies all registered listeners that match the message type.
|
|
50
|
+
* @param message The message to notify listeners about
|
|
51
|
+
*/
|
|
52
|
+
private notifyListeners;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the message history.
|
|
55
|
+
* @param limit Optional limit on the number of messages to return
|
|
56
|
+
* @returns Array of messages from history
|
|
57
|
+
*/
|
|
58
|
+
getMessageHistory(limit?: number): EnyoDataBusMessage[];
|
|
59
|
+
/**
|
|
60
|
+
* Gets messages of specific types from history.
|
|
61
|
+
* @param types Array of message types to filter by
|
|
62
|
+
* @param limit Optional limit on the number of messages to return
|
|
63
|
+
* @returns Filtered array of messages
|
|
64
|
+
*/
|
|
65
|
+
getMessagesByType(types: EnyoDataBusMessageEnum[], limit?: number): EnyoDataBusMessage[];
|
|
66
|
+
/**
|
|
67
|
+
* Gets messages for a specific appliance from history.
|
|
68
|
+
* @param applianceId The appliance ID to filter by
|
|
69
|
+
* @param limit Optional limit on the number of messages to return
|
|
70
|
+
* @returns Filtered array of messages
|
|
71
|
+
*/
|
|
72
|
+
getMessagesByAppliance(applianceId: string, limit?: number): EnyoDataBusMessage[];
|
|
73
|
+
/**
|
|
74
|
+
* Clears the message history.
|
|
75
|
+
*/
|
|
76
|
+
clearHistory(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the current number of registered listeners.
|
|
79
|
+
* @returns The number of active listeners
|
|
80
|
+
*/
|
|
81
|
+
getListenerCount(): number;
|
|
82
|
+
/**
|
|
83
|
+
* Gets information about all registered listeners.
|
|
84
|
+
* @returns Array of listener information
|
|
85
|
+
*/
|
|
86
|
+
getListenerInfo(): Array<{
|
|
87
|
+
id: string;
|
|
88
|
+
types: EnyoDataBusMessageEnum[];
|
|
89
|
+
}>;
|
|
90
|
+
/**
|
|
91
|
+
* Enables or disables console logging.
|
|
92
|
+
* @param enabled Whether to enable console logging
|
|
93
|
+
*/
|
|
94
|
+
setConsoleLogging(enabled: boolean): void;
|
|
95
|
+
/**
|
|
96
|
+
* Simulates receiving a message from an external source.
|
|
97
|
+
* This is useful for testing listeners without actually sending messages.
|
|
98
|
+
* @param message The message to simulate receiving
|
|
99
|
+
*/
|
|
100
|
+
simulateIncomingMessage(message: EnyoDataBusMessage): void;
|
|
101
|
+
/**
|
|
102
|
+
* Gets statistics about the data bus usage.
|
|
103
|
+
* @returns Statistics object
|
|
104
|
+
*/
|
|
105
|
+
getStatistics(): {
|
|
106
|
+
totalMessages: number;
|
|
107
|
+
messagesByType: Record<string, number>;
|
|
108
|
+
activeListeners: number;
|
|
109
|
+
messagesInLastHour: number;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Demo implementation of EnergyAppDataBus that logs messages to console
|
|
3
|
+
* and maintains an in-memory message history and listener registry.
|
|
4
|
+
*/
|
|
5
|
+
export class DemoDataBus {
|
|
6
|
+
enableConsoleLogging;
|
|
7
|
+
messageHistory = [];
|
|
8
|
+
listeners = new Map();
|
|
9
|
+
nextListenerId = 1;
|
|
10
|
+
maxHistorySize = 1000;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new DemoDataBus instance.
|
|
13
|
+
* @param enableConsoleLogging Whether to log messages to console (default: true)
|
|
14
|
+
* @param maxHistorySize Maximum number of messages to keep in history (default: 1000)
|
|
15
|
+
*/
|
|
16
|
+
constructor(enableConsoleLogging = true, maxHistorySize) {
|
|
17
|
+
this.enableConsoleLogging = enableConsoleLogging;
|
|
18
|
+
if (maxHistorySize !== undefined) {
|
|
19
|
+
this.maxHistorySize = maxHistorySize;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Sends messages to the data bus (logs to console and notifies listeners).
|
|
24
|
+
* @param messages Array of messages to send
|
|
25
|
+
* @param options Optional send options (not used in demo)
|
|
26
|
+
*/
|
|
27
|
+
sendMessage(messages, options) {
|
|
28
|
+
for (const message of messages) {
|
|
29
|
+
// Log to console if enabled
|
|
30
|
+
if (this.enableConsoleLogging) {
|
|
31
|
+
console.log('[DEMO DataBus] Message sent:', {
|
|
32
|
+
id: message.id,
|
|
33
|
+
type: message.message,
|
|
34
|
+
applianceId: message.applianceId,
|
|
35
|
+
timestamp: message.timestampIso,
|
|
36
|
+
data: message.data
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// Add to history
|
|
40
|
+
this.addToHistory(message);
|
|
41
|
+
// Notify listeners
|
|
42
|
+
this.notifyListeners(message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sends an answer message to the data bus.
|
|
47
|
+
* @param answer The answer message to send
|
|
48
|
+
* @param options Optional send options (not used in demo)
|
|
49
|
+
*/
|
|
50
|
+
sendAnswer(answer, options) {
|
|
51
|
+
// Log to console if enabled
|
|
52
|
+
if (this.enableConsoleLogging) {
|
|
53
|
+
console.log('[DEMO DataBus] Answer sent:', {
|
|
54
|
+
id: answer.id,
|
|
55
|
+
type: answer.message,
|
|
56
|
+
status: answer.data.status,
|
|
57
|
+
applianceId: answer.applianceId,
|
|
58
|
+
timestamp: answer.timestampIso
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// Add to history
|
|
62
|
+
this.addToHistory(answer);
|
|
63
|
+
// Notify listeners
|
|
64
|
+
this.notifyListeners(answer);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Registers a listener for specific message types.
|
|
68
|
+
* @param types Array of message types to listen for
|
|
69
|
+
* @param listener Callback function to invoke when matching messages are received
|
|
70
|
+
* @returns Listener ID that can be used to unsubscribe
|
|
71
|
+
*/
|
|
72
|
+
listenForMessages(types, listener) {
|
|
73
|
+
const listenerId = `listener_${this.nextListenerId++}`;
|
|
74
|
+
this.listeners.set(listenerId, {
|
|
75
|
+
types,
|
|
76
|
+
callback: listener
|
|
77
|
+
});
|
|
78
|
+
if (this.enableConsoleLogging) {
|
|
79
|
+
console.log(`[DEMO DataBus] Listener ${listenerId} registered for types:`, types);
|
|
80
|
+
}
|
|
81
|
+
return listenerId;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Unsubscribes a listener from the data bus.
|
|
85
|
+
* @param listenerId The ID of the listener to unsubscribe
|
|
86
|
+
*/
|
|
87
|
+
unsubscribe(listenerId) {
|
|
88
|
+
if (this.listeners.has(listenerId)) {
|
|
89
|
+
const listener = this.listeners.get(listenerId);
|
|
90
|
+
this.listeners.delete(listenerId);
|
|
91
|
+
if (this.enableConsoleLogging) {
|
|
92
|
+
console.log(`[DEMO DataBus] Listener ${listenerId} unsubscribed`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
console.warn(`[DEMO DataBus] Attempted to unsubscribe non-existent listener: ${listenerId}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Adds a message to the history, maintaining the maximum size limit.
|
|
101
|
+
* @param message The message to add to history
|
|
102
|
+
*/
|
|
103
|
+
addToHistory(message) {
|
|
104
|
+
this.messageHistory.push(message);
|
|
105
|
+
// Trim history if it exceeds the maximum size
|
|
106
|
+
if (this.messageHistory.length > this.maxHistorySize) {
|
|
107
|
+
this.messageHistory = this.messageHistory.slice(-this.maxHistorySize);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Notifies all registered listeners that match the message type.
|
|
112
|
+
* @param message The message to notify listeners about
|
|
113
|
+
*/
|
|
114
|
+
notifyListeners(message) {
|
|
115
|
+
for (const [listenerId, listener] of this.listeners.entries()) {
|
|
116
|
+
if (listener.types.includes(message.message)) {
|
|
117
|
+
try {
|
|
118
|
+
listener.callback(message);
|
|
119
|
+
if (this.enableConsoleLogging) {
|
|
120
|
+
console.log(`[DEMO DataBus] Notified listener ${listenerId} of ${message.message}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.error(`[DEMO DataBus] Error in listener ${listenerId}:`, error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the message history.
|
|
131
|
+
* @param limit Optional limit on the number of messages to return
|
|
132
|
+
* @returns Array of messages from history
|
|
133
|
+
*/
|
|
134
|
+
getMessageHistory(limit) {
|
|
135
|
+
if (limit && limit > 0) {
|
|
136
|
+
return this.messageHistory.slice(-limit);
|
|
137
|
+
}
|
|
138
|
+
return [...this.messageHistory];
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Gets messages of specific types from history.
|
|
142
|
+
* @param types Array of message types to filter by
|
|
143
|
+
* @param limit Optional limit on the number of messages to return
|
|
144
|
+
* @returns Filtered array of messages
|
|
145
|
+
*/
|
|
146
|
+
getMessagesByType(types, limit) {
|
|
147
|
+
const filtered = this.messageHistory.filter(msg => types.includes(msg.message));
|
|
148
|
+
if (limit && limit > 0) {
|
|
149
|
+
return filtered.slice(-limit);
|
|
150
|
+
}
|
|
151
|
+
return filtered;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Gets messages for a specific appliance from history.
|
|
155
|
+
* @param applianceId The appliance ID to filter by
|
|
156
|
+
* @param limit Optional limit on the number of messages to return
|
|
157
|
+
* @returns Filtered array of messages
|
|
158
|
+
*/
|
|
159
|
+
getMessagesByAppliance(applianceId, limit) {
|
|
160
|
+
const filtered = this.messageHistory.filter(msg => msg.applianceId === applianceId);
|
|
161
|
+
if (limit && limit > 0) {
|
|
162
|
+
return filtered.slice(-limit);
|
|
163
|
+
}
|
|
164
|
+
return filtered;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Clears the message history.
|
|
168
|
+
*/
|
|
169
|
+
clearHistory() {
|
|
170
|
+
this.messageHistory = [];
|
|
171
|
+
if (this.enableConsoleLogging) {
|
|
172
|
+
console.log('[DEMO DataBus] Message history cleared');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Gets the current number of registered listeners.
|
|
177
|
+
* @returns The number of active listeners
|
|
178
|
+
*/
|
|
179
|
+
getListenerCount() {
|
|
180
|
+
return this.listeners.size;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Gets information about all registered listeners.
|
|
184
|
+
* @returns Array of listener information
|
|
185
|
+
*/
|
|
186
|
+
getListenerInfo() {
|
|
187
|
+
return Array.from(this.listeners.entries()).map(([id, listener]) => ({
|
|
188
|
+
id,
|
|
189
|
+
types: listener.types
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Enables or disables console logging.
|
|
194
|
+
* @param enabled Whether to enable console logging
|
|
195
|
+
*/
|
|
196
|
+
setConsoleLogging(enabled) {
|
|
197
|
+
this.enableConsoleLogging = enabled;
|
|
198
|
+
console.log(`[DEMO DataBus] Console logging ${enabled ? 'enabled' : 'disabled'}`);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Simulates receiving a message from an external source.
|
|
202
|
+
* This is useful for testing listeners without actually sending messages.
|
|
203
|
+
* @param message The message to simulate receiving
|
|
204
|
+
*/
|
|
205
|
+
simulateIncomingMessage(message) {
|
|
206
|
+
if (this.enableConsoleLogging) {
|
|
207
|
+
console.log('[DEMO DataBus] Simulating incoming message:', {
|
|
208
|
+
id: message.id,
|
|
209
|
+
type: message.message,
|
|
210
|
+
applianceId: message.applianceId
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
// Add to history
|
|
214
|
+
this.addToHistory(message);
|
|
215
|
+
// Notify listeners
|
|
216
|
+
this.notifyListeners(message);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Gets statistics about the data bus usage.
|
|
220
|
+
* @returns Statistics object
|
|
221
|
+
*/
|
|
222
|
+
getStatistics() {
|
|
223
|
+
const now = new Date();
|
|
224
|
+
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
|
|
225
|
+
const messagesByType = {};
|
|
226
|
+
let messagesInLastHour = 0;
|
|
227
|
+
for (const message of this.messageHistory) {
|
|
228
|
+
// Count by type
|
|
229
|
+
messagesByType[message.message] = (messagesByType[message.message] ?? 0) + 1;
|
|
230
|
+
// Count messages in last hour
|
|
231
|
+
if (new Date(message.timestampIso) > oneHourAgo) {
|
|
232
|
+
messagesInLastHour++;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
totalMessages: this.messageHistory.length,
|
|
237
|
+
messagesByType,
|
|
238
|
+
activeListeners: this.listeners.size,
|
|
239
|
+
messagesInLastHour
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -109,6 +109,7 @@ export declare enum EnyoDataBusMessageEnum {
|
|
|
109
109
|
AggregatedStateUpdateV1 = "AggregatedStateUpdateV1",
|
|
110
110
|
EnergyTariffUpdateV1 = "EnergyTariffUpdateV1"
|
|
111
111
|
}
|
|
112
|
+
export type EnyoDataBusMessageResolution = '10s' | '30s' | '1m' | '15m' | '1h' | '1d' | 'dynamic';
|
|
112
113
|
export interface EnyoDataBusMessage {
|
|
113
114
|
id: string;
|
|
114
115
|
message: EnyoDataBusMessageEnum;
|
|
@@ -118,7 +119,7 @@ export interface EnyoDataBusMessage {
|
|
|
118
119
|
clockId?: string;
|
|
119
120
|
timestampIso: string;
|
|
120
121
|
/** If you just forward events that occur, use dynamic as resolution */
|
|
121
|
-
resolution?:
|
|
122
|
+
resolution?: EnyoDataBusMessageResolution;
|
|
122
123
|
data: object;
|
|
123
124
|
}
|
|
124
125
|
export interface EnyoDataBusMessageAnswer extends EnyoDataBusMessage {
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED