Exemple #1
0
        //called when the lock is acquired and cameraPort is non-existent in the dictionary
        private void InitCamera(VPort cameraPort)
        {
            VCapability capability = GetCapability(cameraPort, Constants.UserSystem);

            //return if we didn't get a capability
            if (capability == null)
            {
                logger.Log("{0} didn't get a capability for {1}", this.ToString(), cameraPort.ToString());

                return;
            }

            //otherwise, add this to our list of cameras

            logger.Log("{0} adding camera port {1}", this.ToString(), cameraPort.ToString());

            CameraInfo cameraInfo = new CameraInfo();

            cameraInfo.Capability         = capability;
            cameraInfo.LastImageBytes     = new byte[0];
            cameraInfo.VideoWriter        = null;
            cameraInfo.CurrVideoStartTime = DateTime.MinValue;
            cameraInfo.CurrVideoEndTime   = DateTime.MinValue;

            registeredCameras.Add(cameraPort, cameraInfo);

            string cameraFriendlyName = cameraPort.GetInfo().GetFriendlyName();

            cameraFriendlyNames.Add(cameraFriendlyName, cameraPort);

            cameraPort.Subscribe(RoleCamera.RoleName, RoleCamera.OpGetVideo, ControlPort, cameraInfo.Capability, ControlPortCapability);
        }
        public override void PortRegistered(VPort port)
        {
            lock (this)
            {
                if (Role.ContainsRole(port, RoleValve.RoleName))
                {
                    VCapability capability = GetCapability(port, Constants.UserSystem);

                    if (registeredValves.ContainsKey(port))
                    {
                        registeredValves[port] = capability;
                    }
                    else
                    {
                        registeredValves.Add(port, capability);
                    }

                    if (capability != null)
                    {
                        port.Subscribe(RoleSensor.RoleName, RoleSensor.OpGetName,
                                       this.ControlPort, capability, this.ControlPortCapability);
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// called by (app) modules to subscribe to another port's role and operation pair
        /// </summary>
        /// <param name="port"></param>
        /// <param name="role"></param>
        /// <param name="opName"></param>
        /// <returns></returns>
        protected bool Subscribe(VPort port, Role role, string opName)
        {
            //first check the store if we have the capability
            VCapability capability = GetCapabilityFromStore(port);

            //if not, then try to get it from the platform
            if (capability == null)
            {
                capability = GetCapabilityFromPlatform(port);
            }

            //if capability is still null, throw exception
            if (capability == null)
            {
                throw new AccessViolationException("This module does not have access to this port");
            }

            var operation = role.GetOperation(opName);

            if (operation == null)
            {
                throw new InvalidOperationException(opName + " is not a valid operation for Role " + role.Name());
            }

            if (!operation.Subscribeable())
            {
                throw new InvalidOperationException(opName + " is not a subscribable operation for Role " + role.Name());
            }


            return(port.Subscribe(role.Name(), opName, ControlPort, capability, ControlPortCapability));
        }
Exemple #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);
            }
        }
Exemple #5
0
        /// <summary>
        /// Obtains the capability for sending requests to a port
        /// </summary>
        /// <param name="targetPort">The port for which the capability is desired</param>
        /// <param name="userInfo">The user on whose behalf the capability is desired</param>
        /// <returns></returns>
        protected VCapability GetCapability(VPort targetPort, UserInfo userInfo)
        {
            VCapability capability = platform.GetCapability(this, targetPort, userInfo.Name, userInfo.Password);

            if (capability == null)
            {
                logger.Log("{0} failed to get capability for {1}", this.ToString(), targetPort.ToString());
            }
            else
            {
                logger.Log("{0} got capability for {1}", this.ToString(), targetPort.ToString());
            }

            return(capability);
        }
Exemple #6
0
        internal static ICapability V2C(VCapability view)
        {
            if (view == null)
            {
                return(null);
            }

            if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(view) &&
                (view.GetType().Equals(typeof(CapabilityC2V))))
            {
                return(((CapabilityC2V)(view)).GetSourceContract());
            }
            else
            {
                return(new CapabilityV2C(view));
            }
        }
Exemple #7
0
        /// <summary>
        /// Called from the platform to install a new capability for a port that is owned by this module
        /// </summary>
        /// <param name="capability">The capability to install</param>
        /// <param name="targetPort">The port on which to install the capability</param>
        /// <returns></returns>
        public sealed override int InstallCapability(VCapability capability, VPort targetPort)
        {
            ResultCode resultCode;

            if (!IsMyPort(targetPort))
            {
                logger.Log("{0} got InstallCapability request for somemeone else's port {1}", targetPort.ToString());

                resultCode = ResultCode.PortNotFound;
            }
            else
            {
                Port port = (Port)targetPort;
                port.AddCapability(capability);

                resultCode = ResultCode.Success;
            }

            return((int)resultCode);
        }
Exemple #8
0
        /// <summary>
        /// Checks whether we have access to the port by asking for a capability
        /// If we get a valid capability, it has the side effect of storing that capability in the store
        /// </summary>
        /// <param name="targetPort"></param>
        /// <returns></returns>
        protected VCapability GetCapabilityFromPlatform(VPort targetPort)
        {
            VCapability capability = GetCapability(targetPort, Constants.UserSystem);

            if (capability == null)
            {
                return(null);
            }

            lock (capabilityStore)
            {
                if (capabilityStore.ContainsKey(targetPort))
                {
                    capabilityStore[targetPort] = capability;
                }
                else
                {
                    capabilityStore.Add(targetPort, capability);
                }
            }

            return(capability);
        }
Exemple #9
0
 public void CancelAllSubscriptions(VModule module, VPort controlPort, VCapability controlportcap)
 {
     _contract.CancelAllSubscriptions(ModuleAdapter.V2C(module), PortAdapter.V2C(controlPort), CapabilityAdapter.V2C(controlportcap));
 }
Exemple #10
0
 public override int InstallCapability(VCapability capability, VPort targetPort)
 {
     return(_contract.InstallCapability(CapabilityAdapter.V2C(capability), PortAdapter.V2C(targetPort)));
 }
Exemple #11
0
 public CapabilityV2C(VCapability view)
 {
     _view = view;
 }
Exemple #12
0
 public override bool Subscribe(string roleName, string opName, VPort fromPort, VCapability reqCap, VCapability respCap)
 {
     return(_contract.Subscribe(roleName, opName, PortAdapter.V2C(fromPort), CapabilityAdapter.V2C(reqCap), CapabilityAdapter.V2C(respCap)));
 }
Exemple #13
0
 public override void AsyncReturn(string roleName, string opName, IList <VParamType> retVals, VPort p, VCapability respCap)
 {
     _contract.AsyncReturn(roleName, opName,
                           CollectionAdapters.ToIListContract <VParamType, IParamType>(retVals, BaseTypeAdapter.V2C, BaseTypeAdapter.C2V),
                           PortAdapter.V2C(p), CapabilityAdapter.V2C(respCap));
 }
Exemple #14
0
 public override IList <VParamType> Invoke(string roleName, string opName, IList <VParamType> parameters, VPort p, VCapability reqCap, VCapability respCap)
 {
     return(CollectionAdapters.ToIList <IParamType, VParamType>(_contract.Invoke(roleName, opName,
                                                                                 CollectionAdapters.ToIListContract <VParamType, IParamType>(parameters, BaseTypeAdapter.V2C, BaseTypeAdapter.C2V),
                                                                                 PortAdapter.V2C(p),
                                                                                 CapabilityAdapter.V2C(reqCap),
                                                                                 CapabilityAdapter.V2C(respCap)),
                                                                BaseTypeAdapter.C2V, BaseTypeAdapter.V2C));
 }
Exemple #15
0
        public override void PortRegistered(VPort port)
        {
            lock (this)
            {
                if (Role.ContainsRole(port, RoleCamera.RoleName))
                {
                    VCapability capability = GetCapability(port, Constants.UserSystem);

                    if (cameraPorts.ContainsKey(port))
                    {
                        cameraPorts[port] = capability;
                    }
                    else
                    {
                        cameraPorts.Add(port, capability);
                    }

                    logger.Log("{0} added camera port {1}", this.ToString(), port.ToString());
                }

                //lets not monitor switches
                //if (Role.ContainsRole(port, RoleSwitchMultiLevel.RoleName))
                //{
                //    if (port.GetInfo().GetFriendlyName().Equals(switchFriendlyName, StringComparison.CurrentCultureIgnoreCase))
                //    {
                //        switchPort = port;
                //        switchPortCapability = GetCapability(port, Constants.UserSystem);

                //        if (switchPortCapability != null)
                //        {
                //            switchPort.Subscribe(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName,
                //                this.ControlPort, switchPortCapability, this.ControlPortCapability);
                //        }
                //    }
                //}

                if (Role.ContainsRole(port, RoleSensor.RoleName))
                {
                    //if (port.GetInfo().GetFriendlyName().Equals(sensorFriendlyName, StringComparison.CurrentCultureIgnoreCase))
                    //{
                    //    sensorPort = port;
                    VCapability capability = GetCapability(port, Constants.UserSystem);

                    if (registeredSensors.ContainsKey(port))
                    {
                        registeredSensors[port] = capability;
                    }
                    else
                    {
                        registeredSensors.Add(port, capability);
                    }

                    if (capability != null)
                    {
                        port.Subscribe(RoleSensor.RoleName, RoleSensor.OpGetName,
                                       this.ControlPort, capability, this.ControlPortCapability);
                    }
                    //}
                }
            }
        }
Exemple #16
0
        /// <summary>
        /// Called by (app) modules to invoke operations
        /// </summary>
        /// <param name="toPort"></param>
        /// <param name="role"></param>
        /// <param name="opName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public IList <VParamType> Invoke(VPort toPort, Role role, string opName, params ParamType[] values)
        {
            //first check the store if we have the capability
            VCapability capability = GetCapabilityFromStore(toPort);

            //if not, then try to get it from the platform
            if (capability == null)
            {
                capability = GetCapabilityFromPlatform(toPort);
            }

            //if capability is still null, throw exception
            if (capability == null)
            {
                throw new AccessViolationException("This module does not have access to this port");
            }


            var operation = role.GetOperation(opName);

            if (operation == null)
            {
                throw new InvalidOperationException(opName + " is not a valid operation for Role " + role.Name());
            }

            //check the number of parameters
            if (values.Length != operation.Parameters().Count)
            {
                throw new ArgumentException("Incorrect number of arguments for operation " + opName);
            }

            //check the types of arguments
            for (int index = 0; index < values.Length; index++)
            {
                if ((values[index].Maintype() != operation.Parameters()[index].Maintype()))
                {
                    throw new ArgumentException(String.Format("Argument {0} is of invalid type. Expected {1}. Got {2}.", index, (ParamType.SimpleType)operation.Parameters()[index].Maintype(), (ParamType.SimpleType)values[index].Maintype()));
                }
            }

            //now invoke the operation
            IList <VParamType> retVals = toPort.Invoke(role.Name(), opName, values, ControlPort, capability, ControlPortCapability);

            //if we got an error, pass it along
            if (retVals.Count >= 1 && retVals[0].Maintype() == (int)ParamType.SimpleType.error)
            {
                return(retVals);
            }

            //if we didn't get an error, sanity check the return values

            //check the number of elements
            if (retVals.Count != operation.ReturnValues().Count)
            {
                throw new ArgumentException("Incorrect number of return values for operation " + opName);
            }

            //check the types of elements in return values
            for (int index = 0; index < retVals.Count; index++)
            {
                if ((retVals[index].Maintype() != operation.ReturnValues()[index].Maintype()))
                {
                    throw new ArgumentException(String.Format("Return value {0} is of invalid type. Expected {1}. Got {2}.", index, (ParamType.SimpleType)operation.ReturnValues()[index].Maintype(), (ParamType.SimpleType)retVals[index].Maintype()));
                }
            }

            //otherwise, lets return these values
            return(retVals);
        }