#region S# License /****************************************************************************************** NOTICE!!! This program and source code is owned and licensed by StockSharp, LLC, www.stocksharp.com Viewing or use of this code requires your acceptance of the license agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE Removal of this comment is a violation of the license agreement. Project: StockSharp.Community.Community File: NotificationClient.cs Created: 2015, 11, 11, 2:32 PM Copyright 2010 by StockSharp, LLC *******************************************************************************************/ #endregion S# License namespace StockSharp.Community { using System; using System.Threading; using Ecng.Common; using StockSharp.Localization; using StockSharp.Logging; /// /// The client for access to the StockSharp notification service. /// public class NotificationClient : BaseCommunityClient { private Timer _newsTimer; //private long _lastNewsId; /// /// Initializes a new instance of the . /// public NotificationClient() : this("http://stocksharp.com/services/notificationservice.svc".To()) { } /// /// Initializes a new instance of the . /// /// Service address. public NotificationClient(Uri address) : base(address, "notification") { } /// /// The available number of SMS-messages. /// public int SmsCount { get { return Invoke(f => f.GetSmsCount(SessionId)); } } /// /// The available number of email messages. /// public int EmailCount { get { return Invoke(f => f.GetEmailCount(SessionId)); } } /// /// To send a SMS message. /// /// Message body. public void SendSms(string message) { ValidateError(Invoke(f => f.SendSms(SessionId, message))); } /// /// To send an email message. /// /// The message title. /// Message body. public void SendEmail(string caption, string message) { ValidateError(Invoke(f => f.SendEmail(SessionId, caption, message))); } /// /// News received. /// public event Action NewsReceived; /// /// To subscribe for news. /// public void SubscribeNews() { RequestNews(); _newsTimer = ThreadingHelper.Timer(() => { try { RequestNews(); } catch (Exception ex) { ex.LogError(); } }).Interval(TimeSpan.FromDays(1)); } /// /// To unsubscribe from news. /// public void UnSubscribeNews() { if (_newsTimer != null) _newsTimer.Dispose(); } private void RequestNews() { var news = Invoke(f => f.GetNews(Guid.Empty, 0)); //if (news.Length <= 0) // return; //_lastNewsId = news.Last().Id; foreach (var n in news) { n.EndDate = n.EndDate.ChangeKind(DateTimeKind.Utc); NewsReceived.SafeInvoke(n); } //if (news.Length == 100) //{ // RequestNews(); //} } /// /// Release resources. /// protected override void DisposeManaged() { UnSubscribeNews(); base.DisposeManaged(); } private static void ValidateError(byte errorCode) { switch ((ErrorCodes)errorCode) { case ErrorCodes.Ok: return; case ErrorCodes.UnknownServerError: throw new InvalidOperationException(LocalizedStrings.UnknownServerError); // notify error codes case ErrorCodes.SmsNotEnought: throw new InvalidOperationException(LocalizedStrings.SmsNotEnough); case ErrorCodes.EmailNotEnought: throw new InvalidOperationException(LocalizedStrings.EmailNotEnough); case ErrorCodes.PhoneNotExist: throw new InvalidOperationException(LocalizedStrings.PhoneNotSpecified); default: throw new InvalidOperationException(LocalizedStrings.UnknownServerErrorCode.Put(errorCode)); } } } }