~thomas-voss/location-service/next

« back to all changes in this revision

Viewing changes to 3rd-party/ichnaea/examples/client.cpp

Merge lp:~thomas-voss/location-service/add-ichnaea-provider-next.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <ichnaea/client.h>
 
2
 
 
3
#include <com/ubuntu/location/connectivity/manager.h>
 
4
 
 
5
#include <core/dbus/macros.h>
 
6
#include <core/dbus/object.h>
 
7
#include <core/dbus/signal.h>
 
8
 
 
9
#include <core/net/http/client.h>
 
10
 
 
11
#include <thread>
 
12
#include <vector>
 
13
 
 
14
namespace connectivity = com::ubuntu::location::connectivity;
 
15
 
 
16
int main()
 
17
{
 
18
    auto manager = connectivity::platform_default_manager();
 
19
    auto http_client = core::net::http::make_client();
 
20
 
 
21
    std::thread worker1{[http_client]() {http_client->run();}};
 
22
 
 
23
    auto client = std::make_shared<ichnaea::Client>("test", http_client);
 
24
 
 
25
    manager->wireless_network_scan_finished().connect([manager, client]
 
26
    {
 
27
        std::cout << "Wireless network scan finished" << std::endl;
 
28
 
 
29
        std::vector<connectivity::WirelessNetwork::Ptr> wifis;
 
30
 
 
31
        manager->enumerate_visible_wireless_networks([&wifis](const connectivity::WirelessNetwork::Ptr& wifi)
 
32
        {
 
33
            wifis.push_back(wifi);
 
34
        });
 
35
 
 
36
        ichnaea::geolocate::Parameters params;
 
37
        params.consider_ip = true;
 
38
 
 
39
        for (auto wifi : wifis)
 
40
        {
 
41
            ichnaea::WifiAccessPoint ap;
 
42
            ap.bssid = wifi->bssid().get();
 
43
            ap.ssid = wifi->ssid().get();
 
44
            ap.frequency = wifi->frequency().get();
 
45
            ap.signal_strength = wifi->signal_strength().get();
 
46
 
 
47
            params.wifi_access_points.insert(ap);
 
48
        }
 
49
 
 
50
        try
 
51
        {
 
52
            client->geolocate(params, [client, params](const ichnaea::Response<ichnaea::geolocate::Result>& response)
 
53
            {
 
54
                if (not response.is_error())
 
55
                {
 
56
                    std::cout << "Submitting to the service again." << std::endl;
 
57
                    ichnaea::geosubmit::Parameters submission;
 
58
                    ichnaea::geosubmit::Report report;
 
59
                    report.timestamp = std::chrono::system_clock::now();
 
60
                    report.position.latitude = response.result().location.lat;
 
61
                    report.position.longitude = response.result().location.lon;
 
62
                    report.position.accuracy = response.result().accuracy;
 
63
                    report.wifi_access_points = params.wifi_access_points;
 
64
                    submission.reports.push_back(report);
 
65
 
 
66
                    client->geosubmit(submission, [](const ichnaea::Response<ichnaea::geosubmit::Result>& response)
 
67
                    {
 
68
                        if (not response.is_error())
 
69
                            std::cout << "Successfully submitted to service." << std::endl;
 
70
                        else
 
71
                            std::cout << "Error submitting to service: " << response.error() << std::endl;
 
72
                    });
 
73
                }
 
74
                else
 
75
                {
 
76
                    std::cout << "Error querying service for location: " << response.error() << std::endl;
 
77
                }
 
78
            });
 
79
 
 
80
            client->region(params, [](const ichnaea::Response<ichnaea::region::Result>& response)
 
81
            {
 
82
                if (not response.is_error())
 
83
                    std::cout << response.result().country_code << ", " << response.result().country_name << std::endl;
 
84
                else
 
85
                    std::cout << "Error querying service for region information: " << response.error() << std::endl;
 
86
            });
 
87
        }
 
88
        catch (const std::exception& e)
 
89
        {
 
90
            std::cout << e.what() << std::endl;
 
91
        }
 
92
    });
 
93
 
 
94
    while (true)
 
95
    {
 
96
        manager->request_scan_for_wireless_networks();
 
97
        std::this_thread::sleep_for(std::chrono::seconds{15});
 
98
    }
 
99
 
 
100
    return 0;
 
101
}