示例#1
0
        //GetUserEmail function
        public void GetUserEmail()
        {
            if (User_Token != "")
            {
                HttpsClient         MyHttpsConnection = new HttpsClient();
                HttpsClientRequest  MyHttpsRequest    = new HttpsClientRequest();
                HttpsClientResponse MyHttpsResponse;
                string   HttpsUrl;
                string   MyTempString;
                string[] words;
                string   commandstring = "";
                bool     FoundEmail    = false;

                MyHttpsConnection.PeerVerification = false;
                MyHttpsConnection.HostVerification = false;
                MyHttpsConnection.Verbose          = false;
                HttpsUrl = "https://api.pushbullet.com/v2/users/me";

                MyHttpsRequest.KeepAlive = true;
                MyHttpsRequest.Url.Parse(HttpsUrl);
                MyHttpsRequest.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                MyHttpsRequest.Header.SetHeaderValue("Authorization", "Bearer " + User_Token);

                PrintString2 = User_Token;

                MyHttpsRequest.ContentString = commandstring;

                // Dispatch will actually make the request with the server
                MyHttpsResponse = MyHttpsConnection.Dispatch(MyHttpsRequest);
                MyHttpsConnection.Abort();

                MyTempString = MyHttpsResponse.ContentString.ToString();
                words        = MyTempString.Split(',');

                PrintString = "";

                foreach (string word in words)
                {
                    PrintString = PrintString + '+' + word;

                    if (word.Contains("email_normalized"))
                    {
                        User_Email = word.Substring(20, word.Length - 21);

                        FoundEmail = true;
                    }
                }
                if (!FoundEmail)
                {
                    User_Email = "Email Not Found!";
                }
            }
            else
            {
                PrintString = "No Token Found";
            }
        }
        /// <summary>
        /// List Devices
        /// </summary>
        public void MyDevice()
        {
            HttpsClient client = new HttpsClient();

            client.PeerVerification = false;
            client.HostVerification = false;
            client.Verbose          = false;

            HttpsClientRequest  request = new HttpsClientRequest();
            HttpsClientResponse response;
            String url = "https://api.pushbullet.com/v2/devices";

            try
            {
                request.KeepAlive = true;
                request.Url.Parse(url);
                client.UserName     = Access_Token;
                request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                request.Header.SetHeaderValue("Content-Type", "application/json");
                request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Token);
                //request.ContentString = devicestring;
                response = client.Dispatch(request);
                client.Abort();

                if (response.Code >= 200 && response.Code < 300)
                {
                    // A response code between 200 and 300 means it was successful.
                    string   s     = response.ContentString.ToString();
                    string[] words = s.Split(',');
                    foreach (string word in words)
                    {
                        CrestronConsole.PrintLine(word + "\n");
                    }
                }
                else
                {
                    // A reponse code outside this range means the server threw an error.
                    CrestronConsole.Print("Pushbullet https response code: " + response.Code);
                }
            }
            catch (Exception e)
            {
                ErrorLog.Error("Exception in Pushbullet: " + e.ToString());
            }
        }
示例#3
0
        public static void deletePush()
        {
            if (Access_Code != "")
            {
                HttpsClient client = new HttpsClient();
                client.PeerVerification = false;
                client.HostVerification = false;
                client.Verbose          = false;

                HttpsClientRequest  request = new HttpsClientRequest();
                HttpsClientResponse response;
                String url = "https://api.pushbullet.com/v2/pushes";

                request.KeepAlive = true;
                request.Url.Parse(url);
                request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Delete;
                request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Code);
                response = client.Dispatch(request);
                client.Abort();
            }
        }
        /// <summary>
        /// This Method Will Retrieve all pushes since x.
        /// </summary>
        /// <returns></returns>
        public static ushort getPush()
        {            
            string commandstring = "";
            if (Access_Code != "")
            {
                HttpsClient client = new HttpsClient();
                client.PeerVerification = false;
                client.HostVerification = false;
                client.Verbose = false;

                HttpsClientRequest request = new HttpsClientRequest();
                HttpsClientResponse response;
                String url = "https://api.pushbullet.com/v2/pushes";

                try
                {
                    myCC.Enter(); // Will not finish, until you have the Critical Section
                    request.KeepAlive = true;
                    request.Url.Parse(url);
                    request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                    request.Header.SetHeaderValue("Content-Type", "application/json");
                    request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Code);
                    request.ContentString = commandstring;


                    // Dispatch will actually make the request with the server
                    response = client.Dispatch(request);

                    if (response.Code >= 200 && response.Code < 300)
                    {
                        client.Abort();
                        string s = response.ContentString.ToString();
                        string[] words = s.Split(',');
                        string PushIden = "";
                        string PushTitle = "";
                        string PushMessage = "";
                        foreach (string word in words)
                        {
                            //ErrorLog.Notice(word + "\n");
                            if (word.Contains("iden"))
                            {
                                PushIden = word.Substring(8, word.Length - 9);
                            }
                            if (word.Contains("title"))
                            {
                                PushTitle = word.Substring(9, word.Length - 10);
                            }
                            if (word.Contains("message"))
                            {
                                PushMessage = word.Substring(11, word.Length - 12);
                            }
                            if (word.Contains("dismissed"))
                            {
                                //TODO Trigger Event To Output String to S+

                                PushReceived("CMD=" + PushTitle + "." + PushMessage + ";");
                                //ErrorLog.Notice("TX = " + PushTitle + "." + PushMessage + ";");
                                //TODO Delete Push.IDEN
                                PushIden = "";
                                PushTitle = "";
                                PushMessage = "";

                            }
                        }
                        //ErrorLog.Notice(response.ContentString.ToString());
                        // A response code between 200 and 300 means it was successful.
                        return 1;
                    }
                    else
                    {
                        client.Abort();
                        ErrorLog.Notice("Response Code = " + response.Code.ToString() + "\n");
                        // A reponse code outside this range means the server threw an error.
                        return 0;
                    }
                }
                catch (Exception e)
                {
                    client.Abort();
                    ErrorLog.Error("Exception in Pushbullet - GetInfo: " + e.ToString());
                    return 0;
                }
                finally
                {
                    myCC.Leave();
                }
            }
            else
            {
                return 0;
            }
        }
        public static void deletePush()
        {
            if (Access_Code != "")
            {
                HttpsClient client = new HttpsClient();
                client.PeerVerification = false;
                client.HostVerification = false;
                client.Verbose = false;

                HttpsClientRequest request = new HttpsClientRequest();
                HttpsClientResponse response;
                String url = "https://api.pushbullet.com/v2/pushes";

                request.KeepAlive = true;
                request.Url.Parse(url);
                request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Delete;
                request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Code);
                response = client.Dispatch(request);
                client.Abort();
            }
        }
        /// <summary>
        /// This Method Will Retrieve all pushes since x.
        /// </summary>
        /// <returns></returns>
        public ushort getUserInfo()
        {
            string commandstring = "";

            if (Access_Code != "")
            {
                HttpsClient client = new HttpsClient();
                client.PeerVerification = false;
                client.HostVerification = false;
                client.Verbose          = false;

                HttpsClientRequest  request = new HttpsClientRequest();
                HttpsClientResponse response;
                String url = "https://api.pushbullet.com/v2/users/me";

                try
                {
                    myCC.Enter(); // Will not finish, until you have the Critical Section
                    request.KeepAlive = true;
                    request.Url.Parse(url);
                    request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                    request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Code);
                    request.ContentString = commandstring;

                    // Dispatch will actually make the request with the server
                    response = client.Dispatch(request);
                    client.Abort();
                    if (response.Code >= 200 && response.Code < 300)
                    {
                        string s = response.ContentString.ToString();
                        //ErrorLog.Notice(s + "\n");
                        string[] words = s.Split(',');
                        string   senderEmail;
                        foreach (string word in words)
                        {
                            if (word.Contains("\"email\""))
                            {
                                senderEmail = word.Substring(11, word.Length - 12);
                                setSenderEmail(senderEmail);
                            }
                        }

                        //ErrorLog.Notice(response.ContentString.ToString());
                        // A response code between 200 and 300 means it was successful.
                        return(1);
                    }
                    else
                    {
                        ErrorLog.Notice("Response Code = " + response.Code.ToString() + "\n");
                        // A reponse code outside this range means the server threw an error.
                        return(0);
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Error("Exception in Pushbullet - GetInfo: " + e.ToString());
                    return(0);
                }
                finally
                {
                    myCC.Leave();
                }
            }
            else
            {
                return(0);
            }
        }
        /// <summary>
        /// This Method Will Retrieve all pushes since x.
        /// </summary>
        /// <returns></returns>
        public ushort getPush()
        {
            string commandstring = "";

            if (Access_Code != "")
            {
                HttpsClient client = new HttpsClient();
                client.PeerVerification = false;
                client.HostVerification = false;
                client.Verbose          = false;

                HttpsClientRequest  request = new HttpsClientRequest();
                HttpsClientResponse response;
                String url = "https://api.pushbullet.com/v2/pushes";

                try
                {
                    myCC.Enter(); // Will not finish, until you have the Critical Section
                    request.KeepAlive = true;
                    request.Url.Parse(url);
                    request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                    request.Header.SetHeaderValue("Content-Type", "application/json");
                    request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Code);
                    request.ContentString = commandstring;


                    // Dispatch will actually make the request with the server
                    response = client.Dispatch(request);
                    client.Abort();
                    if (response.Code >= 200 && response.Code < 300)
                    {
                        string s = response.ContentString.ToString();
                        //ErrorLog.Notice(s + "\n");
                        string[] words       = s.Split(',');
                        string   PushIden    = "";
                        string   PushTitle   = "";
                        string   PushMessage = "";
                        bool     PushUnread  = true;
                        bool     SentMessage = false;
                        foreach (string word in words)
                        {
                            //ErrorLog.Notice(word + "\n");
                            if (word.Contains("\"iden\""))
                            {
                                PushIden = word.Substring(8, word.Length - 9);
                            }
                            if (word.Contains("title"))
                            {
                                PushTitle = word.Substring(9, word.Length - 10);
                                if (PushTitle.Contains("\""))
                                {
                                    PushTitle.Substring(0, PushTitle.Length - 1);
                                }
                            }
                            if (word.Contains("body"))
                            {
                                if (word.Contains("}"))
                                {
                                    PushMessage = word.Substring(8, word.Length - 10);
                                }
                                else
                                {
                                    PushMessage = word.Substring(8, word.Length - 9);
                                    if (PushMessage.Contains("\""))
                                    {
                                        PushMessage.Substring(0, PushMessage.Length - 1);
                                    }
                                }
                            }
                            if (word.Contains("dismissed"))
                            {
                                if (word.Contains("true"))
                                {
                                    PushUnread = false;
                                    continue;
                                }
                            }
                            if (word.Contains("sender_email") && word.Contains(Sender_Email))
                            {
                                SentMessage = true;
                            }
                            if (word.Contains("}"))
                            {
                                //TODO Trigger Event To Output String to S+
                                if (PushTitle != "" || PushMessage != "")
                                {
                                    if (PushUnread == true)
                                    {
                                        if (SentMessage != true)
                                        {
                                            PushReceived("CMD=" + PushTitle + "." + PushMessage + ";");
                                        }
                                        ushort result = dismissPush(PushIden);
                                    }
                                }
                                PushIden    = "";
                                PushTitle   = "";
                                PushMessage = "";
                                PushUnread  = false;
                            }
                        }
                        //ErrorLog.Notice(response.ContentString.ToString());
                        // A response code between 200 and 300 means it was successful.
                        return(1);
                    }
                    else
                    {
                        ErrorLog.Notice("Response Code = " + response.Code.ToString() + "\n");
                        // A reponse code outside this range means the server threw an error.
                        return(0);
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Error("Exception in Pushbullet - GetInfo: " + e.ToString());
                    return(0);
                }
                finally
                {
                    myCC.Leave();
                }
            }
            else
            {
                return(0);
            }
        }
        /// <summary>
        /// List Devices
        /// </summary>
        public void MyDevice()
        {
            HttpsClient client = new HttpsClient();

            client.PeerVerification = false;
            client.HostVerification = false;
            client.Verbose          = false;

            HttpsClientRequest  request = new HttpsClientRequest();
            HttpsClientResponse response;
            String url            = "https://api.pushbullet.com/v2/devices";
            string MyDeviceName   = "";
            bool   MyDeviceActive = false;

            //CrestronConsole.PrintLine("MyDevice\n");

            try
            {
                request.KeepAlive = true;
                request.Url.Parse(url);
                client.UserName     = DeviceToken;
                request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                request.Header.SetHeaderValue("Content-Type", "application/json");
                request.Header.SetHeaderValue("Authorization", "Bearer " + DeviceToken);
                //request.ContentString = devicestring;
                response = client.Dispatch(request);
                client.Abort();

                if (response.Code >= 200 && response.Code < 300)
                {
                    // A response code between 200 and 300 means it was successful.
                    string   s            = response.ContentString.ToString();
                    bool     MyDeviceFind = false;
                    string[] words        = s.Split(',');
                    foreach (string word in words)
                    {
                        //CrestronConsole.PrintLine("Device : "+word + "\n");
                        if (word.Contains("{\"active\":true"))
                        {
                            //CrestronConsole.PrintLine("Device is active\n");
                            MyDeviceActive = true;
                        }


                        if (word.Contains("\"nickname\"") && MyDeviceActive == true)
                        {
                            MyDeviceName = word.Substring(12, word.Length - 13);
                            //CrestronConsole.PrintLine(MyDeviceName + " = " + DeviceName + " \n");

                            if (MyDeviceName == DeviceName)
                            {
                                MyDeviceFind = true;
                                //CrestronConsole.PrintLine(MyDeviceName + " Finded\n");
                                break;
                            }
                        }
                        //CrestronConsole.PrintLine(word + "\n");
                    }
                    if (MyDeviceFind == false)
                    {
                        CreateDevice();
                    }
                }
                else
                {
                    // A reponse code outside this range means the server threw an error.
                    CrestronConsole.Print("Pushbullet https response code: " + response.Code);
                }
            }
            catch (Exception e)
            {
                ErrorLog.Error("Exception in Pushbullet: " + e.ToString());
            }
        }
示例#9
0
        //GetPush method that establish the https connection with PushBullet API and retrieves the Pushes
        public void GetPush()
        {
            if (User_Token != "")
            {
                HttpsClient         MyHttpsConnection = new HttpsClient();
                HttpsClientRequest  MyHttpsRequest    = new HttpsClientRequest();
                HttpsClientResponse MyHttpsResponse;
                string   HttpsUrl;
                string   MyTempString;
                string[] words;
                string   commandstring = "";
                bool     FoundBody     = false;

                MyHttpsConnection.PeerVerification = false;
                MyHttpsConnection.HostVerification = false;
                MyHttpsConnection.Verbose          = false;
                MyHttpsConnection.UserName         = User_Token;
                //limit the push outputs to 3
                HttpsUrl = "https://api.pushbullet.com/v2/pushes?limit=3";

                MyHttpsRequest.KeepAlive = true;
                MyHttpsRequest.Url.Parse(HttpsUrl);
                MyHttpsRequest.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                MyHttpsRequest.Header.SetHeaderValue("Content-Type", "application/json");
                MyHttpsRequest.Header.SetHeaderValue("Authorization", "Bearer " + User_Token);

                PrintString2 = User_Token;

                MyHttpsRequest.ContentString = commandstring;

                // Dispatch will actually make the request with the server
                MyHttpsResponse = MyHttpsConnection.Dispatch(MyHttpsRequest);
                MyHttpsConnection.Abort();

                MyTempString = MyHttpsResponse.ContentString.ToString();
                words        = MyTempString.Split(',');

                PrintString = "";

                foreach (string word in words)
                {
                    PrintString = PrintString + '+' + word;

                    if (word.Contains("body"))
                    {
                        PushReceived = word.Substring(8, word.Length - 10);

                        PushReceived = PushReceived.ToLower();

                        FoundBody = true;

                        break;
                    }
                }

                if (!FoundBody)
                {
                    User_Email = "Push Message Not Received";
                }
            }
            else
            {
                PrintString = "No Token Found";
            }
        }
示例#10
0
        /// <summary>
        /// This Method Will Retrieve all pushes since x.
        /// </summary>
        /// <returns></returns>
        public ushort getUserInfo()
        {
            string commandstring = "";
            if (Access_Code != "")
            {
                HttpsClient client = new HttpsClient();
                client.PeerVerification = false;
                client.HostVerification = false;
                client.Verbose = false;

                HttpsClientRequest request = new HttpsClientRequest();
                HttpsClientResponse response;
                String url = "https://api.pushbullet.com/v2/users/me";

                try
                {
                    myCC.Enter(); // Will not finish, until you have the Critical Section
                    request.KeepAlive = true;
                    request.Url.Parse(url);
                    request.RequestType = Crestron.SimplSharp.Net.Https.RequestType.Get;
                    request.Header.SetHeaderValue("Authorization", "Bearer " + Access_Code);
                    request.ContentString = commandstring;

                    // Dispatch will actually make the request with the server
                    response = client.Dispatch(request);
                    client.Abort();
                    if (response.Code >= 200 && response.Code < 300)
                    {
                        string s = response.ContentString.ToString();
                        //ErrorLog.Notice(s + "\n");
                        string[] words = s.Split(',');
                        string senderEmail;
                        foreach (string word in words)
                        {
                            if (word.Contains("\"email\""))
                            {
                                senderEmail = word.Substring(11, word.Length - 12);
                                setSenderEmail(senderEmail);
                            }
                        }
                        
                        //ErrorLog.Notice(response.ContentString.ToString());
                        // A response code between 200 and 300 means it was successful.
                        return 1;
                    }
                    else
                    {
                        ErrorLog.Notice("Response Code = " + response.Code.ToString() + "\n");
                        // A reponse code outside this range means the server threw an error.
                        return 0;
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Error("Exception in Pushbullet - GetInfo: " + e.ToString());
                    return 0;
                }
                finally
                {
                    myCC.Leave();
                }
            }
            else
            {
                return 0;
            }
        }