some core changes as well as additions to opencv plugin
authorKevron Rees <tripzero.kev@gmail.com>
Fri, 28 Mar 2014 05:03:05 +0000 (22:03 -0700)
committerKevron Rees <tripzero.kev@gmail.com>
Fri, 28 Mar 2014 05:03:32 +0000 (22:03 -0700)
16 files changed:
ambd/core.cpp
ambd/core.h
ambd/main.cpp
examples/opencvdbusconfig
examples/websocketsink2
examples/websocketsource2
lib/abstractroutingengine.h
lib/abstractsource.cpp
lib/abstractsource.h
plugins/exampleplugin.cpp
plugins/gpsnmea/gpsnmea.cpp
plugins/opencvlux/opencvluxplugin.cpp
plugins/opencvlux/opencvluxplugin.h
plugins/testplugin/testplugin.cpp
plugins/websocket/websocketsource.cpp
plugins/wheel/wheelplugin.cpp

index 9aebff4..8aa2acc 100644 (file)
@@ -66,28 +66,14 @@ Core::~Core()
        mSinks.clear();
 }
 
-void Core::setSupported(PropertyList supported, AbstractSource* source)
+void Core::registerSource(AbstractSource *source)
 {
-       if(!source)
-               return;
-
-       mSources[source->uuid()] = source;
-               
-       handleAddSupported(supported, source);
-
-       /// tell all new sinks about the newly supported properties.
-
-       for(auto itr = mSinks.begin(); itr != mSinks.end(); ++itr)
-       {
-               AbstractSink* s = (*itr);
-               s->supportedChanged(this->supported());
-       }
-
+       mSources.insert(source);
 }
 
 void Core::updateSupported(PropertyList added, PropertyList removed, AbstractSource* source)
 {
-       if(!source || mSources.find(source->uuid()) == mSources.end())
+       if(!source || mSources.find(source) == mSources.end())
                return;
 
        /// add the newly supported to master list
@@ -98,29 +84,27 @@ void Core::updateSupported(PropertyList added, PropertyList removed, AbstractSou
 
        handleRemoveSupported(removed, source);
        
-       /// tell all new sinks about the newly supported properties.
+       /// tell all sinks about the newly supported properties.
 
        for(auto itr = mSinks.begin(); itr != mSinks.end(); ++itr)
        {
-               (*itr)->supportedChanged(supported());
+               AbstractSink* sink = *itr;
+               sink->supportedChanged(supported());
        }
 }
 
 PropertyList Core::supported()
 {
        PropertyList supportedProperties;
-       // TODO: should we care here about duplicates ?
-       // (if multiple sources supports the same property ==> Property will be more than once in PropertyList)
+
        transform(mMasterPropertyList.begin(), mMasterPropertyList.end(), back_inserter(supportedProperties),
                [](const std::multimap<AbstractSource*, VehicleProperty::Property>::value_type& itr) { return itr.second; }
        );
+
        // remove duplicates:
        supportedProperties.sort();
        supportedProperties.unique();
 
-       //DebugOut(1)<<__FUNCTION__<<"supported list: " << endl;
-       //for_each(supportedProperties.begin(), supportedProperties.end(), [](const std::string& str){ DebugOut(1)<<__FUNCTION__<< str << endl; });
-
        return supportedProperties;
 }
 
@@ -208,7 +192,7 @@ AsyncRangePropertyReply * Core::getRangePropertyAsync(AsyncRangePropertyRequest
 
        for(auto itr = mSources.begin(); itr != mSources.end(); ++itr)
        {
-               AbstractSource* src = itr->second;
+               AbstractSource* src = *itr;
                if(((src->supportedOperations() & AbstractSource::GetRanged) == AbstractSource::GetRanged))
                {
                        src->getRangePropertyAsync(reply);
@@ -326,11 +310,18 @@ bool Core::unsubscribeToProperty(VehicleProperty::Property property, AbstractSin
 
 PropertyInfo Core::getPropertyInfo(VehicleProperty::Property property, string sourceUuid)
 {
-       auto srcIt = mSources.find(sourceUuid);
+       if(sourceUuid == "")
+               return PropertyInfo::invalid();
+
+       auto srcIt = find_if(mSources.begin(), mSources.end(),[&sourceUuid](const std::set<AbstractSource*>::value_type & itr)
+       {
+               return (itr)->uuid() == sourceUuid;
+       });
+
        if(srcIt == mSources.end())
                return PropertyInfo::invalid();
 
-       return srcIt->second->getPropertyInfo(property);
+       return (*srcIt)->getPropertyInfo(property);
 }
 
 std::list<string> Core::sourcesForProperty(VehicleProperty::Property property)
@@ -356,10 +347,18 @@ std::list<string> Core::sourcesForProperty(VehicleProperty::Property property)
        return list;
 }
 
+void Core::inspectSupported()
+{
+       for(AbstractSource* src : mSources)
+       {
+               updateSupported(src->supported(), PropertyList(), src);
+       }
+}
+
 void Core::handleAddSupported(const PropertyList& added, AbstractSource* source)
 {
        if(!source)
-               return;
+               throw std::runtime_error("Core::handleAddSupported passed a null source");
 
        for(auto itr = added.begin(); itr != added.end(); ++itr)
        {
@@ -417,9 +416,12 @@ AbstractSource* Core::sourceForProperty(const VehicleProperty::Property& propert
                );
        }
        else{
-               auto itSource = mSources.find(sourceUuidFilter);
+               auto itSource = find_if(mSources.begin(),mSources.end(),[&sourceUuidFilter](const std::set<AbstractSource*>::value_type & it)
+               {
+                       return (it)->uuid() == sourceUuidFilter;
+               });
                if(itSource != mSources.end()){
-                       auto range = mMasterPropertyList.equal_range(itSource->second);
+                       auto range = mMasterPropertyList.equal_range(*itSource);
                        auto temp = find_if(
                                range.first,    // the first property in source
                                range.second,   // one item right after the last property in source
index c1a7053..4199aff 100644 (file)
@@ -35,7 +35,7 @@ public:
        ~Core();
        /// sources:
 
-       void setSupported(PropertyList supported, AbstractSource* source);
+       void registerSource(AbstractSource *src);
        void updateSupported(PropertyList added, PropertyList removed, AbstractSource* source);
        void updateProperty(AbstractPropertyType* value, const std::string &uuid);
        
@@ -61,6 +61,8 @@ public:
                int firedPropertiesPerSecond;
        };
 
+       void inspectSupported();
+
 private:
 
        void handleAddSupported(const PropertyList& added, AbstractSource* source);
@@ -75,7 +77,7 @@ private:
        std::multimap<AbstractSource*, VehicleProperty::Property> mMasterPropertyList;
 
        // K = AbstractSource::uuid(), T = AbstractSource*
-       std::unordered_map<std::string, AbstractSource*> mSources;
+       std::set<AbstractSource*> mSources;
        std::set<AbstractSink*> mSinks;
 
        Performance performance;
index cba0373..07d17e5 100644 (file)
@@ -154,6 +154,8 @@ int main(int argc, char **argv)
        Core routingEngine;
 
        PluginLoader loader(config, &routingEngine , argc, argv);
+
+       routingEngine.inspectSupported();
        
        if(!loader.sources().size())
        {
index 9b326b6..0cd66b6 100644 (file)
@@ -1,4 +1,5 @@
 {
+       "mainloop" : "/usr/lib/automotive-message-broker/qtmainloopplugin.so",
        "sources" : [ 
                {
                        "name" : "OpenCV LUX",
@@ -10,6 +11,9 @@
                        "pixelUpperBound" : "255",
                        "device" : "0"
                },
+               {
+                       "path" : "/usr/lib/automotive-message-broker/examplesourceplugin.so"
+               }
        ],
        "sinks": [
                {
index f087fae..45d4a51 100644 (file)
@@ -13,7 +13,7 @@
                        "interface" : "eth1",
                        "ssl" : "false",
                        "port" : "23000",
-                       "binaryProtocol" : "true",
+                       "binaryProtocol" : "false",
                        "useExtensions" : "true"
                } 
        ]
index 8d7953f..ecb86d8 100644 (file)
@@ -6,7 +6,7 @@
                        "port" : "23000",
                        "ssl" : "false",
                        "ip" : "127.0.0.1",
-                       "binaryProtocol" : "true",
+                       "binaryProtocol" : "false",
                        "useExtensions" : "true"
                }
        ],
index cd5a0c2..aece6c7 100644 (file)
@@ -313,7 +313,7 @@ class AbstractRoutingEngine
 public:
        virtual ~AbstractRoutingEngine();
 
-       virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
+       virtual void registerSource(AbstractSource* src) = 0;
        virtual void updateSupported(PropertyList added, PropertyList removed, AbstractSource* source) = 0;
 
 
index c60c7e6..d8649fe 100644 (file)
@@ -22,7 +22,7 @@
 AbstractSource::AbstractSource(AbstractRoutingEngine* engine, map<string, string> config)
        : AbstractSink(engine,config), routingEngine(engine)
 {
-       
+       engine->registerSource(this);
 }
 
 AbstractSource::~AbstractSource()
index ae9544c..53252a0 100644 (file)
@@ -108,6 +108,11 @@ public:
         */
        virtual PropertyInfo getPropertyInfo(VehicleProperty::Property property);
        
+       /*!
+        * \brief supported
+        * \return returns the supported properties.
+        */
+       virtual PropertyList supported() = 0;
 
 protected:
        /*!
index 5bb86dd..3b7fa8a 100644 (file)
@@ -101,12 +101,6 @@ ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string,
 
        PropertyInfo acInfo(0,acZones);
        propertyInfoMap[VehicleProperty::AirConditioning] = acInfo;
-
-       re->setSupported(supported(), this);
-
-       PropertyList testSupported = re->supported();
-
-       g_assert(contains(testSupported,VehicleProperty::MachineGunTurretStatus));
 }
 
 
index 4ecdeed..dcac3e6 100644 (file)
@@ -442,8 +442,6 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map<string, string> conf
                g_io_channel_set_close_on_unref(chan, true);
                g_io_channel_unref(chan); //Pass ownership of the GIOChannel to the watch.
        }
-
-       re->setSupported(supported(), this);
 }
 
 GpsNmeaSource::~GpsNmeaSource()
index b5f152f..1142376 100644 (file)
@@ -41,11 +41,6 @@ using namespace std;
 OpenCvLuxPlugin::OpenCvLuxPlugin(AbstractRoutingEngine* re, map<string, string> config)
        :AbstractSource(re, config), lastLux(0), speed(0), latitude(0), longitude(0)
 {
-       re->setSupported(supported(), this);
-       re->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
-       re->subscribeToProperty(VehicleProperty::Latitude,this);
-       re->subscribeToProperty(VehicleProperty::Longitude, this);
-
        shared = new Shared;
        shared->parent = this;
 
@@ -171,6 +166,10 @@ OpenCvLuxPlugin::OpenCvLuxPlugin(AbstractRoutingEngine* re, map<string, string>
        }
 #endif
 
+       routingEngine->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
+       routingEngine->subscribeToProperty(VehicleProperty::Latitude,this);
+       routingEngine->subscribeToProperty(VehicleProperty::Longitude, this);
+
 }
 
 
@@ -265,6 +264,11 @@ void OpenCvLuxPlugin::propertyChanged(AbstractPropertyType *value)
        }
 }
 
+void OpenCvLuxPlugin::supportedChanged(PropertyList)
+{
+       DebugOut()<<"OpenCvLuxPlugin::supported changed."<<endl;
+}
+
 static int grabImage(void *data)
 {
        OpenCvLuxPlugin::Shared* shared = static_cast<OpenCvLuxPlugin::Shared*>(data);
@@ -377,10 +381,13 @@ bool OpenCvLuxPlugin::init()
                DebugOut()<<"we failed to open camera device ("<<device<<") or no camera found"<<endl;
                return false;
        }
-       cv::Size s = cv::Size((int) shared->m_capture->get(CV_CAP_PROP_FRAME_WIDTH),
-                                         (int) shared->m_capture->get(CV_CAP_PROP_FRAME_HEIGHT));
+       if(!shared->mWriter || !shared->mWriter->isOpened())
+       {
+               cv::Size s = cv::Size((int) shared->m_capture->get(CV_CAP_PROP_FRAME_WIDTH),
+                                                         (int) shared->m_capture->get(CV_CAP_PROP_FRAME_HEIGHT));
 
-       shared->mWriter = new cv::VideoWriter("/tmp/video.avi",CV_FOURCC('H','2','6','4'),30,s);
+               shared->mWriter = new cv::VideoWriter("/tmp/video.avi",CV_FOURCC('H','2','6','4'),30,s);
+       }
 
        DebugOut()<<"camera frame width: "<<shared->m_capture->get(CV_CAP_PROP_FRAME_WIDTH)<<endl;
        DebugOut()<<"camera frame height: "<<shared->m_capture->get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;
@@ -389,6 +396,8 @@ bool OpenCvLuxPlugin::init()
        return true;
 }
 
+
+
 void OpenCvLuxPlugin::writeVideoFrame(cv::Mat frame)
 {
        //if(speed > 0)
index 7b7477b..4b2278f 100644 (file)
@@ -64,7 +64,7 @@ public:
 
        void propertyChanged(AbstractPropertyType* value);
        
-       void supportedChanged(PropertyList) {}
+       void supportedChanged(PropertyList);
        
        void updateProperty(uint lux);
 
@@ -77,7 +77,6 @@ public Q_SLOTS:
 private: /// methods:
        bool init();
 
-
 private:       
        uint speed;
        uint latitude;
index aa8da7e..c337563 100644 (file)
@@ -79,11 +79,11 @@ bool TestPlugin::testCoreSetSupported()
        //
        // CALL setSupported
        //
-       routingEngine->setSupported(m_supportedProperties, nullptr);// invalid input
+       routingEngine->updateSupported(m_supportedProperties, PropertyList(), nullptr);// invalid input
        TEST(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).isValid() == false);
 
        // valid call
-       routingEngine->setSupported(m_supportedProperties, this);// we are register as source from now
+       routingEngine->updateSupported(m_supportedProperties, PropertyList(), this);// we are register as source from now
 
        TEST(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).isValid() == true);
        Zone::ZoneList zones(routingEngine->getPropertyInfo(TestProptertyName1, uuid()).zones());
index 2b83b24..601a317 100644 (file)
@@ -501,7 +501,6 @@ WebSocketSource::WebSocketSource(AbstractRoutingEngine *re, map<string, string>
        context = libwebsocket_create_context(&info);
 
        setConfiguration(config);
-       re->setSupported(supported(), this);
 
        //printf("websocketsource loaded!!!\n");
        g_timeout_add(1000,checkTimeouts,this); //Do this once per second, check for functions that have timed out and reply with success = false;
index f4dfbb9..6115ce4 100644 (file)
@@ -110,7 +110,6 @@ private:
 WheelSourcePlugin::WheelSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
 :AbstractSource(re, config)
 {
-       re->setSupported(supported(), this);
        this->mWheel = new WheelPrivate(this, re);
 }