private async void Init(PlayerWindow playerWindow) { var uriString = "http://localhost:8090/tvcontrolapi/"; this.Host = new NancyHost(new CustomNancyBoostrapper(), new HostConfiguration { UrlReservations = new UrlReservations { CreateAutomatically = true } }, new Uri(uriString)); this.Host.Start(); TinyIoCContainer ioc = TinyIoCContainer.Current; ioc.AutoRegister(); ioc.Register <ITasksService>((container, overloads) => new LocalTasksServiceDecorator(new FirebaseTasksService())); var playbackControl = new MediaElementPlaybackControl(playerWindow); ioc.Register <IPlaybackControl>(playbackControl); TvControlViewModel viewModel; this.DataContext = playerWindow.DataContext = viewModel = new TvControlViewModel(new TvStations(), playbackControl); ioc.Register <ILogger>(viewModel.Log); viewModel.Log.Write($"API Running on {uriString}", LogLevel.Info); new TasksWindow { DataContext = ioc.Resolve <TasksViewModel>() }.Show(); this.udpListener = new UDPListener(11011, s => viewModel.Log.Write(s, LogLevel.Debug)); await this.udpListener.StartAsync(); }
public TvStationsModule(ITvStations tvStations, IResourceLinker resourceLinker) : base("tvstations") { this.resourceLinker = resourceLinker; this.Get["/", true] = async(o, token) => { TvStation[] stations = await tvStations.GetAllAsync(); List <ApiTvStation> apiTvStations = stations.Select(station => this.MapStation(station)).ToList(); TvStation selectedStation = TvControlViewModel.Current.SelectedStation; return(new { Stations = apiTvStations, Selected = selectedStation == null ? null : this.MapStation(selectedStation), TvControlViewModel.Current.SelectedIndex }); }; this.Post["/{id}"] = o => { TvControlViewModel tvControlViewModel = TvControlViewModel.Current; if (!tvControlViewModel.SetCurrentStation(o.id)) { return(InvalidStationIdResponse(o.id)); } return(HttpStatusCode.Accepted); }; this.Post["/toggleInfo"] = o => { TvControlViewModel.Current.ToggleInfoCommand.Execute(); return(HttpStatusCode.Accepted); }; this.Get["StationImageRoute", "/image/{id}"] = o => { dynamic id = o.id; TvStation station = TvControlViewModel.Current.TvStations.FirstOrDefault(s => s.Id == id); var encoder = new PngBitmapEncoder(); if (station == null) { return(InvalidStationIdResponse(o.id)); } var stationImage = (BitmapSource)station.Image; encoder.Frames.Add(BitmapFrame.Create(stationImage)); var stream = new MemoryStream(); encoder.Save(stream); byte[] bitmapdata = stream.ToArray(); return(new ByteArrayResponse(bitmapdata, "image/png")); }; }