listener_ptr makeStreamListener(StreamingDevice* dev, ClientConn* client, JSONNode &n){ std::auto_ptr<WSStreamListener> listener(new WSStreamListener()); listener->id = jsonIntProp(n, "id"); listener->device = dev; listener->client = client; listener->decimateFactor = jsonIntProp(n, "decimateFactor", 1); // Prevent divide by 0 if (listener->decimateFactor == 0) listener->decimateFactor = 1; int start = jsonIntProp(n, "start", -1); if (start < 0){ // Negative indexes are relative to latest sample start = (dev->buffer_max()) + start + 1; } if (start < 0) listener->index = 0; else listener->index = start; listener->count = jsonIntProp(n, "count"); JSONNode j_streams = n.at("streams"); for(JSONNode::iterator i=j_streams.begin(); i!=j_streams.end(); i++){ listener->streams.push_back( dev->findStream( jsonStringProp(*i, "channel"), jsonStringProp(*i, "stream"))); } JSONNode::iterator t = n.find("trigger"); if (t != n.end() && (t->type()) == JSON_NODE){ JSONNode &trigger = *t; string type = jsonStringProp(trigger, "type", "in"); if (type == "in"){ listener->triggerType = INSTREAM; listener->triggerLevel = jsonFloatProp(trigger, "level"); listener->triggerStream = dev->findStream( jsonStringProp(trigger, "channel"), jsonStringProp(trigger, "stream")); }else if (type == "out"){ listener->triggerType = OUTSOURCE; listener->triggerChannel = dev->channelById(jsonStringProp(trigger, "channel")); if (!listener->triggerChannel) throw ErrorStringException("Trigger channel not found"); }else{ throw ErrorStringException("Invalid trigger type"); } listener->triggerRepeat = jsonBoolProp(trigger, "repeat", true); listener->triggerHoldoff = jsonIntProp(trigger, "holdoff", 0); listener->triggerOffset = jsonIntProp(trigger, "offset", 0); if (listener->triggerOffset<0 && -listener->triggerOffset >= listener->triggerHoldoff){ // Prevent big negative offsets that could cause infinite loops listener->triggerHoldoff = -listener->triggerOffset; } listener->triggerForce = jsonIntProp(trigger, "force", 0); listener->triggerForceIndex = listener->index + listener->triggerForce; } return listener_ptr(listener.release()); }
inline bool jsonBoolProp(JSONNode &n, const char* prop, bool def){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_BOOL) return i->as_bool(); else return def; }
inline double jsonFloatProp(JSONNode &n, const char* prop, double def){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_NUMBER) return i->as_float(); else return def; }
inline int jsonIntProp(JSONNode &n, const char* prop, int def){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_NUMBER) return i->as_int(); else return def; }
inline string jsonStringProp(JSONNode &n, const char* prop, string def){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_STRING) return i->as_string(); else return def; }
inline double jsonFloatProp(JSONNode &n, const char* prop){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_NUMBER) return i->as_float(); else throw ErrorStringException(string("JSON missing float property: ") + prop); }
inline bool jsonBoolProp(JSONNode &n, const char* prop){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_BOOL) return i->as_bool(); else throw ErrorStringException(string("JSON missing bool property: ") + prop); }
inline string jsonStringProp(JSONNode &n, const char* prop){ JSONNode::iterator i = n.find(prop); if (i != n.end() && i->type() == JSON_STRING) return i->as_string(); else throw ErrorStringException(string("JSON missing string property: ") + prop); }