-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhomecontrolpi.cpp
More file actions
executable file
·130 lines (107 loc) · 4.33 KB
/
whomecontrolpi.cpp
File metadata and controls
executable file
·130 lines (107 loc) · 4.33 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
#include <v8.h>
#include <node.h>
#include "HomeControl.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace node;
using namespace v8;
const int kPRODUCT_Elro = 1;
const int kPRODUCT_REV = 2;
static void SwitchElro(char* inSystemCode, char* inUnitCode, const bool switchOn) {
int unitDigit;
if (strcmp(inUnitCode, "A") == 0) {
unitDigit = 1;
} else if (strcmp(inUnitCode, "B") == 0) {
unitDigit = 2;
} else if (strcmp(inUnitCode, "C") == 0) {
unitDigit = 3;
} else if (strcmp(inUnitCode, "D") == 0) {
unitDigit = 4;
} else if (strcmp(inUnitCode, "E") == 0) {
unitDigit = 5;
} else {
ThrowException( Exception::TypeError( String::New( "UnitCode for Elro not supported" ) ) );
}
int kPIN = 0;
HomeControl myHome = HomeControl();
myHome.enableTransmit(kPIN);
if(switchOn) {
printf("sending ON to Elro Switch: inSystemCode[%s] inUnitCode[%s]\n", inSystemCode, inUnitCode);
myHome.switchOn(inSystemCode, unitDigit);
} else {
printf("sending OFF to Elro Switch: inSystemCode[%s] inUnitCode[%s]\n", inSystemCode, inUnitCode);
myHome.switchOff(inSystemCode, unitDigit);
}
}
static void SwitchRev(char* inSystemCode, int inUnitCode, const bool switchOn) {
char systemDigit;
if (strcmp(inSystemCode, "A") == 0) {
systemDigit = 'A';
} else if (strcmp(inSystemCode, "B") == 0) {
systemDigit = 'B';
} else if (strcmp(inSystemCode, "C") == 0) {
systemDigit = 'C';
} else if (strcmp(inSystemCode, "D") == 0) {
systemDigit = 'D';
} else {
ThrowException( Exception::TypeError( String::New( "SystemCode for REV not supported" ) ) );
}
int kPIN = 0;
HomeControl myHome = HomeControl();
myHome.enableTransmit(kPIN);
if(switchOn) {
printf("sending ON to REV Switch: inSystemCode[%s] inUnitCode[%i]\n", inSystemCode, inUnitCode);
myHome.switchOnD(systemDigit, inUnitCode);
} else {
printf("sending OFF to REV Switch: inSystemCode[%s] inUnitCode[%i]\n", inSystemCode, inUnitCode);
myHome.switchOffD(systemDigit, inUnitCode);
}
}
static Handle<Value> Switch(const Arguments& args, const bool switchOn, const int product)
{
HandleScope scope;
if( args.Length() != 2 ) {
ThrowException( Exception::TypeError( String::New( "Wrong number of arguments" ) ) );
return scope.Close( Undefined() );
}
if( !args[0]->IsString() ) {
ThrowException( Exception::TypeError( String::New( "Bad argument type" ) ) );
return scope.Close( Undefined() );
}
v8::String::Utf8Value l_SystemCode(args[0]->ToString());
char* inSystemCode = *l_SystemCode;
if(product == kPRODUCT_Elro) {
if( !args[1]->IsString() ) {
ThrowException( Exception::TypeError( String::New( "Bad argument type" ) ) );
return scope.Close( Undefined() );
}
v8::String::Utf8Value l_UnitCode(args[1]->ToString());
char* inUnitCode = *l_UnitCode;
SwitchElro(inSystemCode, inUnitCode, switchOn);
} else if(product == kPRODUCT_REV) {
if( !args[1]->IsNumber() ) {
ThrowException( Exception::TypeError( String::New( "Bad argument type" ) ) );
return scope.Close( Undefined() );
}
int inUnitCode = args[1]->NumberValue();
SwitchRev(inSystemCode, inUnitCode, switchOn);
}
return scope.Close( Undefined() );
}
static Handle<Value> Switch_ELRO_ON(const Arguments& args) { return Switch(args, true, kPRODUCT_Elro); }
static Handle<Value> Switch_ELRO_OFF(const Arguments& args) { return Switch(args, false, kPRODUCT_Elro); }
static Handle<Value> Switch_REV_ON(const Arguments& args) { return Switch(args, true, kPRODUCT_REV); }
static Handle<Value> Switch_REV_OFF(const Arguments& args) { return Switch(args, false, kPRODUCT_REV); }
extern "C" void init(Handle<Object> target)
{
if( -1 == wiringPiSetup() ) {
ThrowException( Exception::TypeError( String::New( "wiringPiSetup not able to initialize." ) ) );
return;
}
target->Set(String::NewSymbol("switchElroOn"), FunctionTemplate::New(Switch_ELRO_ON)->GetFunction());
target->Set(String::NewSymbol("switchElroOff"), FunctionTemplate::New(Switch_ELRO_OFF)->GetFunction());
target->Set(String::NewSymbol("switchRevOn"), FunctionTemplate::New(Switch_REV_ON)->GetFunction());
target->Set(String::NewSymbol("switchRevOff"), FunctionTemplate::New(Switch_REV_OFF)->GetFunction());
}
NODE_MODULE(whomecontrolpi, init);