example does throttle
[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 uint16_t accelerationX = 0;
30 uint16_t transmissionShiftPostion = 0;
31 uint16_t steeringWheelAngle=0;
32 uint16_t throttlePos = 0;
33
34 static gboolean timeoutCallback(gpointer data)
35 {
36         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
37         
38         src->randomizeProperties();
39         
40         return true;
41 }
42
43 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re)
44 :AbstractSource(re), velocity(0), engineSpeed(0)
45 {
46         re->setSupported(supported(), this);
47         
48         debugOut("setting timeout");
49         g_timeout_add(1000, timeoutCallback, this );
50         
51 }
52
53
54
55 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine)
56 {
57         return new ExampleSourcePlugin(routingengine);
58         
59 }
60
61 string ExampleSourcePlugin::uuid()
62 {
63         return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
64 }
65
66 boost::any ExampleSourcePlugin::getProperty(VehicleProperty::Property property)
67 {
68         if(property == VehicleProperty::VehicleSpeed)
69         {
70                 return velocity;
71         }
72         else if(property == VehicleProperty::EngineSpeed)
73         {
74                 return engineSpeed;
75         }
76 }
77
78 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
79 {
80         if(reply->property == VehicleProperty::VehicleSpeed)
81         {
82                 reply->value = velocity;
83                 reply->completed(reply);
84         }
85         else if(reply->property == VehicleProperty::EngineSpeed)
86         {
87                 reply->value = engineSpeed;
88                 reply->completed(reply);
89         }
90         else if(reply->property == VehicleProperty::AccelerationX)
91         {
92                 reply->value = accelerationX;
93                 reply->completed(reply);
94         }
95         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
96         {
97                 reply->value = transmissionShiftPostion;
98                 reply->completed(reply);
99         }
100         else if(reply->property == VehicleProperty::SteeringWheelAngle)
101         {
102                 reply->value = steeringWheelAngle;
103                 reply->completed(reply);
104         }
105 }
106
107 void ExampleSourcePlugin::setProperty(VehicleProperty::Property , boost::any )
108 {
109
110 }
111
112 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
113 {
114         mRequests.push_back(property);
115 }
116
117 PropertyList ExampleSourcePlugin::supported()
118 {
119         PropertyList props;
120         props.push_back(VehicleProperty::EngineSpeed);
121         props.push_back(VehicleProperty::VehicleSpeed);
122         props.push_back(VehicleProperty::AccelerationX);
123         props.push_back(VehicleProperty::TransmissionShiftPosition);
124         props.push_back(VehicleProperty::SteeringWheelAngle);
125         props.push_back(VehicleProperty::ThrottlePosition);
126         
127         return props;
128 }
129
130 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
131 {
132         mRequests.remove(property);
133 }
134
135 void ExampleSourcePlugin::randomizeProperties()
136 {
137         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
138         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
139         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
140         transmissionShiftPostion = 1 + (6.00 * (rand() / (RAND_MAX + 1.0)));
141         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
142         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
143         
144         DebugOut()<<"setting velocity to: "<<velocity<<endl;
145         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
146         
147         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, velocity);
148         routingEngine->updateProperty(VehicleProperty::EngineSpeed, engineSpeed);
149         routingEngine->updateProperty(VehicleProperty::AccelerationX, accelerationX);
150         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, steeringWheelAngle);
151         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition, transmissionShiftPostion);
152         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, throttlePos);
153 }