Beispiel #1
0
        public void UpdateConnectionStatus(Action <bool> successCallback = null)
        {
            SendGetRequest(UrlFor("getStatus"), async response =>
            {
                if (!response.IsSuccessStatusCode)
                {
                    return;
                }

                HandyResponse status = await response.Content.ReadAsAsync <HandyResponse>();

                if (status.success)
                {
                    CalcServerTimeOffset();
                    SetSyncMode();

                    OnHandyConnected();
                }
                else
                {
                    OnHandyDisconnected();
                }

                successCallback?.Invoke(Connected);
            }, ignoreConnected: true);
        }
Beispiel #2
0
        private void SyncPrepareFinished(HandyResponse resp)
        {
            TimeSpan time = EstimateCurrentTime();

            Debug.WriteLine($"success: (SyncPrepare), resyncing @ " + time.ToString("g"));
            OnOsdRequest("Handy finished downloading Script", TimeSpan.FromSeconds(3), "Handy");
            ResyncNow(time, true);
        }
Beispiel #3
0
 private void AfterHardSync(HandyResponse handyResponse)
 {
     if (_playing)
     {
         SyncPlay(true, EstimateCurrentTime(), null);
     }
     else
     {
         Debug.WriteLine("HandyController.AfterHardSync, but _playing = false");
     }
 }
Beispiel #4
0
        public void StepStroke(bool stepUp)
        {
            SendGetRequest(GetQuery("stepStroke", new
            {
                step    = stepUp,
                timeout = 5000
            }), async response =>
            {
                if (!response.IsSuccessStatusCode)
                {
                    return;
                }

                HandyResponse status = await response.Content.ReadAsAsync <HandyResponse>();

                if (status.success)
                {
                    OnOsdRequest("Stroke Length: " + status.stroke, TimeSpan.FromSeconds(2), "HandyStrokeLength");
                }
            });
        }
Beispiel #5
0
        private void SendGetRequest(string url, Action <HandyResponse> onSuccess)
        {
            SendGetRequest(url, message =>
            {
                HandyResponse resp = message.Content.ReadAsAsync <HandyResponse>().Result;

                ShowRateLimits(message.Headers);

                if (!resp.success)
                {
                    Debug.WriteLine($"error: cmd:{resp.cmd} - {resp.error} - SyncPrepare");

                    if (resp.error == "No machine connected")
                    {
                        OnHandyDisconnected();
                    }
                }
                else
                {
                    onSuccess?.Invoke(resp);
                }
            }, false);
        }
Beispiel #6
0
        private void SendGetRequest(string url, Action <HttpResponseMessage> resultCallback = null, bool ignoreConnected = false, bool waitForAnswer = true)
        {
            if (!ignoreConnected && !Connected)
            {
                return;
            }

            DateTime sendTime = DateTime.Now;

            Task apiCall = new Task(() =>
            {
                using (var client = GetClient())
                {
                    Task <HttpResponseMessage> request = client.GetAsync(url);

                    //if (!waitForAnswer)
                    //{
                    //    request.Wait(1);
                    //    return;
                    //}

                    Task call;

                    TimeSpan duration = DateTime.Now - sendTime;

                    if (resultCallback != null)
                    {
                        Debug.WriteLine($"finished: {url} [{duration.TotalMilliseconds:F0}ms]");
                        call = request.ContinueWith(r => resultCallback(r.Result));
                    }
                    else
                    {
                        call = request.ContinueWith(r =>
                        {
                            HandyResponse resp = r.Result.Content.ReadAsAsync <HandyResponse>().Result;
                            ShowRateLimits(r.Result.Headers);

                            if (!resp.success)
                            {
                                Debug.WriteLine($"error: cmd:{resp.cmd} - {resp.error} - {url} [{duration.TotalMilliseconds:F0}ms]");

                                OnOsdRequest($"Error: {resp.error}", TimeSpan.FromSeconds(3), "HandyError");

                                if (resp.error == "No machine connected")
                                {
                                    OnHandyDisconnected();
                                }
                            }
                            else
                            {
                                Debug.WriteLine($"success: {url} [{duration.TotalMilliseconds:F0}ms]");
                            }
                        });
                    }
                    call.Wait(); // wait for response
                }
            });

            string command = ExtractCommand(url);

            Enqueue(command, apiCall);
        }