Exemplo n.º 1
0
        public async Task <LoginResponse> LoginAsync(LoginRequest connexion)
        {
            try
            {
                if (!string.IsNullOrEmpty(UserProperties.GetApiKey()) && !client.DefaultRequestHeaders.Contains("X-Api-Key"))
                {
                    client.DefaultRequestHeaders.Add("X-Api-Key", UserProperties.GetApiKey());
                }

                var uri = new Uri(string.Format(Constants.BASE_URL + UserProperties.GetLocalIP() + Constants.API_LOGIN, string.Empty));

                var json    = JsonConvert.SerializeObject(connexion);
                var payload = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await client.PostAsync(uri, payload);

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    var result = JsonConvert.DeserializeObject <LoginResponse>(content);
                    return(result);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            return(null);
        }
Exemplo n.º 2
0
        public async Task <bool> DisconnectAsync()
        {
            try
            {
                ConnexionRequest connexion = new ConnexionRequest()
                {
                    command = "disconnect"
                };
                var uri = new Uri(string.Format(Constants.BASE_URL + UserProperties.GetLocalIP() + Constants.API_DISCONNECT, string.Empty));

                var json    = JsonConvert.SerializeObject(connexion);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await client.PostAsync(uri, content);

                if (response.IsSuccessStatusCode)
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            return(false);
        }
Exemplo n.º 3
0
 public async override void ViewAppeared()
 {
     base.ViewAppeared();
     if (!string.IsNullOrEmpty(UserProperties.GetLocalIP()) && !string.IsNullOrEmpty(UserProperties.GetApiKey()))
     {
         await _navigationService.Navigate <MainViewModel>();
     }
 }
Exemplo n.º 4
0
        public override async Task Initialize()
        {
            await base.Initialize();

            this.ConnectionStatus = "Déconnecté";
            this.ConnectionIcon   = "ic_warning_red_dark_36dp";
            this.ConnectionIP     = UserProperties.GetLocalIP();
            ManageWebsocketConnection();
        }
Exemplo n.º 5
0
        public async Task <ConnexionSettings> GetConnectionAsync()
        {
            try
            {
                var uri      = new Uri(string.Format(Constants.BASE_URL + UserProperties.GetLocalIP() + Constants.API_CONNECTION, string.Empty));
                var response = await client.GetAsync(uri);

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    var result = JsonConvert.DeserializeObject <ConnexionSettings>(content);
                    return(result);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            return(null);
        }
Exemplo n.º 6
0
        public async Task Connect()
        {
            try
            {
                await client.ConnectAsync(new Uri("ws://" + UserProperties.GetLocalIP() + "/sockjs/websocket"), cts.Token);
            } catch (Exception e)
            {
                _instance._messenger.Publish(new OctoMessage(_instance, OctoMessage.DisconnectedFromOctoprint));
            }

            UpdateClientState();

            await Task.Factory.StartNew(async() =>
            {
                while (true)
                {
                    WebSocketReceiveResult result;
                    var message          = new ArraySegment <byte>(new byte[4096]);
                    var serialisedMessae = "";
                    do
                    {
                        result            = await client.ReceiveAsync(message, cts.Token);
                        var messageBytes  = message.Skip(message.Offset).Take(result.Count).ToArray();
                        serialisedMessae += Encoding.UTF8.GetString(messageBytes);

                        if (result.EndOfMessage)
                        {
                            try
                            {
                                var msg = JsonConvert.DeserializeObject <WebSocketMessage>(serialisedMessae);
                                Console.WriteLine(msg);
                                if (msg != null)
                                {
                                    if (msg.connected != null)
                                    {
                                        _instance._messenger.Publish(new ConnectMessage(this, msg.connected));
                                    }
                                    if (msg.current != null)
                                    {
                                        _instance._messenger.Publish(new CurrentMessage(this, msg.current));
                                    }
                                    if (msg.events != null)
                                    {
                                        _instance._messenger.Publish(new EventMessage(this, msg.events));
                                    }
                                    if (msg.slicingProgress != null)
                                    {
                                        _instance._messenger.Publish(new SlicingProgressMessage(this, msg.slicingProgress));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Invalide message format. {ex.Message}");
                            }

                            serialisedMessae = "";
                        }
                    } while (result.MessageType != WebSocketMessageType.Close);
                    _instance._messenger.Publish(new OctoMessage(_instance, OctoMessage.DisconnectedFromOctoprint));
                }
            }, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            void UpdateClientState()
            {
                if (client.State == WebSocketState.Open)
                {
                    _instance._messenger.Publish(new OctoMessage(_instance, OctoMessage.ConnectedToOctoprint));
                }
                else
                {
                    _instance._messenger.Publish(new OctoMessage(_instance, OctoMessage.DisconnectedFromOctoprint));
                }
            }
        }