Esempio n. 1
0
        private bool UpdateHandler(HttpMethod method, HttpRequest request, HttpResponse response)
        {
            if (method != HttpMethod.Post || request.Path != "/update")
            {
                return(false);
            }

            if (_watchdogToken == null)
            {
                Logger.WarningS("watchdogApi", "Watchdog token is unset but received POST /update API call. Ignoring");
                return(false);
            }

            var auth = request.Headers["WatchdogToken"];

            if (auth.Count != 1)
            {
                response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(true);
            }

            var authVal = auth[0];

            if (authVal != _watchdogToken)
            {
                response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return(true);
            }

            _taskManager.RunOnMainThread(() => UpdateReceived?.Invoke());

            response.StatusCode = (int)HttpStatusCode.OK;

            return(true);
        }
        /// <summary>
        /// Internal Longpoll method which can be call recursively
        /// </summary>
        private async Task LongPoll(UpdateReceived onUpdateReceived, CancellationToken cancelToken)
        {
            var getUpdateTask = Task.Run(() => DoRequest <UpdateResponse>("getUpdates", new BasicToSend
            {
                Method  = "POST",
                Limit   = UpdatesLimit,
                Timeout = UpdatesTimeout,
                Offset  = UpdatesOffset
            }), cancelToken);
            var result = await getUpdateTask;

            if (result.Ok)
            {
                if (result.Result != null)
                {
                    foreach (var update in result.Result)
                    {
                        UpdatesOffset = update.UpdateID + 1;
                        if (onUpdateReceived != null)
                        {
                            onUpdateReceived.Invoke(update);
                        }
                    }
                }
            }

            await LongPoll(onUpdateReceived, cancelToken);
        }
Esempio n. 3
0
 private static void Client_UpdateReceived(object sender, EventArgs e)
 {
     if (UpdateReceived != null)
     {
         UpdateReceived.Invoke(sender, e);
     }
     BeginUpdate();
 }
Esempio n. 4
0
        void socket_Disconnected(object sender, EventArgs e)
        {
            AsynchronousSocket socket = sender as AsynchronousSocket;

            if (socket.IsReceivingFile)
            {
                socket.EndFileReceive();
            }
            if (Disconnected != null)
            {
                Disconnected.Invoke(this, new EventArgs());
            }
            if (wasUpdateCompleted && (UpdateReceived != null))
            {
                UpdateReceived.Invoke(this, new EventArgs());
            }
        }
        bool HandleMessage(string message)
        {
            if (string.IsNullOrWhiteSpace(message) || message.StartsWith("register") || message.StartsWith("echo"))
            {
                return(true);
            }

            try {
                var resp = Newtonsoft.Json.JsonConvert.DeserializeObject <SmartThingsUpdate> (message);
                SendMessage($"ack {resp.Event.Id}");
                UpdateReceived?.Invoke(resp);
                Console.WriteLine("Server says: {0}", message);
                return(true);
            } catch (Exception ex) {
                Console.WriteLine(ex);
                return(false);
            }
        }
        async Task HttpRequestsHandler(HttpContext ctx, Func <Task> next)
        {
            var path = ctx.Request.Path.ToString();

            if (!IsDisposed && _isInit && path.Contains(_pathWithBotname))
            {
                //When webhook of current bot.
                var requestBodyStr = await ctx.GetRequestBodyText();

                var update = JsonConvert.DeserializeObject <Update>(requestBodyStr, JsonSettings);
                var args   = new UpdateReceivedEventArgs(update);
                UpdateReceived?.Invoke(this, args);
            }
            else
            {
                await next();
            }
        }
Esempio n. 7
0
 protected virtual void OnUpdateReceived(UpdateEventArgs e)
 {
     UpdateReceived?.Invoke(this, e);
 }
        void Handler(object sender, UpdateEventArgs args)
        {
            var passedArgs = new UpdateReceivedEventArgs(args.Update);

            UpdateReceived?.Invoke(sender, passedArgs);
        }
Esempio n. 9
0
 /// <summary>
 /// Called when an telegram update (containing any media) is received.
 /// </summary>
 /// <param name="message">The message</param>
 /// <param name="bot">The bot</param>
 protected virtual void OnUpdateReceived(Message message, User bot)
 {
     UpdateReceived?.Invoke(this, new UpdateReceivedEventArgs(message, bot));
 }
Esempio n. 10
0
 protected virtual void OnUpdateReceived(EventArgs args)
 {
     UpdateReceived?.Invoke(this, args);
 }
        private static void UpdateAvailableCommanders(string commanderStatus)
        {
            if (_lastStatus.Equals(commanderStatus))
            {
                return;
            }

            System.Diagnostics.Debug.WriteLine($"CommanderWatcher Update Detected {DateTime.UtcNow:HH:mm:ss}");

            bool countChanged = false;

            if (!String.IsNullOrEmpty(commanderStatus))
            {
                // We have something to process (hopefully a list of commander statuses)
                Dictionary <string, EDEvent> currentEvents = (Dictionary <string, EDEvent>)JsonSerializer.Deserialize(commanderStatus, typeof(Dictionary <string, EDEvent>));
                _lastStatus = commanderStatus;

                foreach (string commander in currentEvents.Keys)
                {
                    if (_commanderStatuses.ContainsKey(commander))
                    {
                        if (currentEvents[commander].TimeStamp > _commanderStatuses[commander].TimeStamp)
                        {
                            lock (_lock)
                                _commanderStatuses[commander] = currentEvents[commander];
                            UpdateReceived?.Invoke(null, currentEvents[commander]);
                        }
                    }
                    else
                    {
                        lock (_lock)
                            _commanderStatuses.Add(commander, currentEvents[commander]);
                        countChanged = true;
                        UpdateReceived?.Invoke(null, currentEvents[commander]);
                    }
                }

                if (DateTime.UtcNow.Subtract(_lastCheckForStaleData).TotalMinutes > 1)
                {
                    List <string> missingCommanders = new List <string>();
                    foreach (string storedCommander in _commanderStatuses.Keys)
                    {
                        if (!currentEvents.ContainsKey(storedCommander))
                        {
                            missingCommanders.Add(storedCommander);
                        }
                    }

                    if (missingCommanders.Count > 0)
                    {
                        lock (_lock)
                        {
                            foreach (string missingCommander in missingCommanders)
                            {
                                _commanderStatuses.Remove(missingCommander);
                            }
                        }
                        countChanged = true;
                    }
                }

                if (countChanged)
                {
                    OnlineCountChanged?.Invoke(null, null);
                }
            }
        }
Esempio n. 12
0
 public void OnUpdateReceived(UpdateReceived callback)
 {
     updateReceived = callback;
 }
Esempio n. 13
0
 /// <summary>
 /// Executes the longpolling and gives updates through the supplied delegate UpdateReceived
 /// </summary>
 public async Task RunLongPollAsync(UpdateReceived onUpdateReceived, CancellationToken token)
 {
     await InitAsync();
     await LongPoll(onUpdateReceived, token);
 }