Add timestamp val to returned json object on event
authorRomain Forlot <romain.forlot@iot.bzh>
Wed, 13 Sep 2017 09:14:01 +0000 (11:14 +0200)
committerRomain Forlot <romain.forlot@iot.bzh>
Wed, 13 Sep 2017 09:15:48 +0000 (11:15 +0200)
Change-Id: Ibfb514eb27c0378dba7e302755e5f6f95b0ca242
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/can/can-bus.cpp
low-can-binding/utils/openxc-utils.cpp
low-can-binding/utils/openxc-utils.hpp

index e9f9fc7..be40aab 100644 (file)
@@ -165,7 +165,6 @@ void can_bus_t::can_decode_message()
 /// which are events that has to be pushed.
 void can_bus_t::can_event_push()
 {
-       openxc_SimpleMessage s_message;
        json_object* jo;
        utils::signals_manager_t& sm = utils::signals_manager_t::instance();
 
@@ -180,11 +179,10 @@ void can_bus_t::can_event_push()
                        {
                                std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
                                std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
-                               s_message = get_simple_message(v_message.second);
                                if(s.find(v_message.first) != s.end() && afb_event_is_valid(s[v_message.first]->get_event()))
                                {
                                        jo = json_object_new_object();
-                                       jsonify_simple(s_message, jo);
+                                       jsonify_vehicle(v_message.second, jo);
                                        if(afb_event_push(s[v_message.first]->get_event(), jo) == 0)
                                        {
                                                if(v_message.second.has_diagnostic_response)
index 3b404f0..35b7d9c 100644 (file)
@@ -349,3 +349,28 @@ bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
        json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
        return false;
 }
+
+///
+/// @brief Make a JSON object from a VehicleMessage
+///
+/// @param[in] v_msg - const reference to an openxc_VehicleMessage
+/// struct to convert into a json object.
+/// @param[out] json - pointer with the DynamicField converted into json object
+///
+/// @return True if VehicleMessage has been transformed into json object
+///  and false if not. In such case, a json object is returned { "error": "error msg"}
+///
+bool jsonify_vehicle(const openxc_VehicleMessage& v_msg, json_object* json)
+{
+       if(jsonify_simple(get_simple_message(v_msg), json))
+       {
+               if(v_msg.has_timestamp)
+               {
+                       json_object_object_add(json, "timestamp", json_object_new_double(v_msg.timestamp));
+                       return true;
+               }
+               return true;
+       }
+       json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
+       return false;
+}
index 83d6379..5766304 100644 (file)
@@ -44,4 +44,6 @@ const openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg
 
 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value);
 
-bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json);
\ No newline at end of file
+bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json);
+
+bool jsonify_vehicle(const openxc_VehicleMessage& v_msg, json_object* json);