From 116947ef033fe35ebbb894777ef7a52afcf2f8ed Mon Sep 17 00:00:00 2001 From: hg Date: Fri, 20 Jun 2014 00:00:28 +0200 Subject: [PATCH] Improved ipc support. --- src/GrowControl.cpp | 63 +++++++++++++++++++++++ src/GrowControl.h | 3 ++ src/IPCManager.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++-- src/IPCManager.h | 2 + 4 files changed, 184 insertions(+), 3 deletions(-) diff --git a/src/GrowControl.cpp b/src/GrowControl.cpp index 931cb30..734f6ab 100644 --- a/src/GrowControl.cpp +++ b/src/GrowControl.cpp @@ -188,6 +188,27 @@ double GrowControl::getTemp1() { return ret; } +int16_t GrowControl::getTemp1Raw() { + std::lock_guard 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 lock(controlMutex); double ret = 0; @@ -209,6 +230,27 @@ double GrowControl::getTemp2() { return ret; } +int16_t GrowControl::getTemp2Raw() { + std::lock_guard 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 lock(controlMutex); double ret = 0; @@ -230,6 +272,27 @@ double GrowControl::getHum1() { return ret; } +uint16_t GrowControl::getHum1Raw() { + std::lock_guard 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 lock(controlMutex); unsigned ret = 0; diff --git a/src/GrowControl.h b/src/GrowControl.h index 3a89211..8b69432 100644 --- a/src/GrowControl.h +++ b/src/GrowControl.h @@ -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); diff --git a/src/IPCManager.cpp b/src/IPCManager.cpp index 211adb6..8984925 100644 --- a/src/IPCManager.cpp +++ b/src/IPCManager.cpp @@ -7,6 +7,8 @@ #include "IPCManager.h" #include "GCException.h" +#include "GrowControl.h" +#include "GCFanProfile.h" #include #include #include @@ -18,6 +20,7 @@ #include #include #include +#include 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 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 */ diff --git a/src/IPCManager.h b/src/IPCManager.h index 42ee67e..3a5d405 100644 --- a/src/IPCManager.h +++ b/src/IPCManager.h @@ -43,6 +43,8 @@ public: static void workerThreadFunc(IPCManager* obj, int socket, std::thread* self); std::string handleGetValCommand(std::vector args); + std::string handleSetValCommand(std::string obj, std::string value); + }; } /* namespace growcontrol */