reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / examplesink.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
20 #include "examplesink.h"
21 #include "abstractroutingengine.h"
22 #include "debugout.h"
23 #include "listplusplus.h"
24
25 #include <glib.h>
26
27
28 extern "C" void create(AbstractRoutingEngine* routingEngine, map<string, string> config)
29 {
30         new ExampleSink(routingEngine, config);
31 }
32
33 ExampleSink::ExampleSink(AbstractRoutingEngine* engine, map<string, string> config): AbstractSink(engine, config)
34 {
35         routingEngine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
36         routingEngine->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
37         routingEngine->subscribeToProperty(VehicleProperty::Latitude, this);
38         routingEngine->subscribeToProperty(VehicleProperty::Longitude, this);
39         routingEngine->subscribeToProperty(VehicleProperty::ExteriorBrightness, this);
40
41         supportedChanged(engine->supported());
42 }
43
44
45 PropertyList ExampleSink::subscriptions()
46 {
47
48 }
49
50 void ExampleSink::supportedChanged(const PropertyList & supportedProperties)
51 {
52         DebugOut()<<"Support changed!"<<endl;
53
54         if(contains(supportedProperties, VehicleProperty::VehicleSpeed))
55         {
56                 AsyncPropertyRequest velocityRequest;
57                 velocityRequest.property = VehicleProperty::VehicleSpeed;
58                 velocityRequest.completed = [](AsyncPropertyReply* reply)
59                 {
60                         if(!reply->success)
61                                 DebugOut(DebugOut::Error)<<"Velocity Async request failed ("<<reply->error<<")"<<endl;
62                         else
63                                 DebugOut(0)<<"Velocity Async request completed: "<<reply->value->toString()<<endl;
64                         delete reply;
65                 };
66
67                 routingEngine->getPropertyAsync(velocityRequest);
68         }
69
70         if(contains(supportedProperties, VehicleProperty::VIN))
71         {
72                 AsyncPropertyRequest vinRequest;
73                 vinRequest.property = VehicleProperty::VIN;
74                 vinRequest.completed = [](AsyncPropertyReply* reply)
75                 {
76                         if(!reply->success)
77                                 DebugOut(DebugOut::Error)<<"VIN Async request failed ("<<reply->error<<")"<<endl;
78                         else
79                                 DebugOut(0)<<"VIN Async request completed: "<<reply->value->toString()<<endl;
80                         delete reply;
81                 };
82
83                 routingEngine->getPropertyAsync(vinRequest);
84         }
85         if(contains(supportedProperties, VehicleProperty::WMI))
86         {
87                 AsyncPropertyRequest wmiRequest;
88                 wmiRequest.property = VehicleProperty::WMI;
89                 wmiRequest.completed = [](AsyncPropertyReply* reply)
90                 {
91                         if(!reply->success)
92                                 DebugOut(DebugOut::Error)<<"WMI Async request failed ("<<reply->error<<")"<<endl;
93                         else
94                                 DebugOut(1)<<"WMI Async request completed: "<<reply->value->toString()<<endl;
95                         delete reply;
96                 };
97
98                 routingEngine->getPropertyAsync(wmiRequest);
99         }
100         if(contains(supportedProperties, VehicleProperty::BatteryVoltage))
101         {
102                 AsyncPropertyRequest batteryVoltageRequest;
103                 batteryVoltageRequest.property = VehicleProperty::BatteryVoltage;
104                 batteryVoltageRequest.completed = [](AsyncPropertyReply* reply)
105                 {
106                         if(!reply->success)
107                                 DebugOut(DebugOut::Error)<<"BatteryVoltage Async request failed ("<<reply->error<<")"<<endl;
108                         else
109                                 DebugOut(1)<<"BatteryVoltage Async request completed: "<<reply->value->toString()<<endl;
110                         delete reply;
111                 };
112
113                 routingEngine->getPropertyAsync(batteryVoltageRequest);
114         }
115         if(contains(supportedProperties, VehicleProperty::DoorsPerRow))
116         {
117                 AsyncPropertyRequest doorsPerRowRequest;
118                 doorsPerRowRequest.property = VehicleProperty::DoorsPerRow;
119                 doorsPerRowRequest.completed = [](AsyncPropertyReply* reply)
120                 {
121                         if(!reply->success)
122                                 DebugOut(DebugOut::Error)<<"Doors per row Async request failed ("<<reply->error<<")"<<endl;
123                         else
124                                 DebugOut(1)<<"Doors per row: "<<reply->value->toString()<<endl;
125                         delete reply;
126                 };
127
128                 routingEngine->getPropertyAsync(doorsPerRowRequest);
129         }
130
131         if(contains(supportedProperties,VehicleProperty::AirbagStatus))
132         {
133                 AsyncPropertyRequest airbagStatus;
134                 airbagStatus.property = VehicleProperty::AirbagStatus;
135                 airbagStatus.zoneFilter = Zone::FrontRight | Zone::FrontSide;
136                 airbagStatus.completed = [](AsyncPropertyReply* reply)
137                 {
138                         if(!reply->success)
139                         {
140                                 DebugOut(DebugOut::Error)<<"Airbag Async request failed ("<<reply->error<<")"<<endl;
141                         }
142                         else
143                                 DebugOut(1)<<"Airbag Status: "<<reply->value->toString()<<endl;
144                         delete reply;
145                 };
146
147                 routingEngine->getPropertyAsync(airbagStatus);
148         }
149
150         if(contains(supportedProperties, VehicleProperty::ExteriorBrightness))
151         {
152                 AsyncPropertyRequest exteriorBrightness;
153                 exteriorBrightness.property = VehicleProperty::ExteriorBrightness;
154                 exteriorBrightness.completed = [](AsyncPropertyReply* reply)
155                 {
156                         if(!reply->success)
157                                 DebugOut(DebugOut::Error)<<"Exterior Brightness Async request failed ("<<reply->error<<")"<<endl;
158                         else
159                                 DebugOut(1)<<"Exterior Brightness: "<<reply->value->toString()<<endl;
160                         delete reply;
161                 };
162
163                 routingEngine->getPropertyAsync(exteriorBrightness);
164         }
165
166         auto getRangedCb = [](gpointer data)
167         {
168                 AbstractRoutingEngine* routingEngine = (AbstractRoutingEngine*)data;
169
170                 AsyncRangePropertyRequest vehicleSpeedFromLastWeek;
171
172                 vehicleSpeedFromLastWeek.timeBegin = amb::currentTime() - 10;
173                 vehicleSpeedFromLastWeek.timeEnd = amb::currentTime();
174
175                 PropertyList requestList;
176                 requestList.push_back(VehicleProperty::VehicleSpeed);
177                 requestList.push_back(VehicleProperty::EngineSpeed);
178
179                 vehicleSpeedFromLastWeek.properties = requestList;
180                 vehicleSpeedFromLastWeek.completed = [](AsyncRangePropertyReply* reply)
181                 {
182                         std::list<AbstractPropertyType*> values = reply->values;
183                         for(auto itr = values.begin(); itr != values.end(); itr++)
184                         {
185                                 auto val = *itr;
186                                 DebugOut(1)<<"Value from past: ("<<val->name<<"): "<<val->toString()<<" time: "<<val->timestamp<<endl;
187                         }
188
189                         delete reply;
190                 };
191
192                 routingEngine->getRangePropertyAsync(vehicleSpeedFromLastWeek);
193
194                 return 0;
195         };
196
197         g_timeout_add(10000, getRangedCb, routingEngine);
198 }
199
200 void ExampleSink::propertyChanged(AbstractPropertyType *value)
201 {
202         VehicleProperty::Property property = value->name;
203         DebugOut()<<property<<" value: "<<value->toString()<<endl;
204 }
205
206 const string ExampleSink::uuid()
207 {
208         return "f7e4fab2-eb73-4842-9fb0-e1c550eb2d81";
209 }