-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExosite.cpp
225 lines (197 loc) · 6.37 KB
/
Exosite.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//*****************************************************************************
//
// exosite.cpp - Prototypes for the Exosite Cloud API
//
// Copyright (c) 2013 Exosite LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Exosite LLC nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************
#include <DNETcK.h>
#include <DWIFIcK.h>
#include "Exosite.h"
#define serverName "m2.exosite.com"
#define DEBUG_PRINT
/*==============================================================================
* Exosite
*
* constructor for Exosite class
*=============================================================================*/
Exosite::Exosite(void)
{
}
/*==============================================================================
* init
*
* initialization function for Exosite class.
*=============================================================================*/
void Exosite::init(TcpClient *clt, String _cik)
{
client = clt;
cik = _cik;
Serial.println("Exosite init");
}
/*==============================================================================
* sendToCloud
*
* send data to cloud
*=============================================================================*/
int Exosite::sendToCloud(String res, int value)
{
ret = 0;
stringPos = 0;
rxlength = 0;
DataRx = false;
RxLoop = true;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
myDataString = ""; //allocate for actual data sent
if (client->isConnected()) {
myDataString += res;
myDataString += "="; //put into resource
myDataString += value; //just send the value
// Send request using Exosite basic HTTP API
client->println("POST /api:v1/stack/alias HTTP/1.1");
client->println("Host: m2.exosite.com");
client->print("X-Exosite-CIK: ");
client->println(cik);
client->println("Content-Type: application/x-www-form-urlencoded; charset=utf-8");
client->print("Content-Length: ");
client->println(myDataString.length()); //calculate length
client->println();
client->println(myDataString);
// Read response
timeout_time = millis()+ timeout;
while ((timeout_time > time_now) && RxLoop)
{
rxlength = client->available();
if (rxlength > 0)
{
if (!DataRx)
DataRx= true;
rxlength = rxlength < sizeof(rxdata) ? rxlength : sizeof(rxdata);
if (rxlength < sizeof(rxdata))
RxLoop = false;
else
rxlength = sizeof(rxdata);
rxlength = client->readStream((byte*)rxdata, rxlength);
#if defined(DEBUG_PRINT)
for(stringPos = 0; stringPos < rxlength; stringPos++)
{
Serial.print(rxdata[stringPos], BYTE);
}
#endif
}
else
{
rxdata[stringPos] = 0;
if (DataRx)
{
DataRx = false;
RxLoop = false;
ret=1;
}
}//else
time_now = millis();
}// while ((timeout_time > time_now) && RxLoop) {
client->close();
}
return ret;
}
/*==============================================================================
* readFromCloud
*
* read data from cloud
*=============================================================================*/
int Exosite::readFromCloud(String res ,String* pResult)
{
ret = 0;
stringPos = 0;
DataRx= false;
RxLoop = true;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
myDataString = ""; //allocate for actual data sent
if (client->isConnected())
{ // Send request using Exosite basic HTTP API
client->print("GET /onep:v1/stack/alias?");
client->print(res);
client->println(" HTTP/1.1");
client->println("Host: m2.exosite.com");
client->print("X-Exosite-CIK: ");
client->println(cik);
client->println("Accept: application/x-www-form-urlencoded; charset=utf-8");
client->println();
// Read from the nic or the IC buffer overflows with no warning and goes out to lunch
timeout_time = millis()+ timeout;
while ((timeout_time > time_now) && RxLoop)
{
rxlength = client->available();
if (rxlength > 0)
{
if (!DataRx)
DataRx= true;
rxlength = rxlength < sizeof(rxdata) ? rxlength : sizeof(rxdata);
if (rxlength < sizeof(rxdata))
RxLoop = false;
else
rxlength = sizeof(rxdata);
rxlength = client->readStream((byte*)rxdata, rxlength);
#if defined(DEBUG_PRINT)
for(stringPos = 0; stringPos < rxlength; stringPos++)
{
Serial.print(rxdata[stringPos], BYTE);
}
#endif
}
else
{
rxdata[stringPos] = 0;
}
if (DataRx)
{
DataRx = false;
RxLoop = false;
String rxstrg = String(rxdata);
int length = 0;
int rxresultpos = 0;
int subStringLength = 0;
if (rxstrg.startsWith("HTTP/1.1 200 OK"))
{
length = rxstrg.length();
rxresultpos=rxstrg.indexOf('=');
subStringLength = length - rxresultpos;
*pResult= String(rxstrg.substring(rxresultpos+1));
}
else
{
rxresultpos=rxstrg.indexOf('\n');
subStringLength = rxresultpos;
*pResult= String(rxstrg.substring(0,rxresultpos));
}
ret=1;
}
time_now = millis();
} // end of while
client->close();
}
return ret;
}