Пример #1
0
        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;
        }
Пример #2
0
        public void ClosePort()
        {
            if (!IsOpen)
            {
                return;
            }

            RealState = AlarmingState.None;
            State     = AlarmingState.Unarmed;

            m_Port.Close();
        }
Пример #3
0
        private void Watchdog_Triggered(object sender, ElapsedEventArgs e)
        {
            var old = RealState;

            RealState |= AlarmingState.Level4;

            if (ConnectivityEnabled &&
                !old.HasFlag(AlarmingState.Level4) &&
                RealState.HasFlag(AlarmingState.Level4) &&
                IsOpen)
            {
                State |= AlarmingState.Level4;
                ConnLost?.Invoke();
            }
        }
Пример #4
0
        private void CheckAlarm(Report report)
        {
            var old = RealState;

            RealState = AlarmingState.None;
            if (report.Distance > MaxDist)
            {
                RealState |= AlarmingState.Level1;
            }
            if (report.Illuminance > MaxIllum)
            {
                RealState |= AlarmingState.Level2;
            }
            if (m_AccSmoother.Occured)
            {
                RealState |= AlarmingState.Level3;
            }
            m_Watchdog.Stop();
            if (ConnectivityEnabled)
            {
                m_Watchdog.Start();
            }

            if (!(DistanceEnabled || IlluminanceEnabled || ShakingEnabled || ConnectivityEnabled))
            {
                State = AlarmingState.Unarmed;
            }

            if (DistanceEnabled &&
                !old.HasFlag(AlarmingState.Level1) &&
                RealState.HasFlag(AlarmingState.Level1))
            {
                State |= AlarmingState.Level1;
            }
            if (IlluminanceEnabled &&
                !old.HasFlag(AlarmingState.Level2) &&
                RealState.HasFlag(AlarmingState.Level2))
            {
                State |= AlarmingState.Level2;
            }
            if (ShakingEnabled &&
                !old.HasFlag(AlarmingState.Level3) &&
                RealState.HasFlag(AlarmingState.Level3))
            {
                State |= AlarmingState.Level3;
            }
        }