-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
169 lines (138 loc) · 5.24 KB
/
server.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
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
160
161
162
163
164
165
166
167
/*******************************************************************************
* Copyright (c) 2013 Stefan Ferber.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stefan Ferber - initial API and implementation and/or initial documentation
*******************************************************************************/
/*******************************************************************************
* node.js express REST API to remote control velux windows
* via the KMtronic USB 4 Relay Board RS232
* sigma-shop.com/product/75/usb-4-relay-board-rs232-serial-controlled-pcb.html
*******************************************************************************/
var express = require('express');
var app = express();
var serialport = require("serialport");
// on Mac mini USB port name
//var myPortPath = "/dev/cu.usbserial-A101LV3L";
// on Dreamplug Debian USB port name
var myPortPath = "/dev/ttyUSB0";
var portCmdShadesOpenStart = Buffer('FF0101', 'hex');
var portCmdShadesOpenEnd = Buffer('FF0100', 'hex');
var portCmdShadesCloseStart = Buffer('FF0201', 'hex');
var portCmdShadesCloseEnd = Buffer('FF0200', 'hex');
var portCmdWindowsOpenStart = Buffer('FF0301', 'hex');
var portCmdWindowsOpenEnd = Buffer('FF0300', 'hex');
var portCmdWindowsCloseStart = Buffer('FF0401', 'hex');
var portCmdWindowsCloseEnd = Buffer('FF0400', 'hex');
var portCmdRelayStatus = Buffer('FF0900', 'hex');
var portCmdShadesDelay = 0.5*1000; //milliseconds
var portCmdWindowsDelay = 0.5*1000; //milliseconds
var myPort = new serialport.SerialPort(myPortPath, {
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1
// parser: serialport.parsers.readline("\r\n")
});
myPort.on("open", function () {
console.log("open serialport " +myPortPath);
});
//no global callback as data has to be fetched in context
//myPort.on("data", function(data) {
// console.log("data received: " + data.toString());
//});
myPort.on('error', function(err) {
console.log(err);
});
myPort.on('close', function (err) {
console.log('closed serialport' +myPortPath);
});
app.get('/hello.txt', function(req, res){
var body = 'Hello World';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', body.length);
res.end(body);
});
app.get('/connectedhome/windows', function(req, res){
myPort.write(portCmdRelayStatus, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
});
var windowStatus="Window status is unknown. Relais for window control are set:";
myPort.on("data", function(data) {
var j=0;
for (var i = 0; i < data.length ; i++){
j = i+1;
windowStatus = windowStatus + "\n Relay[" + j + "] = ";
if(data[i] == [0]){
windowStatus = windowStatus + "OFF"
} else {
if(data[i] == [1]){
windowStatus = windowStatus + "ON"
} else {
windowStatus = windowStatus + "ERROR"
}
}
}
console.log(windowStatus);
});
res.send(windowStatus);
});
app.get('/connectedhome/shades/open', function(req, res){
myPort.write(portCmdShadesOpenStart, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
});
setTimeout(function(){
myPort.write(portCmdShadesOpenEnd, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
})},
portCmdShadesDelay);
res.send('Shades are opening...');
});
app.get('/connectedhome/shades/close', function(req, res){
myPort.write(portCmdShadesCloseStart, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
});
setTimeout(function(){
myPort.write(portCmdShadesCloseEnd, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
})},
portCmdShadesDelay);
res.send('Shades are closing...');
});
app.get('/connectedhome/windows/open', function(req, res){
myPort.write(portCmdWindowsOpenStart, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
});
setTimeout(function(){
myPort.write(portCmdWindowsOpenEnd, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
})},
portCmdWindowsDelay);
res.send('Windows are opening...');
});
app.get('/connectedhome/windows/close', function(req, res){
myPort.write(portCmdWindowsCloseStart, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
});
setTimeout(function(){
myPort.write(portCmdWindowsCloseEnd, function(err, bytesWritten) {
console.log("err: " + err);
console.log("bytes successul written: "+ bytesWritten);
})},
portCmdWindowsDelay);
res.send('Windows are closing...');
});
app.listen(3000);
console.log('Listening on port 3000');