Exemple #1
0
        public static Settings GetDefault()
        {
            Settings defaultSettings = new Settings();

            // DEFINE DEFAULT SETTINGS HERE
            defaultSettings.IP = "127.0.0.1";
            defaultSettings.Port = 42424;
            defaultSettings.EnableHttp = true;
            defaultSettings.LogToFile = false;
            defaultSettings.HttpPort = 8080;
            defaultSettings.BaudRate = 19200;
            defaultSettings.FileRoot = Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().Location);
            defaultSettings.HttpRoot = Path.Combine(
                defaultSettings.FileRoot, "www/");
            defaultSettings.RoadDetectTimeOut = 300;

            return defaultSettings;
        }
        public RoadplusService(Settings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            else if (!Directory.Exists(settings.HttpRoot))
            {
                Directory.CreateDirectory(settings.HttpRoot);
            }

            IPAddress ip = IPAddress.Parse(settings.IP);

            channels = new List<Channel>();

            if (settings.EnableHttp)
            {
                httpService = new HttpService(
                    new IPEndPoint(
                    ip,
                    settings.HttpPort),
                    settings.HttpRoot);
            }

            roadLinkService = new RoadLinkManager(
                new CommandProcessorText(),
                new TextFormatter(),
                settings.BaudRate,
                settings.RoadDetectTimeOut);

            roadLinkService.CommandProcessor.RegisteredCommands.AddRange(
            new ICommand[]
            {
                new TemperatureMessage(Path.Combine(settings.HttpRoot, "data.json")),
                    new DensityMessage(Path.Combine(settings.HttpRoot, "data.json"), roadLinkService)
            }); 

            channels.Add(roadLinkService);

            CommandProcessorJson processorJson = new CommandProcessorJson();
            processorJson.RegisteredCommands.AddRange(
            new ICommand[]
            {
                new CreateZoneCommand(),
                new CreateSchoolCommand(),
                new CreateRoadConstructionCommand(),
                new EdgeSetCommand(),

                new GetZonesCommand(),
                new GetSchoolsCommand(),
                new GetRoadconstructionsCommand(),

                new RemoveZoneCommand(),
                new RemoveSchoolCommand(),
                new RemoveRoadConstructionCommand(),

                new ConnectRoadToZoneCommand(roadLinkService, this),
                new GetConnectedRoadsCommand(roadLinkService),

                new GetMapCommand()
            });

            websocketService = new WSSessionManager(
                new IPEndPoint(
                    ip,
                    settings.Port),
                processorJson,
                new JsonFormatter());

            channels.Add(websocketService);
        }
Exemple #3
0
        private static void ParseArguments(Settings settings, string[] args)
        {
            try
            {
                string newHttpRoot = null;

                int i = 0;
                foreach (string s in args)
                {
                    switch (s)
                    {
                        case "-l":
                            settings.LogToFile = true;
                            break;
                        case "-nohttp":
                            settings.EnableHttp = false;
                            break;
                        case "-a":
                            string ip = args[i + 1];
                            IPAddress temp;
                            if (!IPAddress.TryParse(ip, out temp))
                            {
                                throw new ArgumentException("Invalid IP");
                            }
                            settings.IP = ip;
                            break;
                        case "-wsport":
                            int wsport = Convert.ToInt32(args[i + 1]);
                            settings.Port = wsport;
                            break;
                        case "-httpport":
                            int httpport = Convert.ToInt32(args[i + 1]);
                            settings.HttpPort = httpport;
                            break;
                        case "-b":
                            int rate = Convert.ToInt32(args[i + 1]);
                            settings.BaudRate = rate;
                            break;
                        case "-r":
                            string folder = args[i + 1];
                            if (!System.IO.Directory.Exists(folder))
                            {
                                throw new ArgumentException(
                                    "Server root directory not found");
                            }
                            settings.FileRoot = folder;
                            break;
                        case "-httpfolder":
                            newHttpRoot = args[i + 1];
                            break;
                        case "-h":
                        case "-H":
                        case "-help":
                        case "--help":
                            DisplayHelp();
                            Environment.Exit(0);
                            break;
                        default:
                            // do nothing
                        break;
                    }
                    i++;
                }

                if (newHttpRoot != null)
                {
                    string httpfolder = Path.Combine(settings.FileRoot, newHttpRoot);
                    if (!System.IO.Directory.Exists(httpfolder))
                    {
                        throw new ArgumentException(
                            "Http service root directory not found");
                    }
                    settings.HttpRoot = httpfolder;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DisplayHelp();
                Environment.Exit(1);
            }
        }