simulation_world_updater.cc 2.7 KB
Newer Older
S
siyangy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

17
#include "modules/dreamview/backend/simulation_world/simulation_world_updater.h"
S
siyangy 已提交
18 19 20 21

#include <string>

#include "google/protobuf/util/json_util.h"
22
#include "modules/dreamview/backend/common/dreamview_gflags.h"
S
siyangy 已提交
23 24 25 26

namespace apollo {
namespace dreamview {

S
siyangy 已提交
27
using apollo::common::adapter::AdapterManager;
S
siyangy 已提交
28 29 30
using google::protobuf::util::MessageToJsonString;
using Json = nlohmann::json;

31
SimulationWorldUpdater::SimulationWorldUpdater(WebSocketHandler *websocket,
S
siyangy 已提交
32 33 34
                                               MapService *map_service,
                                               bool routing_from_file)
    : sim_world_service_(map_service, routing_from_file),
35
      map_service_(map_service),
S
siyangy 已提交
36 37 38 39 40 41 42
      websocket_(websocket) {
  websocket_->RegisterMessageHandler(
      "RetrieveMapData",
      [this](const Json &json, WebSocketHandler::Connection *conn) {
        auto iter = json.find("elements");
        if (iter != json.end()) {
          MapElementIds map_element_ids(*iter);
43
          auto retrieved = map_service_->RetrieveMapElements(map_element_ids);
S
siyangy 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56

          std::string retrieved_json_string;
          MessageToJsonString(retrieved, &retrieved_json_string);

          Json response;
          response["type"] = "MapData";
          response["data"] = Json::parse(retrieved_json_string);

          websocket_->SendData(response.dump(), conn);
        }
      });
}

S
siyangy 已提交
57 58 59 60 61 62 63
void SimulationWorldUpdater::Start() {
  // start ROS timer, one-shot = false, auto-start = true
  timer_ =
      AdapterManager::CreateTimer(ros::Duration(kSimWorldTimeInterval),
                                  &SimulationWorldUpdater::OnPushTimer, this);
}

S
siyangy 已提交
64 65 66 67 68 69 70
void SimulationWorldUpdater::OnPushTimer(const ros::TimerEvent &event) {
  sim_world_service_.Update();
  if (!sim_world_service_.ReadyToPush()) {
    AWARN << "Not sending simulation world as the data is not ready!";
    return;
  }
  auto json = sim_world_service_.GetUpdateAsJson();
71
  websocket_->BroadcastData(json.dump());
S
siyangy 已提交
72 73 74 75
}

}  // namespace dreamview
}  // namespace apollo