Commit ec7f07cf authored by Sergey 'Jin' Bostandzhyan's avatar Sergey 'Jin' Bostandzhyan

Merge branch 'master' into testing

parents 25623076 53f74066
......@@ -98,6 +98,7 @@ typedef struct netatmo_vdcd
#define NETATMO_DEVLIST_FAILED -13
#define NETATMO_GETMEASURE_FAILED -13
#define NETATMO_BAD_ACCESS_TOKEN -14
#define NETATMO_TOKEN_EXPIRED -15
extern const char *g_cfgfile;
......
......@@ -327,7 +327,10 @@ netatmo_get_token()
} else if (!strcmp(key, "expires_in")) {
if (type == json_type_int) {
int exp = json_object_get_int(val);
g_access_token_valid_until = time(NULL) + exp - 120; // 2 minutes before expiration
g_access_token_valid_until = time(NULL) + exp;
if (exp > 60 * 60 * 2) {
g_access_token_valid_until -= 60 * 60 * 2; // 2 hours before expiration
}
vdc_report(LOG_INFO, "network: token expires_in: %d -> valid until %ld\n", exp, g_access_token_valid_until);
}
} else if (!strcmp(key, "scope")) {
......@@ -479,6 +482,7 @@ int netatmo_get_devices()
if (g_access_token) {
free(g_access_token);
g_access_token = NULL;
g_access_token_valid_until = 0;
}
}
} else if (!strcmp(key, "message")) {
......@@ -608,6 +612,12 @@ int netatmo_get_devices()
}
}
json_object_put(jobj);
free(response->memory);
free(response);
if (g_access_token == NULL) {
return NETATMO_TOKEN_EXPIRED;
}
if ((NULL == netatmo.base.bssid) || (strlen(netatmo.base.bssid) == 0)) {
if (netatmo.base.modules[0].id) {
......@@ -663,9 +673,6 @@ int netatmo_get_devices()
}
pthread_mutex_unlock(&g_network_mutex);
free(response->memory);
free(response);
return newDevices;
}
......@@ -843,8 +850,23 @@ netatmo_get_values(netatmo_module_t* mod)
json_object *jobj2 = val;
json_object_object_foreach(jobj2, key, val)
{
enum json_type type = json_object_get_type(val);
vdc_report(LOG_ERR, "network: getmeasure returned error: key %s, type %d\n", key, type);
enum json_type type = val ? json_object_get_type(val) : -1;
if (!strcmp(key, "code")) {
int err = json_object_get_int(val);
if (err == 3) {
// access token expired
vdc_report(LOG_WARNING, "network: getvalues access failed, token expired!\n");
if (g_access_token) {
free(g_access_token);
g_access_token = NULL;
g_access_token_valid_until = 0;
}
}
} else if (!strcmp(key, "message")) {
vdc_report(LOG_ERR, "network: getvalues response error: %s\n", json_object_get_string(val));
} else {
vdc_report(LOG_ERR, "network: getvalues unknown error: key %s, type %d\n", key, type);
}
}
}
}
......@@ -854,6 +876,10 @@ netatmo_get_values(netatmo_module_t* mod)
free(response);
}
if (g_access_token == NULL) {
return NETATMO_TOKEN_EXPIRED;
}
pthread_mutex_lock(&g_network_mutex);
netatmo_vdcd_t* dev;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment