{ "subscribe", JSVehicle::subscribe, kJSPropertyAttributeNone },
{ "supported", JSVehicle::supported, kJSPropertyAttributeNone },
{ "set", JSVehicle::set, kJSPropertyAttributeNone },
+ { "getHistory", JSVehicle::getHistory, kJSPropertyAttributeNone },
{ 0, 0, 0 }
};
std::string objectName = validator.toString(0);
+ LoggerD("objectName to getHistory on = "<<objectName);
+ time_t begin = validator.toTimeT(1, false);
+
+ LoggerD("begin time: "<<(double)begin);
+
+ time_t end = validator.toTimeT(2, false);
+
+ LoggerD("end time: "<<(double)end);
+
+ JSObjectRef successCallback = validator.toFunction(3, false);
+ JSObjectRef errorCallback = validator.toFunction(4, true);
+
+ JSValueProtect(context, successCallback);
+ JSValueProtect(context, errorCallback);
+
+ vehicle->getHistory(objectName, (double)begin, (double)end, successCallback, errorCallback, context);
+
+ return JSValueMakeUndefined(context);
}
}
#include <CommonsJavaScript/Converter.h>
#include <json-glib/json-gvariant.h>
+#include <boost/uuid/uuid.hpp> // uuid class
+#include <boost/uuid/uuid_generators.hpp> // generators
+#include <boost/lexical_cast.hpp>
+#include <boost/uuid/uuid_io.hpp>
+
namespace DeviceAPI
{
namespace Vehicle
using namespace WrtDeviceApis::Commons;
using namespace WrtDeviceApis::CommonsJavaScript;
+std::map<std::string, VehicleSubscribeCB> VehicleMaster::historyMap;
+
+class DataClass
+{
+public:
+ std::string uuid;
+};
+
GVariant* jsValueToGVariant(JSContextRef ctx, JSValueRef jsVal, std::string signature)
{
LoggerD("Entered");
return nullptr;
}
+std::string mapToJSon(std::map<std::string, GVariant*> propertyMap)
+{
+ std::stringstream json;
+
+ json<<"{";
+
+ for(auto itr = propertyMap.begin(); itr != propertyMap.end(); itr++)
+ {
+ if(json.str() != "{") json<<",";
+
+ std::string key = (*itr).first;
+
+ /// make lowerCamelCase:
+ std::transform(key.begin(), key.begin()+1, key.begin(), ::tolower);
+
+ GVariant* var = (*itr).second;
+
+ gsize size;
+
+ json<<"\""<<key<<"\" : "<<json_gvariant_serialize_data(var,&size);
+
+ //g_variant_unref(var);
+
+ }
+
+ json << "}";
+
+ return json.str();
+}
+
VehicleMaster::VehicleMaster()
:mAutomotiveManager(nullptr)
{
if(error)
{
- LoggerD("failed to call GetAll on interface " << interfaceName);
+ LoggerD("failed to call GetAll on interface " << interfaceName<< " "<<error->message);
///TODO: call error callback
return;
}
{
LoggerD("Entered");
- std::stringstream json;
-
- json<<"{";
-
- for(auto itr = propertyMap.begin(); itr != propertyMap.end(); itr++)
- {
- if(json.str() != "{") json<<",";
+ std::string json = mapToJSon(propertyMap);
- std::string key = (*itr).first;
-
- /// make lowerCamelCase:
- std::transform(key.begin(), key.begin()+1, key.begin(), ::tolower);
-
- GVariant* var = (*itr).second;
-
- gsize size;
-
- json<<"\""<<key<<"\" : "<<json_gvariant_serialize_data(var,&size);
-
- //g_variant_unref(var);
-
- }
-
- json << "}";
-
-
- LoggerD("get json: " << json.str().c_str());
-
- JSStringRef jsonString = JSStringCreateWithUTF8CString(json.str().c_str());
+ JSStringRef jsonString = JSStringCreateWithUTF8CString(json.c_str());
std::list<VehicleSubscribeCB> cbs = objectToFunctionListMap[objectName];
return None;
}
+void VehicleMaster::getHistory(std::string objectName, double begin, double end, JSObjectRef successCallback, JSObjectRef errorCallback, JSContextRef context)
+{
+ LoggerD("Entered");
+
+ std::string objectPath = findProperty(objectName);
+
+ GError* error = nullptr;
+
+ std::string interfaceName = "org.automotive." + objectName;
+
+ GDBusProxy* proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ "org.automotive.message.broker",
+ objectPath.c_str(),
+ interfaceName.c_str(),
+ NULL,
+ &error);
+
+ //auto cb = [](GObject* source, GAsyncResult *res, gpointer user_data)
+ {
+ LoggerD("GetHistory DBus call completed");
+
+ GError *error = nullptr;
+ GVariant *result;
+
+ //result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error);
+
+ result = g_dbus_proxy_call_sync(proxy, "GetHistory", g_variant_new("(dd)", begin, end), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+
+ //DataClass* uuid = static_cast<DataClass*>(user_data);
+
+ VehicleSubscribeCB histCb; // = VehicleMaster::historyMap[uuid->uuid];
+
+ histCb.callback = successCallback;
+ histCb.errorCallback = errorCallback;
+ histCb.context = context;
+
+ //delete uuid;
+
+ if(error)
+ {
+ LoggerD("error getting history: "<<error->message);
+ LoggerD("pre-error-callback");
+ const JSValueRef arguments[1] = { JSValueMakeNumber(histCb.context, 10) };
+ JSObjectCallAsFunction(histCb.context, histCb.errorCallback, NULL, 1, arguments, NULL);
+ LoggerD("post error callback");
+ return;
+ }
+
+ LoggerD("TRACE");
+
+ GVariantIter* iter;
+ char* key;
+ GVariant *value;
+ double time;
+
+ LoggerD("variant sig type: "<<g_variant_get_type_string(result));
+
+ g_variant_get(result,"(a(svd))",&iter);
+
+ gsize s = g_variant_iter_n_children(iter);
+
+ LoggerD("Results: "<<s);
+
+ std::list<std::string > superList;
+ std::map<std::string, GVariant*> obj;
+
+ while(g_variant_iter_next(iter,"(svd)", &key, &value, &time))
+ {
+ if(obj.find(key) != obj.end())
+ {
+ superList.push_back(mapToJSon(obj));
+ obj.clear();
+ }
+
+ obj[key] = value;
+ obj["time"] = g_variant_new_double(time);
+ }
+
+ JSValueRef values[superList.size()];
+
+ int i=0;
+
+ LoggerD("populating array");
+
+ for(auto itr = superList.begin(); itr != superList.end(); itr++)
+ {
+ std::string jsonObj = *itr;
+ JSStringRef jsonString = JSStringCreateWithUTF8CString(jsonObj.c_str());
+
+ values[i] = JSValueMakeFromJSONString(histCb.context, jsonString);
+ i++;
+
+ JSStringRelease(jsonString);
+ }
+
+ LoggerD("making array");
+
+ JSObjectRef jsresult = JSObjectMakeArray(histCb.context, superList.size(), values, NULL);
+
+ const JSValueRef arguments[1] = { jsresult };
+
+ LoggerD("calling callback");
+ JSObjectCallAsFunction(histCb.context, histCb.callback, NULL, 1, arguments, NULL);
+ LoggerD("finished calling callback");
+
+ JSValueUnprotect(histCb.context, histCb.callback);
+ JSValueUnprotect(histCb.context, histCb.errorCallback);
+ };
+
+ /*VehicleSubscribeCB cbObj;
+
+ cbObj.callback = successCallback;
+ cbObj.errorCallback = errorCallback;
+ cbObj.context = context;
+
+ boost::uuids::uuid uuid = boost::uuids::random_generator()();
+
+ DataClass* data = new DataClass();
+ data->uuid = boost::lexical_cast<std::string>(uuid);
+
+ VehicleMaster::historyMap[data->uuid] = cbObj;
+
+
+ //g_dbus_proxy_call(proxy, "GetHistory", g_variant_new("(dd)", begin, end), G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback)cb, data);*/
+}
+
}
}
\ No newline at end of file