From 1800f61f5a033aa2430aa72ec1c440ed0f9b9e4e Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Tue, 11 Sep 2012 09:10:10 -0700 Subject: [PATCH] fixed up AbstractPropertyType so it can use enums --- lib/abstractpropertytype.h | 28 +++++++++++- lib/listplusplus.h | 1 - lib/vehicleproperty.cpp | 1 + lib/vehicleproperty.h | 14 +++++- plugins/CMakeLists.txt | 2 +- plugins/demosink/CMakeLists.txt | 16 +++++++ plugins/demosink/demosinkplugin.cpp | 91 +++++++++++++++++++++++++++++++++++++ plugins/demosink/demosinkplugin.h | 56 +++++++++++++++++++++++ plugins/wheel/wheelplugin.cpp | 12 ++--- 9 files changed, 210 insertions(+), 11 deletions(-) create mode 100644 plugins/demosink/CMakeLists.txt create mode 100644 plugins/demosink/demosinkplugin.cpp create mode 100644 plugins/demosink/demosinkplugin.h diff --git a/lib/abstractpropertytype.h b/lib/abstractpropertytype.h index 3ba0307..1177346 100644 --- a/lib/abstractpropertytype.h +++ b/lib/abstractpropertytype.h @@ -24,6 +24,8 @@ #include #include #include +#include +#include class AbstractPropertyType { @@ -76,7 +78,9 @@ public: BasicPropertyType(std::string val) { if(!val.empty() && val != "") - setValue(boost::lexical_cast(val)); + { + serialize(val); + } else throw std::runtime_error("value cannot be empty"); } @@ -87,6 +91,28 @@ public: return stream.str(); } + +private: + + template + void serialize(std::string val, typename std::enable_if::value, N>::type* = 0) + { + int someTemp; + + std::stringstream stream(val); + + stream>>someTemp; + setValue((N)someTemp); + } + + template + void serialize(std::string val, typename std::enable_if::value, N>::type* = 0) + { + std::stringstream stream(val); + N someTemp; + stream>>someTemp; + setValue(someTemp); + } }; class StringPropertyType: public AbstractPropertyType diff --git a/lib/listplusplus.h b/lib/listplusplus.h index 2821b08..df815f3 100644 --- a/lib/listplusplus.h +++ b/lib/listplusplus.h @@ -45,7 +45,6 @@ public: } } - bool contains(T value) { return (std::find(mList->begin(), mList->end(), value) != mList->end()); diff --git a/lib/vehicleproperty.cpp b/lib/vehicleproperty.cpp index 51f344c..dd40664 100644 --- a/lib/vehicleproperty.cpp +++ b/lib/vehicleproperty.cpp @@ -100,6 +100,7 @@ AbstractPropertyType* VehicleProperty::getPropertyTypeForPropertyNameValue(Vehic else if(name == AccelerationY) return new AccelerationType(value); else if(name == AccelerationZ) return new AccelerationType(value); else if(name == MassAirFlow) return new MassAirFlowType(value); + else if(name == ButtonEvent) return new ButtonEventType(value); else if(name == AirIntakeTemperature) return new AirIntakeTemperatureType(value); else if(name == BatteryVoltage) return new BatteryVoltageType(value); else if(name == InteriorTemperature) return new InteriorTemperatureType(value); diff --git a/lib/vehicleproperty.h b/lib/vehicleproperty.h index e9828ff..7732c63 100644 --- a/lib/vehicleproperty.h +++ b/lib/vehicleproperty.h @@ -45,6 +45,16 @@ enum ButtonEventType { }; } +namespace TurnSignals { +enum TurnSignalType +{ + Off=0, + Right=1, + Left=2, + Hazard=3 +}; +} + class VehicleProperty { @@ -103,7 +113,7 @@ public: /**< 0=off, 1=right, 2=left, 3=hazard */ static const Property TurnSignal; - typedef BasicPropertyType TurnSignalType; + typedef BasicPropertyType TurnSignalType; /**< Clutch pedal status 0=off, 1=on */ static const Property ClutchStatus; @@ -137,7 +147,7 @@ public: /**< Button Event @see ButtonEvents::ButtonEventType */ static const Property ButtonEvent; - typedef BasicPropertyType ButtonEventType; + typedef BasicPropertyType ButtonEventType; /**< Air intake temperature in degrees celcius */ static const Property AirIntakeTemperature; diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 1a9fd0a..f7518f8 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -23,5 +23,5 @@ add_subdirectory(wheel) add_subdirectory(dbus) add_subdirectory(websocketsink) add_subdirectory(obd2plugin) - +add_subdirectory(demosink) add_subdirectory(websocketsourceplugin) diff --git a/plugins/demosink/CMakeLists.txt b/plugins/demosink/CMakeLists.txt new file mode 100644 index 0000000..4a1c749 --- /dev/null +++ b/plugins/demosink/CMakeLists.txt @@ -0,0 +1,16 @@ + +include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs}) + +pkg_check_modules(giounix REQUIRED gio-unix-2.0) +set(link_libraries ${link_libraries} ${giounix_LIBRARIES}) +#set(include_dirs ${include_dirs} ${giounix_INCLUDE_DIRS}) +include_directories(${giounix_INCLUDE_DIRS}) + +set(demosinkplugin_headers demosinkplugin.h) +set(demosinkplugin_sources demosinkplugin.cpp) + +add_library(demosinkplugin MODULE ${demosinkplugin_sources}) +set_target_properties(demosinkplugin PROPERTIES PREFIX "") +target_link_libraries(demosinkplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries}) + +install(TARGETS demosinkplugin LIBRARY DESTINATION lib/automotive-message-broker) diff --git a/plugins/demosink/demosinkplugin.cpp b/plugins/demosink/demosinkplugin.cpp new file mode 100644 index 0000000..99b6be1 --- /dev/null +++ b/plugins/demosink/demosinkplugin.cpp @@ -0,0 +1,91 @@ +/* +Copyright (C) 2012 Intel Corporation + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "demosinkplugin.h" +#include "abstractroutingengine.h" + +#include +#include +#include +#include + +using namespace std; + +#include "debugout.h" + +string findReplace(string str, string tofind, string replacewith, string exclusions="") +{ + size_t i=0; + + size_t exclusionPos = exclusions.find(tofind,0); + + while((i = str.find(tofind,i)) != string::npos) + { + if( exclusionPos != string::npos ) + { + if(str.substr(i-exclusionPos,exclusions.length()) == exclusions) + { + i+=replacewith.size(); + continue; + } + } + + str.replace(i,tofind.size(),replacewith); + i+=replacewith.size(); + } + +} + + +DemoSink::DemoSink(AbstractRoutingEngine* re) +:AbstractSink(re) +{ + +} + +DemoSink::~DemoSink() +{ + +} + +extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine) +{ + return new DemoSinkManager(routingengine); +} + +string DemoSink::uuid() +{ + return "5b0e8a04-d6d7-43af-b827-1663627a25d9"; +} + +void DemoSink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, string uuid) +{ + std::string app = configuration["script"]; + + string cmdline = findReplace(app,"%1",value->toString()); + GError* error = NULL; + + g_spawn_command_line_async(cmdline.c_str(), &error); + +} + +void DemoSink::supportedChanged(PropertyList list) +{ + routingEngine->subscribeToProperty(VehicleProperty::ButtonEvent, this); + routingEngine->subscribeToProperty(VehicleProperty::TurnSignal, this); +} diff --git a/plugins/demosink/demosinkplugin.h b/plugins/demosink/demosinkplugin.h new file mode 100644 index 0000000..9556764 --- /dev/null +++ b/plugins/demosink/demosinkplugin.h @@ -0,0 +1,56 @@ +/* +Copyright (C) 2012 Intel Corporation + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef WHEELPLUGIN_H +#define WHEELPLUGIN_H + +#include +#include + +using namespace std; + +class DemoSink: public AbstractSink +{ + +public: + DemoSink(AbstractRoutingEngine* re); + ~DemoSink(); + + string uuid(); + + void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid); + void supportedChanged(PropertyList); + + friend class WheelPrivate; + +private: + + PropertySet mRequests; +}; + +class DemoSinkManager: public AbstractSinkManager +{ +public: + DemoSinkManager(AbstractRoutingEngine* engine) + :AbstractSinkManager(engine) + { + new DemoSink(engine); + } +}; + +#endif // WHEELPLUGIN_H diff --git a/plugins/wheel/wheelplugin.cpp b/plugins/wheel/wheelplugin.cpp index db31f77..101c6d4 100644 --- a/plugins/wheel/wheelplugin.cpp +++ b/plugins/wheel/wheelplugin.cpp @@ -94,7 +94,7 @@ private: char *button; uint16_t machineGuns; - uint16_t turnSignal; + TurnSignals::TurnSignalType turnSignal; uint16_t currentGear; uint16_t oilPSI; uint16_t coolantTemp; @@ -190,7 +190,7 @@ void readCallback(GObject *srcObj, GAsyncResult *res, gpointer userData) WheelPrivate::WheelPrivate(WheelSourcePlugin *parent, AbstractRoutingEngine *route) :re(route), gis(nullptr), axis(nullptr), button(nullptr), -oilPSI(10), coolantTemp(100), turnSignal(0), throttle(0), +oilPSI(10), coolantTemp(100), turnSignal(TurnSignals::Off), throttle(0), machineGuns(false), currentGear(0), steeringAngle(0), clutch(false), oldClutch(false), brake(false), oldBrake(false) { @@ -415,12 +415,12 @@ void WheelPrivate::changeMachineGuns(bool val) void WheelPrivate::changeTurnSignal(TurnSignal dir, bool val) { - int tsVal=0; + TurnSignals::TurnSignalType tsVal= TurnSignals::Off; if (val) { - if (dir == TS_LEFT) - tsVal = 2; + if (dir == TurnSignals::Left) + tsVal = TurnSignals::Left; else - tsVal = 1; + tsVal = TurnSignals::Right; } this->turnSignal = tsVal; VehicleProperty::TurnSignalType temp(this->turnSignal); -- 2.7.4