This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor-node.cpp
executable file
·144 lines (113 loc) · 4.48 KB
/
sensor-node.cpp
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
#include "sensor-node.h"
#include <vector>
#include <string>
using namespace v8;
Persistent<Function> SensorNode::constructor;
SensorNode::SensorNode() {
_sensor = NULL;
}
SensorNode::~SensorNode() {
if(_sensor!=NULL) {
delete _sensor;
_sensor = NULL;
}
}
void SensorNode::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
if (args.IsConstructCall()) {
// Invoked as constructor: `new DeviceNode()`
SensorNode* obj = new SensorNode();
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
// Invoked as plain function `DeviceNode(...)`, turn into construct call.
Local<Function> cons = Local<Function>::New(isolate, constructor);
args.GetReturnValue().Set(cons->NewInstance());
}
}
void SensorNode::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
const unsigned argc = 1;
Handle<Value> argv[argc] = { args[0] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Object> instance = cons->NewInstance(argc, argv);
args.GetReturnValue().Set(instance);
}
//Getters
void SensorNode::GetId(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Sensor* sensor = getSensorBinding(args)->_sensor;
args.GetReturnValue().Set(Integer::New(isolate, sensor->getId()));
}
void SensorNode::GetModel(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Sensor* sensor = getSensorBinding(args)->_sensor;
args.GetReturnValue().Set(String::NewFromUtf8(isolate, sensor->getModel().c_str()));
}
void SensorNode::GetProtocol(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Sensor* sensor = getSensorBinding(args)->_sensor;
args.GetReturnValue().Set(String::NewFromUtf8(isolate, sensor->getProtocol().c_str()));
}
//Actions
Local<Object> getValueObject(const Sensor::Value* value, Isolate* isolate) {
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "value"), Number::New(isolate, value->value));
obj->Set(String::NewFromUtf8(isolate, "timestamp"), String::NewFromUtf8(isolate, value->timestamp.c_str()));
obj->Set(String::NewFromUtf8(isolate, "timestamp_raw"), Integer::New(isolate, value->timestamp_raw));
return obj;
}
void SensorNode::GetTemperature(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
const Sensor::Value* value = getSensorBinding(args)->_sensor->getTemperature();
if(value==NULL) {
args.GetReturnValue().Set(v8::Null(isolate));
return;
}
args.GetReturnValue().Set(getValueObject(value, isolate));
}
void SensorNode::GetHumidity(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
const Sensor::Value* value = getSensorBinding(args)->_sensor->getHumidity();
if(value==NULL) {
args.GetReturnValue().Set(v8::Null(isolate));
return;
}
args.GetReturnValue().Set(getValueObject(value, isolate));
}
//Other
void SensorNode::setSensor(Sensor* sensor) {
this->_sensor = sensor;
}
SensorNode* SensorNode::getObjectInternal(Local<Object> obj) {
return ObjectWrap::Unwrap<SensorNode>(obj);
}
SensorNode* SensorNode::getSensorBinding(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
return ObjectWrap::Unwrap<SensorNode>(args.Holder());
}
//Init
void SensorNode::Init(v8::Handle<v8::Object> exports) {
Isolate* isolate = exports->GetIsolate();
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "SensorNode"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "getId", SensorNode::GetId);
NODE_SET_PROTOTYPE_METHOD(tpl, "getModel", SensorNode::GetModel);
NODE_SET_PROTOTYPE_METHOD(tpl, "getProtocol", SensorNode::GetProtocol);
NODE_SET_PROTOTYPE_METHOD(tpl, "getTemperature", SensorNode::GetTemperature);
NODE_SET_PROTOTYPE_METHOD(tpl, "getHumidity", SensorNode::GetHumidity);
constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "SensorNode"),
tpl->GetFunction());
}