Skip to content

Commit

Permalink
refactoring & setMaxRetries implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
momenso committed Oct 8, 2016
1 parent 83a4098 commit ccbf800
Showing 1 changed file with 63 additions and 52 deletions.
115 changes: 63 additions & 52 deletions node-dht-sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ extern float last_humidity[32];

std::mutex sensorMutex;

int GPIOPort = 4;
int SensorType = 11;
int _gpio_pin = 4;
int _sensor_type = 11;
int _max_retries = 3;

class ReadWorker : public Nan::AsyncWorker {
public:
Expand Down Expand Up @@ -66,13 +67,14 @@ class ReadWorker : public Nan::AsyncWorker {
return;
}

int retry = 3;
temperature = last_temperature[gpio_pin],
humidity = last_humidity[gpio_pin];
int retry = _max_retries;
int result = 0;
while (true) {
result = readDHT(sensor_type, gpio_pin, temperature, humidity);
if (result == 0 || --retry <= 0) break;
last_read[GPIOPort] = 0;
sleep(1);
if (result == 0 || --retry < 0) break;
usleep(450000);
}
failed = result != 0;
}
Expand All @@ -86,77 +88,76 @@ void ReadAsync(const Nan::FunctionCallbackInfo<Value>& args) {
Nan::AsyncQueueWorker(new ReadWorker(callback, sensor_type, gpio_pin));
}

void ReadSpec(const Nan::FunctionCallbackInfo<Value>& args) {
int sensorType = args[0]->Uint32Value();
if (sensorType != 11 && sensorType != 22) {
Nan::ThrowTypeError("specified sensor type is invalid");
return;
}
void ReadSync(const Nan::FunctionCallbackInfo<Value>& args) {
int sensor_type;
int gpio_pin;

if (!initialized) {
initialized = initialize() == 0;
if (!initialized) {
Nan::ThrowTypeError("failed to initialize");
if (args.Length() == 2) {
gpio_pin = args[1]->Uint32Value();
// TODO: validate gpio_pin

sensor_type = args[0]->Uint32Value();
if (sensor_type != 11 && sensor_type != 22) {
Nan::ThrowTypeError("specified sensor type is invalid");
return;
}
}

int gpio = args[1]->Uint32Value();
float temperature = 0, humidity = 0;
int retry = 3;
int result = 0;
do {
result = readDHT(sensorType, gpio, temperature, humidity);
if (--retry < 0) break;
} while (result != 0);

Local<Object> readout = Nan::New<Object>();
readout->Set(Nan::New("humidity").ToLocalChecked(), Nan::New<Number>(humidity));
readout->Set(Nan::New("temperature").ToLocalChecked(), Nan::New<Number>(temperature));
readout->Set(Nan::New("isValid").ToLocalChecked(), Nan::New<Boolean>(result == 0));
readout->Set(Nan::New("errors").ToLocalChecked(), Nan::New<Number>(2 - retry));

args.GetReturnValue().Set(readout);
}
// initialization (on demand)
if (!initialized) {
initialized = initialize() == 0;
if (!initialized) {
Nan::ThrowTypeError("failed to initialize");
return;
}
}
} else {
sensor_type = _sensor_type;
gpio_pin = _gpio_pin;
}

void ReadPreset(const Nan::FunctionCallbackInfo<Value>& args) {
float temperature = last_temperature[GPIOPort],
humidity = last_humidity[GPIOPort];
int retry = 0;
float temperature = last_temperature[gpio_pin],
humidity = last_humidity[gpio_pin];
int retry = _max_retries;
int result = 0;
while (true) {
result = readDHT(SensorType, GPIOPort, temperature, humidity);
if (result == 0 || ++retry > 3) break;
last_read[GPIOPort] = 0;
sleep(1);
result = readDHT(sensor_type, gpio_pin, temperature, humidity);
if (result == 0 || --retry < 0) break;
usleep(450000);
}

Local<Object> readout = Nan::New<Object>();
readout->Set(Nan::New("humidity").ToLocalChecked(), Nan::New<Number>(humidity));
readout->Set(Nan::New("temperature").ToLocalChecked(), Nan::New<Number>(temperature));
readout->Set(Nan::New("isValid").ToLocalChecked(), Nan::New<Boolean>(result == 0));
readout->Set(Nan::New("errors").ToLocalChecked(), Nan::New<Number>(retry));
readout->Set(Nan::New("errors").ToLocalChecked(), Nan::New<Number>(_max_retries - retry));

args.GetReturnValue().Set(readout);
}

void Read(const Nan::FunctionCallbackInfo<Value>& args) {
int params = args.Length();
switch(params) {
case 0:
ReadPreset(args);
break;
case 2:
ReadSpec(args);
case 0: // no parameters, use synchronous interface
case 2: // sensor type and GPIO pin, use synchronous interface
ReadSync(args);
break;
case 3:
// sensorType, gpioPin and callback, use asynchronous interface
ReadAsync(args);
break;
default:
Nan::ThrowTypeError("invalid number of arguments");
}
}

void SetMaxRetries(const Nan::FunctionCallbackInfo<Value>& args) {
if (args.Length() != 1) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
}
_max_retries = args[0]->Uint32Value();
}

void Initialize(const Nan::FunctionCallbackInfo<Value>& args) {
if (args.Length() < 2) {
Nan::ThrowTypeError("Wrong number of arguments");
Expand All @@ -168,22 +169,32 @@ void Initialize(const Nan::FunctionCallbackInfo<Value>& args) {
return;
}

int sensorType = args[0]->Uint32Value();
if (sensorType != 11 && sensorType != 22) {
if (args.Length() >= 3) {
if (!args[2]->IsNumber()) {
Nan::ThrowTypeError("Invalid maxRetries parameter");
return;
} else {
_max_retries = args[2]->Uint32Value();
}
}

int sensor_type = args[0]->Uint32Value();
if (sensor_type != 11 && sensor_type != 22) {
Nan::ThrowTypeError("Specified sensor type is not supported");
return;
}

// update parameters
SensorType = sensorType;
GPIOPort = args[1]->Uint32Value();
_sensor_type = sensor_type;
_gpio_pin = args[1]->Uint32Value();

args.GetReturnValue().Set(Nan::New<Boolean>(initialize() == 0));
}

void Init(Handle<Object> exports) {
Nan::SetMethod(exports, "read", Read);
Nan::SetMethod(exports, "initialize", Initialize);
Nan::SetMethod(exports, "setMaxRetries", SetMaxRetries);
}

NODE_MODULE(node_dht_sensor, Init);

0 comments on commit ccbf800

Please sign in to comment.