Exemplo n.º 1
0
        public async Task Close(BackgroundTaskCancellationReason reason)
        {
            await SendMessageAsync("Bot geht aus.");

            await _log.InfoAsync($"Task wurde beendet, weil: {reason.ToString()}");

            _CheckServerStateTimer.Cancel();
            _ctSrc.Cancel();
            _ctSrc.Dispose();
        }
Exemplo n.º 2
0
        private static async Task <bool> IsServerTheAwake()
        {
            ResourceLoader config  = ResourceLoader.GetForViewIndependentUse("Config");
            bool           isAwake = false;
            StreamSocket   socket  = new StreamSocket();

            Windows.Networking.HostName serverHost = new Windows.Networking.HostName(config.GetString("NetworkHelper_HostName"));
            string serverPort = config.GetString("NetworkHelper_Port");

            try
            {
                await socket.ConnectAsync(serverHost, serverPort);

                isAwake = true;
                await socket.CancelIOAsync();
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                await _log.InfoAsync("Der Server ist nicht eingeschaltet", ex);
            }
            catch (Exception ex)
            {
                await _log.ErrorAsync("Die Überprüfung, ob der Server eingeschaltet ist, ist fehlgeschlagen.", ex);
            }
            finally
            {
                socket.Dispose();
            }
            return(isAwake);
        }
Exemplo n.º 3
0
        private async void WebSock_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            using (DataReader reader = args.GetDataReader())
            {
                reader.UnicodeEncoding = UnicodeEncoding.Utf8;
                try
                {
                    string msg       = string.Empty;
                    int    messageID = 0;
                    string read      = reader.ReadString(reader.UnconsumedBufferLength);
                    await _log.DebugAsync($"Aria2: Message received: {read}");

                    JsonRessponse response = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject <JsonRessponse>(read));

                    if (response.Error == null && response.Parameters != null)
                    {
                        string gid = response.Parameters[0].Gid.ToString();
                        _downloads.TryGetValue(gid, out messageID);
                        if (response.Method.ToString() == "aria2.onDownloadStart")
                        {
                            await _telegramBot.SendMessageAsync("Download gestartet");

                            if (_UpdateDownloadStateTimer == null)
                            {
                                _UpdateDownloadStateTimer = ThreadPoolTimer.CreatePeriodicTimer(UpdateDownloadStateTimerElapsedHandler, new TimeSpan(0, 0, 5));
                            }
                        }
                        else
                        {
                            if (response.Method.ToString() == "aria2.onDownloadComplete")
                            {
                                msg = "Download beendet";
                                lock (_monitor)
                                {
                                    _downloads.Remove(gid);
                                }
                            }
                            else
                            {
                                msg = $"Nachricht zum Download: {response.Method.ToString()}";
                            }
                            await _telegramBot.SendMessageAsync(msg, messageID);
                        }
                        if (_downloads.Count == 0)
                        {
                            CloseWebSocketConnection();
                        }
                    }
                    else if (response.Error != null)
                    {
                        await _log.ErrorAsync($"Fehler bei Aria2:{response.ToString()}");

                        await _telegramBot.SendMessageAsync($"Nachricht zu diesem Download:{Environment.NewLine}{response.Error.ToString()}");
                    }
                    else
                    {
                        await _log.InfoAsync($"Aria2 Nachricht ohne Auswirkung:{response.ToString()}");
                    }
                }
                catch (Exception ex)
                {
                    await _log.ErrorAsync("Fehler beim Lesen des Json-Objects", ex);
                }
            }
        }