private async Task StartHttp()
        {
            var baseAddress = new Uri($"http://localhost:{PortSetting}/");
            try
            {
                // Set up server configuration
                var config = new HttpSelfHostConfiguration(baseAddress);
                // config.Services.Replace(typeof(ITraceWriter), new SimpleTracer());

                //Add our way too simple Authentication 
                config.Filters.Add(new ZvsAuthenticatioFilter(this));

                // Web API routes
                config.MapHttpAttributeRoutes();

                var resolver = new WebApi2PluginDependencyResolver(this);
                config.DependencyResolver = resolver;

                config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //);

                var builder = new ODataConventionModelBuilder();
                
                var scheduledTaskType = builder.EntityType<ScheduledTask>();
                scheduledTaskType.Ignore(t => t.StartTime);
                scheduledTaskType.Property(t => t.StartTimeOffset).Name = "StartTime";

                var deviceValueHistoryTaskType = builder.EntityType<DeviceValueHistory>();
                deviceValueHistoryTaskType.Ignore(t => t.DateTime);
                deviceValueHistoryTaskType.Property(t => t.DateTimeOffset).Name = "DateTime";
                
                var logEntryType = builder.EntityType<LogEntry>();
                logEntryType.Ignore(t => t.Datetime);
                logEntryType.Property(t => t.DateTimeOffset).Name = "Datetime";

                builder.EntitySet<Command>("Commands");
                var cExecute = builder.EntityType<Command>().Action("Execute");
                cExecute.Parameter<string>("Argument");
                cExecute.Parameter<string>("Argument2");


                builder.EntitySet<BuiltinCommand>("BuiltinCommands");
                builder.EntitySet<Device>("Devices");
                builder.EntitySet<DeviceCommand>("DeviceCommands");
                builder.EntitySet<DeviceTypeCommand>("DeviceTypeCommands");
                builder.EntitySet<DeviceValueTrigger>("DeviceValueTriggers");
                builder.EntitySet<DeviceValue>("DeviceValues");
                builder.EntitySet<DeviceValueHistory>("DeviceValueHistories");
                builder.EntitySet<Group>("Groups");
                builder.EntitySet<Scene>("Scenes");
                builder.EntitySet<SceneStoredCommand>("SceneStoredCommands");
                builder.EntitySet<ScheduledTask>("ScheduledTasks");
                builder.EntitySet<LogEntry>("LogEntries");
                builder.Namespace = "Actions";
                config.MapODataServiceRoute("ODataRoute", "odata4", builder.GetEdmModel());

                //config.MapODataServiceRoute(
                //routeName: "ODataRoute",
                //routePrefix: null,
                //model: builder.GetEdmModel());

                // Create server
                HttpSelfHostServer = new HttpSelfHostServer(config);

                // Start listening
                await HttpSelfHostServer.OpenAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not start server: {0}", e.GetBaseException().Message);
            }

            await Log.ReportInfoFormatAsync(CancellationToken, "WebApi2 Server Online on port {0} {1} SSL", baseAddress, UseSslSetting ? "using" : "not using");
        }
Beispiel #2
0
        private async Task StartHttp()
        {
            var baseAddress = new Uri($"http://localhost:{PortSetting}/");

            try
            {
                // Set up server configuration
                var config = new HttpSelfHostConfiguration(baseAddress);
                // config.Services.Replace(typeof(ITraceWriter), new SimpleTracer());

                //Add our way too simple Authentication
                config.Filters.Add(new ZvsAuthenticatioFilter(this));

                // Web API routes
                config.MapHttpAttributeRoutes();

                var resolver = new WebApi2PluginDependencyResolver(this);
                config.DependencyResolver = resolver;

                config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //);

                var builder = new ODataConventionModelBuilder();

                var scheduledTaskType = builder.EntityType <ScheduledTask>();
                scheduledTaskType.Ignore(t => t.StartTime);
                scheduledTaskType.Property(t => t.StartTimeOffset).Name = "StartTime";

                var deviceValueHistoryTaskType = builder.EntityType <DeviceValueHistory>();
                deviceValueHistoryTaskType.Ignore(t => t.DateTime);
                deviceValueHistoryTaskType.Property(t => t.DateTimeOffset).Name = "DateTime";

                var logEntryType = builder.EntityType <LogEntry>();
                logEntryType.Ignore(t => t.Datetime);
                logEntryType.Property(t => t.DateTimeOffset).Name = "Datetime";

                builder.EntitySet <Command>("Commands");
                var cExecute = builder.EntityType <Command>().Action("Execute");
                cExecute.Parameter <string>("Argument");
                cExecute.Parameter <string>("Argument2");


                builder.EntitySet <BuiltinCommand>("BuiltinCommands");
                builder.EntitySet <Device>("Devices");
                builder.EntitySet <DeviceCommand>("DeviceCommands");
                builder.EntitySet <DeviceTypeCommand>("DeviceTypeCommands");
                builder.EntitySet <DeviceValueTrigger>("DeviceValueTriggers");
                builder.EntitySet <DeviceValue>("DeviceValues");
                builder.EntitySet <DeviceValueHistory>("DeviceValueHistories");
                builder.EntitySet <Group>("Groups");
                builder.EntitySet <Scene>("Scenes");
                builder.EntitySet <SceneStoredCommand>("SceneStoredCommands");
                builder.EntitySet <ScheduledTask>("ScheduledTasks");
                builder.EntitySet <LogEntry>("LogEntries");
                builder.Namespace = "Actions";
                config.MapODataServiceRoute("ODataRoute", "odata4", builder.GetEdmModel());

                //config.MapODataServiceRoute(
                //routeName: "ODataRoute",
                //routePrefix: null,
                //model: builder.GetEdmModel());

                // Create server
                HttpSelfHostServer = new HttpSelfHostServer(config);

                // Start listening
                await HttpSelfHostServer.OpenAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not start server: {0}", e.GetBaseException().Message);
            }

            await Log.ReportInfoFormatAsync(CancellationToken, "WebApi2 Server Online on port {0} {1} SSL", baseAddress, UseSslSetting? "using" : "not using");
        }