major breakage ahead...
[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 uint16_t engineCoolant = 40;
34
35 static gboolean timeoutCallback(gpointer data)
36 {
37         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
38         
39         src->randomizeProperties();
40         
41         return true;
42 }
43
44 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re)
45 :AbstractSource(re), velocity(0), engineSpeed(0)
46 {
47         re->setSupported(supported(), this);
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 = BasicPropertyType<uint16_t>(velocity);
83                 reply->completed(reply);
84         }
85         else if(reply->property == VehicleProperty::EngineSpeed)
86         {
87                 reply->value = BasicPropertyType<uint16_t>(engineSpeed);
88                 reply->completed(reply);
89         }
90         else if(reply->property == VehicleProperty::AccelerationX)
91         {
92                 reply->value = BasicPropertyType<uint16_t>(accelerationX);
93                 reply->completed(reply);
94         }
95         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
96         {
97                 reply->value = BasicPropertyType<uint16_t>(transmissionShiftPostion);
98                 reply->completed(reply);
99         }
100         else if(reply->property == VehicleProperty::SteeringWheelAngle)
101         {
102                 reply->value = BasicPropertyType<uint16_t>(steeringWheelAngle);
103                 reply->completed(reply);
104         }
105 }
106
107 void ExampleSourcePlugin::setProperty(VehicleProperty::Property , AbstractPropertyType )
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         props.push_back(VehicleProperty::EngineCoolantTemperature);
127         
128         return props;
129 }
130
131 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
132 {
133         mRequests.remove(property);
134 }
135
136 void ExampleSourcePlugin::randomizeProperties()
137 {
138         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
139         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
140         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
141         transmissionShiftPostion = 1 + (6.00 * (rand() / (RAND_MAX + 1.0)));
142         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
143         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
144         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
145         
146         DebugOut()<<"setting velocity to: "<<velocity<<endl;
147         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
148         
149         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, BasicPropertyType<uint16_t>(velocity));
150         routingEngine->updateProperty(VehicleProperty::EngineSpeed, BasicPropertyType<uint16_t>(engineSpeed));
151         routingEngine->updateProperty(VehicleProperty::AccelerationX, BasicPropertyType<uint16_t>(accelerationX));
152         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, BasicPropertyType<uint16_t>(steeringWheelAngle));
153         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition, BasicPropertyType<uint16_t>(transmissionShiftPostion));
154         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, BasicPropertyType<uint16_t>(throttlePos));
155         routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, BasicPropertyType<uint16_t>(engineCoolant));
156 }