// ====================================================================================== // Implementation of Cumulocity Device Cloud // ====================================================================================== // Cumulocity Handshake - Request - Response public HandshakeResponse[] DeviceCloudHandshake(HandshakeRequest handshakeRequest, string service_url) { HandshakeResponse[] handshakeResp = new HandshakeResponse[0]; try { HttpWebRequest request = deviceCloudHttpRequest(service_url); DataContractJsonSerializer ser = new DataContractJsonSerializer(handshakeRequest.GetType()); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, handshakeRequest); String json = Encoding.UTF8.GetString(ms.ToArray()); StreamWriter writer = new StreamWriter(request.GetRequestStream()); ms.Close(); writer.Write(json); writer.Close(); using (var httpwebResponse = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(httpwebResponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); string responseJsonText = reader.ReadToEnd(); handshakeResp = js.Deserialize <HandshakeResponse[]>(responseJsonText); // ========================== ServiceEventSource.Current.ServiceMessage(this, "Cumulocity handshake established - {0}", handshakeResp[0].successful); // ========================== reader.Close(); } } request.Abort(); } catch (Exception e) { ServiceEventSource.Current.ServiceMessage(this, "Exception caught - {0} - unknown error during Handshake request", System.Convert.ToString(e)); //System.Diagnostics.Trace.WriteLine("Exception caught" + System.Convert.ToString(e), "Error"); handshakeResp[0].successful = false; handshakeResp[0].error = "Exeption - unknown error during Handshake request"; } return(handshakeResp); }
// ====================================================================================== // Implementation of Cumulocity Device Cloud // ====================================================================================== // Cumulocity Handshake - Request - Response public HandshakeResponse[] DeviceCloudHandshake(HandshakeRequest handshakeRequest, string service_url) { HandshakeResponse[] handshakeResp = new HandshakeResponse[0]; try { HttpWebRequest request = deviceCloudHttpRequest(service_url); DataContractJsonSerializer ser = new DataContractJsonSerializer(handshakeRequest.GetType()); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, handshakeRequest); String json = Encoding.UTF8.GetString(ms.ToArray()); StreamWriter writer = new StreamWriter(request.GetRequestStream()); ms.Close(); writer.Write(json); writer.Close(); using (var httpwebResponse = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(httpwebResponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); string responseJsonText = reader.ReadToEnd(); handshakeResp = js.Deserialize<HandshakeResponse[]>(responseJsonText); // ========================== ServiceEventSource.Current.ServiceMessage(this, "Cumulocity handshake established - {0}", handshakeResp[0].successful); // ========================== reader.Close(); } } request.Abort(); } catch (Exception e) { ServiceEventSource.Current.ServiceMessage(this, "Exception caught - {0} - unknown error during Handshake request", System.Convert.ToString(e)); //System.Diagnostics.Trace.WriteLine("Exception caught" + System.Convert.ToString(e), "Error"); handshakeResp[0].successful = false; handshakeResp[0].error = "Exeption - unknown error during Handshake request"; } return handshakeResp; }