Update the name and datatype of AMB object
[profile/ivi/automotive-message-broker.git] / xwalk / vehicle_instance.cc
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "vehicle_instance.h"
6
7 #include <abstractpropertytype.h>
8
9 #include <algorithm>
10 #include <string>
11
12 #include "vehicle.h"
13
14 VehicleInstance::VehicleInstance(): vehicle_(new Vehicle(this)) {
15         DebugOut::setDebugThreshhold(5);
16 }
17
18 void VehicleInstance::HandleMessage(const char* message) {
19         DebugOut() << "VehicleInstance message received " << message << endl;
20
21         picojson::value v;
22
23         std::string err;
24         picojson::parse(v, message, message + strlen(message), &err);
25         if (!err.empty()) {
26                 return;
27         }
28
29         std::string method = v.get("method").to_str();
30
31         Zone::Type amb_zone = 0;
32         if (v.contains("zone")) {
33                 picojson::value zone = v.get("zone");
34                 if (zone.is<picojson::object>() && zone.contains("value")) {
35                         picojson::value::array zones = zone.get("value").get<picojson::value::array>();
36                         amb_zone = ZoneToAMBZone(zones);
37                         DebugOut() << "Converted W3C zone " << picojson::value(zones).to_str() << " to AMB zone: " << amb_zone << endl;
38                 } else {
39                         int callback_id = -1;
40                         if (v.contains("asyncCallId"))
41                                 callback_id = v.get("asyncCallId").get<double>();
42                         PostError(callback_id, method, "invalid_zone");
43                         return;
44                 }
45         }
46
47         if (method == "get") {
48                 std::string attribute = v.get("name").to_str();
49                 int callback_id = v.get("asyncCallId").get<double>();
50
51                 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
52                                            ::toupper);
53
54                 vehicle_->Get(attribute, amb_zone, callback_id);
55         } else if (method == "zones") {
56                 std::string attribute = v.get("name").to_str();
57                 int callback_id = v.get("asyncCallId").get<double>();
58                 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
59                                            ::toupper);
60
61                 vehicle_->GetZones(attribute, callback_id);
62         } else if (method == "subscribe") {
63                 std::string attribute = v.get("name").to_str();
64                 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
65                                            ::toupper);
66
67                 vehicle_->Subscribe(attribute, amb_zone);
68         } else if (method == "unsubscribe") {
69                 std::string attribute = v.get("name").to_str();
70                 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
71                                            ::toupper);
72
73                 vehicle_->Unsubscribe(attribute, amb_zone);
74         } else if (method == "set") {
75                 std::string attribute = v.get("name").to_str();
76                 int callback_id = v.get("asyncCallId").get<double>();
77
78                 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
79                                            ::toupper);
80
81                 if (!v.get("value").is<picojson::object>()) {
82                         PostError(callback_id, "set", "invalid_operation");
83                 }
84                 picojson::object value = v.get("value").get<picojson::object>();
85
86                 vehicle_->Set(attribute, value, amb_zone, callback_id);
87         } else if (method == "supported") {
88                 std::string attribute = v.get("name").to_str();
89                 int callback_id = v.get("asyncCallId").get<double>();
90
91                 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
92                                            ::toupper);
93                 vehicle_->Supported(attribute, callback_id);
94         }
95 }
96
97 void VehicleInstance::HandleSyncMessage(const char* message) {
98         DebugOut() << "VehicleInstance Sync message received " << message << endl;
99         picojson::value v;
100
101         std::string err;
102         picojson::parse(v, message, message + strlen(message), &err);
103         if (!err.empty()) {
104                 return;
105         }
106
107         std::string method = v.get("method").to_str();
108         std::string objectName = v.get("name").to_str();
109         std::string attName = v.get("attName").to_str();
110
111         std::transform(objectName.begin(), objectName.begin() + 1, objectName.begin(),
112                                    ::toupper);
113
114         std::transform(attName.begin(), attName.begin() + 1, attName.begin(),
115                                    ::toupper);
116
117         if(method == "availableForRetrieval")
118         {
119                 std::string reply = vehicle_->AvailableForRetrieval(objectName, attName) ? "true" : "false";
120                 DebugOut() << "VehicleInstance reply: " << reply << endl;
121                 SendSyncReply(reply.c_str());
122         }
123
124 }
125
126 int VehicleInstance::ZoneToAMBZone(picojson::array zones) {
127         Zone::Type amb_zone = 0;
128
129         for (auto zone : zones) {
130                 std::string tempzone = zone.to_str();
131
132                 std::transform(tempzone.begin(), tempzone.end(), tempzone.begin(),
133                                                            ::tolower);
134
135                 if (tempzone == "front") {
136                         amb_zone |= Zone::Front;
137                 } else if (tempzone == "middle") {
138                         amb_zone |= Zone::Middle;
139                 } else if (tempzone == "right") {
140                         amb_zone |= Zone::Right;
141                 } else if (tempzone == "left") {
142                         amb_zone |= Zone::Left;
143                 } else if (tempzone == "rear") {
144                         amb_zone |= Zone::Rear;
145                 } else if (tempzone == "center") {
146                         amb_zone |= Zone::Center;
147                 }
148         }
149
150         return amb_zone;
151 }
152
153 void VehicleInstance::PostError(double callback_id, const std::string& method,
154                                                                 const std::string& error) {
155         picojson::object msg;
156         msg["method"] = picojson::value(method);
157         msg["error"] = picojson::value(true);
158         msg["value"] = picojson::value(error);
159         if (callback_id != -1) {
160                 msg["asyncCallId"] =
161                                 picojson::value(static_cast<double>(callback_id));
162         }
163
164         std::string message = picojson::value(msg).serialize();
165
166         DebugOut() << "Error Reply message: " << message << endl;
167
168         PostMessage(message.c_str());
169 }