コード例 #1
0
/**
 * @brief Write data to the cloud
 * Note: you have to register components prior.
 * E.g. using "iotkit-admin register <comp_name> <catalogid>" or via iotkit-agent
 */
void writeDataToCloud(string &component, string &data) {
    UdpClient client;
    stringstream ss;

    /* Create a udp connection for iotkit-agent to communicate over */
    if (client.isConnected() == false) {
        if (client.connectUdp(NODE, SERVICE) < 0) {
            cerr << "connection failed" << endl;
            exit(EXIT_FAILURE);
        }
    }

    /*
     * Create the string to send to the cloud
     * eg. {"n":"gpsv1","v":"45.550953, -122.968719"}
     */
    ss << "{\"n\":\"" << component << "\",\"v\":\"" << data << "\"}" << endl;
    cout << ss.str();

    if (client.writeData(ss) < 0)
        cerr << "Cannot write to cloud\n" << endl;

    ss.str("");
}