Improved ipc support.

This commit is contained in:
hg
2014-06-20 00:00:28 +02:00
parent 4c24d7a834
commit 116947ef03
4 changed files with 184 additions and 3 deletions
+63
View File
@@ -188,6 +188,27 @@ double GrowControl::getTemp1() {
return ret;
}
int16_t GrowControl::getTemp1Raw() {
std::lock_guard<std::mutex> lock(controlMutex);
int16_t ret = 0;
if (temp1Bricklet != nullptr) {
if (temp1Bricklet->getDeviceState() == TfDeviceState::CONNECTED) {
try {
ret = temp1Bricklet->getTemperature();
} catch (TfError &e) {
std::ostringstream ss;
ss << "unknown exception while getting temp1: " << e.what();
throw GCException(ss.str(), GCErrorCode::UNKNOWN_TFERROR);
}
} else {
throw GCException("temp1 sensor not connected.", GCErrorCode::SENSOR_DISCONNECTED);
}
} else {
throw GCException("No temp1 sensor present.", GCErrorCode::NO_SENSOR);
}
return ret;
}
double GrowControl::getTemp2() {
std::lock_guard<std::mutex> lock(controlMutex);
double ret = 0;
@@ -209,6 +230,27 @@ double GrowControl::getTemp2() {
return ret;
}
int16_t GrowControl::getTemp2Raw() {
std::lock_guard<std::mutex> lock(controlMutex);
int16_t ret = 0;
if (temp2Bricklet != nullptr) {
if (temp2Bricklet->getDeviceState() == TfDeviceState::CONNECTED) {
try {
ret = temp2Bricklet->getTemperature();
} catch (TfError &e) {
std::ostringstream ss;
ss << "unknown exception while getting temp2: " << e.what();
throw GCException(ss.str(), GCErrorCode::UNKNOWN_TFERROR);
}
} else {
throw GCException("temp2 sensor not connected.", GCErrorCode::SENSOR_DISCONNECTED);
}
} else {
throw GCException("No temp2 sensor present.", GCErrorCode::NO_SENSOR);
}
return ret;
}
double GrowControl::getHum1() {
std::lock_guard<std::mutex> lock(controlMutex);
double ret = 0;
@@ -230,6 +272,27 @@ double GrowControl::getHum1() {
return ret;
}
uint16_t GrowControl::getHum1Raw() {
std::lock_guard<std::mutex> lock(controlMutex);
uint16_t ret = 0;
if (hum1Bricklet != nullptr) {
if (hum1Bricklet->getDeviceState() == TfDeviceState::CONNECTED) {
try {
ret = hum1Bricklet->getHumidity();
} catch (TfError &e) {
std::ostringstream ss;
ss << "unknown exception while getting hum1: " << e.what();
throw GCException(ss.str(), GCErrorCode::UNKNOWN_TFERROR);
}
} else {
throw GCException("hum1 sensor not connected.", GCErrorCode::SENSOR_DISCONNECTED);
}
} else {
throw GCException("No hum1 sensor present.", GCErrorCode::NO_SENSOR);
}
return ret;
}
unsigned GrowControl::getFan1Level(bool force) {
std::lock_guard<std::mutex> lock(controlMutex);
unsigned ret = 0;
+3
View File
@@ -85,10 +85,13 @@ public:
void mainLoop();
double getTemp1();
int16_t getTemp1Raw();
double getTemp2();
int16_t getTemp2Raw();
double getHum1();
uint16_t getHum1Raw();
bool isFan1Connected();
unsigned getFan1Level(bool force = false);
+116 -3
View File
@@ -7,6 +7,8 @@
#include "IPCManager.h"
#include "GCException.h"
#include "GrowControl.h"
#include "GCFanProfile.h"
#include <algorithm>
#include <vector>
#include <stdio.h>
@@ -18,6 +20,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <error.h>
#include <sstream>
namespace growcontrol {
@@ -76,8 +79,16 @@ void IPCManager::workerThreadFunc(IPCManager* obj, int socket, std::thread* self
} else {
response = obj->handleGetValCommand(args);
}
} else if (firstPart.compare("SET") == 0) { // set command
response = "ERROR TODO";
} else if (firstPart.compare("SETVAL") == 0) { // set command
std::string object;
std::string val;
if (!std::getline(commandStream, object, ' ')) {
response = "ERROR NO_OBJ";
} else if (!std::getline(commandStream, val, ' ')) {
response = "ERROR NO_VAL";
} else {
response = obj->handleSetValCommand(object, val);
}
} else {
logger << log4cpp::Priority::ERROR << "Received unknown ipc command: " << buffer << ".";
response = "ERROR UNKNOWN_COMMAND";
@@ -163,8 +174,110 @@ void IPCManager::shutdown() {
std::string IPCManager::handleGetValCommand(std::vector<std::string> args) {
return "ERROR TODO";
std::ostringstream ss;
ss << "ACK";
for (std::string arg : args) {
ss << " ";
try {
if (arg.compare("temp1") == 0) {
ss << growControl->getTemp1();
} else if (arg.compare("temp1raw") == 0) {
ss << growControl->getTemp1Raw();
} else if (arg.compare("temp2") == 0) {
ss << growControl->getTemp2();
} else if (arg.compare("temp2raw") == 0) {
ss << growControl->getTemp2Raw();
} else if (arg.compare("hum1") == 0) {
ss << growControl->getHum1();
} else if (arg.compare("hum1raw") == 0) {
ss << growControl->getHum1Raw();
} else if (arg.compare("fan1level") == 0) {
ss << growControl->getFan1Level();
} else if (arg.compare("fan1auto") == 0) {
if (growControl->getFan1AutoMode()) {
ss << "on";
} else {
ss << "off";
}
} else if (arg.compare("fan1limit") == 0) {
ss << growControl->getFan1UpperLimit();
} else if (arg.compare("fan1profile") == 0) {
auto p = growControl->getFan1Profile();
if (p != nullptr) {
ss << p->getName();
} else {
ss << "NONE";
}
} else {
ss << "UNKNOWN";
}
} catch (GCException &e) {
logger << log4cpp::Priority::ERROR << "Error while fetching " << arg << ": " << e.what();
ss << "ERROR";
}
}
return ss.str();
}
std::string IPCManager::handleSetValCommand(std::string obj, std::string value) {
std::ostringstream ss;
if (obj.compare("fan1level") == 0) {
if (growControl->getFan1AutoMode()) {
ss << "NAK AUTO_MODE";
} else {
try {
unsigned val = std::stoul(value);
growControl->setFan1Level(val);
ss << "ACK";
} catch (GCException& e) {
ss << "ERROR";
} catch (std::exception& e) {
ss << "NAK INVALID_VALUE";
}
}
} else if (obj.compare("fan1auto") == 0) {
if (value.compare("on") == 0) {
growControl->setFan1AutoMode(true);
ss << "ACK";
} else if (value.compare("off") == 0) {
growControl->setFan1AutoMode(false);
ss << "ACK";
} else {
ss << "ERROR INVALID_VALUE";
}
} else if (obj.compare("fan1limit") == 0) {
try {
unsigned val = std::stoul(value);
growControl->setFan1UpperLimit(val);
ss << "ACK";
} catch (GCException& e) {
ss << "ERROR";
} catch (std::exception& e) {
ss << "NAK INVALID_VALUE";
}
} else if (obj.compare("fan1profile") == 0) {
try {
bool isOk = false;
for (auto p : growControl->getFanProfiles()) {
if (p->getName().compare(value) == 0) {
growControl->setFan1Profile(p);
isOk = true;
break;
}
}
if (isOk) {
ss << "ACK";
} else {
ss << "NAK UNKNOWN_VALUE";
}
} catch (GCException& e) {
ss << "ERROR";
}
} else {
ss << "ERROR UNKNOWN_OBJ";
}
return ss.str();
}
} /* namespace growcontrol */
+2
View File
@@ -43,6 +43,8 @@ public:
static void workerThreadFunc(IPCManager* obj, int socket, std::thread* self);
std::string handleGetValCommand(std::vector<std::string> args);
std::string handleSetValCommand(std::string obj, std::string value);
};
} /* namespace growcontrol */