Exemplo n.º 1
0
/*
 * Send the response to a new universe request
 */
void OladHTTPServer::SendCreateUniverseResponse(
    HTTPResponse *response,
    unsigned int universe_id,
    bool included_name,
    class ActionQueue *action_queue) {
  unsigned int action_count = action_queue->ActionCount();
  if (included_name)
    action_count--;
  bool failed = true;
  // it only takes one port patch to pass
  for (unsigned int i = 0; i < action_count; i++) {
    failed &= action_queue->GetAction(i)->Failed();
  }

  JsonObject json;
  json.Add("ok", !failed);
  json.Add("universe", universe_id);
  json.Add("message", (failed ? "Failed to patch any ports" : ""));

  response->SetNoCache();
  response->SetContentType(HTTPServer::CONTENT_TYPE_PLAIN);
  response->SendJson(json);
  delete action_queue;
  delete response;
}
JsonObject*
SamiAccessToken::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pAccess_tokenKey = new JsonString(L"access_token");
    pJsonObject->Add(pAccess_tokenKey, toJson(getPAccessToken(), "String", ""));

    
    JsonString *pRefresh_tokenKey = new JsonString(L"refresh_token");
    pJsonObject->Add(pRefresh_tokenKey, toJson(getPRefreshToken(), "String", ""));

    
    JsonString *pExpire_inKey = new JsonString(L"expire_in");
    pJsonObject->Add(pExpire_inKey, toJson(getPExpireIn(), "Integer", ""));

    
    JsonString *pExpires_inKey = new JsonString(L"expires_in");
    pJsonObject->Add(pExpires_inKey, toJson(getPExpiresIn(), "Integer", ""));

    
    JsonString *pScopeKey = new JsonString(L"scope");
    pJsonObject->Add(pScopeKey, toJson(getPScope(), "String", "array"));

    
    return pJsonObject;
}
Exemplo n.º 3
0
/*
 * Handle the plugin list callback
 * @param response the HTTPResponse that is associated with the request.
 * @param plugins a list of plugins
 * @param error an error string.
 */
void OladHTTPServer::HandlePluginList(HTTPResponse *response,
                                      const client::Result &result,
                                      const vector<OlaPlugin> &plugins) {
  if (!result.Success()) {
    m_server.ServeError(response, result.Error());
    return;
  }

  JsonObject *json = new JsonObject();

  // fire off the universe request now. the main server is running in a
  // separate thread.
  m_client.FetchUniverseList(
      NewSingleCallback(this,
                        &OladHTTPServer::HandleUniverseList,
                        response,
                        json));

  JsonArray *plugins_json = json->AddArray("plugins");
  vector<OlaPlugin>::const_iterator iter;
  for (iter = plugins.begin(); iter != plugins.end(); ++iter) {
    JsonObject *plugin = plugins_json->AppendObject();
    plugin->Add("name", iter->Name());
    plugin->Add("id", iter->Id());
  }
}
Exemplo n.º 4
0
/*
 * Handle the plugin list callback
 * @param response the HTTPResponse that is associated with the request.
 * @param plugins a list of plugins
 * @param error an error string.
 */
void OladHTTPServer::HandlePluginList(HTTPResponse *response,
                                     const vector<OlaPlugin> &plugins,
                                     const string &error) {
  if (!error.empty()) {
    m_server.ServeError(response, error);
    return;
  }

  JsonObject *json = new JsonObject();

  // fire off the universe request now. the main server is running in a
  // separate thread.
  bool ok = m_client.FetchUniverseList(
      NewSingleCallback(this,
                        &OladHTTPServer::HandleUniverseList,
                        response,
                        json));

  if (!ok) {
    m_server.ServeError(response, K_BACKEND_DISCONNECTED_ERROR);
    delete json;
    return;
  }

  JsonArray *plugins_json = json->AddArray("plugins");
  vector<OlaPlugin>::const_iterator iter;
  for (iter = plugins.begin(); iter != plugins.end(); ++iter) {
    JsonObject *plugin = plugins_json->AppendObject();
    plugin->Add("name", iter->Name());
    plugin->Add("id", iter->Id());
  }
}
Exemplo n.º 5
0
/*
 * Handle the universe info
 * @param response the HTTPResponse that is associated with the request.
 * @param universe the OlaUniverse object
 * @param error an error string.
 */
void OladHTTPServer::HandleUniverseInfo(HTTPResponse *response,
                                        const client::Result &result,
                                        const OlaUniverse &universe) {
  if (!result.Success()) {
    m_server.ServeError(response, result.Error());
    return;
  }

  JsonObject *json = new JsonObject();

  // fire off the device/port request now. the main server is running in a
  // separate thread.
  m_client.FetchDeviceInfo(
      ola::OLA_PLUGIN_ALL,
      NewSingleCallback(this,
                        &OladHTTPServer::HandlePortsForUniverse,
                        response,
                        json,
                        universe.Id()));

  json->Add("id", universe.Id());
  json->Add("name", universe.Name());
  json->Add("merge_mode",
           (universe.MergeMode() == OlaUniverse::MERGE_HTP ? "HTP" : "LTP"));
}
Exemplo n.º 6
0
/**
 * @brief Print the server stats JSON
 * @param request the HTTPRequest
 * @param response the HTTPResponse
 * @returns MHD_NO or MHD_YES
 */
int OladHTTPServer::JsonServerStats(const HTTPRequest*,
                                    HTTPResponse *response) {
  char start_time_str[50];
#ifdef _WIN32
  strftime(start_time_str, sizeof(start_time_str), "%c",
      localtime(&m_start_time_t));
#else
  struct tm start_time;
  localtime_r(&m_start_time_t, &start_time);
  strftime(start_time_str, sizeof(start_time_str), "%c", &start_time);
#endif

  JsonObject json;
  json.Add("hostname", ola::network::FQDN());
  json.Add("instance_name", m_ola_server->InstanceName());
  json.Add("config_dir",
           m_ola_server->GetPreferencesFactory()->ConfigLocation());
  json.Add("ip", m_interface.ip_address.ToString());
  json.Add("broadcast", m_interface.bcast_address.ToString());
  json.Add("subnet", m_interface.subnet_mask.ToString());
  json.Add("hw_address", m_interface.hw_address.ToString());
  json.Add("version", ola::base::Version::GetVersion());
  json.Add("up_since", start_time_str);
  json.Add("quit_enabled", m_enable_quit);

  response->SetNoCache();
  response->SetContentType(HTTPServer::CONTENT_TYPE_PLAIN);
  int r = response->SendJson(json);
  delete response;
  return r;
}
Exemplo n.º 7
0
/*
 * Handle the universe info
 * @param response the HTTPResponse that is associated with the request.
 * @param universe the OlaUniverse object
 * @param error an error string.
 */
void OladHTTPServer::HandleUniverseInfo(HTTPResponse *response,
                                       OlaUniverse &universe,
                                       const string &error) {
  if (!error.empty()) {
    m_server.ServeError(response, error);
    return;
  }

  JsonObject *json = new JsonObject();

  // fire off the device/port request now. the main server is running in a
  // separate thread.
  bool ok = m_client.FetchDeviceInfo(
      ola::OLA_PLUGIN_ALL,
      NewSingleCallback(this,
                        &OladHTTPServer::HandlePortsForUniverse,
                        response,
                        json,
                        universe.Id()));

  if (!ok) {
    m_server.ServeError(response, K_BACKEND_DISCONNECTED_ERROR);
    delete json;
    return;
  }

  json->Add("id", universe.Id());
  json->Add("name", universe.Name());
  json->Add("merge_mode",
           (universe.MergeMode() == OlaUniverse::MERGE_HTP ? "HTP" : "LTP"));
}
Exemplo n.º 8
0
void SelectItem::SetValue(JsonObject *item) const {
  JsonArray *options = item->AddArray("value");
  vector<pair<string, string> >::const_iterator iter = m_values.begin();
  for (; iter != m_values.end(); ++iter) {
    JsonObject *option = options->AppendObject();
    option->Add("label", iter->first);
    option->Add("value", iter->second);
  }
}
JsonObject*
SamiAlgorithm::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    JsonString *pProblem_typeKey = new JsonString(L"problem_type");
    pJsonObject->Add(pProblem_typeKey, toJson(getPProblemType(), "String", ""));

    JsonString *pObjectiveKey = new JsonString(L"objective");
    pJsonObject->Add(pObjectiveKey, toJson(getPObjective(), "String", ""));

    return pJsonObject;
}
JsonObject*
SamiShipment::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    JsonString *pIdKey = new JsonString(L"id");
    pJsonObject->Add(pIdKey, toJson(getPId(), "String", ""));

    JsonString *pNameKey = new JsonString(L"name");
    pJsonObject->Add(pNameKey, toJson(getPName(), "String", ""));

    JsonString *pPriorityKey = new JsonString(L"priority");
    pJsonObject->Add(pPriorityKey, toJson(getPPriority(), "Integer", ""));

    JsonString *pPickupKey = new JsonString(L"pickup");
    pJsonObject->Add(pPickupKey, toJson(getPPickup(), "SamiStop", ""));

    JsonString *pDeliveryKey = new JsonString(L"delivery");
    pJsonObject->Add(pDeliveryKey, toJson(getPDelivery(), "SamiStop", ""));

    JsonString *pSizeKey = new JsonString(L"size");
    pJsonObject->Add(pSizeKey, toJson(getPSize(), "Integer", "array"));

    JsonString *pRequired_skillsKey = new JsonString(L"required_skills");
    pJsonObject->Add(pRequired_skillsKey, toJson(getPRequiredSkills(), "String", "array"));

    JsonString *pAllowed_vehiclesKey = new JsonString(L"allowed_vehicles");
    pJsonObject->Add(pAllowed_vehiclesKey, toJson(getPAllowedVehicles(), "String", "array"));

    return pJsonObject;
}
Exemplo n.º 11
0
/**
 * @brief Handle the plugin description response.
 * @param response the HTTPResponse that is associated with the request.
 * @param description the plugin description
 * @param result the result of the API call.
 * @param state the state of the plugin.
 */
void OladHTTPServer::HandlePluginInfo(HTTPResponse *response,
                                      string description,
                                      const client::Result &result,
                                      const ola::client::PluginState &state) {
  if (!result.Success()) {
    m_server.ServeError(response, result.Error());
    return;
  }

  string escaped_description = description;
  // Replace \n before passing in so we get \\n out the far end
  ReplaceAll(&escaped_description, "\n", "\\n");

  JsonObject json;
  json.Add("description", escaped_description);
  json.Add("name", state.name);
  json.Add("enabled", state.enabled);
  json.Add("active", state.active);
  json.Add("preferences_source", state.preferences_source);
  JsonArray *plugins = json.AddArray("conflicts_with");
  vector<OlaPlugin>::const_iterator iter = state.conflicting_plugins.begin();
  for (; iter != state.conflicting_plugins.end(); ++iter) {
    JsonObject *plugin = plugins->AppendObject();
    plugin->Add("active", iter->IsActive());
    plugin->Add("id", iter->Id());
    plugin->Add("name", iter->Name());
  }

  response->SetNoCache();
  response->SetContentType(HTTPServer::CONTENT_TYPE_PLAIN);
  response->SendJson(json);
  delete response;
}
Exemplo n.º 12
0
/*
 * Handle the plugin description response.
 * @param response the HTTPResponse that is associated with the request.
 * @param description the plugin description.
 * @param error an error string.
 */
void OladHTTPServer::HandlePluginInfo(
    HTTPResponse *response,
    string description,
    const OlaCallbackClient::PluginState &state,
    const string &error) {
  if (!error.empty()) {
    m_server.ServeError(response, error);
    return;
  }
  string escaped_description = description;
  Escape(&escaped_description);

  JsonObject json;
  json.Add("description", description);
  json.Add("name", state.name);
  json.Add("enabled", state.enabled);
  json.Add("active", state.active);
  json.Add("preferences_source", state.preferences_source);
  JsonArray *plugins = json.AddArray("conflicts_with");
  vector<OlaPlugin>::const_iterator iter = state.conflicting_plugins.begin();
  for (; iter != state.conflicting_plugins.end(); ++iter) {
    JsonObject *plugin = plugins->AppendObject();
    plugin->Add("active", iter->IsActive());
    plugin->Add("id", iter->Id());
    plugin->Add("name", iter->Name());
  }

  response->SetNoCache();
  response->SetContentType(HTTPServer::CONTENT_TYPE_PLAIN);
  response->SendJson(json);
  delete response;
}
Exemplo n.º 13
0
/*
 * Test a simple object.
 */
void JsonTest::testSimpleObject() {
  JsonObject object;
  object.Add("age", 10);
  object.Add("name", "simon");
  object.Add("male", true);

  string expected = (
      "{\n"
      "  \"age\": 10,\n"
      "  \"male\": true,\n"
      "  \"name\": \"simon\"\n"
      "}");
  OLA_ASSERT_EQ(expected, JsonWriter::AsString(object));
}
Exemplo n.º 14
0
/*
 * Return the section as a string.
 */
string JsonSection::AsString() const {
  JsonObject json;

  json.Add("refresh", m_allow_refresh);
  json.Add("error", m_error);
  if (!m_save_button_text.empty())
    json.Add("save_button", m_save_button_text);

  JsonArray *items = json.AddArray("items");
  vector<const GenericItem*>::const_iterator iter = m_items.begin();
  for (; iter != m_items.end(); ++iter) {
    JsonObject *item = items->AppendObject();
    (*iter)->PopulateItem(item);
  }
  return JsonWriter::AsString(json);
}
JsonObject*
SamiMeasurementValue::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pStart_timeKey = new JsonString(L"start_time");
    pJsonObject->Add(pStart_timeKey, toJson(getPStartTime(), "Long", ""));

    
    JsonString *pValueKey = new JsonString(L"value");
    pJsonObject->Add(pValueKey, toJson(getPValue(), "Float", ""));

    
    return pJsonObject;
}
Exemplo n.º 16
0
JsonObject*
SamiApiResponse::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    JsonString *pCodeKey = new JsonString(L"code");
    pJsonObject->Add(pCodeKey, toJson(getPCode(), "Integer", ""));

    JsonString *pTypeKey = new JsonString(L"type");
    pJsonObject->Add(pTypeKey, toJson(getPType(), "String", ""));

    JsonString *pMessageKey = new JsonString(L"message");
    pJsonObject->Add(pMessageKey, toJson(getPMessage(), "String", ""));

    return pJsonObject;
}
Exemplo n.º 17
0
JsonObject*
SamiCategory::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pIdKey = new JsonString(L"id");
    pJsonObject->Add(pIdKey, toJson(getPId(), "Long", ""));

    
    JsonString *pNameKey = new JsonString(L"name");
    pJsonObject->Add(pNameKey, toJson(getPName(), "String", ""));

    
    return pJsonObject;
}
Exemplo n.º 18
0
JsonObject*
SamiQuery::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pIncludeListKey = new JsonString(L"$includeList");
    pJsonObject->Add(pIncludeListKey, toJson(getPIncludeList(), "String", "array"));

    
    JsonString *pIncludeKey = new JsonString(L"$include");
    pJsonObject->Add(pIncludeKey, toJson(getPInclude(), "String", "array"));

    
    return pJsonObject;
}
Exemplo n.º 19
0
JsonObject*
SamiQueueBody::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pDocumentKey = new JsonString(L"document");
    pJsonObject->Add(pDocumentKey, toJson(getPDocument(), "SamiQueue", ""));

    
    JsonString *pKeyKey = new JsonString(L"key");
    pJsonObject->Add(pKeyKey, toJson(getPKey(), "String", ""));

    
    return pJsonObject;
}
JsonObject*
SamiInline_response_200_28::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pDataKey = new JsonString(L"data");
    pJsonObject->Add(pDataKey, toJson(getPData(), "SamiVariable", ""));

    
    JsonString *pSuccessKey = new JsonString(L"success");
    pJsonObject->Add(pSuccessKey, toJson(getPSuccess(), "Boolean", ""));

    
    return pJsonObject;
}
JsonObject*
SamiStatus::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pCodeKey = new JsonString(L"code");
    pJsonObject->Add(pCodeKey, toJson(getPCode(), "String", ""));

    
    JsonString *pInfoKey = new JsonString(L"info");
    pJsonObject->Add(pInfoKey, toJson(getPInfo(), "String", ""));

    
    return pJsonObject;
}
JsonObject*
SamiNATimeTableItem::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();


    JsonString *pIdKey = new JsonString(L"id");
    pJsonObject->Add(pIdKey, toJson(getPId(), "Integer", ""));


    JsonString *pM_offsetKey = new JsonString(L"m_offset");
    pJsonObject->Add(pM_offsetKey, toJson(getPMOffset(), "Integer", ""));


    return pJsonObject;
}
Exemplo n.º 23
0
/*
 * Test a complex object.
 */
void JsonTest::testComplexObject() {
  JsonObject object;
  object.Add("age", 10);
  object.Add("name", "simon");
  object.Add("male", true);

  JsonArray *array = object.AddArray("lucky numbers");
  array->Append(2);
  array->Append(5);

  string expected = (
      "{\n"
      "  \"age\": 10,\n"
      "  \"lucky numbers\": [2, 5],\n"
      "  \"male\": true,\n"
      "  \"name\": \"simon\"\n"
      "}");
  OLA_ASSERT_EQ(expected, JsonWriter::AsString(object));
}
Exemplo n.º 24
0
void CreateProc::_ResizeFiles()
{
    AppSetting *setting = AppSetting::Instance();

    JsonObject *projData = new JsonObject();
    projData->Add("pid",_pid);

    JsonArray *pages = new JsonArray();
    projData->AddObject("pages", pages);

    for (auto fileItor = _filesMapping->begin(); fileItor != _filesMapping->end(); ++fileItor)
    {
        JsonObject *singleFile = new JsonObject();

        string srcFilename = FileSys::GetFilename(fileItor->first);
        string saveFilename = FileSys::GetFilename(fileItor->second);
        string fileExtension = FileSys::GetFileExtension(fileItor->first);

        singleFile->Add("filename", srcFilename);
        singleFile->Add("imagedir", fileItor->first);

        for (auto sizeItor = setting->pic_size->begin(); sizeItor != setting->pic_size->end(); ++sizeItor)
        {
            string optDir = FileSys::FormatDir(setting->file_save_dir + _zipRandName + "/" + sizeItor->first);
            FileSys::CreateDirectory(optDir);

            //  Resize Picture, but need to rename
            saveFilename = Assist::GenerateRandomFilename(saveFilename) + fileExtension;
            OpencvEx::ResizePictureFile(fileItor->second, optDir + saveFilename, sizeItor->second->x, sizeItor->second->y);

            singleFile->Add("image" + sizeItor->first, optDir + saveFilename);
        }

        pages->AddObject(singleFile);
        safe_del(singleFile);
    }

    //  push to server
    _PushJsonToServer(projData->ToString());

    safe_del(pages);
    safe_del(projData);
}
JsonObject*
SamiNAThermMeasure::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pTimeKey = new JsonString(L"time");
    pJsonObject->Add(pTimeKey, toJson(getPTime(), "Integer", ""));

    
    JsonString *pTemperatureKey = new JsonString(L"temperature");
    pJsonObject->Add(pTemperatureKey, toJson(getPTemperature(), "Float", ""));

    
    JsonString *pSetpoint_tempKey = new JsonString(L"setpoint_temp");
    pJsonObject->Add(pSetpoint_tempKey, toJson(getPSetpointTemp(), "Float", ""));

    
    return pJsonObject;
}
JsonObject*
SamiNAMeasureBodyElem::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pBeg_timeKey = new JsonString(L"beg_time");
    pJsonObject->Add(pBeg_timeKey, toJson(getPBegTime(), "Integer", ""));

    
    JsonString *pStep_timeKey = new JsonString(L"step_time");
    pJsonObject->Add(pStep_timeKey, toJson(getPStepTime(), "Integer", ""));

    
    JsonString *pValueKey = new JsonString(L"value");
    pJsonObject->Add(pValueKey, toJson(getPValue(), "IList", "array"));

    
    return pJsonObject;
}
Exemplo n.º 27
0
JsonObject*
SamiPasswordChangeBody::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pOldPasswordKey = new JsonString(L"oldPassword");
    pJsonObject->Add(pOldPasswordKey, toJson(getPOldPassword(), "String", ""));

    
    JsonString *pNewPasswordKey = new JsonString(L"newPassword");
    pJsonObject->Add(pNewPasswordKey, toJson(getPNewPassword(), "String", ""));

    
    JsonString *pKeyKey = new JsonString(L"key");
    pJsonObject->Add(pKeyKey, toJson(getPKey(), "String", ""));

    
    return pJsonObject;
}
Exemplo n.º 28
0
JsonObject*
SamiBoolQuery::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pBoolKey = new JsonString(L"bool");
    pJsonObject->Add(pBoolKey, toJson(getPBool(), "SamiSearchQuery", ""));

    
    return pJsonObject;
}
Exemplo n.º 29
0
JsonObject*
SamiSources::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pSourcesKey = new JsonString(L"sources");
    pJsonObject->Add(pSourcesKey, toJson(getPSources(), "SamiSource", "array"));

    
    return pJsonObject;
}
JsonObject*
SamiShowcaseDatatypeDate::asJsonObject() {
    JsonObject *pJsonObject = new JsonObject();
    pJsonObject->Construct();

    
    JsonString *pDateKey = new JsonString(L"date");
    pJsonObject->Add(pDateKey, toJson(getPDate(), "DateTime", ""));

    
    return pJsonObject;
}