public void SendMessage(ServerRequest request)
 {
     var messageBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));
     Task.Run(async () =>
     {
         await _udpReceiver.SendToAsync(messageBytes, _udpServerAddress, _udpServerPort);
     });
 }
Esempio n. 2
0
        public MapPage()
        {
            InitializeComponent();

            Map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(DefaultLat, DefaultLng), Distance.FromMiles(5)));

            // Communication with server.
            _serverConnection = new ServerConnection();

            // Communication with map.
            MessagingCenter.Subscribe<MyMap, string>(this, "OnMarkerClick", (sender, arg) =>
            {

            });

            MessagingCenter.Subscribe<MyMap, AlertMessage>(this, "DisplayAlert", (sender, arg) =>
            {
                DisplayAlertAsync(arg.Title, arg.Message);
            });

            MessagingCenter.Subscribe<MyMap, Station>(this, "DisplayStationActionSheet", (sender, arg) =>
            {
                var routes = App.Database.GetRoutesByStation(arg.postId);
                List<VariantButton> options = new List<VariantButton>();
                foreach (Route route in routes)
                {
                    var variant = App.Database.GetVariantByRoute(route.lineName, route.variantId);
                    options.Add(new VariantButton(variant));
                }
                DisplayActionSheet("Wybierz linie:", "Anuluj", null, VariantButton.ToStringArray(options.ToArray())).ContinueWith((option) =>
                {
                    var selectedVariant = options.Find((_option) =>
                    {
                        return (option.Result == _option.ToString());
                    });
                    string lineName = selectedVariant.Variant.lineName;
                    string variantName = selectedVariant.Variant.variantName;
                    var stations = App.Database.GetStationsByVariant(lineName, variantName);
                    MessagingCenter.Send(this, "DrawRoute", stations);
                });
            });

            MessagingCenter.Subscribe<MyMap, double[]>(this, "SendServerRequest", (sender, args) =>
            {
                var request = new ServerRequest
                {
                    start = new LatLng(args[0], args[1]),
                    end = new LatLng(args[2], args[3]),
                    time = ServerRequestTime.Now,
                    solutions = 1,
                    mode = ServerRequestMode.Test
                };
                _serverConnection.SendMessage(request);
            });

            MessagingCenter.Subscribe<ServerConnection, ServerResponse>(this, "ReceivedServerResponse", (sender, arg) =>
            {
                Logger.Log(JsonConvert.SerializeObject(arg.response));
                MessagingCenter.Send(this, "DrawRoute", arg);
            });
        }