public AlarmSystem() { m_WatchDogTimeout = new TimeSpan(0, 0, 0, 0, 800); DefaultTimeout = new TimeSpan(0, 0, 0, 2); try { if (ProfileKeeper.IsExists()) { TheProfile = ProfileKeeper.LoadProfile(); } } catch (Exception) { // ignore } if (TheProfile == null) { var ports = SerialPort.GetPortNames(); var name = ports.Length == 0 ? "COM1" : ports[0]; TheProfile = new Profile { Name = name, BaudRate = 115200, DataBits = 8, StopBits = StopBits.One, Parity = Parity.None }; ProfileKeeper.SaveProfile(TheProfile); } ShakingEnabled = true; IlluminanceEnabled = true; DistanceEnabled = true; ConnectivityEnabled = true; RealState = AlarmingState.None; State = AlarmingState.Unarmed; m_Watchdog = new Timer(WatchDogTimeout.TotalMilliseconds) { AutoReset = false }; m_Watchdog.Elapsed += Watchdog_Triggered; //m_IllumSmoother = new ExponentSmoother(0.2); m_AccSmoother = new CuSumEventDetecter(0.5, 0.9, MaxAcc); m_DistSmoother = new MinSmoother(5); m_Port = new AsyncSerialPort(TheProfile, 12); m_Port.OpenPort += Port_Open; m_Port.ClosePort += Port_Close; m_Port.PackageArrived += Port_Package; m_Port.ReadWriteError += Port_Error; IsOpen = false; }
private void Port_Open(Exception e) { if (e != null) { OpenPortResult?.Invoke(e); return; } IsOpen = true; if (!(DistanceEnabled || IlluminanceEnabled || ShakingEnabled || ConnectivityEnabled)) { State = AlarmingState.Unarmed; } else { State = AlarmingState.None; } OpenPortResult?.Invoke(null); ProfileKeeper.SaveProfile(TheProfile); }