#![feature(proc_macro)]
extern crate wifiscanner;
extern crate reqwest;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use wifiscanner::Wifi;
use reqwest::header::Accept;
const BASE_URL: &'static str = "https://maps.googleapis.com/maps/api/browserlocation/json";
const BASE_PARAMS: &'static str = "?browser=firefox&sensor=true";
#[derive(Debug, PartialEq)]
pub enum Error {
JSON,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct GpsLocation {
pub accuracy: u32,
pub location: Location,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Location {
pub lat: f64,
pub lng: f64,
}
pub fn get_towers() -> Vec<Wifi> {
wifiscanner::scan().unwrap() }
pub fn get_location(towers: Vec<Wifi>) -> Result<GpsLocation, Error> {
let mut url = String::new();
url.push_str(BASE_URL);
url.push_str(BASE_PARAMS);
let mut url_params = String::new();
for tower in towers {
url_params.push_str(&format!("&wifi=mac:{}&7Cssid:{}&7Css:{}",
tower.mac,
tower.ssid,
tower.signal_level)
.to_string());
}
url.push_str(&url_params);
let client = reqwest::Client::new().unwrap();
let mut res = client.post(&url)
.header(Accept::json())
.send()
.expect("Failed to connect to google api!");
let gps = try!(res.json::<GpsLocation>().map_err(|_| Error::JSON));
Ok(gps)
}