Example #1
0
        static void GetData()
        {
            DebugHelper.WriteLogEntry("Notificator GetData");
            bool showMessage, showComments, showFriends, showFriendsNews, showFriendsPhotos, showWallMessages, newEvent, oldEvent = true;
            bool newMessage, newComments, newFriends, newFriendsNews, newFriendsPhotos, newWallMessages;
            Icon curIcon = Resources.Messages;
            showMessage = showComments = showFriends = showFriendsNews = showFriendsPhotos = showWallMessages = newEvent = newMessage = newComments = newFriends = newFriendsNews = newFriendsPhotos = newWallMessages = false;
            string nfnText = "";

            try
            {
                //Инициализация _eventsGetResponsePrev значениями из реестра
                if (_eventsGetResponsePrev.events.Count == 0)
                {
                    _eventsGetResponsePrev = _baseLogic.IDataLogic.EventsGet();
                }

                //Оновление событий с сервера
                //получение обновленных значений из реестра
                EventsGetResponse eventsGetResponse = null;
                try
                {
                    eventsGetResponse = _baseLogic.EventsGet(true, false, true);
                }
                catch (VKException ex)
                {
                    DebugHelper.WriteLogEntry("***7");
                    CatchErrors(ex);
                    DebugHelper.WriteLogEntry("***8");
                }
                catch (Exception ex)
                {
                    DebugHelper.WriteLogEntry(ex, "Unexpected error");
                }
                DebugHelper.WriteLogEntry("***9");

                //Если значений в реестре не было, то делаю количество всех ивентов 0
                if (_eventsGetResponsePrev.events.Count == 0)
                {
                    _eventsGetResponsePrev = eventsGetResponse;
                    foreach (Event ev in _eventsGetResponsePrev.events)
                    {
                        ev.number = 0;
                    }
                }
                DebugHelper.WriteLogEntry("***10");

                if (eventsGetResponse != null)
                {

                    #region Проверка какие события показывать
                    foreach (Event ev in eventsGetResponse.events)
                    {
                        switch (ev.type)
                        {
                            case EventType.Messages:
                                if (ev.number > 0) showMessage = true;
                                break;
                            case EventType.Comments:
                                if (ev.number > 0) showComments = true;
                                break;
                            case EventType.Friends:
                                if (ev.number > 0) showFriends = true;
                                break;
                            case EventType.FriendsNews:
                                if (ev.number > 0) showFriendsNews = true;
                                break;
                                /*
                            case EventType.FriendsPhotos:
                                if (ev.number > 0) showFriendsPhotos = true;
                                break;
                            case EventType.WallMessages:
                                if (ev.number > 0) showWallMessages = true;
                                break;
                                 */
                        }
                    }
                    #endregion
                    DebugHelper.WriteLogEntry("***11");

                    #region Проверяем изменился ли счетчик событий с последнего обновления
                    foreach (Event ev in eventsGetResponse.events)
                    {
                        switch (ev.type)
                        {
                            case EventType.Messages:
                                foreach (Event evPrev in _eventsGetResponsePrev.events)
                                    if (evPrev.type == ev.type)
                                        if (ev.number > evPrev.number) newMessage = true;
                                break;
                            case EventType.Comments:
                                foreach (Event evPrev in _eventsGetResponsePrev.events)
                                    if (evPrev.type == ev.type)
                                        if (ev.number > evPrev.number) newComments = true;
                                break;
                            case EventType.Friends:
                                foreach (Event evPrev in _eventsGetResponsePrev.events)
                                    if (evPrev.type == ev.type)
                                        if (ev.number > evPrev.number) newFriends = true;
                                break;
                            case EventType.FriendsNews:
                                foreach (Event evPrev in _eventsGetResponsePrev.events)
                                    if (evPrev.type == ev.type)
                                        if (ev.number > evPrev.number) newFriendsNews = true;
                                break;
                                /*
                case EventType.FriendsPhotos:
                    foreach (Event evPrev in _eventsGetResponsePrev.events)
                        if (evPrev.type == ev.type)
                             if (ev.number > evPrev.number) newFriendsPhotos = true;
                    break;
                case EventType.WallMessages:
                    foreach (Event evPrev in _eventsGetResponsePrev.events)
                        if (evPrev.type == ev.type)
                             if (ev.number > evPrev.number) newWallMessages = true;
                    break;
                     */
                        }
                    }

                    #endregion
                    DebugHelper.WriteLogEntry("***12");

                    #region Проверка происходило ли событие раньше
                    oldEvent = true;
                    foreach (Event evPrev in _eventsGetResponsePrev.events)
                    {
                        switch (evPrev.type)
                        {
                            case EventType.Messages:
                                if (newMessage)
                                    if (evPrev.number == 0) oldEvent = false;
                                break;
                            case EventType.Comments:
                                if (newComments)
                                    if (evPrev.number == 0) oldEvent = false;
                                break;
                            case EventType.Friends:
                                if (newFriends)
                                    if (evPrev.number == 0) oldEvent = false;
                                break;
                            case EventType.FriendsNews:
                                if (newFriendsNews)
                                    if (evPrev.number == 0) oldEvent = false;
                                break;
                                /*
                        case EventType.FriendsPhotos:
                            if (newFriendsPhotos)
                                if (evPrev.number == 0) oldEvent = false;
                            break;
                        case EventType.WallMessages:
                            if (newWallMessages)
                                if (evPrev.number == 0) oldEvent = false;
                            break;
                             */
                        }
                    }
                    #endregion
                    DebugHelper.WriteLogEntry("***13");

                    #region Определение иконки
                    curIcon = showMessage ? Resources.Messages : Resources.Events;

                    #endregion
                    DebugHelper.WriteLogEntry("***14");

                    //MessageBox.Show(showMessage + ":" + showFriends + ":" + showFriendsNews);
                    //MessageBox.Show(newMessage + ":" + newFriends + ":" + newFriendsNews);

                    #region Формирование текста сообщения

                    if (newMessage || newComments || newFriends || newFriendsNews /*|| newFriendsPhotos || newWallMessages*/)
                    {
                        DebugHelper.WriteLogEntry("Got new event");
                        nfnText = "";

                        #region текст
                        foreach (Event ev in eventsGetResponse.events)
                        {
                            switch (ev.type)
                            {
                                case EventType.Messages:
                                    if (showMessage) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnMess, ev.number);
                                    break;
                            }
                        }
                        foreach (Event ev in eventsGetResponse.events)
                        {
                            switch (ev.type)
                            {
                                case EventType.Comments:
                                    if (showComments) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnCmnts, ev.number);
                                    break;
                            }
                        }
                        foreach (Event ev in eventsGetResponse.events)
                        {
                            switch (ev.type)
                            {
                                case EventType.Friends:
                                    if (showFriends) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrnds, ev.number);
                                    break;
                            }
                        }

                        foreach (Event ev in eventsGetResponse.events)
                        {
                            switch (ev.type)
                            {
                                case EventType.FriendsNews:
                                    if (showFriendsNews) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrndsNs, ev.number);
                                    break;
                            }
                        }
                        /*
                        foreach (Event ev in eventsGetResponse.events)
                        {
                            switch (ev.type)
                            {
                                case EventType.FriendsPhotos:
                                    if (showFriendsPhotos) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrndsPh, ev.number);
                                    break;
                            }
                        }

                        foreach (Event ev in eventsGetResponse.events)
                        {
                            switch (ev.type)
                            {
                                case EventType.WallMessages:
                                    if (showWallMessages) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrnds, ev.number);
                                    break;
                            }
                        }
                         */

                        #endregion
                        DebugHelper.WriteLogEntry("***15");

                        DebugHelper.WriteLogEntry("Notification text(new event): " + nfnText);
                        if (nfnText != "") newEvent = true;

                        //Чтобы попапилась и будила
                        _notification.StraightToTray = false;
                        _notification.DisplayOn = true;
                    }
                    else
                    {
                        #region Если не пришло новое событие - модификация текста
                        // меняем текст в нотификаторе
                        DebugHelper.WriteLogEntry("No new events, just change text (dec counters)");
                        if (_notification != null)
                        {
                            nfnText = "";

                            #region текст
                            foreach (Event ev in eventsGetResponse.events)
                            {
                                switch (ev.type)
                                {
                                    case EventType.Messages:
                                        if (showMessage) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnMess, ev.number);
                                        break;
                                }
                            }
                            foreach (Event ev in eventsGetResponse.events)
                            {
                                switch (ev.type)
                                {
                                    case EventType.Comments:
                                        if (showComments) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnCmnts, ev.number);
                                        break;
                                }
                            }
                            foreach (Event ev in eventsGetResponse.events)
                            {
                                switch (ev.type)
                                {
                                    case EventType.Friends:
                                        if (showFriends) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrnds, ev.number);
                                        break;
                                }
                            }

                            foreach (Event ev in eventsGetResponse.events)
                            {
                                switch (ev.type)
                                {
                                    case EventType.FriendsNews:
                                        if (showFriendsNews) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrndsNs, ev.number);
                                        break;
                                }
                            }

                            /*
                            foreach (Event ev in eventsGetResponse.events)
                            {
                                switch (ev.type)
                                {
                                    case EventType.FriendsPhotos:
                                        if (showFriendsPhotos) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrnds, ev.number);
                                        break;
                                }
                            }

                            foreach (Event ev in eventsGetResponse.events)
                            {
                                switch (ev.type)
                                {
                                    case EventType.WallMessages:
                                        if (showWallMessages) nfnText += String.Format("{0} (+{1})<br>", Resources.Program_OnTimerTick_NfnFrnds, ev.number);
                                        break;
                                }
                            }
                             */
                            #endregion
                            DebugHelper.WriteLogEntry("***16");

                            DebugHelper.WriteLogEntry("Notification text(mod): " + nfnText);

                            //Чтобы не попапилась и не будила
                            _notification.StraightToTray = true;
                            _notification.DisplayOn = false;
                            //Если текст нотификации изменился на "ничто" - скрыть ее
                            if (nfnText == "") _notification.Visible = false;
                            else _notification.Text = nfnText;
                        }
                        #endregion
                    }
                    #endregion

                    //if (nfnText != "")
                    //nfnText += string.Format("<br>{0}", GetBirthdayString());

                    DebugHelper.WriteLogEntry("***17");
                }

                DebugHelper.WriteLogEntry("***21");

                #region Вывод нотификации если есть новые события
                //Вывод нотификации если есть новые события
                try
                {
                    if (newEvent)
                    {
                        DebugHelper.WriteLogEntry("***22");

                        //SetEvent для приложения, сигнализирующий о том, что есть новые события
                        _baseLogic.IDataLogic.SetRefreshEventsFlag("1");

                        OnNotificationShow(nfnText, curIcon, oldEvent);
                    }
                }
                catch (Exception ex)
                {
                    DebugHelper.WriteLogEntry(ex, Resources.Program_OnTimerTick_NfnError);
                }
                #endregion
                DebugHelper.WriteLogEntry("***23");

                if (eventsGetResponse != null)
                    _eventsGetResponsePrev = eventsGetResponse;
            }
            catch (Exception ex)
            {
                DebugHelper.WriteLogEntry(ex, Resources.Program_OnTimerTick_Error);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            InitNotification();

            //Определение нотификаций и событий
            _notification = new NotificationWithSoftKeys();
            _notification.LeftSoftKey = new NotificationSoftKey(SoftKeyType.Hide, Properties.Resources.Program_LeftSoftKey);
            _notification.RightSoftKey = new NotificationSoftKey(SoftKeyType.Dismiss, Properties.Resources.Program_RightSoftKey);
            _notification.RightSoftKeyClick += OnNotificationRightSoftKeyClick;

            //Запуск сервиса и регистрация сообщения для него
            ServiceApplication.Name = Interprocess.ServiceName;
            ServiceApplication.RegisterMessage(Interprocess.WM_QUIT_SERVICE);
            ServiceApplication.RegisterMessage(Interprocess.WM_TIMER_TICK);
            ServiceApplication.OnRegisteredMessage += ServiceApplication_OnRegisteredMessage;

            Cache.InitPath(SystemConfiguration.AppInstallPath + "\\Cache");
            _baseLogic = new BaseLogic(new DataLogic(), new CommunicationLogic());
            //_baseLogic.IDataLogic.ClearNotificationTempDataInCache();
            _eventsGetResponsePrev = new EventsGetResponse();

            //автовход
            if (_baseLogic.IDataLogic.GetToken() != "")
            {
                try
                {
                    _baseLogic.AutoLogin();
                    _eventsGetResponsePrev = _baseLogic.EventsGet(true, false, true);

                    DebugHelper.WriteLogEntry("Notificator AutoLogin success.");

                    /*_eventsGetResponsePrev.CommentsCount = 0;
                    _eventsGetResponsePrev.FriendsCount = 0;
                    _eventsGetResponsePrev.MessagesCount = 0;*/

                }
                catch (VKException ex)
                {
                    CatchErrors(ex);
                }
                catch (Exception ex)
                {
                    DebugHelper.WriteLogEntry(ex, "Unexpected error.");
                }
            }
            else
            {
                DebugHelper.WriteLogEntry("Notificator service can't be started, there are no token in registry.");
                Application.Exit();
            }

            //Устанавливаем время последней проверки
            _lastCheckedTime = DateTime.Now;

            //Считываем из реестра значение периода проверки
            _checkInterval = GetCheckInterval();

            //Задаем время, когда необходимо будет разбудить устройство
            try
            {
                WakeupScheduler.ScheduleWakeUp(_lastCheckedTime.AddMilliseconds(_checkInterval));
            }
            catch (ExternalException exception)
            {
                DebugHelper.WriteLogEntry(exception, exception.Message);
                throw;
            }
            catch (Exception exception)
            {
                DebugHelper.WriteLogEntry(exception, "Error in scheduling wakeup program launch");
                throw;
            }

            ServiceApplication.Run();
        }
Example #3
0
        public EventsGetResponse EventsGet()
        {
            EventsGetResponse eventsGetResponse = new EventsGetResponse();

            try
            {
                DebugHelper.WriteTraceEntry("DataLogic.GetEvents: messages");

                // проверяю наличие записи messages в реестре по одному параметру
                if (!string.IsNullOrEmpty(RegistryUtility.GetValue("MessagesCount", SystemConfiguration.EventsRegKey)))
                {
                    Event eventMessages = new Event();

                    eventMessages.type = EventType.Messages;
                    eventMessages.number = Convert.ToInt32(RegistryUtility.GetValue("MessagesCount", SystemConfiguration.EventsRegKey));

                    eventsGetResponse.events.Add(eventMessages);
                }

                DebugHelper.WriteTraceEntry("DataLogic.GetEvents: comments");

                // проверяю наличие записи comments в реестре по одному параметру
                if (!string.IsNullOrEmpty(RegistryUtility.GetValue("CommentsCount", SystemConfiguration.EventsRegKey)))
                {
                    Event ev = new Event();

                    ev.type = EventType.Comments;
                    ev.number = Convert.ToInt32(RegistryUtility.GetValue("CommentsCount", SystemConfiguration.EventsRegKey));

                    eventsGetResponse.events.Add(ev);
                }

                DebugHelper.WriteTraceEntry("DataLogic.GetEvents: friends");

                // проверяю наличие записи friends в реестре по одному параметру
                if (!string.IsNullOrEmpty(RegistryUtility.GetValue("FriendsCount", SystemConfiguration.EventsRegKey)))
                {
                    Event ev = new Event();

                    ev.type = EventType.Friends;
                    ev.number = Convert.ToInt32(RegistryUtility.GetValue("FriendsCount", SystemConfiguration.EventsRegKey));

                    eventsGetResponse.events.Add(ev);
                }

                DebugHelper.WriteTraceEntry("DataLogic.GetEvents: friends_photos");

                // проверяю наличие записи friendsphotos в реестре по одному параметру
                if (!string.IsNullOrEmpty(RegistryUtility.GetValue("FriendsPhotosCount", SystemConfiguration.EventsRegKey)))
                {
                    Event ev = new Event();

                    ev.type = EventType.FriendsPhotos;
                    ev.number = Convert.ToInt32(RegistryUtility.GetValue("FriendsPhotosCount", SystemConfiguration.EventsRegKey));

                    eventsGetResponse.events.Add(ev);
                }

                DebugHelper.WriteTraceEntry("DataLogic.GetEvents: friends_news");

                // проверяю наличие записи friendsnews в реестре по одному параметру
                if (!string.IsNullOrEmpty(RegistryUtility.GetValue("FriendsNewsCount", SystemConfiguration.EventsRegKey)))
                {
                    Event ev = new Event();

                    ev.type = EventType.FriendsNews;
                    ev.number = Convert.ToInt32(RegistryUtility.GetValue("FriendsNewsCount", SystemConfiguration.EventsRegKey));

                    eventsGetResponse.events.Add(ev);
                }

                DebugHelper.WriteTraceEntry("DataLogic.GetEvents: wall_messages");

                // проверяю наличие записи wallmessages в реестре по одному параметру
                if (!string.IsNullOrEmpty(RegistryUtility.GetValue("WallCount", SystemConfiguration.EventsRegKey)))
                {
                    Event ev = new Event();

                    ev.type = EventType.WallMessages;
                    ev.number = Convert.ToInt32(RegistryUtility.GetValue("WallCount", SystemConfiguration.EventsRegKey));

                    eventsGetResponse.events.Add(ev);
                }
            }
            catch (Exception ex)
            {
                DebugHelper.WriteLogEntry(ex, null);
            }

            return eventsGetResponse;
        }