removed many deprecated interfaces. made variant type an abstractproperty
[profile/ivi/automotive-message-broker.git] / plugins / dbus / varianttype.cpp
1 #include "varianttype.h"
2 #include "abstractroutingengine.h"
3 #include "abstractdbusinterface.h"
4 #include "debugout.h"
5 #include "listplusplus.h"
6
7 VariantType::VariantType(AbstractRoutingEngine* re, VehicleProperty::Property ambPropertyName, std::string propertyName,  Access access)
8         :AbstractPropertyType(ambPropertyName), mInitialized(false), mAccess(access), mPropertyName(propertyName),
9           routingEngine(re)
10 {
11         name = ambPropertyName;
12         //set default value:
13         setValue(VehicleProperty::getPropertyTypeForPropertyNameValue(name));
14 }
15
16 void VariantType::initialize()
17 {
18         if(mInitialized) return;
19         AsyncPropertyRequest request;
20         request.property = name;
21         request.sourceUuidFilter = sourceUuid;
22         request.zoneFilter = zone;
23
24         using namespace std::placeholders;
25         request.completed = [this](AsyncPropertyReply* r)
26         {
27                 auto reply = amb::make_unique(r);
28                 if(reply->success)
29                         setValue(reply->value->copy());
30                 else
31                         DebugOut(DebugOut::Error)<<"get request unsuccessful for "<<reply->property<<" : "<<reply->error<<endl;
32
33                 mInitialized = true;
34         };
35
36         /// do not request if not supported:
37         PropertyList proplist = routingEngine->supported();
38
39         if(contains(proplist, name))
40                 routingEngine->getPropertyAsync(request);
41 }
42
43 GVariant *VariantType::toVariant()
44 {
45         if(!value())
46         {
47                 setValue(VehicleProperty::getPropertyTypeForPropertyNameValue(name));
48         }
49
50         auto v = value();
51
52         return v->toVariant();
53 }
54
55 void VariantType::fromVariant(GVariant *val)
56 {
57         AbstractPropertyType *v = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
58         v->fromVariant(val);
59
60         AsyncSetPropertyRequest request;
61         request.property = name;
62         request.value = v;
63         request.zoneFilter = zone;
64         request.completed = [&](AsyncPropertyReply* r)
65         {
66                 auto reply = amb::make_unique(r);
67                 /// TODO: throw dbus exception
68                 if(!reply->success)
69                 {
70                         DebugOut(DebugOut::Error)<<"SetProperty fail: "<<reply->error<<endl;
71                 }
72         };
73
74         routingEngine->setProperty(request);
75 }
76