Merge branch 'master' of https://github.com/otcshare/automotive-message-broker
[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
24 #include <glib.h>
25
26
27 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
28 {
29         return new ExampleSinkManager(routingengine, config);
30 }
31
32 ExampleSink::ExampleSink(AbstractRoutingEngine* engine, map<string, string> config): AbstractSink(engine, config)
33 {
34         routingEngine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
35         routingEngine->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
36
37         supportedChanged(routingEngine->supported());
38
39 }
40
41
42 PropertyList ExampleSink::subscriptions()
43 {
44
45 }
46
47 void ExampleSink::supportedChanged(PropertyList supportedProperties)
48 {
49         printf("Support changed!\n");
50         routingEngine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
51         routingEngine->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
52
53         AsyncPropertyRequest velocityRequest;
54         velocityRequest.property = VehicleProperty::VehicleSpeed;
55         velocityRequest.completed = [](AsyncPropertyReply* reply)
56         {
57                 DebugOut()<<"Velocity Async request completed: "<<reply->value->toString()<<endl; delete reply;
58         };
59
60         routingEngine->getPropertyAsync(velocityRequest);
61
62         AsyncPropertyRequest vinRequest;
63         vinRequest.property = VehicleProperty::VIN;
64         vinRequest.completed = [](AsyncPropertyReply* reply) { DebugOut(1)<<"VIN Async request completed: "<<reply->value->toString()<<endl; delete reply; };
65
66         routingEngine->getPropertyAsync(vinRequest);
67
68         AsyncPropertyRequest wmiRequest;
69         wmiRequest.property = VehicleProperty::WMI;
70         wmiRequest.completed = [](AsyncPropertyReply* reply) { DebugOut(1)<<"WMI Async request completed: "<<reply->value->toString()<<endl; delete reply; };
71
72         routingEngine->getPropertyAsync(wmiRequest);
73
74         AsyncPropertyRequest batteryVoltageRequest;
75         batteryVoltageRequest.property = VehicleProperty::BatteryVoltage;
76         batteryVoltageRequest.completed = [](AsyncPropertyReply* reply) { DebugOut(1)<<"BatteryVoltage Async request completed: "<<reply->value->toString()<<endl; delete reply; };
77
78         routingEngine->getPropertyAsync(batteryVoltageRequest);
79
80         auto getRangedCb = [](gpointer data)
81         {
82                 AbstractRoutingEngine* routingEngine = (AbstractRoutingEngine*)data;
83
84                 AsyncRangePropertyRequest vehicleSpeedFromLastWeek;
85
86                 vehicleSpeedFromLastWeek.timeBegin = amb::currentTime() - 10;
87                 vehicleSpeedFromLastWeek.timeEnd = amb::currentTime();
88                 vehicleSpeedFromLastWeek.property = VehicleProperty::VehicleSpeed;
89                 vehicleSpeedFromLastWeek.completed = [](AsyncRangePropertyReply* reply)
90                 {
91                         std::list<AbstractPropertyType*> values = reply->values;
92                         for(auto itr = values.begin(); itr != values.end(); itr++)
93                         {
94                                 auto val = *itr;
95                                 DebugOut(1)<<"Velocity value from past: "<<val->toString()<<" time: "<<val->timestamp<<endl;
96                         }
97                 };
98
99                 routingEngine->getRangePropertyAsync(vehicleSpeedFromLastWeek);
100
101                 return 0;
102         };
103
104         g_timeout_add(10000, getRangedCb, routingEngine);
105 }
106
107 void ExampleSink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
108 {
109         DebugOut()<<property<<" value: "<<value->toString()<<endl;
110 }
111
112 std::string ExampleSink::uuid()
113 {
114         return "f7e4fab2-eb73-4842-9fb0-e1c550eb2d81";
115 }