private tCommandReply PostApi(tCommandSend oCommandSend, string strSerialNo) { HttpWebRequest request; HttpWebResponse response = null /* TODO Change to default(_) if this is not a reference type */; StreamReader reader; Uri address; string strResponse = ""; Stream postStream = null; string strUrl; byte[] byteData; StringBuilder data; tCommandReply oCommandReply = new tCommandReply(); oCommandReply.PostStatus = "offline"; strUrl = strApiUrl; strUrl = strUrl + oCommandSend.UrlPath; strUrl = strUrl.Replace("{devid}", strSerialNo); strUrl = strUrl.Replace("{pathelement}", oCommandSend.PathElement); address = new Uri(strUrl); try { // The reply is mainly the same as the send, for easier processing by the client, ie send and reply together oCommandReply.CommandJson = oCommandSend.CommandJson; oCommandReply.CommandName = oCommandSend.CommandName; oCommandReply.CompanionJson = oCommandSend.CompanionJson; oCommandReply.CompanionName = oCommandSend.CompanionName; oCommandReply.MbusID = oCommandSend.MbusID; oCommandReply.ToEco = oCommandSend.ToEco; request = (HttpWebRequest)WebRequest.Create(address); if (oCommandSend.ToEco == true) { request.Method = "PATCH"; } else { request.Method = "GET"; } request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add("x-api-key", strApiKey); if (oCommandSend.ToEco == true) { data = new StringBuilder(); data.Append(oCommandSend.CommandJson); byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); // Set the content length in the request headers request.ContentLength = byteData.Length; // Write data try { postStream = request.GetRequestStream(); postStream.Write(byteData, 0, byteData.Length); } finally { if (postStream != null) { postStream.Close(); } } } else { if (postStream != null) { postStream.Close(); } } try { // Get response response = (HttpWebResponse)request.GetResponse(); // Get the response stream into a reader reader = new StreamReader(response.GetResponseStream()); strResponse = reader.ReadToEnd(); if (response.StatusCode == HttpStatusCode.OK) { oCommandReply.PostStatus = "ok"; oCommandReply.ReplyJson = strResponse; return(oCommandReply); } else { } } catch (Exception ex) { oCommandReply.PostStatusDetail = ex.Message; } finally { if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { oCommandReply.PostStatus = "ok"; } if (response.StatusCode.ToString().IndexOf("504") != -1) { oCommandReply.PostStatus = "timeout"; } response.Close(); } else { oCommandReply.PostStatus = "offline"; } } } catch (Exception ex) { } return(oCommandReply); }
public tResult FunctionHandler(tInput oInput, ILambdaContext context) { DataSet dsCommands; SqlConnection oSqlConnection = null; int intIdx; tResult oResult = new tResult(); int intEhiuInstallID; int intEhiuInstallDetailID; string strConfigName; int intMbusID = -1; string strIMEI; // Get profile commands // Call ehiu api to send // register property/history try { oSqlConnection = new SqlConnection(ecoCommon.GetSecret("CloudEcoPlus", context)); oSqlConnection.Open(); context.Logger.LogLine("FunctionHandler 2"); } catch (Exception ex) { context.Logger.LogLine("WriteRecord Ex 1" + ex.Message); } try { oSqlConnection.Open(); } catch (Exception) { } try { strIMEI = ecoCommon.GetDeviceIMEINumber(oInput.SerialNumber, context, ref oSqlConnection); if (strIMEI == "") { context.Logger.LogLine("Not recognised serial number " + oInput.SerialNumber); oResult.Ok = false; oResult.Info = "Not recognised serial number"; return(oResult); } ; dsCommands = getCommands(oInput.EhiuSiteConfigID, context, ref oSqlConnection); strConfigName = (string)dsCommands.Tables[0].Rows[0]["Name"]; intEhiuInstallID = getExistingEcoInstallThisSerialNumber(oInput.SerialNumber, oInput.PropertyID, context, ref oSqlConnection); if (intEhiuInstallID != -1) { // This unit as already installed in another property, cant procede oResult.Ok = false; oResult.Info = "This Eco+ is already installed in another property"; return(oResult); } //intEhiuInstallID = getExistingEcoInstallThisProperty(oInput.SerialNumber, oInput.PropertyID, context, ref oSqlConnection); //if (intEhiuInstallID == -1) //{ // intEhiuInstallID = RegisterUnit(oInput.PropertyID, oInput.SerialNumber, oInput.EhiuSiteConfigID, strConfigName, oInput.UserName, context, ref oSqlConnection); // Add //}; //intEhiuInstallDetailID = RegisterConfigApplication(intEhiuInstallID, oInput.EhiuSiteConfigID, DateTime.Now, oInput.UserName, context, ref oSqlConnection); for (intIdx = 0; intIdx <= dsCommands.Tables[0].Rows.Count - 1; intIdx++) { tCommandSend oCommandSend = new tCommandSend(); oCommandSend.CommandJson = (string)dsCommands.Tables[0].Rows[intIdx]["JsonConfigItem"]; oCommandSend.CommandName = (string)dsCommands.Tables[0].Rows[intIdx]["CommandName"]; if ((string)dsCommands.Tables[0].Rows[intIdx]["JsonCompanionItem"] != "") { oCommandSend.CompanionJson = (string)dsCommands.Tables[0].Rows[intIdx]["JsonCompanionItem"]; } ; if ((string)dsCommands.Tables[0].Rows[intIdx]["CompanionName"] != "") { oCommandSend.CompanionName = (string)dsCommands.Tables[0].Rows[intIdx]["CompanionName"]; } ; oCommandSend.MbusID = (int)dsCommands.Tables[0].Rows[intIdx]["MbusID"]; oCommandSend.ToEco = (bool)dsCommands.Tables[0].Rows[intIdx]["ToEco"]; oCommandSend.UrlPath = (string)dsCommands.Tables[0].Rows[intIdx]["UrlPath"]; oCommandSend.PathElement = ""; if (oCommandSend.MbusID != -1) { intMbusID = oCommandSend.MbusID; oCommandSend.PathElement = intMbusID.ToString(); } tCommandReply oCommandReply; oCommandReply = PostApi(oCommandSend, strIMEI); if (oCommandReply.PostStatus != "ok") { oResult.Ok = false; oResult.Info = oCommandReply.PostStatus; if (oCommandReply.PostStatusDetail != null) { oResult.InfoDetail = oCommandReply.PostStatusDetail; } break; } else { intEhiuInstallID = getExistingEcoInstallThisProperty(oInput.SerialNumber, oInput.PropertyID, context, ref oSqlConnection); if (intEhiuInstallID == -1) { intEhiuInstallID = RegisterUnit(oInput.PropertyID, oInput.SerialNumber, oInput.EhiuSiteConfigID, strConfigName, oInput.UserName, context, ref oSqlConnection); // Add } ; intEhiuInstallDetailID = RegisterConfigApplication(intEhiuInstallID, oInput.EhiuSiteConfigID, DateTime.Now, oInput.UserName, context, ref oSqlConnection); WriteRegisterEcoDetailCommands(intEhiuInstallDetailID, oCommandSend.CompanionName, oCommandSend.CompanionJson, oCommandSend.CommandName, oCommandSend.CommandJson, oCommandReply.ReplyJson, oInput.UserName, oCommandSend.MbusID, context, ref oSqlConnection); } } ; } catch (Exception ex) { oResult.Ok = false; oResult.Info = "Internal error"; context.Logger.LogLine("WriteRecord Ex 2" + ex.Message); } return(oResult); }