예제 #1
0
        private bool SuppressAlert(Alert newAlert)
        {
            lock (alertHistory)
            {
                int count = alertHistory.Count;

                DateTime currTime = DateTime.Now;

                for (int index = count - 1; index >= 0; index--)
                {
                    KeyValuePair<DateTime,Alert> kvPair = alertHistory.ElementAt(index);

                    TimeSpan timeDiff = currTime - kvPair.Key;

                    //if we've gone far back into history, lets stop
                    if (timeDiff.TotalSeconds > settings.SuppressSeconds)
                    {
                        return false;
                    }

                    //we found an equivalent notification
                    if (kvPair.Value.Equals(newAlert))
                        return true;
                }
            }

            return false;
        }
예제 #2
0
        private void InsertAlert(Alert alert)
        {
            lock (alertHistory)
            {
                if (alertHistory.Count == MaxAlertHistory)
                    alertHistory.RemoveAt(0);

                //take care of duplicates
                while (alertHistory.ContainsKey(alert.TimeTriggered))
                    alert.TimeTriggered += TimeSpan.FromMilliseconds(1);

                alertHistory.Add(alert.TimeTriggered, alert);
            }
        }
예제 #3
0
 public bool Equals(Alert other)
 {
     return (SensorFriendlyName.Equals(other.SensorFriendlyName) &&
             SensorLocation.Equals(other.SensorLocation) &&
             Value == other.Value);
 }
예제 #4
0
        private void GenerateMessage(Alert newAlert)
        {
            List<Attachment> attachmentList = new List<Attachment>();

            string linkMessage = String.Format("Go to Lab of Things Alerts application to see list of alerts.");

            string subject = "Alert";
            string message = String.Format("Dear {0} - \n\n{1}.\n\n{2}\n\nCheers.\n",
                                            settings.UserName,
                                            newAlert.FriendlyToString(),
                                            linkMessage
                                            );
            string alertTxtName = "Alert-" + DateTime.Now.ToString("yyyyMMdd-HHmmss");
            AddTxtDataStream(alertTxtName,newAlert.ToString());

            foreach (VPort cameraPort in cameraPorts.Keys)
            {
                VCapability capability = cameraPorts[cameraPort];

                IList<VParamType> retVals = cameraPort.Invoke(RoleCamera.RoleName, RoleCamera.OpGetImageName, new List<VParamType>(),
                                                                   ControlPort, capability, ControlPortCapability);

                if (retVals[0].Maintype() != (int)ParamType.SimpleType.error)
                {
                    string cameraFriendlyName = cameraPort.GetInfo().GetFriendlyName();
                    logger.Log("{0} got image from {1}", this.ToString(), cameraFriendlyName);

                    if (retVals.Count >= 1 && retVals[0].Value() != null)
                    {
                        byte[] imageBytes = (byte[])retVals[0].Value();
                        string mimeType = "image/jpeg";

                        Attachment attachment = new Attachment(new MemoryStream(imageBytes), cameraFriendlyName + "." + "jpg", mimeType);
                        attachmentList.Add(attachment);

                        string alertImgName = "WaterAlertImg-" + DateTime.Now.ToString("yyyyMMdd-HHmmss");
                        AddPicDataStream(alertImgName,imageBytes);
                    }
                    else
                    {
                        logger.Log("{0} got null image", this.ToString());
                    }
                }
            }

            if (settings.Mode == AlertMode.emailonly || settings.Mode == AlertMode.both)
                SendEmail(subject, message, attachmentList);

            if (settings.Mode == AlertMode.smsonly || settings.Mode == AlertMode.both)
                SendSms(subject, message);
        }
예제 #5
0
        ////Email address to receive the alert pictures.
        //string emailAdrs;
        public override void Start()
        {
            logger.Log("Started: {0}", ToString());

            try
            {
                settings = new AlertSettings();

                settings.Mode = (moduleInfo.Args().Length > 0) ?
                                    (AlertMode)Enum.Parse(typeof(AlertMode), moduleInfo.Args()[0], true) :
                                    AlertMode.emailonly;

                settings.StartHourMin = (moduleInfo.Args().Length > 1) ? int.Parse(moduleInfo.Args()[1]) : 0;

                settings.EndHourMin = (moduleInfo.Args().Length > 2) ? int.Parse(moduleInfo.Args()[2]) : 2400;

                settings.SuppressSeconds = (moduleInfo.Args().Length > 3) ? int.Parse(moduleInfo.Args()[3]) : 5;  //AJB shorten suppression

                settings.UserName = (moduleInfo.Args().Length > 4) ? moduleInfo.Args()[4] : "user";

                settings.emailAddress = GetPrivateConfSetting("NotificationEmail");

            }
            catch (Exception exception)
            {
                logger.Log("{0}: error parsing arguments: {1}", exception.ToString(), String.Join(" ", moduleInfo.Args()));
            }

            DoorNotifierSvc service = new DoorNotifierSvc(logger, this);

            //serviceHost = DoorNotifierSvc.CreateServiceHost(
            //    service,
            //    new Uri(moduleInfo.BaseURL()+"/webapp"));

            serviceHost = DoorNotifierSvc.CreateServiceHost(logger, this, service, moduleInfo.BaseURL() + "/webapp");

            serviceHost.Open();

            webUiServer = new WebFileServer(moduleInfo.BinaryDir(), moduleInfo.BaseURL(), logger);

            logger.Log("{0}: service is open for business at {1}", ToString(), moduleInfo.BaseURL());

            //no services are exported by this application

            //..... get the list of current ports from the platform
            IList<VPort> allPortsList = GetAllPortsFromPlatform();

            if (allPortsList != null)
            {
                foreach (VPort port in allPortsList)
                {
                    PortRegistered(port);
                }
            }

            //insert a fake notification for testing
            Alert newAlert = new Alert()
            {
                TimeTriggered = DateTime.Now,
                SensorFriendlyName = "fake sensor",
                SensorLocation = "fake location",
                Value = 1,
                Acknowledged = false,
            };

            InsertAlert(newAlert);
        }
예제 #6
0
        public override void OnNotification(string roleName, string opName, IList<VParamType> retVals, VPort senderPort)
        {
            if (retVals.Count >= 1)
            {
                byte val = (byte) (int) retVals[0].Value();

                //hack for techfest since we are using a multi-level switch as a doorbell
                //if (RoleSwitchMultiLevel.RoleName.Equals(roleName, StringComparison.CurrentCultureIgnoreCase))
                //    val = 0;

                Alert newAlert = new Alert() { TimeTriggered = DateTime.Now,
                                                                 SensorFriendlyName = senderPort.GetInfo().GetFriendlyName(),
                                                                 SensorLocation = senderPort.GetInfo().GetLocation().Name(),
                                                                 Value = val,
                                                                 Acknowledged = false, };

                bool notify = //settings.Mode != AlertMode.none &&
                              IsAlertTime() &&
                              !SuppressAlert(newAlert) &&
                              ((RoleSwitchMultiLevel.RoleName.Equals(roleName, StringComparison.CurrentCultureIgnoreCase) && (val == 99 || val == 0)) ||
                               (RoleSensor.RoleName.Equals(roleName, StringComparison.CurrentCultureIgnoreCase) && val == 255));

                logger.Log("{0}: got notified by {1} [{2}] val = {3} notify = {4}\n",
                           this.ToString(), newAlert.SensorFriendlyName, roleName, val.ToString(), notify.ToString());

                if (notify)
                {
                    InsertAlert(newAlert);
                    GenerateMessage(newAlert);
                }
            }
            else
            {
                logger.Log("{0}: got unexpected retvals [{1}] from {2}", ToString(), retVals.Count.ToString(), senderPort.ToString());
            }
        }