~thomas-voss/location-service/refactor-location-position

« back to all changes in this revision

Viewing changes to src/location/providers/mls/provider.cpp

  • Committer: Thomas Voß
  • Date: 2016-08-14 19:36:05 UTC
  • Revision ID: thomas.voss@canonical.com-20160814193605-e6c0xitja9yncmw8
Add back mls provider relying on Mozilla's location service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012-2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include "provider.h"
 
20
 
 
21
#include <location/logging.h>
 
22
 
 
23
#include <core/net/http/client.h>
 
24
 
 
25
#include <thread>
 
26
 
 
27
namespace mls = location::providers::mls;
 
28
 
 
29
std::string mls::Provider::class_name()
 
30
{
 
31
    return "mls::Provider";
 
32
}
 
33
 
 
34
location::Provider::Ptr mls::Provider::create_instance(const location::ProviderFactory::Configuration& config)
 
35
{
 
36
    mls::Configuration configuration;
 
37
    configuration.host = config.get("Host", ichnaea::Client::default_host);
 
38
    configuration.api_key = config.get("ApiKey", "test");
 
39
 
 
40
    return location::Provider::Ptr{new mls::Provider{configuration}};
 
41
}
 
42
 
 
43
mls::Provider::Provider(const mls::Configuration& config)
 
44
    : connectivity_manager{com::ubuntu::location::connectivity::platform_default_manager()},
 
45
      http_client{core::net::http::make_client()},
 
46
      ichnaea_client{std::make_shared<ichnaea::Client>(config.host, config.api_key, http_client)},
 
47
      stop_requested{false},
 
48
      rt{location::Runtime::create(1)},
 
49
      timer{rt->service()},
 
50
      http_worker{[this]() { http_client->run(); }}
 
51
{
 
52
    connectivity_manager->wireless_network_scan_finished().connect([this]()
 
53
    {
 
54
        VLOG(1) << "Wireless network scan finished.";
 
55
        std::vector<com::ubuntu::location::connectivity::WirelessNetwork::Ptr> wifis;
 
56
 
 
57
        connectivity_manager->enumerate_visible_wireless_networks([&wifis](const com::ubuntu::location::connectivity::WirelessNetwork::Ptr& wifi)
 
58
        {
 
59
            wifis.push_back(wifi);
 
60
        });
 
61
 
 
62
        ichnaea::geolocate::Parameters params;
 
63
        params.consider_ip = true;
 
64
 
 
65
        for (auto wifi : wifis)
 
66
        {
 
67
            ichnaea::WifiAccessPoint ap;
 
68
            ap.bssid = wifi->bssid().get();
 
69
            ap.ssid = wifi->ssid().get();
 
70
            ap.frequency = wifi->frequency().get();
 
71
            ap.signal_strength = wifi->signal_strength().get();
 
72
 
 
73
            params.wifi_access_points.insert(ap);
 
74
        }
 
75
 
 
76
        try
 
77
        {
 
78
            ichnaea_client->geolocate(params, [this](const ichnaea::Response<ichnaea::geolocate::Result>& response)
 
79
            {
 
80
                if (not response.is_error())
 
81
                {
 
82
                    Update<Position> update;
 
83
                    update.when = Clock::now();
 
84
                    update.value = Position
 
85
                    {
 
86
                        wgs84::Latitude{response.result().location.lat * units::Degrees},
 
87
                        wgs84::Longitude{response.result().location.lon * units::Degrees}
 
88
                    };
 
89
                    Position::Accuracy accuracy; accuracy.horizontal = response.result().accuracy * units::Meters;
 
90
                    update.value.accuracy = accuracy;
 
91
                    updates.position(update);
 
92
                }
 
93
                else
 
94
                {
 
95
                    LOG(WARNING) << "Service returned error for location query: " << response.error();
 
96
                }
 
97
            });
 
98
        }
 
99
        catch (const std::runtime_error& e)
 
100
        {
 
101
            LOG(WARNING) << "Error querying location for new wifi readings: " << e.what();
 
102
        }
 
103
    });
 
104
 
 
105
    rt->start();
 
106
}
 
107
 
 
108
mls::Provider::~Provider() noexcept
 
109
{
 
110
    deactivate();
 
111
    rt->stop();
 
112
}
 
113
 
 
114
void mls::Provider::on_new_event(const Event&)
 
115
{
 
116
}
 
117
 
 
118
location::Provider::Requirements mls::Provider::requirements() const
 
119
{
 
120
    return Requirements::none;
 
121
}
 
122
 
 
123
bool mls::Provider::satisfies(const location::Criteria&)
 
124
{
 
125
    return true;
 
126
}
 
127
 
 
128
void mls::Provider::enable()
 
129
{
 
130
}
 
131
 
 
132
void mls::Provider::disable()
 
133
{
 
134
}
 
135
 
 
136
void mls::Provider::activate()
 
137
{
 
138
    on_timeout(boost::system::error_code{});
 
139
}
 
140
 
 
141
void mls::Provider::deactivate()
 
142
{
 
143
    boost::system::error_code ec; timer.cancel(ec);
 
144
}
 
145
 
 
146
const core::Signal<location::Update<location::Position>>& mls::Provider::position_updates() const
 
147
{
 
148
    return updates.position;
 
149
}
 
150
 
 
151
const core::Signal<location::Update<location::Heading>>& mls::Provider::heading_updates() const
 
152
{
 
153
    return updates.heading;
 
154
}
 
155
 
 
156
const core::Signal<location::Update<location::Velocity>>& mls::Provider::velocity_updates() const
 
157
{
 
158
    return updates.velocity;
 
159
}
 
160
 
 
161
void mls::Provider::on_timeout(const boost::system::error_code& ec)
 
162
{
 
163
    if (ec) return;
 
164
 
 
165
    auto thiz = shared_from_this();
 
166
    std::weak_ptr<Provider> wp{thiz};
 
167
 
 
168
    connectivity_manager->request_scan_for_wireless_networks();
 
169
 
 
170
    timer.expires_from_now(boost::posix_time::seconds{10});
 
171
    timer.async_wait([wp](const boost::system::error_code& ec)
 
172
    {
 
173
        if (auto sp = wp.lock()) sp->on_timeout(ec);
 
174
    });
 
175
}