updated example to produce fake data for wheel and transmission shift properties
[profile/ivi/automotive-message-broker.git] / plugins / exampleplugin.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 "exampleplugin.h"
20
21 #include <iostream>
22 #include <boost/assert.hpp>
23 #include <glib.h>
24
25 using namespace std;
26
27 #include "debugout.h"
28
29 static gboolean timeoutCallback(gpointer data)
30 {
31         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
32         
33         src->randomizeProperties();
34         
35         return true;
36 }
37
38 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re)
39 :AbstractSource(re), velocity(0), engineSpeed(0)
40 {
41         re->setSupported(supported(), this);
42         
43         debugOut("setting timeout");
44         g_timeout_add(1000, timeoutCallback, this );
45         
46 }
47
48
49
50 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine)
51 {
52         return new ExampleSourcePlugin(routingengine);
53         
54 }
55
56 string ExampleSourcePlugin::uuid()
57 {
58         return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
59 }
60
61 boost::any ExampleSourcePlugin::getProperty(VehicleProperty::Property property)
62 {
63         if(property == VehicleProperty::VehicleSpeed)
64         {
65                 return velocity;
66         }
67         else if(property == VehicleProperty::EngineSpeed)
68         {
69                 return engineSpeed;
70         }
71 }
72
73 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
74 {
75         if(reply->property == VehicleProperty::VehicleSpeed)
76         {
77                 reply->value = velocity;
78                 reply->completed(reply);
79         }
80         else if(reply->property == VehicleProperty::EngineSpeed)
81         {
82                 reply->value = engineSpeed;
83                 reply->completed(reply);
84         }
85 }
86
87 void ExampleSourcePlugin::setProperty(VehicleProperty::Property , boost::any )
88 {
89
90 }
91
92 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
93 {
94         mRequests.push_back(property);
95 }
96
97 PropertyList ExampleSourcePlugin::supported()
98 {
99         PropertyList props;
100         props.push_back(VehicleProperty::EngineSpeed);
101         props.push_back(VehicleProperty::VehicleSpeed);
102         props.push_back(VehicleProperty::AccelerationX);
103         props.push_back(VehicleProperty::TransmissionShiftPosition);
104         props.push_back(VehicleProperty::SteeringWheelAngle);
105         
106         return props;
107 }
108
109 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
110 {
111         mRequests.remove(property);
112 }
113
114 void ExampleSourcePlugin::randomizeProperties()
115 {
116         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
117         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
118         int accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
119         int transmissionShiftPostion = 1 + (6.00 * (rand() / (RAND_MAX + 1.0)));
120         int steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
121         
122         DebugOut()<<"setting velocity to: "<<velocity<<endl;
123         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
124         
125         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, velocity);
126         routingEngine->updateProperty(VehicleProperty::EngineSpeed, engineSpeed);
127         routingEngine->updateProperty(VehicleProperty::AccelerationX, accelerationX);
128         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, steeringWheelAngle);
129         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition, transmissionShiftPostion);
130 }