private void SendAsync(object state)
        {
            System.IO.MemoryStream ms = null;

            try
            {
                APIRequestInfo info = (APIRequestInfo)state;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(info.Url);
                request.UserAgent   = "Growl for Windows/2.0";
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ServicePoint.Expect100Continue = false;

                byte[] file     = info.ImageBytes;
                byte[] postBody = GetPostBody(info.Data, file, ref request);    // this also handles updating the content type with the correct boundary information
                request.ContentLength = postBody.Length;

                Utility.WriteDebugInfo(System.Text.Encoding.UTF8.GetString(postBody));

                System.IO.Stream stream = request.GetRequestStream();
                using (stream)
                {
                    stream.Write(postBody, 0, postBody.Length);
                }

                string          responseText = null;
                HttpWebResponse response     = (HttpWebResponse)request.GetResponse();
                using (response)
                {
                    System.IO.Stream responseStream = response.GetResponseStream();
                    using (responseStream)
                    {
                        System.IO.StreamReader reader = new System.IO.StreamReader(responseStream);
                        using (reader)
                        {
                            responseText = reader.ReadToEnd();
                        }
                    }
                }
                Utility.WriteDebugInfo(responseText);
            }
            catch (Exception ex)
            {
                Utility.WriteDebugInfo(String.Format("Toasty forwarding failed: {0}", ex.Message));
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                    ms = null;
                }
            }
        }
示例#2
0
        public string LogAPIRequest(string logCode, APIRequestInfo requestInfo)
        {
            Requires.NotNullOrEmpty(logCode, nameof(logCode));
            Requires.NotNull(requestInfo, nameof(requestInfo));

            var session = GetMySession();

            if (session == null)
            {
                return(string.Empty);
            }

            var log = InitLog <RunningTimeLog>(session);

            log.LogCode = logCode;
            log.LogData = JsonConvert.SerializeObject(requestInfo);
            log.Spend   = requestInfo.Spend;

            m_Logger.LogInformation(JsonConvert.SerializeObject(log));

            return(log.SessionId);
        }
        public override void ForwardNotification(Growl.Connector.Notification notification, Growl.Connector.CallbackContext callbackContext, Growl.Connector.RequestInfo requestInfo, bool isIdle, ForwardedNotificationCallbackHandler callbackFunction)
        {
            bool send = true;

            if (requestInfo == null) requestInfo = new Growl.Connector.RequestInfo();

            // if this notification originated from Toasty in the first place, dont re-forward it
            if (((notification.ApplicationName == "Toasty") && notification.CustomTextAttributes.ContainsKey("ToastyDeviceID")) && notification.CustomTextAttributes["ToastyDeviceID"].Equals(this.DeviceID, StringComparison.InvariantCultureIgnoreCase))
            {
                requestInfo.HandlingInfo.Add(String.Format("Aborted forwarding due to circular notification (deviceID:{0})", this.DeviceID));
                send = false;
            }

            // if a minimum priority is set, check that
            if (send && this.MinimumPriority != null && this.MinimumPriority.HasValue && notification.Priority < this.MinimumPriority.Value)
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarding to Toasty ({0}) cancelled - Notification priority must be at least '{1}' (was actually '{2}').", this.Description, this.MinimumPriority.Value.ToString(), notification.Priority.ToString()));
                send = false;
            }

            // if only sending when idle, check that
            if (send && this.OnlyWhenIdle && !isIdle)
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarding to Toasty ({0}) cancelled - Currently only configured to forward when idle", this.Description));
                send = false;
            }

            // if quiet hours enabled, check that
            if (send && DuringQuietHours())
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarding to Toasty ({0}) cancelled - Quiet hours enabled on {1} from {2} to {3}. Current time: {4} {5}", this.Description, this.QuietHoursDaysChoice, this.QuietHoursStart.ToShortTimeString(), this.QuietHoursEnd.ToShortTimeString(), DateTime.Now.DayOfWeek, DateTime.Now.ToShortTimeString()));
                send = false;
            }

            if (send)
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarded to Toasty '{0}' - Minimum Priority:'{1}', Actual Priority:'{2}'", this.Description, (this.MinimumPriority != null && this.MinimumPriority.HasValue ? this.MinimumPriority.Value.ToString() : "<any>"), notification.Priority.ToString()));

                string url = BuildUrl(NOTIFY_URL_FORMAT, this.DeviceID);

                System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
                data.Add("target", this.DeviceID);
                data.Add("sender", notification.ApplicationName);
                data.Add("title", notification.Title);
                data.Add("text", notification.Text);

                byte[] bytes = null;
                if (notification.Icon != null && notification.Icon.IsSet)
                {
                    System.Drawing.Image image = (System.Drawing.Image)notification.Icon;
                    using (image)
                    {
                        bytes = GenerateThumbnail(image, 128, 128);
                    }
                }

                APIRequestInfo info = new APIRequestInfo();
                info.RequestInfo = requestInfo;
                info.Url = url;
                info.Data = data;
                info.ImageBytes = bytes;

                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendAsync), info);
            }
        }
        public override void ForwardNotification(Growl.Connector.Notification notification, Growl.Connector.CallbackContext callbackContext, Growl.Connector.RequestInfo requestInfo, bool isIdle, ForwardedNotificationCallbackHandler callbackFunction)
        {
            bool send = true;

            if (requestInfo == null)
            {
                requestInfo = new Growl.Connector.RequestInfo();
            }

            // if this notification originated from Toasty in the first place, dont re-forward it
            if (((notification.ApplicationName == "Toasty") && notification.CustomTextAttributes.ContainsKey("ToastyDeviceID")) && notification.CustomTextAttributes["ToastyDeviceID"].Equals(this.DeviceID, StringComparison.InvariantCultureIgnoreCase))
            {
                requestInfo.HandlingInfo.Add(String.Format("Aborted forwarding due to circular notification (deviceID:{0})", this.DeviceID));
                send = false;
            }

            // if a minimum priority is set, check that
            if (send && this.MinimumPriority != null && this.MinimumPriority.HasValue && notification.Priority < this.MinimumPriority.Value)
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarding to Toasty ({0}) cancelled - Notification priority must be at least '{1}' (was actually '{2}').", this.Description, this.MinimumPriority.Value.ToString(), notification.Priority.ToString()));
                send = false;
            }

            // if only sending when idle, check that
            if (send && this.OnlyWhenIdle && !isIdle)
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarding to Toasty ({0}) cancelled - Currently only configured to forward when idle", this.Description));
                send = false;
            }

            // if quiet hours enabled, check that
            if (send && DuringQuietHours())
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarding to Toasty ({0}) cancelled - Quiet hours enabled on {1} from {2} to {3}. Current time: {4} {5}", this.Description, this.QuietHoursDaysChoice, this.QuietHoursStart.ToShortTimeString(), this.QuietHoursEnd.ToShortTimeString(), DateTime.Now.DayOfWeek, DateTime.Now.ToShortTimeString()));
                send = false;
            }

            if (send)
            {
                requestInfo.SaveHandlingInfo(String.Format("Forwarded to Toasty '{0}' - Minimum Priority:'{1}', Actual Priority:'{2}'", this.Description, (this.MinimumPriority != null && this.MinimumPriority.HasValue ? this.MinimumPriority.Value.ToString() : "<any>"), notification.Priority.ToString()));

                string url = BuildUrl(NOTIFY_URL_FORMAT, this.DeviceID);

                System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
                data.Add("target", this.DeviceID);
                data.Add("sender", notification.ApplicationName);
                data.Add("title", notification.Title);
                data.Add("text", notification.Text);

                byte[] bytes = null;
                if (notification.Icon != null && notification.Icon.IsSet)
                {
                    System.Drawing.Image image = (System.Drawing.Image)notification.Icon;
                    using (image)
                    {
                        bytes = GenerateThumbnail(image, 128, 128);
                    }
                }

                APIRequestInfo info = new APIRequestInfo();
                info.RequestInfo = requestInfo;
                info.Url         = url;
                info.Data        = data;
                info.ImageBytes  = bytes;

                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendAsync), info);
            }
        }