forked from rodtoll/isy-js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscratch.mjs
106 lines (96 loc) · 5.04 KB
/
scratch.mjs
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
// File for scratch testing. Will be removed in the future at is for my own internal purposes
import winston, { config } from 'winston';
import { ISY } from './lib/ISY.js'
import { Logger } from 'winston';
function handleInitialized() {
var deviceList = isy.deviceList;
console.log("Device count: "+deviceList.length);
// if(deviceList == null) {
// console.log("No device list returned!");
// } else {
// console.log("Got device list. Device count: "+deviceList.length);
// for(var index = 0; index < deviceList.length; index++ ) {
// console.log("Device: "+deviceList[index].name+", "+deviceList[index].deviceType+", "+deviceList[index].address+", "+deviceList[index].deviceFriendlyName);
// }
// }
// var fanDevice = isy.getDevice('14 A8 BC 2');
// var fanSpeed = fanDevice.getCurrentFanState();
// console.log('Current fan speed '+fanSpeed);
// fanDevice.sendFanCommand(fanDevice.FAN_LEVEL_MEDIUM, function() {});
// setTimeout(function() {
// console.log('Fan state (should be medium): '+fanDevice.getCurrentFanState());
// fanDevice.sendFanCommand(fanDevice.FAN_LEVEL_HIGH, function() {});
// setTimeout(function() {
// console.log('Fan state (should be high): '+fanDevice.getCurrentFanState());
// fanDevice.sendFanCommand((fanDevice.FAN_OFF), function() {});
// }, 4000)
// },4000);
//runBasicSceneTest(deviceList);
}
function handleChanged(isy, device) {
var logMessage = 'From isy: '+isy.address+' device changed: '+device.name;
if(device.deviceType == isy.DEVICE_TYPE_FAN) {
logMessage += ' fan state: '+device.getCurrentFanState();
} else if(device.deviceType == isy.DEVICE_TYPE_LIGHT) {
logMessage += ' light state: '+device.getCurrentLightState();
} else if(device.deviceType == isy.DEVICE_TYPE_DIMMABLE_LIGHT) {
logMessage += ' dimmable light state: '+device.getCurrentLightState()+' dimm Level: '+device.getCurrentLightDimState();
} else if(device.deviceType == isy.DEVICE_TYPE_LOCK || device.deviceType == isy.DEVICE_TYPE_SECURE_LOCK) {
logMessage += ' lock state: '+device.getCurrentLockState();
} else if(device.deviceType == isy.DEVICE_TYPE_OUTLET) {
logMessage += ' outlet state: '+device.getCurrentOutletState();
} else if(device.deviceType == isy.DEVICE_TYPE_ALARM_DOOR_WINDOW_SENSOR) {
logMessage += ' door window sensor state: '+device.getCurrentDoorWindowState()+' logical: '+device.getLogicalState()+' physical: '+device.getPhysicalState();
} else if(device.deviceType == isy.DEVICE_TYPE_DOOR_WINDOW_SENSOR) {
logMessage += ' door window sensor state: '+device.getCurrentDoorWindowState();
} else if(device.deviceType == isy.DEVICE_TYPE_ALARM_PANEL) {
logMessage += ' alarm panel state: '+device.getAlarmStatusAsText();
} else if(device.deviceType == isy.DEVICE_TYPE_MOTION_SENSOR) {
logMessage += ' motion sensor state: '+device.getCurrentMotionSensorState();
} else if(device.deviceType == isy.DEVICE_TYPE_SCENE) {
logMessage += ' scene. light state: '+device.getCurrentLightState()+' dimm Level: '+device.getCurrentLightDimState();
} else {
logMessage += ' unknown device, cannot parse state';
}
console.log(logMessage);
}
//var isy = new ISY.ISY('10.0.1.19', 'admin', 'password', true, handleChanged, false, true,true);
var logger = winston.createLogger();
if (process.env.NODE_ENV !== 'production') {
logger.level = "debug";
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
var isy = new ISY({host:'localhost:8080', username: 'admin', password:'qazWSX12',useHttps:false},logger);
isy.debugLoggingEnabled = true;
isy.logger = logger;
isy.initialize().then((p) => handleInitialized());
console.log('initialize completed');
function runBasicSceneTest(devices) {
for(var index = 0; index < devices.length; index++) {
var device = devices[index];
if(device.deviceType == isy.DEVICE_TYPE_SCENE && device.childDevices.length > 0 && device.childDevices.length < 5) {
console.log('Using scene: '+device.name);
console.log('Contains devices: ');
for(var childIndex = 0; childIndex < device.childDevices.length; childIndex++) {
console.log('Device: name='+device.childDevices[childIndex].name+' type='+device.childDevices[childIndex].deviceType);
}
console.log('Light state: '+devices[index].getCurrentLightState());
device.sendLightCommand(true, function() {
console.log('Turned them off to start so we make sure to get an on');
device.sendLightCommand(true, function() {
console.log('SHould have seen them all turn on');
device.sendLightCommand(false, function() {
console.log('Should have seen them all turn off');
device.sendLightDimCommand(50, function() {
console.log('Should have seen them all set to 50%');
device.sendLightCommand(false, function() {});
});
});
});
});
break;
}
}
}