forked from johnmckerrell/WeasleyClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapMe_At.cpp
104 lines (91 loc) · 2.27 KB
/
MapMe_At.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "MapMe_At.h"
#include <string.h>
#define DEBUG 1
// Request current location
// Check if we're still getting a location
// Get currently stored location
void MapMe_At::setup(const char *_username) {
client = NULL;
error = 0;
strcpy( username, _username);
location[0] = '\0';
}
boolean MapMe_At::wasError() {
return error;
}
void MapMe_At::loop() {
if( client ) {
if( client->available() ) {
char c = client->read();
Serial.print(c);
jsonParser.handleInput(c);
if( jsonParser.getParseState() == JSON_PARSE_HAVEVAL ) {
char *key = jsonParser.getKey();
if( strcmp("error", key) == 0 ) {
error = MAPME_AT_ERR_RESULT;
}
else if( strcmp("label", key) == 0 ) {
strcpy( location, jsonParser.getVal() );
}
/*
Serial.print( key );
Serial.print( "\t = \"" );
Serial.print( jsonParser.getVal() );
Serial.println( "\"" );
*/
}
}
if( error || ! client->connected() ) {
#ifdef DEBUG
if( error ) {
Serial.print("error=");
Serial.println(error);
}
Serial.println("disconnecting");
#endif
client->stop();
client = NULL;
if( jsonParser.getParseState() == JSON_PARSE_FINDJSON )
error = MAPME_AT_ERR_RESULT;
}
}
}
// Crude cachebusting to get round the Maker Faire proxy
int gRequestCount = 0;
void MapMe_At::requestLocation() {
byte server[] = {
188, 40, 54, 143 }; // mapme.at
client = new Client;
#ifdef DEBUG
Serial.println("connecting...");
#endif
if( client->connect("mapme.at", 80) ) {
jsonParser.clearState();
location[0] = '\0';
#ifdef DEBUG
Serial.println("connected");
#endif
client->print("GET /api/where.json?username=");
client->print(username);
// Crude cachebusting to get round makerfaire proxy
client->print("&mfuk=");
client->print(gRequestCount++);
client->println(" HTTP/1.0");
client->print("Host: mapme.at");
client->println();
client->println();
}
else {
client = NULL;
error = MAPME_AT_ERR_CONNECTION;
#ifdef DEBUG
Serial.println("Connection failed");
#endif
}
}
boolean MapMe_At::isActive() {
return ( client ? true : false );
}
char* MapMe_At::getLocation() {
return location;
}