@enyo-energy/energy-app-sdk 0.0.37 → 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.cjs +399 -0
- package/dist/cjs/implementations/appliances/appliance-manager.d.cts +191 -0
- package/dist/cjs/implementations/appliances/identifier-strategies.cjs +180 -0
- package/dist/cjs/implementations/appliances/identifier-strategies.d.cts +140 -0
- 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/implementations/modbus/EnergyAppModbusDataTypeConverter.d.cts +2 -2
- package/dist/cjs/implementations/modbus/EnergyAppModbusFaultTolerantReader.cjs +76 -0
- package/dist/cjs/implementations/modbus/EnergyAppModbusFaultTolerantReader.d.cts +31 -1
- package/dist/cjs/implementations/modbus/EnergyAppModbusRegisterMapper.d.cts +2 -2
- package/dist/cjs/implementations/modbus/interfaces.d.cts +7 -93
- package/dist/cjs/implementations/modbus/sunspec/sunspec-devices.cjs +342 -0
- package/dist/cjs/implementations/modbus/sunspec/sunspec-devices.d.cts +95 -0
- package/dist/cjs/implementations/modbus/sunspec/sunspec-modbus-client.cjs +433 -0
- package/dist/cjs/implementations/modbus/sunspec/sunspec-modbus-client.d.cts +171 -0
- package/dist/cjs/index.cjs +2 -3
- package/dist/cjs/index.d.cts +2 -3
- 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 +191 -0
- package/dist/implementations/appliances/appliance-manager.js +395 -0
- 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/identifier-strategies.d.ts +140 -0
- package/dist/implementations/appliances/identifier-strategies.js +171 -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/implementations/modbus/EnergyAppModbusDataTypeConverter.d.ts +2 -2
- package/dist/implementations/modbus/EnergyAppModbusFaultTolerantReader.d.ts +31 -1
- package/dist/implementations/modbus/EnergyAppModbusFaultTolerantReader.js +76 -0
- package/dist/implementations/modbus/EnergyAppModbusRegisterMapper.d.ts +2 -2
- package/dist/implementations/modbus/interfaces.d.ts +7 -93
- package/dist/implementations/modbus/sunspec/sunspec-devices.d.ts +95 -0
- package/dist/implementations/modbus/sunspec/sunspec-devices.js +335 -0
- package/dist/implementations/modbus/sunspec/sunspec-modbus-client.d.ts +171 -0
- package/dist/implementations/modbus/sunspec/sunspec-modbus-client.js +429 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +2 -3
- 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,119 @@
|
|
|
1
|
+
import type { EnergyApp } from "../../index.cjs";
|
|
2
|
+
import type { EnyoNetworkDevice } from "../../types/enyo-network-device.cjs";
|
|
3
|
+
import { EnyoAppliance, EnyoApplianceConnectionType, EnyoApplianceMetadata, EnyoApplianceStateEnum, EnyoApplianceTypeEnum } from "../../types/enyo-appliance.cjs";
|
|
4
|
+
import { ApplianceConfig, ApplianceManager, ApplianceManagerConfig, FindResult } from "./appliance-manager.cjs";
|
|
5
|
+
import { IdentifierStrategy } from "./identifier-strategies.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* Demo implementation of ApplianceManager that stores all data in memory.
|
|
8
|
+
* This class provides the same interface as ApplianceManager but doesn't
|
|
9
|
+
* use energyApp.useAppliances() for persistence.
|
|
10
|
+
*/
|
|
11
|
+
export declare class InMemoryApplianceManager extends ApplianceManager {
|
|
12
|
+
private memoryStore;
|
|
13
|
+
private nextId;
|
|
14
|
+
private networkDevicesStore;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new DemoApplianceManager instance.
|
|
17
|
+
* @param energyApp The EnergyApp instance (not used for persistence in demo)
|
|
18
|
+
* @param config Configuration options for the manager
|
|
19
|
+
*/
|
|
20
|
+
constructor(energyApp: EnergyApp, config?: ApplianceManagerConfig);
|
|
21
|
+
/**
|
|
22
|
+
* Creates or updates an appliance in memory.
|
|
23
|
+
* @param config The appliance configuration
|
|
24
|
+
* @param existingApplianceId Optional ID of an existing appliance to update
|
|
25
|
+
* @returns The ID of the created or updated appliance
|
|
26
|
+
*/
|
|
27
|
+
createOrUpdateAppliance(config: ApplianceConfig, existingApplianceId?: string): Promise<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Gets an appliance by ID from memory.
|
|
30
|
+
* @param applianceId The ID of the appliance
|
|
31
|
+
* @returns The appliance or null if not found
|
|
32
|
+
*/
|
|
33
|
+
getApplianceById(applianceId: string): Promise<EnyoAppliance | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Gets all appliances from memory.
|
|
36
|
+
* @returns Array of all appliances
|
|
37
|
+
*/
|
|
38
|
+
getAllAppliances(): Promise<EnyoAppliance[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Gets network devices associated with an appliance.
|
|
41
|
+
* @param appliance The appliance
|
|
42
|
+
* @returns Array of network devices
|
|
43
|
+
*/
|
|
44
|
+
protected getNetworkDevicesForApplianceDemo(appliance: EnyoAppliance): Promise<EnyoNetworkDevice[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Finds appliances by their identifier using the configured strategy.
|
|
47
|
+
* @param identifier The identifier to search for
|
|
48
|
+
* @returns Array of matching appliances
|
|
49
|
+
*/
|
|
50
|
+
findByIdentifier(identifier: string): Promise<EnyoAppliance[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Finds an appliance using multiple strategies.
|
|
53
|
+
* @param searchValue The value to search for
|
|
54
|
+
* @param strategies Array of strategies to try
|
|
55
|
+
* @returns The first matching result or undefined
|
|
56
|
+
*/
|
|
57
|
+
findWithStrategies(searchValue: string, strategies: IdentifierStrategy[]): Promise<FindResult | undefined>;
|
|
58
|
+
/**
|
|
59
|
+
* Gets all appliances of a specific type.
|
|
60
|
+
* @param type The appliance type to filter by
|
|
61
|
+
* @returns Array of appliances of the specified type
|
|
62
|
+
*/
|
|
63
|
+
getAppliancesByType(type: EnyoApplianceTypeEnum): Promise<EnyoAppliance[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Updates the state of an appliance in memory.
|
|
66
|
+
* @param applianceId The ID of the appliance to update
|
|
67
|
+
* @param connectionType The new connection type
|
|
68
|
+
* @param state The new state
|
|
69
|
+
*/
|
|
70
|
+
updateApplianceState(applianceId: string, connectionType: EnyoApplianceConnectionType, state: EnyoApplianceStateEnum): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Updates metadata for an appliance in memory.
|
|
73
|
+
* @param applianceId The ID of the appliance
|
|
74
|
+
* @param metadata The metadata to update
|
|
75
|
+
*/
|
|
76
|
+
updateApplianceMetadata(applianceId: string, metadata: Partial<EnyoApplianceMetadata> & {
|
|
77
|
+
connectionType: EnyoApplianceConnectionType;
|
|
78
|
+
}): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Removes an appliance from memory.
|
|
81
|
+
* @param applianceId The ID of the appliance to remove
|
|
82
|
+
*/
|
|
83
|
+
removeAppliance(applianceId: string): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Performs a bulk update on multiple appliances in memory.
|
|
86
|
+
* @param updates Array of updates to perform
|
|
87
|
+
* @returns Results of the bulk update operation
|
|
88
|
+
*/
|
|
89
|
+
bulkUpdate(updates: Array<{
|
|
90
|
+
applianceId: string;
|
|
91
|
+
data: Partial<Omit<EnyoAppliance, 'id'>>;
|
|
92
|
+
}>): Promise<{
|
|
93
|
+
succeeded: string[];
|
|
94
|
+
failed: string[];
|
|
95
|
+
}>;
|
|
96
|
+
/**
|
|
97
|
+
* Gets statistics about the managed appliances in memory.
|
|
98
|
+
* @returns Statistics object
|
|
99
|
+
*/
|
|
100
|
+
getStatistics(): Promise<{
|
|
101
|
+
total: number;
|
|
102
|
+
byType: Record<string, number>;
|
|
103
|
+
byState: Record<string, number>;
|
|
104
|
+
cached: number;
|
|
105
|
+
}>;
|
|
106
|
+
/**
|
|
107
|
+
* Refreshes the cache with all appliances from memory.
|
|
108
|
+
*/
|
|
109
|
+
refreshCache(): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Clears all data from memory (for testing purposes).
|
|
112
|
+
*/
|
|
113
|
+
clearAllData(): void;
|
|
114
|
+
/**
|
|
115
|
+
* Gets the current size of the memory store.
|
|
116
|
+
* @returns The number of appliances in memory
|
|
117
|
+
*/
|
|
118
|
+
getMemoryStoreSize(): number;
|
|
119
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DemoDataBus = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Demo implementation of EnergyAppDataBus that logs messages to console
|
|
6
|
+
* and maintains an in-memory message history and listener registry.
|
|
7
|
+
*/
|
|
8
|
+
class DemoDataBus {
|
|
9
|
+
enableConsoleLogging;
|
|
10
|
+
messageHistory = [];
|
|
11
|
+
listeners = new Map();
|
|
12
|
+
nextListenerId = 1;
|
|
13
|
+
maxHistorySize = 1000;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new DemoDataBus instance.
|
|
16
|
+
* @param enableConsoleLogging Whether to log messages to console (default: true)
|
|
17
|
+
* @param maxHistorySize Maximum number of messages to keep in history (default: 1000)
|
|
18
|
+
*/
|
|
19
|
+
constructor(enableConsoleLogging = true, maxHistorySize) {
|
|
20
|
+
this.enableConsoleLogging = enableConsoleLogging;
|
|
21
|
+
if (maxHistorySize !== undefined) {
|
|
22
|
+
this.maxHistorySize = maxHistorySize;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Sends messages to the data bus (logs to console and notifies listeners).
|
|
27
|
+
* @param messages Array of messages to send
|
|
28
|
+
* @param options Optional send options (not used in demo)
|
|
29
|
+
*/
|
|
30
|
+
sendMessage(messages, options) {
|
|
31
|
+
for (const message of messages) {
|
|
32
|
+
// Log to console if enabled
|
|
33
|
+
if (this.enableConsoleLogging) {
|
|
34
|
+
console.log('[DEMO DataBus] Message sent:', {
|
|
35
|
+
id: message.id,
|
|
36
|
+
type: message.message,
|
|
37
|
+
applianceId: message.applianceId,
|
|
38
|
+
timestamp: message.timestampIso,
|
|
39
|
+
data: message.data
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
// Add to history
|
|
43
|
+
this.addToHistory(message);
|
|
44
|
+
// Notify listeners
|
|
45
|
+
this.notifyListeners(message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sends an answer message to the data bus.
|
|
50
|
+
* @param answer The answer message to send
|
|
51
|
+
* @param options Optional send options (not used in demo)
|
|
52
|
+
*/
|
|
53
|
+
sendAnswer(answer, options) {
|
|
54
|
+
// Log to console if enabled
|
|
55
|
+
if (this.enableConsoleLogging) {
|
|
56
|
+
console.log('[DEMO DataBus] Answer sent:', {
|
|
57
|
+
id: answer.id,
|
|
58
|
+
type: answer.message,
|
|
59
|
+
status: answer.data.status,
|
|
60
|
+
applianceId: answer.applianceId,
|
|
61
|
+
timestamp: answer.timestampIso
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
// Add to history
|
|
65
|
+
this.addToHistory(answer);
|
|
66
|
+
// Notify listeners
|
|
67
|
+
this.notifyListeners(answer);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Registers a listener for specific message types.
|
|
71
|
+
* @param types Array of message types to listen for
|
|
72
|
+
* @param listener Callback function to invoke when matching messages are received
|
|
73
|
+
* @returns Listener ID that can be used to unsubscribe
|
|
74
|
+
*/
|
|
75
|
+
listenForMessages(types, listener) {
|
|
76
|
+
const listenerId = `listener_${this.nextListenerId++}`;
|
|
77
|
+
this.listeners.set(listenerId, {
|
|
78
|
+
types,
|
|
79
|
+
callback: listener
|
|
80
|
+
});
|
|
81
|
+
if (this.enableConsoleLogging) {
|
|
82
|
+
console.log(`[DEMO DataBus] Listener ${listenerId} registered for types:`, types);
|
|
83
|
+
}
|
|
84
|
+
return listenerId;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Unsubscribes a listener from the data bus.
|
|
88
|
+
* @param listenerId The ID of the listener to unsubscribe
|
|
89
|
+
*/
|
|
90
|
+
unsubscribe(listenerId) {
|
|
91
|
+
if (this.listeners.has(listenerId)) {
|
|
92
|
+
const listener = this.listeners.get(listenerId);
|
|
93
|
+
this.listeners.delete(listenerId);
|
|
94
|
+
if (this.enableConsoleLogging) {
|
|
95
|
+
console.log(`[DEMO DataBus] Listener ${listenerId} unsubscribed`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
console.warn(`[DEMO DataBus] Attempted to unsubscribe non-existent listener: ${listenerId}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Adds a message to the history, maintaining the maximum size limit.
|
|
104
|
+
* @param message The message to add to history
|
|
105
|
+
*/
|
|
106
|
+
addToHistory(message) {
|
|
107
|
+
this.messageHistory.push(message);
|
|
108
|
+
// Trim history if it exceeds the maximum size
|
|
109
|
+
if (this.messageHistory.length > this.maxHistorySize) {
|
|
110
|
+
this.messageHistory = this.messageHistory.slice(-this.maxHistorySize);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Notifies all registered listeners that match the message type.
|
|
115
|
+
* @param message The message to notify listeners about
|
|
116
|
+
*/
|
|
117
|
+
notifyListeners(message) {
|
|
118
|
+
for (const [listenerId, listener] of this.listeners.entries()) {
|
|
119
|
+
if (listener.types.includes(message.message)) {
|
|
120
|
+
try {
|
|
121
|
+
listener.callback(message);
|
|
122
|
+
if (this.enableConsoleLogging) {
|
|
123
|
+
console.log(`[DEMO DataBus] Notified listener ${listenerId} of ${message.message}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
console.error(`[DEMO DataBus] Error in listener ${listenerId}:`, error);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Gets the message history.
|
|
134
|
+
* @param limit Optional limit on the number of messages to return
|
|
135
|
+
* @returns Array of messages from history
|
|
136
|
+
*/
|
|
137
|
+
getMessageHistory(limit) {
|
|
138
|
+
if (limit && limit > 0) {
|
|
139
|
+
return this.messageHistory.slice(-limit);
|
|
140
|
+
}
|
|
141
|
+
return [...this.messageHistory];
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Gets messages of specific types from history.
|
|
145
|
+
* @param types Array of message types to filter by
|
|
146
|
+
* @param limit Optional limit on the number of messages to return
|
|
147
|
+
* @returns Filtered array of messages
|
|
148
|
+
*/
|
|
149
|
+
getMessagesByType(types, limit) {
|
|
150
|
+
const filtered = this.messageHistory.filter(msg => types.includes(msg.message));
|
|
151
|
+
if (limit && limit > 0) {
|
|
152
|
+
return filtered.slice(-limit);
|
|
153
|
+
}
|
|
154
|
+
return filtered;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Gets messages for a specific appliance from history.
|
|
158
|
+
* @param applianceId The appliance ID to filter by
|
|
159
|
+
* @param limit Optional limit on the number of messages to return
|
|
160
|
+
* @returns Filtered array of messages
|
|
161
|
+
*/
|
|
162
|
+
getMessagesByAppliance(applianceId, limit) {
|
|
163
|
+
const filtered = this.messageHistory.filter(msg => msg.applianceId === applianceId);
|
|
164
|
+
if (limit && limit > 0) {
|
|
165
|
+
return filtered.slice(-limit);
|
|
166
|
+
}
|
|
167
|
+
return filtered;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Clears the message history.
|
|
171
|
+
*/
|
|
172
|
+
clearHistory() {
|
|
173
|
+
this.messageHistory = [];
|
|
174
|
+
if (this.enableConsoleLogging) {
|
|
175
|
+
console.log('[DEMO DataBus] Message history cleared');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Gets the current number of registered listeners.
|
|
180
|
+
* @returns The number of active listeners
|
|
181
|
+
*/
|
|
182
|
+
getListenerCount() {
|
|
183
|
+
return this.listeners.size;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Gets information about all registered listeners.
|
|
187
|
+
* @returns Array of listener information
|
|
188
|
+
*/
|
|
189
|
+
getListenerInfo() {
|
|
190
|
+
return Array.from(this.listeners.entries()).map(([id, listener]) => ({
|
|
191
|
+
id,
|
|
192
|
+
types: listener.types
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Enables or disables console logging.
|
|
197
|
+
* @param enabled Whether to enable console logging
|
|
198
|
+
*/
|
|
199
|
+
setConsoleLogging(enabled) {
|
|
200
|
+
this.enableConsoleLogging = enabled;
|
|
201
|
+
console.log(`[DEMO DataBus] Console logging ${enabled ? 'enabled' : 'disabled'}`);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Simulates receiving a message from an external source.
|
|
205
|
+
* This is useful for testing listeners without actually sending messages.
|
|
206
|
+
* @param message The message to simulate receiving
|
|
207
|
+
*/
|
|
208
|
+
simulateIncomingMessage(message) {
|
|
209
|
+
if (this.enableConsoleLogging) {
|
|
210
|
+
console.log('[DEMO DataBus] Simulating incoming message:', {
|
|
211
|
+
id: message.id,
|
|
212
|
+
type: message.message,
|
|
213
|
+
applianceId: message.applianceId
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
// Add to history
|
|
217
|
+
this.addToHistory(message);
|
|
218
|
+
// Notify listeners
|
|
219
|
+
this.notifyListeners(message);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Gets statistics about the data bus usage.
|
|
223
|
+
* @returns Statistics object
|
|
224
|
+
*/
|
|
225
|
+
getStatistics() {
|
|
226
|
+
const now = new Date();
|
|
227
|
+
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
|
|
228
|
+
const messagesByType = {};
|
|
229
|
+
let messagesInLastHour = 0;
|
|
230
|
+
for (const message of this.messageHistory) {
|
|
231
|
+
// Count by type
|
|
232
|
+
messagesByType[message.message] = (messagesByType[message.message] ?? 0) + 1;
|
|
233
|
+
// Count messages in last hour
|
|
234
|
+
if (new Date(message.timestampIso) > oneHourAgo) {
|
|
235
|
+
messagesInLastHour++;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
totalMessages: this.messageHistory.length,
|
|
240
|
+
messagesByType,
|
|
241
|
+
activeListeners: this.listeners.size,
|
|
242
|
+
messagesInLastHour
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
exports.DemoDataBus = DemoDataBus;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { EnergyAppDataBus, EnergyAppDataBusSendDataOptions } from "../../packages/energy-app-data-bus.cjs";
|
|
2
|
+
import { EnyoDataBusMessage, EnyoDataBusMessageAnswer, EnyoDataBusMessageEnum } from "../../types/enyo-data-bus-value.cjs";
|
|
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
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EnergyAppModbusDataType } from "./interfaces.cjs";
|
|
2
2
|
/**
|
|
3
3
|
* Data Type Converter for Modbus Register Values
|
|
4
4
|
*
|
|
5
5
|
* @description Converts raw buffer data from Modbus registers into appropriate JavaScript types
|
|
6
6
|
*/
|
|
7
|
-
export declare class EnergyAppModbusDataTypeConverter
|
|
7
|
+
export declare class EnergyAppModbusDataTypeConverter {
|
|
8
8
|
/**
|
|
9
9
|
* Converts raw buffer data from Modbus registers into appropriate JavaScript types
|
|
10
10
|
*
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EnergyAppModbusFaultTolerantReader = void 0;
|
|
4
|
+
const EnergyAppModbusDataTypeConverter_js_1 = require("./EnergyAppModbusDataTypeConverter.cjs");
|
|
4
5
|
class EnergyAppModbusFaultTolerantReader {
|
|
5
6
|
_modbusInstance;
|
|
6
7
|
_connectionHealth;
|
|
8
|
+
_converter;
|
|
7
9
|
constructor(modbusInstance, connectionHealth) {
|
|
8
10
|
this._modbusInstance = modbusInstance;
|
|
9
11
|
this._connectionHealth = connectionHealth;
|
|
12
|
+
this._converter = new EnergyAppModbusDataTypeConverter_js_1.EnergyAppModbusDataTypeConverter();
|
|
10
13
|
}
|
|
11
14
|
async readHoldingRegisters(startAddress, quantity) {
|
|
12
15
|
try {
|
|
@@ -45,5 +48,78 @@ class EnergyAppModbusFaultTolerantReader {
|
|
|
45
48
|
isHealthy() {
|
|
46
49
|
return this._connectionHealth.isHealthy();
|
|
47
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Reads holding registers with automatic type conversion
|
|
53
|
+
*
|
|
54
|
+
* @param address - Starting register address
|
|
55
|
+
* @param dataType - The data type to convert to
|
|
56
|
+
* @param options - Optional parameters for scaling and string length
|
|
57
|
+
* @returns Register value converted to the specified type, or error
|
|
58
|
+
*/
|
|
59
|
+
async readHoldingRegisterWithType(address, dataType, options) {
|
|
60
|
+
try {
|
|
61
|
+
// Calculate register quantity based on data type
|
|
62
|
+
let registerQuantity;
|
|
63
|
+
if (dataType === 'string') {
|
|
64
|
+
// For strings, quantity represents character count
|
|
65
|
+
// Each register holds 2 ASCII characters
|
|
66
|
+
if (!options?.quantity || options.quantity <= 0) {
|
|
67
|
+
throw new Error('String data type requires a valid quantity parameter (character count)');
|
|
68
|
+
}
|
|
69
|
+
registerQuantity = Math.ceil(options.quantity / 2);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// For numeric types, get register count from converter
|
|
73
|
+
registerQuantity = this._converter.getRegisterQuantity(dataType);
|
|
74
|
+
}
|
|
75
|
+
// Read registers with fault tolerance
|
|
76
|
+
const result = await this.readHoldingRegisters(address, registerQuantity);
|
|
77
|
+
if (!result.success || !result.value) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: result.error || new Error('Failed to read registers')
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// Convert buffer to appropriate type
|
|
84
|
+
const convertedValue = this._converter.convertFromBuffer(result.value, dataType, options?.scale, options?.quantity);
|
|
85
|
+
// Validate the converted value
|
|
86
|
+
if (!this._converter.isValidValue(convertedValue, dataType)) {
|
|
87
|
+
console.warn(`Invalid value for ${dataType} at register ${address}: ${convertedValue}`);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
success: true,
|
|
91
|
+
value: convertedValue
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
const err = error;
|
|
96
|
+
console.error(`Failed to read ${dataType} from register ${address}: ${err.message}`);
|
|
97
|
+
return {
|
|
98
|
+
success: false,
|
|
99
|
+
error: err
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Convenience method for reading string values from holding registers
|
|
105
|
+
*
|
|
106
|
+
* @param address - Starting register address
|
|
107
|
+
* @param length - String length in characters
|
|
108
|
+
* @returns String value or error
|
|
109
|
+
*/
|
|
110
|
+
async readString(address, length) {
|
|
111
|
+
return this.readHoldingRegisterWithType(address, 'string', { quantity: length });
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Convenience method for reading numeric values from holding registers
|
|
115
|
+
*
|
|
116
|
+
* @param address - Starting register address
|
|
117
|
+
* @param dataType - Numeric data type (uint16, int16, uint32, int32, float32)
|
|
118
|
+
* @param scale - Optional scaling factor (divide by 10^scale)
|
|
119
|
+
* @returns Numeric value or error
|
|
120
|
+
*/
|
|
121
|
+
async readNumeric(address, dataType, scale) {
|
|
122
|
+
return this.readHoldingRegisterWithType(address, dataType, { scale });
|
|
123
|
+
}
|
|
48
124
|
}
|
|
49
125
|
exports.EnergyAppModbusFaultTolerantReader = EnergyAppModbusFaultTolerantReader;
|
|
@@ -1,10 +1,40 @@
|
|
|
1
1
|
import type { EnergyAppModbusInstance } from "../../packages/energy-app-modbus.cjs";
|
|
2
|
-
import type { IRegisterReader, IConnectionHealth, RegisterReadResult } from './interfaces.cjs';
|
|
2
|
+
import type { IRegisterReader, IConnectionHealth, RegisterReadResult, EnergyAppModbusDataType } from './interfaces.cjs';
|
|
3
3
|
export declare class EnergyAppModbusFaultTolerantReader implements IRegisterReader {
|
|
4
4
|
private readonly _modbusInstance;
|
|
5
5
|
private readonly _connectionHealth;
|
|
6
|
+
private readonly _converter;
|
|
6
7
|
constructor(modbusInstance: EnergyAppModbusInstance, connectionHealth: IConnectionHealth);
|
|
7
8
|
readHoldingRegisters(startAddress: number, quantity: number): Promise<RegisterReadResult<Buffer>>;
|
|
8
9
|
readInputRegisters(startAddress: number, quantity: number): Promise<RegisterReadResult<Buffer>>;
|
|
9
10
|
isHealthy(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Reads holding registers with automatic type conversion
|
|
13
|
+
*
|
|
14
|
+
* @param address - Starting register address
|
|
15
|
+
* @param dataType - The data type to convert to
|
|
16
|
+
* @param options - Optional parameters for scaling and string length
|
|
17
|
+
* @returns Register value converted to the specified type, or error
|
|
18
|
+
*/
|
|
19
|
+
readHoldingRegisterWithType<T = any>(address: number, dataType: EnergyAppModbusDataType, options?: {
|
|
20
|
+
scale?: number;
|
|
21
|
+
quantity?: number;
|
|
22
|
+
}): Promise<RegisterReadResult<T>>;
|
|
23
|
+
/**
|
|
24
|
+
* Convenience method for reading string values from holding registers
|
|
25
|
+
*
|
|
26
|
+
* @param address - Starting register address
|
|
27
|
+
* @param length - String length in characters
|
|
28
|
+
* @returns String value or error
|
|
29
|
+
*/
|
|
30
|
+
readString(address: number, length: number): Promise<RegisterReadResult<string>>;
|
|
31
|
+
/**
|
|
32
|
+
* Convenience method for reading numeric values from holding registers
|
|
33
|
+
*
|
|
34
|
+
* @param address - Starting register address
|
|
35
|
+
* @param dataType - Numeric data type (uint16, int16, uint32, int32, float32)
|
|
36
|
+
* @param scale - Optional scaling factor (divide by 10^scale)
|
|
37
|
+
* @returns Numeric value or error
|
|
38
|
+
*/
|
|
39
|
+
readNumeric(address: number, dataType: Exclude<EnergyAppModbusDataType, 'string'>, scale?: number): Promise<RegisterReadResult<number>>;
|
|
10
40
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type EnergyAppModbusRegisterConfig, type EnergyAppRegisterMap, type IDataTypeConverter, type IRegisterMapper, type IRegisterReader, type RegisterReadResult } from './interfaces.cjs';
|
|
2
2
|
export declare class EnergyAppModbusRegisterMapper implements IRegisterMapper {
|
|
3
3
|
private readonly dataTypeConverter;
|
|
4
4
|
constructor(dataTypeConverter?: IDataTypeConverter);
|
|
5
|
-
readRegister<T>(reader: IRegisterReader, config: EnergyAppModbusRegisterConfig): Promise<RegisterReadResult<T>>;
|
|
5
|
+
readRegister<T, E>(reader: IRegisterReader, config: EnergyAppModbusRegisterConfig<E>): Promise<RegisterReadResult<T>>;
|
|
6
6
|
readMultipleRegisters(reader: IRegisterReader, registerMap: EnergyAppRegisterMap): Promise<{
|
|
7
7
|
[key: string]: any;
|
|
8
8
|
}>;
|