private DataSet getCommands(int intEhiuSiteConfigID, ILambdaContext context, ref SqlConnection oSqlConnection)
        {
            string         strQuery = "";
            SqlDataAdapter daCheck  = new SqlDataAdapter();
            DataSet        dsCheck  = new DataSet();
            tResult        oResult  = new tResult();

            try
            {
                context.Logger.LogLine("getCommands 1 " + intEhiuSiteConfigID.ToString());

                strQuery = "SELECT EhiuSiteConfigDetail.EhiuSiteConfigID, EhiuSiteConfigDetail.CommandName, EhiuSiteConfigDetail.ToEco, EhiuSiteConfigDetail.JsonConfigItem, " +
                           " ISNULL(EhiuSiteConfigDetail.CompanionName, '') AS CompanionName, EhiuSiteConfigDetail.UrlPath, " +
                           " ISNULL(EhiuSiteConfigDetail.JsonCompanionItem, '') AS JsonCompanionItem, ISNULL(EhiuSiteConfigDetail.MbusID, - 1) AS MbusID, EhiuSiteConfig.Name " +
                           " FROM EhiuSiteConfigDetail INNER JOIN " +
                           " EhiuSiteConfig ON EhiuSiteConfigDetail.EhiuSiteConfigID = EhiuSiteConfig.EhiuSiteConfigID " +
                           " WHERE (EhiuSiteConfigDetail.EhiuSiteConfigID = @EhiuSiteConfigID)";

                daCheck = new SqlDataAdapter(strQuery, oSqlConnection);

                SqlParameter sqlParamEhiuSiteConfigID = new SqlParameter("@EhiuSiteConfigID", SqlDbType.Int);
                sqlParamEhiuSiteConfigID.Value = intEhiuSiteConfigID;
                daCheck.SelectCommand.Parameters.Add(sqlParamEhiuSiteConfigID);

                daCheck.Fill(dsCheck);
            }
            catch (Exception ex)
            {
                context.Logger.LogLine("getCommands ex " + ex.Message);
            }
            return(dsCheck);
        }
        private int getExistingEcoInstallThisSerialNumber(string strSerialNumber, int intPropertyID, ILambdaContext context, ref SqlConnection oSqlConnection)
        {
            string         strQuery = "";
            SqlDataAdapter daCheck  = new SqlDataAdapter();
            DataSet        dsCheck  = new DataSet();
            tResult        oResult  = new tResult();
            int            intIdx;
            int            intReturn = -1;

            try
            {
                context.Logger.LogLine("getExistingEcoInstallThisProperty 1 " + strSerialNumber.ToString());

                strQuery = "SELECT TOP (1) EhiuInstallID " +
                           " FROM EhiuInstall  " +
                           " WHERE (PropertyID <> @PropertyID) AND  " +
                           " ToDate Is Null And  " +
                           " SerialNumber = @SerialNumber";

                daCheck = new SqlDataAdapter(strQuery, oSqlConnection);

                SqlParameter sqlParamPropertyID = new SqlParameter("@PropertyID", SqlDbType.Int);
                sqlParamPropertyID.Value = intPropertyID;
                daCheck.SelectCommand.Parameters.Add(sqlParamPropertyID);

                SqlParameter sqlParamSerialNumber = new SqlParameter("@SerialNumber", SqlDbType.NVarChar);
                sqlParamSerialNumber.Value = strSerialNumber;
                daCheck.SelectCommand.Parameters.Add(sqlParamSerialNumber);

                daCheck.Fill(dsCheck);


                for (intIdx = 0; intIdx <= dsCheck.Tables[0].Rows.Count - 1; intIdx++)
                {
                    intReturn = (int)dsCheck.Tables[0].Rows[intIdx]["EhiuInstallID"];
                }
            }
            catch (Exception)
            {
            }
            return(intReturn);
        }
        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);
        }