fixed up AbstractPropertyType so it can use enums
authorKevron Rees <kevron_m_rees@linux.intel.com>
Tue, 11 Sep 2012 16:10:10 +0000 (09:10 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Tue, 11 Sep 2012 16:10:17 +0000 (09:10 -0700)
lib/abstractpropertytype.h
lib/listplusplus.h
lib/vehicleproperty.cpp
lib/vehicleproperty.h
plugins/CMakeLists.txt
plugins/demosink/CMakeLists.txt [new file with mode: 0644]
plugins/demosink/demosinkplugin.cpp [new file with mode: 0644]
plugins/demosink/demosinkplugin.h [new file with mode: 0644]
plugins/wheel/wheelplugin.cpp

index 3ba0307..1177346 100644 (file)
@@ -24,6 +24,8 @@
 #include <stdexcept>
 #include <boost/any.hpp>
 #include <boost/lexical_cast.hpp>
+#include <boost/utility.hpp>
+#include <type_traits>
 
 class AbstractPropertyType
 {
@@ -76,7 +78,9 @@ public:
        BasicPropertyType(std::string val)
        {
                if(!val.empty() && val != "")
-                       setValue(boost::lexical_cast<T,std::string>(val));
+               {
+                       serialize<T>(val);
+               }
                else throw std::runtime_error("value cannot be empty");
        }
 
@@ -87,6 +91,28 @@ public:
 
                return stream.str();
        }
+
+private:
+
+       template <class N>
+       void serialize(std::string val,  typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
+       {
+               int someTemp;
+
+               std::stringstream stream(val);
+
+               stream>>someTemp;
+               setValue((N)someTemp);
+       }
+
+       template <class N>
+       void serialize(std::string  val,  typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
+       {
+               std::stringstream stream(val);
+               N someTemp;
+               stream>>someTemp;
+               setValue(someTemp);
+       }
 };
 
 class StringPropertyType: public AbstractPropertyType
index 2821b08..df815f3 100644 (file)
@@ -45,7 +45,6 @@ public:
                }
        }
        
-       
        bool contains(T value)
        {
                return (std::find(mList->begin(), mList->end(), value) != mList->end());
index 51f344c..dd40664 100644 (file)
@@ -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);
index e9828ff..7732c63 100644 (file)
@@ -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<uint16_t> TurnSignalType;
+       typedef BasicPropertyType<TurnSignals::TurnSignalType> 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<int> ButtonEventType;
+       typedef BasicPropertyType<ButtonEvents::ButtonEventType> ButtonEventType;
 
        /**< Air intake temperature in degrees celcius */
        static const Property AirIntakeTemperature;
index 1a9fd0a..f7518f8 100644 (file)
@@ -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 (file)
index 0000000..4a1c749
--- /dev/null
@@ -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 (file)
index 0000000..99b6be1
--- /dev/null
@@ -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 <iostream>
+#include <stdexcept>
+#include <boost/assert.hpp>
+#include <glib.h>
+
+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 (file)
index 0000000..9556764
--- /dev/null
@@ -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 <abstractsink.h>
+#include <string>
+
+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
index db31f77..101c6d4 100644 (file)
@@ -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);