added trip meter property
[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         if(reply->property == VehicleProperty::VehicleSpeed)
72         {
73                 VehicleProperty::VehicleSpeedType temp(velocity);
74                 reply->value = &temp;
75                 reply->completed(reply);
76         }
77         else if(reply->property == VehicleProperty::EngineSpeed)
78         {
79                 VehicleProperty::EngineSpeedType temp(engineSpeed);
80                 reply->value = &temp;
81                 reply->completed(reply);
82         }
83         else if(reply->property == VehicleProperty::AccelerationX)
84         {
85                 VehicleProperty::AccelerationType temp(accelerationX);
86                 reply->value = &temp;
87                 reply->completed(reply);
88         }
89         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
90         {
91                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
92                 reply->value = &temp;
93                 reply->completed(reply);
94         }
95         else if(reply->property == VehicleProperty::SteeringWheelAngle)
96         {
97                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
98                 reply->value = &temp;
99                 reply->completed(reply);
100         }
101         else if(reply->property == VehicleProperty::VIN)
102         {
103                 VehicleProperty::VINType temp("ABC00000000000000");
104                 reply->value = &temp;
105                 reply->completed(reply);
106         }
107         else if(reply->property == VehicleProperty::WMI)
108         {
109                 VehicleProperty::WMIType temp("abc");
110                 reply->value = &temp;
111                 reply->completed(reply);
112         }
113         else if(reply->property == VehicleProperty::BatteryVoltage)
114         {
115                 VehicleProperty::BatteryVoltageType temp(12.6);
116                 reply->value = &temp;
117                 reply->completed(reply);
118         }
119 }
120
121 void ExampleSourcePlugin::setProperty(VehicleProperty::Property , AbstractPropertyType *)
122 {
123
124 }
125
126 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
127 {
128         mRequests.push_back(property);
129 }
130
131 PropertyList ExampleSourcePlugin::supported()
132 {
133         PropertyList props;
134         props.push_back(VehicleProperty::EngineSpeed);
135         props.push_back(VehicleProperty::VehicleSpeed);
136         props.push_back(VehicleProperty::AccelerationX);
137         props.push_back(VehicleProperty::TransmissionShiftPosition);
138         props.push_back(VehicleProperty::SteeringWheelAngle);
139         props.push_back(VehicleProperty::ThrottlePosition);
140         props.push_back(VehicleProperty::EngineCoolantTemperature);
141         props.push_back(VehicleProperty::VIN);
142         props.push_back(VehicleProperty::WMI);
143         props.push_back(VehicleProperty::BatteryVoltage);
144         props.push_back(VehicleProperty::MachineGunTurretStatus);
145         
146         return props;
147 }
148
149 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
150 {
151         mRequests.remove(property);
152 }
153
154 void ExampleSourcePlugin::randomizeProperties()
155 {
156         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
157         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
158         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
159         transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
160         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
161         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
162         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
163         
164         DebugOut()<<"setting velocity to: "<<velocity<<endl;
165         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
166         
167         VehicleProperty::VehicleSpeedType vel(velocity);
168         VehicleProperty::EngineSpeedType es(engineSpeed);
169         VehicleProperty::AccelerationType ac(accelerationX);
170         VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
171         VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
172         VehicleProperty::ThrottlePositionType tp(throttlePos);
173         VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
174         VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
175
176         machineGun = !machineGun;
177
178         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, &vel);
179         routingEngine->updateProperty(VehicleProperty::EngineSpeed, &es);
180         routingEngine->updateProperty(VehicleProperty::AccelerationX, &ac);
181         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, &swa);
182         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition,&tsp);
183         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, &tp);
184         routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, &ec);
185         routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt);
186 }