19 #ifndef AMB_JSON_PROTOCOL_H_
20 #define AMB_JSON_PROTOCOL_H_
25 #include <unordered_map>
28 #include <abstractroutingengine.h>
29 #include <mappropertytype.hpp>
31 #include <uuidhelper.h>
32 #include <vehicleproperty.h>
34 #include "abstractio.hpp"
44 typedef std::shared_ptr<T> Ptr;
46 static std::shared_ptr<T> create()
52 class Object :
public std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>,
public PtrMaker<Object>
55 Object(): std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>() { }
56 Object(
const std::string & ifaceName): std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>(),
57 interfaceName(ifaceName)
62 static Object::Ptr fromJson(
const picojson::object & obj);
66 std::string interfaceName;
80 messageId = amb::createUuid();
92 std::string messageId;
105 return json.is<picojson::object>() && json.contains(
"type") && json.contains(
"name") && json.contains(
"messageId");
108 template <
typename T>
139 sourceUuid = other.sourceUuid;
145 return (msg->type ==
"method");
156 return json.contains(
"source") && json.get(
"source").is<std::string>()
157 && json.contains(
"zone") && json.get(
"zone").is<
double>();
163 std::string sourceUuid;
173 MethodReply(std::shared_ptr<T> t,
bool success): mMethod(t), methodSuccess(success), err(AsyncPropertyReply::NoError) { }
180 picojson::object obj = v.get<picojson::object>();
189 if(!mMethod) mMethod = std::shared_ptr<T>(
new T());
190 mMethod->fromJson(json);
191 methodSuccess = json.get(
"methodSuccess").get<
bool>();
199 return v.contains(
"methodSuccess") && v.contains(
"error") && v.get(
"methodSuccess").is<
bool>()
200 && v.get(
"error").is<
double>() && T::is(v);
210 const std::shared_ptr<T> method() {
return mMethod; }
213 std::shared_ptr<T> mMethod;
217 typedef std::function<void (std::vector<Object::Ptr>)> ListCallback;
227 throw std::runtime_error(
"type not list");
233 std::vector<Object::Ptr> objectNames;
235 ListCallback replyCallback;
239 return msg->name ==
"list";
244 return json.get(
"name").to_str() ==
"list";
248 typedef std::function<void (Object::Ptr)> ObjectCallback;
264 return msg->name ==
"get";
269 return json.get(
"name").to_str() ==
"get";
274 ObjectCallback replyCallback;
277 typedef std::function<void (bool)> SetCallback;
293 return msg->name ==
"set";
298 return json.get(
"name").to_str() ==
"set";
302 SetCallback replyCallback;
314 :
MethodCall(
"subscribe"), interfaceName(ifaceName)
324 return msg->name ==
"subscribe";
329 return json.get(
"name").to_str() ==
"subscribe";
332 std::string interfaceName;
347 sourceUuid = call.sourceUuid;
352 :
MethodCall(
"unsubscribe"), interfaceName(ifaceName)
359 return msg->name ==
"unsubscribe";
364 return json.get(
"name").to_str() ==
"unsubscribe";
370 std::string interfaceName;
381 return msg.type ==
"event";
386 return json.get(
"type").to_str() ==
"event";
406 return msg.type ==
"message" && msg.name ==
"timeSync";
411 return json.contains(
"serverTime") && json.get(
"name").to_str() ==
"timeSync" && json.get(
"serverTime").is<
double>();
426 return msg.type ==
"event" && msg.name ==
"propertyChanged";
431 return EventMessage::is(json) && json.get(
"name").to_str() ==
"propertyChanged" && json.contains(
"data") && json.get(
"data").is<picojson::object>();
435 std::string sourceUuid;
447 std::function<void (void)> disconnected;
456 std::string buff = msg.toJson().serialize()+
"\n";
457 DebugOut() <<
"writing: " << buff << endl;
462 void send(std::shared_ptr<T> msg)
464 std::string buff = msg->toJson().serialize()+
"\n";
465 DebugOut() <<
"writing: " << buff << endl;
469 std::shared_ptr<AbstractIo> mIo;
475 std::string incompleteMessage;
487 :call(subscribeCall), callback(cb) {}
490 return rhs.subscriptionId() == subscriptionId();
493 const std::string subscriptionId()
const {
return call.messageId; }
495 ObjectCallback callback;
500 void list(ListCallback cb);
502 void get(
const std::string & objectName, ObjectCallback cb);
504 void get(
const std::string & objectName,
const std::string & sourceUuid, ObjectCallback cb);
506 void get(
const std::string & objectName, Zone::Type
zone, ObjectCallback cb);
508 void get(
const std::string & objectName,
const std::string & sourceUuid, Zone::Type
zone, ObjectCallback cb);
510 void set(
const std::string & objectName, Object::Ptr
value, SetCallback cb);
512 void set(
const std::string & objectName, Object::Ptr
value,
const std::string & sourceUuid, Zone::Type
zone, SetCallback cb);
514 const std::string subscribe(
const std::string & objectName,
const std::string & sourceUuid, Zone::Type
zone, ObjectCallback cb);
516 void subscribe(
const std::string & objectName, ObjectCallback cb);
518 void unsubscribe(
const std::string & subscribeId);
522 double correctTimeFromServer(
double serverTimestamp);
528 std::string createSubscriptionId(
const std::string & objectName,
const std::string & sourceUuid, Zone::Type
zone);
529 std::vector<ListMethodCall::Ptr> mListCalls;
530 std::vector<GetMethodCall::Ptr> mGetMethodCalls;
531 std::vector<SetMethodCall::Ptr> mSetMethodCalls;
532 std::unordered_map<std::string, std::vector<Subscription>> mSubscriptions;
534 double serverTimeOffset;
547 virtual void list(ListMethodCall::Ptr call);
552 virtual void get(GetMethodCall::Ptr
get);
557 virtual void set(SetMethodCall::Ptr
set);
561 virtual void subscribe(SubscribeMethodCall::Ptr call);
563 virtual void unsubscribe(UnsubscribeMethodCall::Ptr call);