-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite-operations-demo.js
More file actions
159 lines (128 loc) · 6.11 KB
/
Copy pathwrite-operations-demo.js
File metadata and controls
159 lines (128 loc) · 6.11 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { DanfossAir } = require('../src/index');
/**
* Example demonstrating write operations with Danfoss Air API
*
* This example shows how to:
* - Activate/deactivate boost mode
* - Set fan speed step
* - Write custom parameter values
*
* Note: Replace '192.168.1.100' with your actual Danfoss Air unit's IP address
*/
async function demonstrateWriteOperations() {
// Initialize the Danfoss Air connection
const dfair = new DanfossAir({
ip: process.env.DANFOSS_AIR_IP || '192.168.1.100', // Replace with your device IP
delaySeconds: 3,
debug: false,
callbackFunction: (data) => {
console.log('Received updated data after write operations:');
const boost = data.find(param => param.id === 'Boost');
const fanStep = data.find(param => param.name === 'Fan Step');
const mode = data.find(param => param.id === 'operation_mode');
if (boost) {
console.log(` Boost mode: ${boost.value ? 'ACTIVE' : 'INACTIVE'}`);
}
if (fanStep) {
console.log(` Fan step: ${fanStep.value}`);
}
if (mode) {
console.log(` Operation mode: ${mode.value}`);
}
},
errorCallback: (error) => {
console.error('❌ Write operation failed:', error);
console.log(' This callback is called when socket-level write operations fail');
}
});
await dfair.start();
try {
console.log('=== Danfoss Air Write Operations Demo ===\n');
const fanModeParam = dfair.getParameter('operation_mode');
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
console.log('1. Activating manual mode...');
await dfair.setMode(2);
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
console.log(' ✓ Manual mode activated\n');
// Wait a moment
await sleep(5000);
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
// Example 2: Set fan step to 5
console.log('2. Setting fan step to 5...');
await dfair.setFanStep(5);
console.log(' ✓ Fan step set to 5\n');
// Wait a moment
await sleep(10000);
// Example 2: Set fan step to 2
console.log('2. Setting fan step to 2...');
await dfair.setFanStep(2);
console.log(' ✓ Fan step set to 2\n');
// Wait a moment
await sleep(10000);
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
// Example 3: Deactivate manual mode
console.log('3. Deactivating manual mode...');
await dfair.setMode(0);
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
console.log(' ✓ Manual mode deactivated\n');
await sleep(6000);
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
// Example 5: Check parameter status
console.log('5. Checking current parameter values...');
const boostParam = dfair.getParameter('boost');
const fanParam = dfair.getParameter('fan_step');
await sleep(20000);
console.log(` Boost parameter: ${boostParam?.value} (last updated: ${new Date(boostParam?.valuetimestamp || 0).toLocaleTimeString()})`);
console.log(` Fan step parameter: ${fanParam?.value} (last updated: ${new Date(fanParam?.valuetimestamp || 0).toLocaleTimeString()})`);
console.log(` Fan mode parameter: ${fanModeParam?.value} (last updated: ${new Date(fanModeParam?.valuetimestamp || 0).toLocaleTimeString()})`);
console.log('\n=== Write Operations Demo Complete ===');
} catch (error) {
console.error('Error during write operations:', error?.message || error);
if (error?.message?.includes('ECONNREFUSED') || error?.message?.includes('connect')) {
console.log('\n📡 Connection failed - this is expected if no Danfoss Air unit is available at the specified IP.');
console.log(' To test with a real device:');
console.log(' 1. Find your Danfoss Air unit\'s IP address');
console.log(' 2. Update the IP address in this script');
console.log(' 3. Ensure the device is connected to your network');
}
} finally {
// Clean up
//dfair.cleanup();
}
}
// Helper function for delays
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async () => {
try {
// Show available write operations
console.log('📝 Available Write Operations:');
console.log(' • activateBoost() / deactivateBoost()');
console.log(' • setFanStep(1-10)');
console.log(' • writeParameterValue(id, value)');
console.log('');
console.log('🔧 Supported Writable Parameters:');
const dfairTemp = new DanfossAir({ ip: '127.0.0.1', delaySeconds: 30 });
['boost', 'bypass', 'automatic_bypass', 'operation_mode', 'fan_step'].forEach(paramId => {
const param = dfairTemp.getParameter(paramId);
if (param) {
console.log(` • ${paramId}: ${param.name} (${param.datatype})`);
}
});
dfairTemp.cleanup();
console.log('');
// Run the demonstration
await demonstrateWriteOperations();
} catch (e) {
console.error(e);
}
})();
// Handle the SIGINT signal (Ctrl+C)
process.on('SIGINT', () => {
dfair.cleanup();
console.log("\nExiting the application. Goodbye!");
process.exit(0); // Exit gracefully
});
// Keep the process running
process.stdin.resume();