refactored setProperty: made it async with callback
[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 Transmission::TransmissionPositions transmissionShiftPostion = Transmission::Neutral;
31 uint16_t steeringWheelAngle=0;
32 uint16_t throttlePos = 0;
33 uint16_t engineCoolant = 40;
34 bool machineGun = false;
35
36 static gboolean timeoutCallback(gpointer data)
37 {
38         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
39         
40         src->randomizeProperties();
41         
42         return true;
43 }
44
45 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
46 :AbstractSource(re, config), velocity(0), engineSpeed(0)
47 {
48         re->setSupported(supported(), this);
49         debugOut("setting timeout");
50         g_timeout_add(1000, timeoutCallback, this );
51         
52 }
53
54
55
56 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
57 {
58         return new ExampleSourcePlugin(routingengine, config);
59         
60 }
61
62 string ExampleSourcePlugin::uuid()
63 {
64         return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
65 }
66
67
68 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
69 {
70         DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
71
72
73
74         if(reply->property == VehicleProperty::VehicleSpeed)
75         {
76                 VehicleProperty::VehicleSpeedType temp(velocity);
77                 reply->value = &temp;
78                 reply->completed(reply);
79         }
80         else if(reply->property == VehicleProperty::EngineSpeed)
81         {
82                 VehicleProperty::EngineSpeedType temp(engineSpeed);
83                 reply->value = &temp;
84                 reply->completed(reply);
85         }
86         else if(reply->property == VehicleProperty::AccelerationX)
87         {
88                 VehicleProperty::AccelerationType temp(accelerationX);
89                 reply->value = &temp;
90                 reply->completed(reply);
91         }
92         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
93         {
94                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
95                 reply->value = &temp;
96                 reply->completed(reply);
97         }
98         else if(reply->property == VehicleProperty::SteeringWheelAngle)
99         {
100                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
101                 reply->value = &temp;
102                 reply->completed(reply);
103         }
104         else if(reply->property == VehicleProperty::VIN)
105         {
106                 VehicleProperty::VINType temp("ABC00000000000000");
107                 reply->value = &temp;
108                 reply->completed(reply);
109         }
110         else if(reply->property == VehicleProperty::WMI)
111         {
112                 VehicleProperty::WMIType temp("abc");
113                 reply->value = &temp;
114                 reply->completed(reply);
115         }
116         else if(reply->property == VehicleProperty::BatteryVoltage)
117         {
118                 VehicleProperty::BatteryVoltageType temp(12.6);
119                 reply->value = &temp;
120                 reply->completed(reply);
121         }
122 }
123
124 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
125 {
126
127 }
128
129 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
130 {
131
132 }
133
134 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
135 {
136         mRequests.push_back(property);
137 }
138
139 PropertyList ExampleSourcePlugin::supported()
140 {
141         PropertyList props;
142         props.push_back(VehicleProperty::EngineSpeed);
143         props.push_back(VehicleProperty::VehicleSpeed);
144         props.push_back(VehicleProperty::AccelerationX);
145         props.push_back(VehicleProperty::TransmissionShiftPosition);
146         props.push_back(VehicleProperty::SteeringWheelAngle);
147         props.push_back(VehicleProperty::ThrottlePosition);
148         props.push_back(VehicleProperty::EngineCoolantTemperature);
149         props.push_back(VehicleProperty::VIN);
150         props.push_back(VehicleProperty::WMI);
151         props.push_back(VehicleProperty::BatteryVoltage);
152         props.push_back(VehicleProperty::MachineGunTurretStatus);
153         
154         return props;
155 }
156
157 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
158 {
159         mRequests.remove(property);
160 }
161
162 void ExampleSourcePlugin::randomizeProperties()
163 {
164         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
165         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
166         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
167         transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
168         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
169         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
170         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
171         
172         DebugOut()<<"setting velocity to: "<<velocity<<endl;
173         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
174         
175         VehicleProperty::VehicleSpeedType vel(velocity);
176         VehicleProperty::EngineSpeedType es(engineSpeed);
177         VehicleProperty::AccelerationType ac(accelerationX);
178         VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
179         VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
180         VehicleProperty::ThrottlePositionType tp(throttlePos);
181         VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
182         VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
183
184         machineGun = !machineGun;
185
186         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, &vel);
187         routingEngine->updateProperty(VehicleProperty::EngineSpeed, &es);
188         routingEngine->updateProperty(VehicleProperty::AccelerationX, &ac);
189         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, &swa);
190         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition,&tsp);
191         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, &tp);
192         routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, &ec);
193         //routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt);
194 }