コード例 #1
0
ファイル: Program.cs プロジェクト: PsichiX/Aegis
 public bool SaveSettings(SettingsModel model, string path = SETTINGS_PATH)
 {
     try
     {
         File.WriteAllText(path, JsonConvert.SerializeObject(model, Formatting.Indented));
         return true;
     }
     catch
     {
         return false;
     }
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: PsichiX/Aegis
        public void Run(string[] args)
        {
            m_settingsModel = LoadSettings();
            m_running = true;
            m_eyeClients = new Dictionary<string, TcpClient>();
            m_controllerClients = new Dictionary<string, ControllerItem>();
            m_eyeDataQueue = new Queue<byte[]>();
            m_controllerDataQueue = new Queue<ControllerData>();
            m_commandsQueue = new Queue<Command>();
            m_waveOutDevice = new WaveOut();
            m_audioFileReader = new AudioFileReader("alarms/default.mp3");
            m_waveOutDevice.Init(m_audioFileReader);

            var argMode = ArgMode.None;
            foreach (var arg in args)
            {
                if (argMode == ArgMode.None)
                {
                    if (arg == "-ep" || arg == "--eye-port")
                        argMode = ArgMode.EyePort;
                    else if (arg == "-cp" || arg == "--controller-port")
                        argMode = ArgMode.ControllerPort;
                    else if (arg == "-t" || arg == "--token")
                        argMode = ArgMode.Token;
                    else if (arg == "-tr" || arg == "--treshold")
                        argMode = ArgMode.Treshold;
                    else if (arg == "-aa" || arg == "--armed-alarm")
                        argMode = ArgMode.ArmedAlarm;
                    else if (arg == "-s" || arg == "--silent")
                        argMode = ArgMode.Silent;
                }
                else if (argMode == ArgMode.EyePort)
                {
                    var v = m_settingsModel.EyePort;
                    if (int.TryParse(arg, out v))
                        m_settingsModel.EyePort = v;
                    argMode = ArgMode.None;
                }
                else if (argMode == ArgMode.ControllerPort)
                {
                    var v = m_settingsModel.ControllerPort;
                    if (int.TryParse(arg, out v))
                        m_settingsModel.ControllerPort = v;
                    argMode = ArgMode.None;
                }
                else if (argMode == ArgMode.Token)
                {
                    m_settingsModel.Token = arg.Trim();
                    argMode = ArgMode.None;
                }
                else if (argMode == ArgMode.Treshold)
                {
                    var v = m_settingsModel.Treshold;
                    if (int.TryParse(arg, out v))
                        m_settingsModel.Treshold = v;
                    argMode = ArgMode.None;
                }
                else if (argMode == ArgMode.ArmedAlarm)
                {
                    if (arg == "y" || arg == "yes" || arg == "t" || arg == "true")
                        m_settingsModel.ArmedAlarm = true;
                    else if (arg == "n" || arg == "no" || arg == "f" || arg == "false")
                        m_settingsModel.ArmedAlarm = false;
                    argMode = ArgMode.None;
                }
                else if (argMode == ArgMode.Silent)
                {
                    if (arg == "y" || arg == "yes" || arg == "t" || arg == "true")
                        m_settingsModel.Silent = true;
                    else if (arg == "n" || arg == "no" || arg == "f" || arg == "false")
                        m_settingsModel.Silent = false;
                    argMode = ArgMode.None;
                }
            }

            m_alarmIsArmed = m_settingsModel.ArmedAlarm;
            Console.WriteLine("* Connection token: " + m_settingsModel.Token);

            SpawnEyeListenerTask();
            SpawnControllerListenerTask();
            SpawnEyeProcessorTask();
            SpawnControllerProcessorTask();
            SpawnCommandsExecutorTask();

            while (m_running)
            {
                var line = Console.ReadLine();
                if (line == "exit")
                    m_running = false;
                else if (line == "arm")
                    ArmAlarm();
                else if (line == "disarm")
                    DisarmAlarm();
                else if (line == "alarm-on")
                    StartAlarm();
                else if (line == "alarm-off")
                    StopAlarm();
            }

            lock (m_commandsLock)
            {
                if (m_commandsQueue != null)
                {
                    foreach (var command in m_commandsQueue)
                        command.PerformAction();
                    m_commandsQueue.Clear();
                }
            }
            lock (m_eyeClientsLock)
            {
                if (m_eyeClients != null)
                    foreach (var eye in m_eyeClients)
                        eye.Value.Close();
            }
            lock (m_controllerClientsLock)
            {
                if (m_controllerClients != null)
                    foreach (var controller in m_controllerClients)
                        controller.Value.Client.Close();
            }
            while (true)
            {
                lock (m_lock)
                {
                    if (m_eyeListener == null && m_controllerListener == null && m_eyeDataQueue == null && m_controllerDataQueue == null && m_commandsQueue == null)
                        break;
                }
                Thread.Sleep(10);
            }
            m_alarmIsPlaying = false;
            lock (m_alarmLock)
            {
                m_waveOutDevice.Stop();
                m_audioFileReader.Dispose();
                m_audioFileReader = null;
                m_waveOutDevice.Dispose();
                m_waveOutDevice = null;
            }
            m_eyeClients = null;
            m_controllerClients = null;
            SaveSettings(m_settingsModel);
            Thread.Sleep(1000);
        }