automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
dbusexport.h
1 #include <string>
2 #include <unordered_map>
3 #include <unordered_set>
4 #include <listplusplus.h>
5 #include <abstractsink.h>
6 #include <abstractroutingengine.h>
7 
8 #include "varianttype.h"
9 
10 #define DBusConnected "DBusConnected"
11 
12 namespace amb
13 {
14 
15 class Exporter
16 {
17 public:
18 
19  std::shared_ptr<GDBusConnection> connection;
20 
21  std::unordered_map<std::string, std::unordered_set<Zone::Type>> getUniqueSourcesList(AbstractRoutingEngine *re, PropertyList implementedProperties)
22  {
23  std::unordered_map<std::string, std::unordered_set<Zone::Type>> uniqueSourcesList;
24 
25  for(auto property : implementedProperties)
26  {
27  std::vector<std::string> sources = re->sourcesForProperty(property);
28 
29  for(auto source : sources)
30  {
31  PropertyInfo info = re->getPropertyInfo(property, source);
32 
33  std::unordered_set<Zone::Type> uniqueZoneList;
34 
35  if(uniqueSourcesList.count(source))
36  {
37  uniqueZoneList = uniqueSourcesList[source];
38  }
39 
40  Zone::ZoneList zoneList = info.zones();
41 
42  if(!zoneList.size())
43  {
44  uniqueZoneList.emplace(Zone::None);
45  }
46 
47  for(auto zoneItr : zoneList)
48  {
49  uniqueZoneList.emplace(zoneItr);
50  }
51 
52  uniqueSourcesList[source] = uniqueZoneList;
53  }
54  }
55 
56  return uniqueSourcesList;
57  }
58 
59  template <typename T>
60  void exportProperty(std::string interfaceName, std::unordered_map<std::string, std::string> properties, AbstractRoutingEngine *re)
61  {
62  T* t = new T(interfaceName, re, connection.get());
63 
64  std::string prop = t->objectName();
65 
66  for(auto props : properties)
67  {
68  t->wantPropertyVariant(props.first, props.second, VariantType::ReadWrite);
69  }
70 
71  PropertyList implementedProperties = t->wantsProperties();
72 
73  std::unordered_map<std::string, std::unordered_set<Zone::Type> > uniqueSourcesList = getUniqueSourcesList(re, implementedProperties);
74 
75  delete t;
76 
77  PropertyList supported = re->supported();
78 
79  for(auto itr : uniqueSourcesList)
80  {
81  std::unordered_set<Zone::Type> zones = itr.second;
82 
83  std::string source = itr.first;
84 
85  std::string objectPath = "/" + source;
86 
87  boost::algorithm::erase_all(objectPath, "-");
88 
89  for(auto zone : zones)
90  {
91  T* t = new T(prop, re, connection.get());
92 
93  for(auto props : properties)
94  {
95  t->wantPropertyVariant(props.first, props.second, VariantType::ReadWrite);
96  }
97 
98  std::stringstream fullobjectPath;
99  fullobjectPath<< objectPath << "/" << zone << "/" <<t->objectName();
100  t->setObjectPath(fullobjectPath.str());
101  t->setSourceFilter(source);
102  t->setZoneFilter(zone);
103  t->supportedChanged(supported);
104  }
105 
106  }
107  }
108 
109  template <typename T>
110  void exportProperty(AbstractRoutingEngine *re)
111  {
112  exportProperty<T>("", std::unordered_map<std::string, std::string>(), re);
113  }
114 
115  template <typename T>
116  void exportProperty(VehicleProperty::Property prop, AbstractRoutingEngine *re)
117  {
118  exportProperty<T>(prop, std::unordered_map<std::string, std::string>(), re);
119  }
120 
121  static Exporter * instance()
122  {
123  if(mInstance)
124  return mInstance;
125  return mInstance = new Exporter();
126  }
127 
128 protected:
129  Exporter();
130 
131 private:
132  static Exporter* mInstance;
133 
134 };
135 
136 }