2 Copyright (C) 2012 Intel Corporation
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.
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.
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
19 #include <vehicleproperty.h>
20 #include <listplusplus.h>
22 #include "ambpluginimpl.h"
24 //----------------------------------------------------------------------------
26 //----------------------------------------------------------------------------
28 AmbPluginImpl::AmbPluginImpl(AbstractRoutingEngine* re, const map<string, string>& config, AbstractSource& parent) :
35 void AmbPluginImpl::init()
39 void AmbPluginImpl::getPropertyAsync(AsyncPropertyReply *reply)
42 DebugOut(DebugOut::Error) << "AmbPluginImpl::getPropertyAsync - reply is null" << std::endl;
46 DebugOut() << "AmbPluginImpl::getPropertyAsync called for property: " << reply->property << endl;
48 reply->success = false;
49 reply->error = AsyncPropertyReply::InvalidOperation;
50 AbstractPropertyType *value = findPropertyType(reply->property, reply->zoneFilter);
53 reply->success = true;
54 reply->error = AsyncPropertyReply::NoError;
58 reply->completed(reply);
61 void AmbPluginImpl::getRangePropertyAsync(AsyncRangePropertyReply *reply)
64 DebugOut(DebugOut::Error) << "AmbPluginImpl::getRangePropertyAsync - reply is null" << std::endl;
68 DebugOut() << "AmbPluginImpl::getRangePropertyAsync not supported "<< std::endl;
69 reply->success = false;
70 reply->error = AsyncPropertyReply::InvalidOperation;
72 reply->completed(reply);
75 AsyncPropertyReply *AmbPluginImpl::setProperty(const AsyncSetPropertyRequest& request )
77 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
78 reply->success = false;
79 reply->error = AsyncPropertyReply::InvalidOperation;
81 AbstractPropertyType *value = findPropertyType(request.property, request.zoneFilter);
82 if (value && request.value) {
83 DebugOut(2) << "updating property "<< request.property << " to: " << request.value->toString() << endl;
84 value->fromString(request.value->toString());
85 DebugOut(2) << "New value of property "<< request.property << " is: " << value->toString() << endl;
86 value->timestamp = amb::currentTime();
87 routingEngine->updateProperty(value, uuid());
89 reply->success = true;
90 reply->error = AsyncPropertyReply::NoError;
95 reply->completed(reply);
102 void AmbPluginImpl::subscribeToPropertyChanges(const VehicleProperty::Property& property)
104 std::list<Zone::Type> zones = getPropertyInfo(property).zones();
105 for( auto it=zones.begin(); it!=zones.end(); ++it) {
106 AbstractPropertyType *value = findPropertyType(property, *it);
108 routingEngine->updateProperty(value, uuid());
112 PropertyList AmbPluginImpl::supported() const
115 for(auto itPropMap = properties.begin(); itPropMap != properties.end(); ++itPropMap)
116 props.push_back(itPropMap->first);
120 int AmbPluginImpl::supportedOperations() const
122 return AbstractSource::Get | AbstractSource::Set;
125 void AmbPluginImpl::unsubscribeToPropertyChanges(const VehicleProperty::Property& property)
129 // if signal does not exits return nullptr(we do not know its datatype), if zone does not exists, add it
130 AbstractPropertyType* AmbPluginImpl::findPropertyType(const VehicleProperty::Property& propertyName, const Zone::Type& zone)
132 auto itPropMap = properties.find(propertyName);
133 if(itPropMap == properties.end())
136 for( auto it = itPropMap->second.begin(); it != itPropMap->second.end(); ++it ) {
137 if(it->first == zone)
138 return it->second.get();
144 std::shared_ptr<AbstractPropertyType> AmbPluginImpl::addPropertySupport(Zone::Type zone, std::function<AbstractPropertyType* (void)> typeFactory)
146 std::shared_ptr<AbstractPropertyType> propertyType(typeFactory());
150 VehicleProperty::Property name(propertyType->name);
151 PropertyList registeredProperties(VehicleProperty::capabilities());
152 bool registeredType(false);
153 if(!contains(registeredProperties,name))
155 registeredType = VehicleProperty::registerProperty(name, typeFactory);
157 if(!registeredType){ // Property type wasn't registered by us. Is it predefined in AMB API or some other source plug-in has already registered it ???
158 std::shared_ptr<AbstractPropertyType> registeredPropertyType(VehicleProperty::getPropertyTypeForPropertyNameValue(name));
159 if(!registeredPropertyType)
161 propertyType.swap(registeredPropertyType);
163 propertyType->zone = zone;
164 propertyType->sourceUuid = uuid();
165 ZonePropertyType& zonePropType = properties[name];
166 zonePropType.insert( make_pair(zone, propertyType));
170 PropertyInfo AmbPluginImpl::getPropertyInfo(const VehicleProperty::Property& property) const
172 auto it = properties.find(property);
173 if(it != properties.end()) {
174 std::list<Zone::Type> zones;
175 for(auto itZonePropType = it->second.begin(); itZonePropType != it->second.end(); ++itZonePropType)
176 zones.push_back(itZonePropType->first);
178 return PropertyInfo( 0, zones );
181 return PropertyInfo::invalid();
184 void AmbPluginImpl::propertyChanged(AbstractPropertyType* value)
188 void AmbPluginImpl::supportedChanged(const PropertyList& supportedProperties)