Exemple #1
0
    static void extract(picojson::value* super,
			picojson::value& v,
			std::vector<action>& action_storage)
    {
      if (v.is<picojson::object>())
      {
      	if(v.contains("action") && super != nullptr)
      	{
      	  action a;
      	  a.link = &v;
      	  std::string type = v.get("action").get<std::string>();
      	  if(type == "loop")
      	  {
      	    a.t = loop;
      	    a.begin = v.get("begin").get<double>();
      	    a.step = v.get("step").get<double>();
      	    a.end = v.get("end").get<double>();
            action_storage.push_back(a);
      	  }
      	  else if(type == "each")
      	  {
      	    a.t = each;
      	    for(const auto& d : v.get("values").get<picojson::array>())
      	      a.values.push_back(d);
            action_storage.push_back(a);
      	  }
      	  else if(type == "pipe")
      	  {
      	    a.t = pipe;
      	    a.data_id = v.get("data_id").get<std::string>();

      	    a.sink = v.get("sink").get<std::string>();
            if(v.contains("log_id"))
            {
              a.log_id = v.get("log_id").get<std::string>();
              action_storage.push_back(a);
            }
      	  }
      	}
      	else
      	{
      	  for(picojson::object::value_type& i : v.get<picojson::object>())
      	  {
      	    action_extractor::extract(&v, i.second, action_storage);
      	  }
      	}
      }
      else if(v.is<picojson::array>())
      {
      	for(picojson::value& i : v.get<picojson::array>())
      	{
      	  action_extractor::extract(&v, i, action_storage);
      	}
      }
    }
bool JsonActionResolver::getObject(picojson::value v, std::string key, picojson::value &value) {
	if (!v.contains(key)) {
		std::cout << "Object does not contain any " << key << std::endl;
		return false;
	}
	picojson::value result = v.get(key);
	if (!result.is<picojson::object>()) {
		std::cout << key << " is not an object" << std::endl;
		return false;
	}
	value = result;
	return true;
}
bool JsonActionResolver::getInt(picojson::value v, std::string key, int &value) {
  if (!v.contains(key)) {
    std::cout << "Object does not contain any " << key << std::endl;
    return false;
  }

  picojson::value result = v.get(key);
  if (!result.is<double>()) {
    std::cout << key << " is not an int" << std::endl;
    return false;
  }
  value = result.get<double>();
  return true;
}
Exemple #4
0
 std::string PicoJsonIF::getString(const picojson::value& value, std::string name) {
     picojson::value objString = value.get(name);
     if (objString.is<std::string>()) {
         return (objString.to_str());
     } else
         throw JsonFormatException("PicoJsonIF::getString() found illegal format : property name = " + name);
 }
Exemple #5
0
 picojson::array PicoJsonIF::getArray(const picojson::value& value, std::string name) {
     picojson::value obj = value.get(name);
     if (obj.is<picojson::array>()) {
         picojson::array arrayObj = obj.get<picojson::array>();
         return arrayObj;
     } else {
         throw JsonFormatException("PicoJsonIF::getArray() found illegal format : property name = " + name);
     }
 }
Exemple #6
0
    picojson::value PicoJsonIF::getObject(const picojson::value& value, std::string name) {

        picojson::value object = value.get(name);
        if (object.is<picojson::object>()) {
            return object;
        } else {
            throw JsonFormatException("PicoJsonIF::getObject() found illegal format : property name = " + name);
        }
    }
std::shared_ptr<AbstractPropertyType> amb::jsonToProperty(const picojson::value &json)
{
	std::string name = json.get("name").to_str();
	std::string type = json.get("type").to_str();

	auto t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);

	if(!t)
	{
		bool ret = VehicleProperty::registerProperty(name, [name, type]() -> AbstractPropertyType* {
			if(type == amb::BasicTypes::UInt16Str)
			{
				return new BasicPropertyType<uint16_t>(name, 0);
			}
			else if(type == amb::BasicTypes::Int16Str)
			{
				return new BasicPropertyType<int16_t>(name, 0);
			}
			else if(type == amb::BasicTypes::UInt32Str)
			{
				return new BasicPropertyType<uint32_t>(name, 0);
			}
			else if(type == amb::BasicTypes::Int32Str)
			{
				return new BasicPropertyType<int32_t>(name, 0);
			}
			else if(type == amb::BasicTypes::StringStr)
			{
				return new StringPropertyType(name);
			}
			else if(type == amb::BasicTypes::DoubleStr)
			{
				return new BasicPropertyType<double>(name, 0);
			}
			else if(type == amb::BasicTypes::BooleanStr)
			{
				return new BasicPropertyType<bool>(name, false);
			}
			DebugOut(DebugOut::Warning) << "Unknown or unsupported type: " << type << endl;
			return nullptr;
		});

		if(!ret)
		{
			DebugOut(DebugOut::Error) << "failed to register property: " << name << endl;
			return nullptr;
		}

		t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
	}

	t->fromJson(json);

	return std::shared_ptr<AbstractPropertyType>(t);
}
void
WifiInstance::InternalPostMessage( picojson::value v )
{
    syslog( LOG_DEBUG, "WifiInstance::InternalPostMessage entry" );
    if ( !is_js_context_initialized_ )
    {
        msg_queue_.push_back( v );
        return;
    }

    FlushPendingMessages();
    PostMessage( v.serialize().c_str() );
    syslog( LOG_DEBUG, "WifiInstance::InternalPostMessage exit" );
}
void
WifiInstance::HandleDisconnect( const picojson::value& message )
{
    disconnect_callback_id_ = message.get( "reply_id" ).to_str();
    if ( service_proxy_ )
    {
        g_dbus_proxy_call(
            service_proxy_,
            "Disconnect",
            NULL,                       // parameters
            G_DBUS_CALL_FLAGS_NONE,     // flags
            20000,                      // timeout
            NULL, NULL, NULL );
    }
}
Exemple #10
0
void
WifiInstance::HandleScan( const picojson::value& message )
{
    scan_callback_id_ = message.get( "reply_id" ).to_str();
    if ( tech_proxy_ )
    {
        g_dbus_proxy_call(
            tech_proxy_,
            "Scan",
            NULL,                       // parameters
            G_DBUS_CALL_FLAGS_NONE,     // flags
            20000,                      // timeout
            NULL, NULL, NULL );
    }
}
Exemple #11
0
void
WifiInstance::HandleDeactivate( const picojson::value& message )
{
    deactivate_callback_id_ = message.get( "reply_id" ).to_str();
    if ( tech_proxy_ )
    {
        g_dbus_proxy_call(
            tech_proxy_,
            "SetProperty",
            g_variant_new( "sb", "Powered", "FALSE" ),
            G_DBUS_CALL_FLAGS_NONE,
            20000,
            NULL, NULL, NULL );
    }
}
Exemple #12
0
void
WifiInstance::HandleGetServices( const picojson::value& message )
{
    connect_callback_id_ = message.get( "reply_id" ).to_str();
    if ( manager_proxy_ )
    {
        g_dbus_proxy_call(
            manager_proxy_,
            "GetServices",
            NULL,                       // parameters
            G_DBUS_CALL_FLAGS_NONE,     // flags
            20000,                      // timeout
            NULL,
            OnGetServicesThunk,
            NULL );
    }
}
Vector2 ParseVertex(const picojson::value& vertices, const picojson::value& id) {
  const auto& vertex = vertices.get(id.to_str());
  const auto& position = vertex.get("Position");
  return ParseVector(position);
}
Vector2 ParseVector(const picojson::value& xyPair) {
  return { xyPair.get("X").get<double>(), xyPair.get("Y").get<double>() };
}
Exemple #15
0
void
WifiInstance::InternalSetSyncReply( picojson::value v )
{
    SendSyncReply( v.serialize().c_str() );
    FlushPendingMessages();
}
void json_error_response(const std::string& error_message, picojson::value& id) {
  picojson::ext response;
  response.initByJson(std::string("{\"result\":null,\"error\":\"") + error_message + "\",\"id\":" + id.serialize() + "}");
  std::cout << "Content-type: application/json; charset=utf-8\n";
  std::cout << "Access-control-allow-origin: *\n\n";
  std::cout << response.toJson();
}