//Dispatch requests
        public void post(string serverUrl, string requestUrl, string body)
        {
            httpsClient.HostVerification = false;
            httpsClient.PeerVerification = false;
            HttpsClientRequest request = new HttpsClientRequest();

            request.KeepAlive          = false;
            request.Header.ContentType = "text/plain";
            request.RequestType        = Crestron.SimplSharp.Net.Https.RequestType.Post;
            request.ContentSource      = ContentSource.ContentString;
            request.ContentString      = body;
            String requestString = serverUrl + requestUrl;

            request.Url.Parse(requestString);
            try
            {
                if (debug > 0)
                {
                    CrestronConsole.PrintLine("\nhttp trying get request: " + requestString);
                }
                httpsClient.DispatchAsync(request, OnHttpsClientResponseCallback);
            }
            catch (Exception exception)
            {
                CrestronConsole.PrintLine("\nhttps exception with requestString: {0} and exception: {1}", requestString, exception.Message);
            }
        }
Exemple #2
0
        private void checkPushes()
        {
            debug("checking for new data since " + lastModified);
            HttpsClient cli = new HttpsClient();

            cli.KeepAlive = false;
            //cli.Verbose = true;

            HttpsClientRequest req = new HttpsClientRequest();

            req.Url.Parse(api + "/pushes?modified_after=" + lastModified);
            req.Header.AddHeader(new HttpsHeader("Access-Token", apiToken));
            cli.DispatchAsync(req, (resp, e) => {
                try {
                    if (resp.Code != 200)
                    {
                        debug("Bad API Token? " + resp.Code + ": " + resp.ContentString);
                        return;
                    }
                    PushesData ps = JsonConvert.DeserializeObject <PushesData>(resp.ContentString);
                    foreach (PushData p in ps.pushes)
                    {
                        handlePush(p);
                        if (p.modified > lastModified)
                        {
                            lastModified = p.modified;
                        }
                    }
                    debug("lastModified is now " + lastModified);
                } catch (Exception e2) {
                    debug("GET failed: " + e2.Message);
                }
            });
        }
Exemple #3
0
        public void Doorbell()
        {
            debug("PBServer.Doorbell");

            HttpsClient cli = new HttpsClient();

            cli.KeepAlive = false;
            //cli.Verbose = true;

            HttpsClientRequest req = new HttpsClientRequest();

            req.Url.Parse(api + "/pushes");
            req.Header.AddHeader(new HttpsHeader("Access-Token", apiToken));
            req.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Post;
            req.Header.AddHeader(new HttpsHeader("Content-Type", "application/json"));
            PushData p = new PushData("Crestron Doorbell!", "Reply Unlock or Ignore.", "note");

            req.ContentString = JsonConvert.SerializeObject(p);

            cli.DispatchAsync(req, (resp, e) => {
                try {
                    if (resp.Code != 200)
                    {
                        debug("Bad API Token? " + resp.Code + ": " + resp.ContentString);
                        return;
                    }
                    debug("Doorbell response: " + resp.ContentString);
                } catch (Exception e2) {
                    debug("POST failed: " + e2.Message);
                }
                onEventCompleted();
            });
        }
Exemple #4
0
        public void checkPushes()
        {
            CrestronConsole.PrintLine("Getting Previous Pushes");

            HttpsClient hsc = new HttpsClient();

            hsc.PeerVerification = false;
            hsc.HostVerification = false;
            HttpsClientRequest hscr = new HttpsClientRequest();

            //HttpsClientResponse hscrs;

            hsc.KeepAlive = true;
            hscr.Url.Parse("https://api.pushbullet.com/v2/pushes?active=true&modified_after=" + lastModified);
            //hscr.Header.AddHeader(new HttpsHeader("Access-Token", token));
            hscr.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
            hscr.Header.AddHeader(new HttpsHeader("Content-Type", "application/json"));
            hscr.Header.AddHeader(new HttpsHeader("Authorization", "Bearer " + token));
            //hscr.ContentString = JsonConvert.SerializeObject(hscr.ToString());
            hsc.DispatchAsync(hscr, (hscrs, e) =>
            {
                try
                {
                    if (hscrs.Code >= 200 && hscrs.Code < 300)
                    {
                        // success
                        //CrestronConsole.PrintLine("Old Pushes Requst Send.");
                        //string s = hscrs.ContentString.ToString();
                        //CrestronConsole.Print("Old Push Data: " + s + "\n");
                        pushReturn pr = JsonConvert.DeserializeObject <pushReturn>(hscrs.ContentString);

                        foreach (pushData pd in pr.pushes)
                        {
                            //CrestronConsole.Print("Json Push Data: " + pr.ToString() + "\n");
                            if (pd.modified > lastModified)
                            {
                                lastModified = pd.modified;
                                CrestronConsole.Print("Json Push Data: " + pd.body + "\n");
                                if (pd.body.ToLower().StartsWith("unlock"))
                                {
                                    OnNewResponse(new SimplSharpString(pd.body.ToLower()));
                                }
                                else if (pd.body.ToLower().StartsWith("ignore"))
                                {
                                    OnNewResponse(new SimplSharpString(pd.body.ToLower()));
                                }
                            }
                        }
                    }
                }
                catch (Exception f)
                {
                    CrestronConsole.PrintLine("Old Push Connection error:" + f.ToString());
                }
            });
        }
Exemple #5
0
        public void doPost(string username, string password)
        {
            string userdefault = Txt.read(Txt.path);

            if (userdefault != "")
            {
                JObject obj = JObject.Parse(userdefault);

                string expire = obj["expires_in"].ToString();
                expire = expire.Substring(1, expire.Length - 2);
                DateTime exp = DateTime.Parse(expire);
                if (exp.CompareTo(DateTime.Now) > 0)
                {
                    handleLogin(userdefault);
                    return;
                }
            }

            string url = "https://home.nest.com/user/login";

            url = url + "?username="******"&password="******"Https Data: " + s);
                        handleLogin(s);
                    }
                }
                catch (Exception f)
                {
                    CrestronConsole.PrintLine("Connection error:" + f.ToString());
                }
            });
        }
Exemple #6
0
        public void doRequest(string param, string nestID, string action)
        {
            string userdefault = Txt.read(Txt.path);
            string url         = "";
            string token       = "";

            if (userdefault != "")
            {
                JObject obj = JObject.Parse(userdefault);
                url   = obj["urls"]["transport_url"].ToString();
                token = obj["access_token"].ToString();
            }


            HttpsClient client = new HttpsClient();

            client.Verbose          = false;
            client.PeerVerification = false;
            client.HostVerification = false;
            HttpsClientRequest request = new HttpsClientRequest();

            request.KeepAlive = true;
            request.Url.Parse(url + "/v2/put/" + action + "." + nestID);

            request.Header.SetHeaderValue("X-nl-protocol-version", "1");
            request.Header.SetHeaderValue("Authorization", "Basic " + token);

            request.ContentString = param;
            request.RequestType   = RequestType.Post;

            client.DispatchAsync(request, (hscrs, e) =>
            {
                try
                {
                    if (hscrs.Code >= 200 && hscrs.Code < 300)
                    {
                        // success
                        CrestronConsole.PrintLine("success.");
                    }
                }
                catch (Exception f)
                {
                    CrestronConsole.PrintLine("Connection error:" + f.ToString());
                }
            });
        }
Exemple #7
0
        public void doGet(string url, Hashtable header)
        {
            HttpsClient client = new HttpsClient();

            client.Verbose          = false;
            client.PeerVerification = false;
            client.HostVerification = false;
            HttpsClientRequest request = new HttpsClientRequest();

            request.KeepAlive = true;
            request.Url.Parse(url);
            request.RequestType = RequestType.Get;
            if (header != null && header.Count > 0)
            {
                foreach (DictionaryEntry a in header)
                {
                    request.Header.SetHeaderValue(a.Key.ToString(), a.Value.ToString());
                }
            }
            client.DispatchAsync(request, (hscrs, e) =>
            {
                try
                {
                    if (hscrs.Code >= 200 && hscrs.Code < 300)
                    {
                        // success
                        string s = hscrs.ContentString.ToString();
                        CrestronConsole.Print("Https Data: " + s + "\n");
                        if (OnRes != null)
                        {
                            OnRes(s);
                        }
                    }
                }
                catch (Exception f)
                {
                    CrestronConsole.PrintLine("Connection error:" + f.ToString());
                }
            });
        }
Exemple #8
0
        public void Doorbell()
        {
            debug("PBServer.Doorbell");

            HttpsClient cli = new HttpsClient();
            cli.KeepAlive = false;
            //cli.Verbose = true;

            HttpsClientRequest req = new HttpsClientRequest();
            req.Url.Parse(api + "/pushes");
            req.Header.AddHeader(new HttpsHeader("Access-Token", apiToken));
            req.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Post;
            req.Header.AddHeader(new HttpsHeader("Content-Type", "application/json"));
            PushData p = new PushData("Crestron Doorbell!", "Reply Unlock or Ignore.", "note");
            req.ContentString = JsonConvert.SerializeObject(p);

            cli.DispatchAsync(req, (resp, e) => {
                try {
                    if (resp.Code != 200) {
                        debug("Bad API Token? " + resp.Code + ": " + resp.ContentString);
                        return;
                    }
                    debug("Doorbell response: " + resp.ContentString);
                } catch (Exception e2) {
                    debug("POST failed: " + e2.Message);
                }
                onEventCompleted();
            });
        }
Exemple #9
0
        private void checkPushes()
        {
            debug("checking for new data since " + lastModified);
            HttpsClient cli = new HttpsClient();
            cli.KeepAlive = false;
            //cli.Verbose = true;

            HttpsClientRequest req = new HttpsClientRequest();
            req.Url.Parse(api + "/pushes?modified_after=" + lastModified);
            req.Header.AddHeader(new HttpsHeader("Access-Token", apiToken));
            cli.DispatchAsync(req, (resp, e) => {
                try {
                    if (resp.Code != 200) {
                        debug("Bad API Token? " + resp.Code + ": " + resp.ContentString);
                        return;
                    }
                    PushesData ps = JsonConvert.DeserializeObject<PushesData>(resp.ContentString);
                    foreach (PushData p in ps.pushes) {
                        handlePush(p);
                        if (p.modified > lastModified) lastModified = p.modified;
                    }
                    debug("lastModified is now " + lastModified);
                } catch (Exception e2) {
                    debug("GET failed: " + e2.Message);
                }
            });
        }