reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / demosink / demosinkplugin.cpp
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "demosinkplugin.h"
20 #include "abstractroutingengine.h"
21
22 #include <iostream>
23 #include <stdexcept>
24 #include <boost/assert.hpp>
25 #include <glib.h>
26
27 using namespace std;
28
29 #include "debugout.h"
30
31 string findReplace(string str, string tofind, string replacewith, string exclusions="")
32 {
33         size_t i=0;
34
35         size_t exclusionPos = exclusions.find(tofind,0);
36
37         while((i = str.find(tofind,i)) != string::npos)
38         {
39                 if( exclusionPos != string::npos )
40                 {
41                         if(str.substr(i-exclusionPos,exclusions.length()) == exclusions)
42                         {
43                                 i+=replacewith.size();
44                                 continue;
45                         }
46                 }
47
48                 str.replace(i,tofind.size(),replacewith);
49                 i+=replacewith.size();
50         }
51
52         return str;
53 }
54
55
56 DemoSink::DemoSink(AbstractRoutingEngine* re, map<string, string> config)
57 :AbstractSink(re, config)
58 {
59         routingEngine->subscribeToProperty(VehicleProperty::ButtonEvent, this);
60         routingEngine->subscribeToProperty(VehicleProperty::TurnSignal, this);
61         routingEngine->subscribeToProperty(VehicleProperty::MachineGunTurretStatus, this);
62 }
63
64 DemoSink::~DemoSink()
65 {
66
67 }
68
69 extern "C" void create(AbstractRoutingEngine* routingEngine, map<string, string> config)
70 {
71         new DemoSink(routingEngine, config);
72 }
73
74 const string DemoSink::uuid()
75 {
76         return "5b0e8a04-d6d7-43af-b827-1663627a25d9";
77 }
78
79 void DemoSink::propertyChanged(AbstractPropertyType *value)
80 {
81         VehicleProperty::Property property = value->name;
82
83         std::string app = configuration["script"];
84         std::string strValue = value->toString();
85
86         if(property == VehicleProperty::TurnSignal)
87         {
88                 if(value->value<TurnSignals::TurnSignalType>() == TurnSignals::Right)
89                 {
90                         strValue = "Right";
91                 }
92                 else if(value->value<TurnSignals::TurnSignalType>() == TurnSignals::Left)
93                 {
94                         strValue = "Left";
95                 }
96                 else if(value->value<TurnSignals::TurnSignalType>() == TurnSignals::Off)
97                 {
98                         strValue = "Off";
99                 }
100         } else if (property == VehicleProperty::ButtonEvent) {
101                 if (value->value<ButtonEvents::ButtonEventType>() == ButtonEvents::Preset1Button)
102                         strValue = "Button1";
103                 else if (value->value<ButtonEvents::ButtonEventType>() == ButtonEvents::Preset2Button)
104                         strValue = "Button2";
105                 else if (value->value<ButtonEvents::ButtonEventType>() == ButtonEvents::Preset3Button)
106                         strValue = "Button3";
107                 else if (value->value<ButtonEvents::ButtonEventType>() == ButtonEvents::Preset4Button)
108                         strValue = "Button4";
109         }
110
111         string cmdline = findReplace(app,"%1",strValue);
112         GError* error = NULL;
113
114         if(!g_spawn_command_line_async(cmdline.c_str(), &error))
115                 DebugOut()<<"Failed to launch command: "<<cmdline<<endl;
116
117 }
118
119 void DemoSink::supportedChanged(const PropertyList & list)
120 {
121         routingEngine->subscribeToProperty(VehicleProperty::ButtonEvent, this);
122         routingEngine->subscribeToProperty(VehicleProperty::TurnSignal, this);
123 }