Exemplo n.º 1
0
        public static int LogDoorCommand
            (DoorCommands priorCommand, int startStatus, DoorCommands command, int endStatus, long commandDuration)
        {
            int rowsInserted = 0;

            if (!EnvironmentVariables.SqlLogging)
            {
                return(0);
            }

            string sql = "";

            using (SqlConnection conn = new SqlConnection(EnvironmentVariables.ConnString))
                using (SqlCommand sqlCommand = new SqlCommand())
                {
                    conn.Open();
                    sqlCommand.Connection = conn;
                    sql =
                        $"INSERT INTO LogCommands VALUES(" +
                        $"'{DateTime.Now}', {(int)priorCommand}, '{((DoorCommands)priorCommand).ToString()}'," +
                        $"{startStatus}, '{((StatusBits)startStatus).ToString()}'," +
                        $"{(int)command}, '{(command).ToString()}', {commandDuration}, " +
                        $"{endStatus}, '{((StatusBits)endStatus).ToString()}')";
                    sqlCommand.CommandText = sql;
                    Debug.WriteLine(sql);
                    rowsInserted = sqlCommand.ExecuteNonQuery();
                }
            return(rowsInserted);
        }
Exemplo n.º 2
0
        public void DoorCommand(DoorCommands operation, int startStatus, DoorCommands lastCommand)
        {
            Stopwatch logSw = new Stopwatch();

            logSw.Start();

            try
            {
                WebRequest request = WebRequest.Create(EnvironmentVariables.ParticleUrl + "/DoorCommand");
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                if (!EnvironmentVariables.IsLocal)
                {
                    request.Proxy = new WebProxy("ntproxyus.lxa.perfora.net", 3128);
                }

                string paramString = $"params={operation}&access_token={EnvironmentVariables.AccessToken}";
                byte[] paramBytes  = Encoding.UTF8.GetBytes(paramString);
                request.ContentLength = paramBytes.Length;

                JProperty return_value;
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(paramBytes, 0, paramBytes.Length);  //...but response is not accepted till this executes
                    WebResponse response = request.GetResponse();
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        StreamReader sr      = new StreamReader(responseStream);
                        var          results = sr.ReadToEnd();
                        JObject      jObj    = JObject.Parse(results);
                        return_value = jObj.Properties().FirstOrDefault(p => p.Name == "return_value");
                    }
                }

                logSw.Stop();
                Log.LogDoorCommand(lastCommand, startStatus, operation, (int)return_value.Value, logSw.ElapsedMilliseconds);

                HttpContext.Current.Response.Write($"{{{return_value}}}");
            }
            catch (Exception ex)
            {
                logSw.Stop();
                Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current));
                // send junk back to the client
                var msg = $"DoorCommand Exception: {ex.Message} {ex.StackTrace}";
                HttpContext.Current.Response.Write($"{msg}");
            }
        }