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.
5 #include "vehicle_instance.h"
7 #include <abstractpropertytype.h>
14 VehicleInstance::VehicleInstance(): vehicle_(new Vehicle(this)) {
15 DebugOut::setDebugThreshhold(5);
18 void VehicleInstance::HandleMessage(const char* message) {
19 DebugOut() << "VehicleInstance message received " << message << endl;
24 picojson::parse(v, message, message + strlen(message), &err);
29 std::string method = v.get("method").to_str();
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;
40 if (v.contains("asyncCallId"))
41 callback_id = v.get("asyncCallId").get<double>();
42 PostError(callback_id, method, "invalid_zone");
47 if (method == "get") {
48 std::string attribute = v.get("name").to_str();
49 int callback_id = v.get("asyncCallId").get<double>();
51 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
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(),
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(),
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(),
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>();
78 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
81 if (!v.get("value").is<picojson::object>()) {
82 PostError(callback_id, "set", "invalid_operation");
84 picojson::object value = v.get("value").get<picojson::object>();
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>();
91 std::transform(attribute.begin(), attribute.begin() + 1, attribute.begin(),
93 vehicle_->Supported(attribute, callback_id);
97 void VehicleInstance::HandleSyncMessage(const char* message) {
98 DebugOut() << "VehicleInstance Sync message received " << message << endl;
102 picojson::parse(v, message, message + strlen(message), &err);
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();
111 std::transform(objectName.begin(), objectName.begin() + 1, objectName.begin(),
114 std::transform(attName.begin(), attName.begin() + 1, attName.begin(),
117 if(method == "availableForRetrieval")
119 std::string reply = vehicle_->AvailableForRetrieval(objectName, attName) ? "true" : "false";
120 DebugOut() << "VehicleInstance reply: " << reply << endl;
121 SendSyncReply(reply.c_str());
126 int VehicleInstance::ZoneToAMBZone(picojson::array zones) {
127 Zone::Type amb_zone = 0;
129 for (auto zone : zones) {
130 std::string tempzone = zone.to_str();
132 std::transform(tempzone.begin(), tempzone.end(), tempzone.begin(),
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;
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) {
161 picojson::value(static_cast<double>(callback_id));
164 std::string message = picojson::value(msg).serialize();
166 DebugOut() << "Error Reply message: " << message << endl;
168 PostMessage(message.c_str());