summaryrefslogtreecommitdiff
path: root/libforecastio/locator.c
blob: ef2216d1a98034fdd490fb44ba3b418ba0da3830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <vdr/plugin.h>
#include <jansson.h>
#include "../tools/curlfuncs.h"
#include "../tools/jsonhelpers.h"
#include "locator.h"

using namespace std;

cForecastLocator::cForecastLocator(void) {
    urlIPLApi = "http://ip-api.com/json";
    city = "";
    lat = 0.0;
    lon = 0.0;
}

cForecastLocator::~cForecastLocator() {

}

bool cForecastLocator::ReadLocationByIP(void) {
    string outputJson;
    CurlGetUrl(urlIPLApi.c_str(), &outputJson);
    
    json_t *root;
    json_error_t error;

    root = json_loads(outputJson.c_str(), 0, &error);
    if ( !root ) {
        return false;
    }
    if ( !json_is_object(root) ) {
        return false;
    }

    city    = JsonGetString(root, "city");
    lat     = JsonGetFloat(root, "lat");
    lon     = JsonGetFloat(root, "lon");

    dsyslog("weatherforecast: location read from IP: city \"%s\", latitude: %f, longitude: %f", city.c_str(), lat, lon);
    
    return true;
}

void cForecastLocator::WriteToSetup(cPlugin *weatherPlug) {
    //save to setup
    weatherPlug->SetupStore("city", city.c_str());
    weatherPlug->SetupStore("lat", FloatToString(lat).c_str());
    weatherPlug->SetupStore("lon", FloatToString(lon).c_str());
    //set values in plugin config
    weatherPlug->SetupParse("city", city.c_str());
    weatherPlug->SetupParse("lat", FloatToString(lat).c_str());
    weatherPlug->SetupParse("lon", FloatToString(lon).c_str());
}