#region Control if we still have enough free ram (memory leak in https) and evtl. prepare for resetting the mainboard
uint remainingRam = Debug.GC(false); // Get remaining Ram because of the memory leak in https
bool willReboot = (remainingRam < freeRamThreshold); // If the ram is below this value, the Mainboard will reboot
if (willReboot)
{
forceSend = true;
switchMessage = "Going to reboot the Mainboard due to not enough free RAM";
}
#endregion
...... do your writing to the cloud...
#region Prepare rebooting of the mainboard e.g. if not enough ram due to memory leak
if (_willRebootAfterNextEvent)
{
Microsoft.SPOT.Hardware.PowerState.RebootDevice(true, 3000);
}
if (willReboot)
{ _willRebootAfterNextEvent = true; }
#endregion
Ok I will think about it later. Now Iām worried about process time of GetRequestStream() method - it takes about 1 sec! Is it normal?
Here is the code:
int poczatek = Environment.TickCount;
/* Data to be sent */
String temp = "{\r\n";
temp += " \"stream_id\": \"" + streamId + "\",\r\n";
temp += " \"stream_type\": \"STRING\",\r\n";
temp += " \"value\": \"" + streamValue + "\"\r\n";
temp += "}\r\n";
byte[] data = UTF8Encoding.UTF8.GetBytes(temp);
/* Request URL */
Uri url = new Uri("https://devicecloud.digi.com/ws/v1/streams/history");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
Debug.Print("1: " + (Environment.TickCount - poczatek).ToString() + " ms");
/* Request Method */
request.Method = "POST";
/* Request Credentials */
request.Credentials = new NetworkCredential(dcUsername, dcPassword, AuthenticationType.Basic);
/* Request other settings */
request.KeepAlive = false;
request.ContentLength = data.Length;
request.ContentType = "application/json";
request.Accept = "application/json";
Debug.Print("2: " + (Environment.TickCount - poczatek).ToString() + " ms");
/* Prepare Stream */
Stream dataStream;
Debug.Print("2.5: " + (Environment.TickCount - poczatek).ToString() + " ms");
dataStream = request.GetRequestStream();
Debug.Print("3: " + (Environment.TickCount - poczatek).ToString() + " ms");
/* Write data to Stream and close */
dataStream.Write(data, 0, data.Length);
dataStream.Close();
Debug.Print("4: " + (Environment.TickCount - poczatek).ToString() + " ms");
/* Get response */
WebResponse response = request.GetResponse();
//Debug.Print("Response status: " + ((HttpWebResponse)response).StatusCode);
Stream respData = response.GetResponseStream();
StreamReader reader = new StreamReader(respData);
Debug.Print("5: " + (Environment.TickCount - poczatek).ToString() + " ms");
/* Read response */
char[] read = new char[256];
int count = reader.Read(read, 0, 256);
while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
String str = new String(read, 0, count);
//Debug.Print(str);
count = reader.Read(read, 0, 256);
}
Debug.Print("6: " + (Environment.TickCount - poczatek).ToString() + " ms");
/* Clean up the streams */
reader.Close();
dataStream.Close();
response.Close();
request.Dispose();
Debug.Print("7: " + (Environment.TickCount - poczatek).ToString() + " ms");
And here the result of 2 iterations:
1: 9 ms
2: 18 ms
2.5: 19 ms
3: 1521 ms
4: 1528 ms
5: 1908 ms
6: 1925 ms
7: 1942 ms
1: 9 ms
2: 18 ms
2.5: 19 ms
3: 985 ms
4: 992 ms
5: 1367 ms
6: 1384 ms
7: 1401 ms