Ejemplo n.º 1
0
        public override void OnException(ExceptionContext filterContext)
        {
            MyLog.Error("WebAjax异常处理过滤器", "WebAjax", "ERROR", filterContext.Exception);
            var resp = new WebResp("ERROR", "操作异常");

            //篡改Response
            filterContext.HttpContext.Response.Clear();
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            filterContext.Result = new JsonResult()
            {
                Data = resp,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                ContentType         = "application/json",
            };
        }
Ejemplo n.º 2
0
        public static void GetSensorBathRoom(out double temperature, out int luminiscence, out int humidity, out bool movement)
        {
            HttpWebRequest  WebReq;
            HttpWebResponse WebResp;

            Debug.GC(true);

            temperature  = 0;
            luminiscence = 0;
            humidity     = 0;
            movement     = false;
            try
            {
                var request = System.Net.WebRequest.Create("http://192.168.2.25:8083/ZAutomation/api/v1/devices/") as System.Net.HttpWebRequest;
                request.KeepAlive = true;

                request.Method = "PUT";

                request.ContentType = "application/json";
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes("{\"id\":\"ZWayVDev_20:0:49:1\",\"deviceType\":\"probe\",\"metrics\":{\"probeTitle\":\"Temperature\",\"scaleTitle\":\"°C\",\"level\":7.599999904632568,\"title\":\"Temperature Sensor\",\"iconBase\":\"zwave\"},\"tags\":[],\"location\":null,\"updateTime\":1387882443}");
                request.ContentLength = byteArray.Length;
                using (var writer = request.GetRequestStream()) { writer.Write(byteArray, 0, byteArray.Length); }

                string responseContent = null;
                using (var response = request.GetResponse() as System.Net.HttpWebResponse)
                {
                    using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        responseContent = reader.ReadToEnd();
                    }
                }

                WebReq                  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZAutomation/api/v1/devices/ZWayVDev_zway_5-0-48-1/command/update");
                WebReq.KeepAlive        = true;
                WebReq.Method           = "GET";
                WebReq.Timeout          = 1000;
                WebReq.ReadWriteTimeout = 1000;
                WebResp                 = (HttpWebResponse)WebReq.GetResponse();
                WebResp.Close();
                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZAutomation/api/v1/devices/ZWayVDev_zway_5-0-49-1/command/update");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                WebResp.Close();
                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZAutomation/api/v1/devices/ZWayVDev_zway_5-0-49-3/command/update");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                WebResp.Close();
                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZAutomation/api/v1/devices/ZWayVDev_zway_5-0-49-5/command/update");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                WebResp.Close();

                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZWaveAPI/Run/devices[5].instances[0].commandClasses[0x31].data[0x01].val.value");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                using (var reader = new StreamReader(WebResp.GetResponseStream()))
                {
                    double result2 = double.Parse(reader.ReadToEnd().ToString()) * 10;
                    int    result1 = (int)result2;
                    double result  = (double)result1 / 10;
                    Logging.LogMessageToFile("Main - Temperature = [" + result.ToString() + "]", "SENSOR");
                    temperature = result;
                    reader.Close();
                }
                WebResp.Close();
                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZWaveAPI/Run/devices[5].instances[0].commandClasses[0x31].data[0x03].val.value");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                using (var reader = new StreamReader(WebResp.GetResponseStream()))
                {
                    int result = Int32.Parse(reader.ReadToEnd().ToString()); // do something fun...
                    Logging.LogMessageToFile("Main - Luminiscence = [" + result.ToString() + "]", "SENSOR");
                    luminiscence = result;
                    reader.Close();
                }
                WebResp.Close();
                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZWaveAPI/Run/devices[5].instances[0].commandClasses[0x31].data[0x05].val.value");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                using (var reader = new StreamReader(WebResp.GetResponseStream()))
                {
                    int result = Int32.Parse(reader.ReadToEnd().ToString()); // do something fun...
                    Logging.LogMessageToFile("Main - Humidity = [" + result.ToString() + "]", "SENSOR");
                    humidity = result;
                    reader.Close();
                }
                WebResp.Close();

                WebReq  = (HttpWebRequest)WebRequest.Create("http://192.168.2.25:8083/ZWaveAPI/Run/devices[5].instances[0].commandClasses[0x30].data[1].level.value");
                WebResp = (HttpWebResponse)WebReq.GetResponse();
                using (var reader = new StreamReader(WebResp.GetResponseStream()))
                {
                    if (reader.ReadToEnd().ToString().ToUpper() == "FALSE")
                    {
                        Logging.LogMessageToFile("Main - Movement = [FALSE]", "SENSOR");
                        movement = false;
                    }
                    else
                    {
                        Logging.LogMessageToFile("Main - Movement = [TRUE]", "SENSOR");
                        movement = true;
                    }
                    reader.Close();
                }
                WebResp.Close();
                Debug.Print(DateTime.Now + "," + temperature.ToString().Substring(0, 4) + "," + humidity.ToString() + "," + luminiscence.ToString() + "," + movement.ToString());
            }
            catch (Exception e)
            {
                Debug.Print("*********" + e.Message);
            }
        }
Ejemplo n.º 3
0
    private void start_get()
    {
        Movie movies;
        //CHANGE PATH
        string         text = File.ReadAllText(@"C:\Users\Gaurav And Jai\Desktop\1.3\IOT\imdb2\movies.txt");
        HttpWebRequest WebReq;

        HttpWebResponse WebResp;

        string jsonString;

        /*using (Stream stream = WebResp.GetResponseStream())   //modified from your code since the using statement disposes the stream automatically when done
         * {
         *  StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
         *  jsonString = reader.ReadToEnd();
         * }*/

        List <Item> items = JsonConvert.DeserializeObject <List <Item> >(text);

        string[] result;

        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Gaurav And Jai\Desktop\1.3\IOT\imdb2\users.accdb'";


        for (int i = 0; i < items.Count; i++)
        {
            result = items[i].url.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

            WebReq        = (HttpWebRequest)WebRequest.Create(string.Format("http://www.omdbapi.com/?apikey=84739f5e&i=" + result[1]));
            WebReq.Method = "GET";
            WebResp       = (HttpWebResponse)WebReq.GetResponse();

            using (Stream stream = WebResp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                jsonString = reader.ReadToEnd();
            }

            movies = JsonConvert.DeserializeObject <Movie>(jsonString);

            Label1.Text = "Adding " + i;

            OleDbCommand cmdd = new OleDbCommand("INSERT into top100(imdbId,title,plot,poster,release,rating,pos) Values(@id,@name,@plot,@poster,@date,@rating,@pos)");

            cmdd.Parameters.Add("@Id", OleDbType.VarChar).Value     = movies.imdbID;
            cmdd.Parameters.Add("@name", OleDbType.VarChar).Value   = movies.Title;
            cmdd.Parameters.Add("@rating", OleDbType.VarChar).Value = movies.imdbRating;
            //cmdd.Parameters.AddWithValue("@rated", movies.Rated);
            cmdd.Parameters.Add("@date", OleDbType.VarChar).Value = movies.Released;
            //cmdd.Parameters.AddWithValue("@genre",  movies.Genre);
            cmdd.Parameters.Add("@poster", OleDbType.VarChar).Value = movies.Poster;
            cmdd.Parameters.Add("@plot", OleDbType.VarChar).Value   = movies.Plot;
            cmdd.Parameters.Add("@pos", OleDbType.VarChar).Value    = items[i].position;

            /* cmdd.Parameters.AddWithValue("@director", movies.Director);
             * cmdd.Parameters.AddWithValue("@writer", movies.Writer);
             * cmdd.Parameters.AddWithValue("@actors", movies.Actors);*/

            cmdd.Connection = conn;
            try
            {
                conn.Open();
                cmdd.ExecuteNonQuery();
                Label1.Text = "Data Added";
                conn.Close();
                //Response.Redirect("login.aspx");
            }

            catch (OleDbException ex)
            {
                Label1.Text = ex.Message;
                conn.Close();
            }

            finally
            {
                conn.Close();
            }
        }
    }