Пример #1
0
        /// <summary>
        /// This method looks for pushes to the doorbell that we may have missed while controller was offline.
        /// We only look back 5 minutes and only respond to the last relevent message.
        /// </summary>
        private void PushBulletGetMissedPushes(string accessToken, string doorbellIden)
        {
            // workout timestamp
            DateTime pushRequestTime  = DateTime.Now.ToUniversalTime();
            DateTime pushRequetPeriod = pushRequestTime.Add(new TimeSpan(0, -5, 0));
            DateTime sTime            = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            long     unixTimestamp    = ((long)(pushRequetPeriod - sTime).TotalSeconds);

            // make request and respond if need be
            String url             = String.Format("https://api.pushbullet.com/v2/pushes?modified_after={0}&active=true", unixTimestamp);
            string requestResponse = PushBulletHttpRequest(url, accessToken, Crestron.SimplSharp.Net.Https.RequestType.Get, "");

            if (requestResponse != "Error")
            {
                PushBulletResponse pushBulletResponse = JsonConvert.DeserializeObject <PushBulletResponse>(requestResponse);
                List <PushObject>  messagesToDoorbell = new List <PushObject>();

                foreach (var response in pushBulletResponse.pushes)
                {
                    if ((response.target_device_iden == doorbellIden) && (response.body.Length > 0))
                    {
                        messagesToDoorbell.Add(response);
                    }
                }

                if (messagesToDoorbell.Count > 0)
                {
                    string messageToDoorbell = messagesToDoorbell[0].body;
                    OnDoorLockMessageReceived(messageToDoorbell.ToUpper().Trim());
                }
                else
                {
                    OnDoorLockMessageReceived("No new messages");
                }
            }
            else
            {
                OnDoorLockMessageReceived("Server Response Error. Check error log");
            }
        }
Пример #2
0
        /// <summary>
        /// Finds the doorbell device and returns the doorbell iden.  If doorbell does not exist we create it
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private string GetDoorbellIden(string requestResponse, string objectName)
        {
            string             iden;
            PushBulletResponse pushBulletResponse = JsonConvert.DeserializeObject <PushBulletResponse>(requestResponse);
            List <Device>      devices            = new List <Device>();

            foreach (var device in pushBulletResponse.devices)
            {
                devices.Add(device);
            }

            Device result = devices.Find(item => item.nickname == objectName);

            if (result != null)
            {
                iden = result.iden;
                return(iden);
            }
            else
            {
                iden = null;
                return(iden);
            }
        }