示例#1
0
文件: test.cpp 项目: sjmeijer/sdk
int test_Client(const std::string &host, const unsigned &port)
{
  try {
    using Motion::SDK::Client;
    using Motion::SDK::Format;

    // Open connection to the data server.
    Client client(host, port);
    std::cout << "Connected to " << host << ":" << port << std::endl;

    if (client.waitForData()) {
      std::size_t sample_count = 0;

      // Read data samples in a loop. This is a blocking call so
      // we can simply wait on an open connection until a data
      // sample comes in.
      Client::data_type data;
      while ((sample_count++ < NSample) && client.readData(data)) {

        if (PortPreview == port) {
          typedef Format::preview_service_type map_type;

          map_type preview = Format::Preview(data.begin(), data.end());
          if (!preview.empty()) {
            std::cout << Format::PreviewElement::Name << ": " << preview.size();

            for (map_type::iterator itr=preview.begin(); itr!=preview.end();
                 ++itr) {
              Format::PreviewElement::data_type q =
                itr->second.getQuaternion(false);
              std::cout
                << " q(" << itr->first << ") = (" << q[0] << ", " << q[1]
                << ", " << q[2] << ", " << q[3] << ")"
                << std::endl;
            }
          }
        }


        if (PortSensor == port) {
          typedef Format::sensor_service_type map_type;

          map_type sensor = Format::Sensor(data.begin(), data.end());
          if (!sensor.empty()) {
            std::cout << Format::SensorElement::Name << ": " << sensor.size();

            for (map_type::iterator itr=sensor.begin(); itr!=sensor.end();
                 ++itr) {
              Format::SensorElement::data_type acc =
                itr->second.getAccelerometer();
              std::cout
                << " a(" << itr->first << ") = " << acc[0] << " " << acc[1]
                << " " << acc[2]
                << std::endl;
            }
          }
        }


        if (PortRaw == port) {
          typedef Format::raw_service_type map_type;
          
          map_type raw = Format::Raw(data.begin(), data.end());
          if (!raw.empty()) {
            std::cout << Format::RawElement::Name << ": "<< raw.size();

            for (map_type::iterator itr=raw.begin(); itr!=raw.end(); ++itr) {
              Format::RawElement::data_type acc =
                itr->second.getAccelerometer();
              std::cout
                << " a(" << itr->first << ") = " << acc[0] << " " << acc[1]
                << " " << acc[2]
                << std::endl;
            }
          }
        }

      }
    } else {
      std::cout << "No current data available, giving up" << std::endl;
    }

    std::string message;
    if (client.getErrorString(message)) {
      std::cerr << "Error: " << message << std::endl;
    }

  } catch (std::runtime_error &e) {
    std::cerr << e.what() << std::endl;
    return 1;
  }

  return 0;
}
示例#2
0
文件: test.cpp 项目: sjmeijer/sdk
int test_Configurable(const std::string &host, const unsigned &port)
{
  try {
    using Motion::SDK::Client;
    using Motion::SDK::Format;

    // Open connection to the data server.
    Client client(host, port);
    std::cout << "Connected to " << host << ":" << port << std::endl;

    // The Configurable data service requires an XML definition of the
    // requested channel names.
    {
      Client::data_type xml_definition;
      {
        std::ifstream fin("../../test_data/configurable.xml",
                          std::ios_base::binary | std::ios_base::in);
        if (fin.is_open()) {
          fin.seekg(0, std::ios_base::end);
          int num_bytes = fin.tellg();
          fin.seekg(0, std::ios_base::beg);

          if (num_bytes > 0) {
            xml_definition.resize(num_bytes);
            if (fin.read(&xml_definition[0], num_bytes)) {
            } else {
              xml_definition.clear();
            }
          }
        }
      }

      // Make a default definition here, in case we could not find our file.
      // Access the global quaternion and calibrated accelerometer streams.
      if (xml_definition.empty()) {
        std::string xml_string =
          "<?xml version=\"1.0\"?>"
          "<configurable>"
          "<preview><Gq/></preview>"
          "<sensor><a/></sensor>"
          "</configurable>";
        xml_definition.assign(xml_string.begin(), xml_string.end());
      }

      if (!xml_definition.empty() && client.writeData(xml_definition)) {
        std::cout
          << "Sent active channel definition to Configurable service"
          << std::endl;
      }
    }

    if (client.waitForData()) {
      std::size_t sample_count = 0;

      // Read data samples in a loop. This is a blocking call so
      // we can simply wait on an open connection until a data
      // sample comes in.
      Client::data_type data;
      while ((sample_count++ < NSample) && client.readData(data)) {
        typedef Format::configurable_service_type map_type;

        map_type container = Format::Configurable(data.begin(), data.end());
        if (!container.empty()) {
          std::cout
            << Format::ConfigurableElement::Name << ": " << container.size();

          for (map_type::iterator itr=container.begin(); itr!=container.end();
               ++itr) {
            std::cout << " data(" << itr->first << ") = ";
            for (std::size_t i=0; i<itr->second.size(); i++) {
              std::cout << itr->second[i] << " ";
            }

            std::cout << std::endl;
          }
        }

        std::cout << std::endl;
      }
    } else {
      std::cout << "No current data available, giving up" << std::endl;
    }

    std::string message;
    if (client.getErrorString(message)) {
      std::cerr << "Error: " << message << std::endl;
    }

  } catch (std::runtime_error &e) {
    std::cerr << e.what() << std::endl;
    return 1;
  }

  return 0;
}
  /**
    Thread function. Make a connection to the remote Host:Port and read
    Preview data in a loop.
  */
  bool operator()()
  {
    using Motion::SDK::Client;
    using Motion::SDK::Format;

    try {
      // Open a connection to the Motion Service Preview stream running
      // on Host:Port.
      Client client(Host, Port);

      std::cout << "Connected to " << Host << ":" << Port << std::endl;

      bool quit_thread = false;
      while (!quit_thread) {

        // Make a check for the quit flag here.
        {
          sdl_mutex_lock lock(m_mutex);
          if (m_quit) {
            quit_thread = true;
          }
        }

        // Block on this call until a single data sample arrives from the
        // remote service. Use default time out of 5 seconds.
        if (client.waitForData()) {

          // The Client#readData method will time out after 1 second. Then
          // just go back to the blocking Client#waitForData method to wait
          // for more incoming data.
          Client::data_type data;
          while (!quit_thread && client.readData(data)) {

            // We have a message from the remote Preview service. Use the
            // Format::Preview method to create a std::map<integer,Format::PreviewElement>
            // of all active device connections.

            std::vector<float> transform;
            std::vector<float> euler;
            {
              // Iterate through Nodes, but just grab the first one.
              Format::preview_service_type preview = Format::Preview(data.begin(), data.end());
              for (Format::preview_service_type::iterator itr=preview.begin(); itr!=preview.end(); ++itr) {
                transform = itr->second.getMatrix(false);
                euler = itr->second.getEuler();
                break;
              }
            }

            // Copy the new transformation matrix into the mutex protected
            // state. The drawing thread reads this.
            {
              sdl_mutex_lock lock(m_mutex);
              if (transform.size() == m_transform.size()) {
                m_transform = transform;
              }
              if (euler.size() == m_euler.size()) {
                m_euler = euler;
              }
              if (m_quit) {
                quit_thread = true;
                break;
              }
            }
          }

        }

      }

    } catch (std::exception &e) {
      std::cerr << e.what() << std::endl;
      return false;
    }

    
    std::cout << "Leaving client thread" << std::endl;

    return true;
  }