Example #1
0
        public override void OnNotification(string roleName, string opName, IList<VParamType> retVals, VPort senderPort)
        {
            string message;
            string sensorData;
            string sensorTag = senderPort.GetInfo().GetFriendlyName() + roleName;

            lock (this)
            {
                if (roleName.Contains(RoleSensor.RoleName) && opName.Equals(RoleSensor.OpGetName))
                {
                        byte rcvdNum = (byte) (int) retVals[0].Value();
                        sensorData = rcvdNum.ToString();

                }
                else if (roleName.Contains(RoleSensorMultiLevel.RoleName) && opName.Equals(RoleSensorMultiLevel.OpGetName))
                {
                    double rcvdNum = (double) retVals[0].Value();
                    sensorData = rcvdNum.ToString();
                }
                else
                {
                    sensorData = String.Format("Invalid role->op {0}->{1} from {2}", roleName, opName, sensorTag);
                }
            }

            //Write to the stream
            WriteToStream(sensorTag, sensorData);
            //Create local list of alerts for display
            message = String.Format("{0} {1},{2}", DateTime.Now, sensorTag, sensorData);
            this.receivedMessageList.Add(message);
            //Log
            logger.Log("{0},{1}", this.ToString(), message);
        }
Example #2
0
        /// <summary>
        /// Does the given port contain the given role
        /// </summary>
        /// <param name="port"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public static bool ContainsRole(VPort port, string roleName)
        {
            foreach (VRole role in port.GetInfo().GetRoles())
            {
                if (ContainsRole(role.Name(), roleName))
                    return true;
            }

            return false;
        }
Example #3
0
 public override void PortDeregistered(VPort port)
 {
     lock (this)
     {
         if (accessibleDummyPorts.Contains(port))
         {
             accessibleDummyPorts.Remove(port);
             logger.Log("{0} deregistered port {1}", this.ToString(), port.GetInfo().ModuleFacingName());
         }
     }
 }
        private void StopRecording(VPort cameraPort, bool force)
        {
            bool stopConditionMet = false;
            CameraInfo cameraInfo = registeredCameras[cameraPort];

            //if ((DateTime.Now - registeredCameras[cameraPort].CurrVideoStartTime).TotalMinutes >=
            //            MAX_VIDEO_CLIP_LEN_IN_MINUTES)

            if (DateTime.Now >= registeredCameras[cameraPort].CurrVideoEndTime)
            {
                stopConditionMet = true;
            }

            if ((force || stopConditionMet) && (cameraInfo.VideoWriter != null))
            {
                string cameraName = cameraPort.GetInfo().GetFriendlyName();
                VideoWriter VideoWriter = cameraInfo.VideoWriter;

                SafeThread helper = new SafeThread(delegate() { StopRecordingHelper(VideoWriter, cameraName); },
                                                    "stoprecordinghelper-" + cameraName, logger);
                helper.Start();

                cameraInfo.RecordVideo = false;
                cameraInfo.VideoWriter = null;
                cameraInfo.CurrVideoStartTime = DateTime.MinValue;
                cameraInfo.CurrVideoEndTime = DateTime.MinValue;

                if (stopConditionMet)
                {
                    logger.Log("Stop recording because the clip time has elapsed for {0}",
                            cameraPort.GetInfo().GetFriendlyName());
                }
                else
                {
                    logger.Log("Stop recording for {0}", cameraPort.GetInfo().GetFriendlyName());
                }
            }
        }
        // Starts a new recording if there isn't one already under way
        private void StartRecording(VPort cameraPort, int videoWidth, int videoHeight, int videoFPSNum, int videoFPSDen, int videoEncBitrate)
        {
            if (registeredCameras[cameraPort].VideoWriter != null)
            {
                return;
            }

            logger.Log("Started new clip for {0}", cameraPort.GetInfo().GetFriendlyName());
            CameraInfo cameraInfo = registeredCameras[cameraPort];

            string fileName = GetMediaFileName(cameraPort.GetInfo().GetFriendlyName(), MediaType.MediaType_Video_MP4);

            if (null == registeredCameras[cameraPort].VideoWriter)
            {
                registeredCameras[cameraPort].VideoWriter = new VideoWriter();
            }

            cameraInfo.CurrVideoStartTime = DateTime.Now;
            cameraInfo.CurrVideoEndTime = cameraInfo.CurrVideoStartTime + DEFAULT_VIDEO_CLIP_LEN;

            int result = cameraInfo.VideoWriter.Init(fileName, videoWidth, videoHeight, videoFPSNum, videoFPSDen, videoEncBitrate);

            if (result != 0)
            {
                string message = String.Format("Failed to start recording for {0} at {1}. Error code = {2}",
                                                cameraPort.GetInfo().GetFriendlyName(), DateTime.Now, result);
                logger.Log(message);
            }
        }
        public override void OnNotification(string roleName, string opName, IList<VParamType> retVals, VPort senderPort)
        {
            if (registeredCameras.ContainsKey(senderPort))
            {
                if (retVals.Count >= 1 && retVals[0].Value() != null)
                {
                    byte[] imageBytes = (byte[])retVals[0].Value();

                    lock (this)
                    {
                        registeredCameras[senderPort].LastImageBytes = imageBytes;

                        if (registeredCameras[senderPort].RecordVideo ||
                            registeredCameras[senderPort].EnableObjectTrigger)
                        {
                            bool addFrame = false;
                            Rectangle rectObject = new Rectangle(0, 0, 0, 0);
                            MemoryStream stream = new MemoryStream(imageBytes);
                            Bitmap image = null;
                            image = (Bitmap)Image.FromStream(stream);
                            if (null != registeredCameras[senderPort].BitmapImage)
                            {
                                registeredCameras[senderPort].BitmapImage.Dispose();
                                registeredCameras[senderPort].BitmapImage = null;
                            }
                            registeredCameras[senderPort].BitmapImage = image;

                            //lets check if the image is what we expect
                            if (image.PixelFormat != PixelFormat.Format24bppRgb)
                            {
                                string message = String.Format("Image format from {0} is not correct. PixelFormat: {1}",
                                                                senderPort.GetInfo().GetFriendlyName(), image.PixelFormat);
                                logger.Log(message);

                                return;
                            }

                            // stop if needed
                            StopRecording(senderPort, false /* force*/);

                            //// if recording is underway don't bother that, it will stop after that clip time lapses
                            //// if recording needs to be done only on motion (object) triggers, check with the result of the object
                            //// detector above
                            //if (registeredCameras[senderPort].RecordVideo)
                            //{
                            //    //if record video is still true, see if we need to add his frame
                            //    if (registeredCameras[senderPort].VideoWriter != null || !registeredCameras[senderPort].EnableObjectTrigger)
                            //    {
                            //        addFrame = true;
                            //    }
                            //    else
                            //    {
                            //        if (registeredCameras[senderPort].ObjectFound)
                            //            addFrame = true;
                            //    }
                            //}

                            if (registeredCameras[senderPort].RecordVideo)
                            {
                                addFrame = true;
                            }
                            else
                            {
                                if (registeredCameras[senderPort].EnableObjectTrigger &&
                                    registeredCameras[senderPort].ObjectFound)
                                    addFrame = true;
                            }

                            if (addFrame)
                            {

                                StartRecording(senderPort, image.Width, image.Height, VIDEO_FPS_NUM, VIDEO_FPS_DEN, VIDEO_ENC_FRAMERATE);

                                long sampleTime = (DateTime.Now - registeredCameras[senderPort].CurrVideoStartTime).Ticks;

                                AddFrameToVideo(image, senderPort, sampleTime);

                                if (registeredCameras[senderPort].ObjectFound)
                                {
                                    registeredCameras[senderPort].ObjectFound = false;
                                    rectObject = registeredCameras[senderPort].LastObjectRect;
                                    WriteObjectImage(senderPort, image, rectObject, true /* center */);
                                }

                            }
                        }
                    }
                }
                else
                {
                    logger.Log("{0} got null image", this.ToString());
                }
            }
        }
Example #7
0
        //, int imgWidth, int imgHeight, Image img)
        // Starts a new recording if there isn't one already under way
        //int i = 0;
        private void TakeImage(VPort cameraPort)
        {
            CameraInfo cameraInfo = registeredCameras[cameraPort];
            Image test = cameraInfo.BitmapImage;

            if (registeredCameras[cameraPort].BitmapImage == null)
            {
                return;
            }

            logger.Log("Taking image for {0}", cameraPort.GetInfo().GetFriendlyName());

            string filepath = GetMediaFileName(cameraPort.GetInfo().GetFriendlyName(), MediaType.MediaType_Image_JPEG);

            if (null == filepath)
            {
                logger.Log("GetMediaFileName failed to get a file name, are there more than 10 files of the same name?");
                return;
            }

            //string fileName = GetMediaFileName(cameraPort.GetInfo().GetFriendlyName(), MediaType.MediaType_Image_JPEG);
            //test.Save("C:\\Users\\seth\\Pictures\\Camera Roll\\suckmycock" + i + ".bmp", ImageFormat.Bmp);
            test.Save(filepath, ImageFormat.Jpeg);
            //i++;

            //if (registeredCameras[cameraPort].BitmapImage == null)
            //{
            //    return;
            //}

            //logger.Log("Taking image for {0}", cameraPort.GetInfo().GetFriendlyName());

            //Image test = cameraInfo.BitmapImage;

            //string fileName = GetMediaFileName(cameraPort.GetInfo().GetFriendlyName(), MediaType.MediaType_Image_JPEG);

            //if (null == registeredCameras[cameraPort].VideoWriter)
            //{
            //    registeredCameras[cameraPort].VideoWriter = new VideoWriter();
            //}

            //cameraInfo.CurrVideoStartTime = DateTime.Now;
            //cameraInfo.CurrVideoEndTime = cameraInfo.CurrVideoStartTime + DEFAULT_VIDEO_CLIP_LEN;

            //int result = cameraInfo.VideoWriter.Init(fileName, videoWidth, videoHeight, videoFPSNum, videoFPSDen, videoEncBitrate);
            //string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            //img.Save(path + "suckmycock.bmp", ImageFormat.Bmp);

            //if (result != 0)
            //{
            //    string message = String.Format("Failed to start recording for {0} at {1}. Error code = {2}",
            //                                    cameraPort.GetInfo().GetFriendlyName(), DateTime.Now, result);
            //    logger.Log(message);
            //}
        }
        private void AddFrameToVideo(Bitmap image, VPort cameraPort, long sampleTime)
        {
            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            int result;

            unsafe
            {
                result = registeredCameras[cameraPort].VideoWriter.AddFrame((byte*)ptr, 3 * image.Width * image.Height, image.Width, image.Height, sampleTime);
            }

            image.UnlockBits(bmpData);

            if (result != 0)
            {
                string message = String.Format("Failed to add frame for {0}. ResultCode: {1:x}", cameraPort.GetInfo().GetFriendlyName(), ((uint)result));
                logger.Log(message);

            }
        }
        private bool ExtractObjectFromFrame(Bitmap image, VPort cameraPort, ref Rectangle rectObject)
        {
            bool foundObject = false;

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            if (null == registeredCameras[cameraPort].ObjectDetector)
            {
                registeredCameras[cameraPort].ObjectDetector = new ObjectDetector();
            }

            unsafe
            {
                if (!registeredCameras[cameraPort].ObjectDetector.IsInitialized())
                {
                    registeredCameras[cameraPort].ObjectDetector.InitializeFromFrame((byte*)ptr, 3 * image.Width * image.Height, image.Width, image.Height, null);
                }
                else
                {
                    rectObject = registeredCameras[cameraPort].ObjectDetector.GetObjectRect((byte*)ptr, 3 * image.Width * image.Height);
                    if (rectObject.Width != 0 && rectObject.Height != 0)
                        foundObject = true;
                }

                if (foundObject)
                {
                    logger.Log("Object detected by camera {0} with co-ordinates X={1}, Y={2}, Width={3}, Height={4}",
                        cameraPort.GetInfo().GetFriendlyName(), rectObject.X.ToString(), rectObject.Y.ToString(), rectObject.Width.ToString(), rectObject.Height.ToString());
                }
            }

            image.UnlockBits(bmpData);

            return foundObject;

        }
Example #10
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());
            }
        }
        public override void OnNotification(string roleName, string opName, IList<VParamType> retVals, VPort senderPort)
        {
            timerCount++;
            if (timerCount >= 50)
            {
                timerCount = 0;
                //push_event_check = 0;

                /*
                if (push_event_check == 1)
                {
                    push_event_check = 2; timerCount = 0;
                    recordingController = 2;
                    Console.WriteLine(this.ToString() + " : Stopped Recording");
                }*/
            }

            if (registeredCameras.ContainsKey(senderPort))
            {
                if (retVals.Count >= 1 && retVals[0].Value() != null)
                {
                    byte[] imageBytes = (byte[])retVals[0].Value();

                    lock (this)
                    {
                        if (recordingController == 1)
                        {
                            registeredCameras[senderPort].RecordVideo = true;
                            recordingController = 0;
                        }
                        else if (recordingController == 2)
                        {
                            StopRecording(senderPort, true);
                            recordingController = 0;
                        }

                        registeredCameras[senderPort].LastImageBytes = imageBytes;

                        if (registeredCameras[senderPort].RecordVideo ||
                            registeredCameras[senderPort].EnableObjectTrigger)
                        {
                            bool addFrame = false;
                            Rectangle rectObject = new Rectangle(0, 0, 0, 0);
                            MemoryStream stream = new MemoryStream(imageBytes);
                            Bitmap image = null;
                            image = (Bitmap)Image.FromStream(stream);
                            if (null != registeredCameras[senderPort].BitmapImage)
                            {
                                registeredCameras[senderPort].BitmapImage.Dispose();
                                registeredCameras[senderPort].BitmapImage = null;
                            }
                            registeredCameras[senderPort].BitmapImage = image;

                            //lets check if the image is what we expect
                            if (image.PixelFormat != PixelFormat.Format24bppRgb)
                            {
                                string message = String.Format("Image  format from {0} is not correct. PixelFormat: {1}",
                                                                senderPort.GetInfo().GetFriendlyName(), image.PixelFormat);
                                logger.Log(message);

                                return;
                            }

                            // stop if needed
                            StopRecording(senderPort, false /* force*/);

                            //// if recording is underway don't bother that, it will stop after that clip time lapses
                            //// if recording needs to be done only on motion (object) triggers, check with the result of the object
                            //// detector above
                            //if (registeredCameras[senderPort].RecordVideo)
                            //{
                            //    //if record video is still true, see if we need to add his frame
                            //    if (registeredCameras[senderPort].VideoWriter != null || !registeredCameras[senderPort].EnableObjectTrigger)
                            //    {
                            //        addFrame = true;
                            //    }
                            //    else
                            //    {
                            //        if (registeredCameras[senderPort].ObjectFound)
                            //            addFrame = true;
                            //    }
                            //}

                            if (registeredCameras[senderPort].RecordVideo)
                            {
                                addFrame = true;
                            }
                            else
                            {
                                if (registeredCameras[senderPort].EnableObjectTrigger &&
                                    registeredCameras[senderPort].ObjectFound)
                                    addFrame = true;
                            }

                            if (addFrame)
                            {

                                StartRecording(senderPort, image.Width, image.Height, VIDEO_FPS_NUM, VIDEO_FPS_DEN, VIDEO_ENC_FRAMERATE);

                                long sampleTime = (DateTime.Now - registeredCameras[senderPort].CurrVideoStartTime).Ticks;

                                AddFrameToVideo(image, senderPort, sampleTime);

                                if (registeredCameras[senderPort].ObjectFound)
                                {
                                    registeredCameras[senderPort].ObjectFound = false;
                                    rectObject = registeredCameras[senderPort].LastObjectRect;
                                    WriteObjectImage(senderPort, image, rectObject, true /* center */);
                                }

                            }
                        }
                    }
                }
                else
                {
                    logger.Log("{0} got null image", this.ToString());
                }
            }
            else if (accessibleDummyPorts.Contains(senderPort))
            {

                string message;
                lock (this)
                {
                    switch (opName.ToLower())
                    {
                        case RoleDummy.OpEchoSubName:
                            int rcvdNum = (int)retVals[0].Value();
                            if (rcvdNum == 1)
                            {
                                message = "Bluetooth Signal";
                                if (timer_motion != null)
                                {
                                    timer_motion.Stop();
                                    timer_stoprecord.Stop();
                                }
                                if (timer_bluetooth != null)
                                {
                                    timer_bluetooth.Stop();
                                    timer_bluetooth.Start();
                                }
                                if (push_event_check == 1)
                                {
                                    push_event_check = 2;
                                    timerCount = 0;
                                    recordingController = 2;
                                    Console.WriteLine(this.ToString() + " : Stopped Recording");
                                }
                            }
                            else if (rcvdNum == 2)
                            {
                                message = "Motion Signal";
                                if (myThread == null)
                                {
                                    myThread = new Thread(new ThreadStart(SendMail));
                                    myThread.Start();
                                }

                                if (smartHome == null)
                                {
                                    smartHome = new Thread(new ThreadStart(SmartHomeRun));
                                    smartHome.Start();
                                }
                                if (timer_bluetooth == null)
                                {
                                    timer_bluetooth = new System.Timers.Timer();
                                    timer_bluetooth.Interval = 10 * 1000;
                                    timer_bluetooth.Elapsed += new System.Timers.ElapsedEventHandler(timer_bluetooth_Elapsed);
                                    timer_bluetooth.Start();
                                }

                                if (timer_motion == null)
                                {
                                    timer_motion = new System.Timers.Timer();
                                    timer_motion.Interval = 5 * 1000;
                                    timer_motion.Elapsed += new System.Timers.ElapsedEventHandler(timer_motion_Elapsed);
                                    timer_motion.Start();

                                    timer_stoprecord = new System.Timers.Timer();
                                    timer_stoprecord.Interval = 60 * 1000;
                                    timer_stoprecord.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                                    timer_stoprecord.Start();
                                }
                                else
                                {
                                    timer_motion.Start();
                                    timer_stoprecord.Start();
                                }
                            }
                            else if (rcvdNum == 3)
                            {
                                message = "Bluetooth & Motion Siganl";
                                if (smartHome == null)
                                {
                                    smartHome = new Thread(new ThreadStart(SmartHomeRun));
                                    smartHome.Start();
                                }
                                if (timer_bluetooth == null)
                                {
                                    timer_bluetooth = new System.Timers.Timer();
                                    timer_bluetooth.Interval = 10 * 1000;
                                    timer_bluetooth.Elapsed += new System.Timers.ElapsedEventHandler(timer_bluetooth_Elapsed);
                                    timer_bluetooth.Start();
                                }
                                if (timer_bluetooth != null)
                                {
                                    timer_bluetooth.Stop();
                                    timer_bluetooth.Start();
                                }
                                if (timer_motion != null)
                                {
                                    timer_motion.Stop();
                                    timer_stoprecord.Stop();
                                }
                                if (push_event_check == 1)
                                {
                                    push_event_check = 2; timerCount = 0;
                                    recordingController = 2;
                                    Console.WriteLine(this.ToString() + " : stopped Recording");
                                }
                            }
                            else if (rcvdNum == 0)
                            {
                                message = "Waitting";
                                smartHome = null;
                                myThread = null;
                            }
                            else
                            {
                                message = "error";
                            }
                            this.receivedMessageList.Add(message);
                            Console.WriteLine(message);
                            break;
                        default:
                            message = String.Format("Invalid async operation return {0} from {1}", opName.ToLower(), senderPort.ToString());
                            logger.Log(message);
                            break;
                    }
                }
                logger.Log("{0} {1}", this.ToString(), message);
            }

            lock (this)
            {
                //check if notification is speech event
                if (roleName.Contains(RoleSpeechReco.RoleName) && opName.Equals(RoleSpeechReco.OpPhraseRecognizedSubName))
                {
                    string rcvdCmd = (string)retVals[0].Value();

                    switch (rcvdCmd)
                    {
                        case "ALLON":
                            SetAllSwitches(1.0);
                            break;

                        case "ALLOFF":
                            SetAllSwitches(0.0);
                            break;

                        case "PLAYMOVIE":
                            SetAllSwitches(0.1);
                            break;

                        case "DISCO":
                            DiscoSwitches();
                            break;

                    }
                    return;
                }

                switch (opName)
                {
                    case RoleSwitchBinary.OpGetName:
                        {
                            if (retVals.Count >= 1 && retVals[0].Value() != null)
                            {
                                bool level = (bool)retVals[0].Value();

                                registeredSwitches[senderPort].Level = (level) ? 1 : 0;
                            }
                            else
                            {
                                logger.Log("{0} got bad result for getlevel subscription from {1}", this.ToString(), senderPort.ToString());
                            }
                        }
                        break;
                    case RoleSwitchMultiLevel.OpGetName:
                        {
                            if (retVals.Count >= 1 && retVals[0].Value() != null)
                            {
                                double level = (double)retVals[0].Value();

                                registeredSwitches[senderPort].Level = level;
                            }
                            else
                            {
                                logger.Log("{0} got bad result for getlevel subscription from {1}", this.ToString(), senderPort.ToString());
                            }
                        }
                        break;
                    case RoleLightColor.OpGetName:
                        {
                            if (!registeredSwitches[senderPort].IsColored)
                            {
                                logger.Log("Got {0} for non-colored switch {1}", opName, senderPort.ToString());

                                return;
                            }

                            if (retVals.Count >= 3)
                            {
                                byte red, green, blue;

                                red = Math.Min(Math.Max((byte)(int)retVals[0].Value(), (byte)0), (byte)255);
                                green = Math.Min(Math.Max((byte)(int)retVals[1].Value(), (byte)0), (byte)255);
                                blue = Math.Min(Math.Max((byte)(int)retVals[2].Value(), (byte)0), (byte)255);

                                registeredSwitches[senderPort].Color = Color.FromArgb(red, green, blue);
                            }
                            else
                            {
                                logger.Log("{0} got bad result for getlevel subscription from {1}", this.ToString(), senderPort.ToString());
                            }
                        }
                        break;
                    default:
                        logger.Log("Got notification from incomprehensible operation: " + opName);
                        break;
                }
            }
        }
        /// <summary>
        ///  Called when a new port is deregistered with the platform
        /// </summary>
        /// <param name="port"></param>
        public override void PortDeregistered(VPort port)
        {
            lock (this)
            {
                if (Role.ContainsRole(port, RoleCamera.RoleName))
                {
                    if (registeredCameras.ContainsKey(port))
                    {
                        ForgetCamera(port);
                        logger.Log("{0} deregistered camera port {1}", this.ToString(), port.GetInfo().ModuleFacingName());
                    }
                }
                else if (accessibleDummyPorts.Contains(port))
                {
                    accessibleDummyPorts.Remove(port);
                    logger.Log("{0} deregistered port {1}", this.ToString(), port.GetInfo().ModuleFacingName());
                }
                else if (registeredSwitches.ContainsKey(port))
                {
                    ForgetSwitch(port);
                }

            }
        }
        /// <summary>
        ///  Called when a new port is registered with the platform
        /// </summary>
        /// <param name="port"></param>
        public override void PortRegistered(VPort port)
        {
            lock (this)
            {
                if (Role.ContainsRole(port, RoleCamera.RoleName))
                {
                    if (!registeredCameras.ContainsKey(port))
                    {
                        InitCamera(port);
                    }
                    else
                    {
                        //the friendly name of the port might have changed. update that.
                        string oldFriendlyName = null;

                        foreach (var pair in cameraFriendlyNames)
                        {
                            if (pair.Value.Equals(port) &&
                                !pair.Key.Equals(port.GetInfo().GetFriendlyName()))
                            {
                                oldFriendlyName = pair.Key;
                                break;
                            }
                        }

                        if (oldFriendlyName != null)
                        {
                            cameraFriendlyNames.Remove(oldFriendlyName);
                            cameraFriendlyNames.Add(port.GetInfo().GetFriendlyName(), port);
                        }
                    }

                }
                else if (!accessibleDummyPorts.Contains(port) &&
                    Role.ContainsRole(port, RoleDummy.RoleName) &&
                    GetCapabilityFromPlatform(port) != null)
                {
                    accessibleDummyPorts.Add(port);

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

                    if (Subscribe(port, RoleDummy.Instance, RoleDummy.OpEchoSubName))
                        logger.Log("{0} subscribed to port {1}", this.ToString(), port.ToString());
                    else
                        logger.Log("failed to subscribe to port {1}", this.ToString(), port.ToString());
                }
                else if (Role.ContainsRole(port, RoleSwitchMultiLevel.RoleName) ||
                    Role.ContainsRole(port, RoleSwitchBinary.RoleName) ||
                    Role.ContainsRole(port, RoleLightColor.RoleName))
                {
                    if (!registeredSwitches.ContainsKey(port) &&
                        GetCapabilityFromPlatform(port) != null)
                    {
                        var switchType = (Role.ContainsRole(port, RoleSwitchMultiLevel.RoleName)) ? SwitchType.Multi : SwitchType.Binary;

                        bool colored = Role.ContainsRole(port, RoleLightColor.RoleName);

                        InitSwitch(port, switchType, colored);
                    }

                }

                else if (Role.ContainsRole(port, RoleSpeechReco.RoleName))
                {

                    if (!speechPorts.Contains(port) &&
                        GetCapabilityFromPlatform(port) != null)
                    {

                        speechPorts.Add(port);

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


                        //TODO Call it with phrases we care about - FOR NOW HARD CODED in Kinect driver
                        //  var retVal = Invoke(port, RoleSpeechReco.Instance, RoleSpeechReco.OpSetSpeechPhraseName, new ParamType(ParamType.SimpleType.text, "on"));

                        //subscribe to speech reco
                        if (Subscribe(port, RoleSpeechReco.Instance, RoleSpeechReco.OpPhraseRecognizedSubName))
                            logger.Log("{0} subscribed to port {1}", this.ToString(), port.ToString());
                    }
                }
            }
        }
        void ForgetSwitch(VPort switchPort)
        {
            switchFriendlyNames.Remove(switchPort.GetInfo().GetFriendlyName());

            registeredSwitches.Remove(switchPort);

            logger.Log("{0} removed switch/light port {1}", this.ToString(), switchPort.ToString());
        }
        void InitSwitch(VPort switchPort, SwitchType switchType, bool isColored)
        {

            logger.Log("{0} adding switch {1} {2}", this.ToString(), switchType.ToString(), switchPort.ToString());

            SwitchInfo switchInfo = new SwitchInfo();
            switchInfo.Capability = GetCapability(switchPort, Constants.UserSystem);
            switchInfo.Level = 0;
            switchInfo.Type = switchType;

            switchInfo.IsColored = isColored;
            switchInfo.Color = Color.Black;

            registeredSwitches.Add(switchPort, switchInfo);

            string switchFriendlyName = switchPort.GetInfo().GetFriendlyName();
            switchFriendlyNames.Add(switchFriendlyName, switchPort);

            if (switchInfo.Capability != null)
            {
                IList<VParamType> retVals;

                if (switchType == SwitchType.Multi)
                {
                    retVals = switchPort.Invoke(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName, null,
                    ControlPort, switchInfo.Capability, ControlPortCapability);

                    switchPort.Subscribe(RoleSwitchMultiLevel.RoleName, RoleSwitchMultiLevel.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability);

                    if (retVals[0].Maintype() < 0)
                    {
                        logger.Log("SwitchController could not get current level for {0}", switchFriendlyName);
                    }
                    else
                    {
                        switchInfo.Level = (double)retVals[0].Value();
                    }
                }
                else
                {
                    retVals = switchPort.Invoke(RoleSwitchBinary.RoleName, RoleSwitchBinary.OpGetName, null,
                    ControlPort, switchInfo.Capability, ControlPortCapability);

                    switchPort.Subscribe(RoleSwitchBinary.RoleName, RoleSwitchBinary.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability);

                    if (retVals[0].Maintype() < 0)
                    {
                        logger.Log("SwitchController could not get current level for {0}", switchFriendlyName);
                    }
                    else
                    {
                        bool boolLevel = (bool)retVals[0].Value();
                        switchInfo.Level = (boolLevel) ? 1 : 0;
                    }
                }

                //fix the color up now

                if (isColored)
                {
                    var retValsColor = switchPort.Invoke(RoleLightColor.RoleName, RoleLightColor.OpGetName, null,
                                                          ControlPort, switchInfo.Capability, ControlPortCapability);

                    switchPort.Subscribe(RoleLightColor.RoleName, RoleLightColor.OpGetName, ControlPort, switchInfo.Capability, ControlPortCapability);

                    if (retVals[0].Maintype() < 0)
                    {
                        logger.Log("SwitchController could not get color for {0}", switchFriendlyName);
                    }
                    else
                    {
                        byte red, green, blue;

                        red = Math.Min(Math.Max((byte)(int)retValsColor[0].Value(), (byte)0), (byte)255);
                        green = Math.Min(Math.Max((byte)(int)retValsColor[1].Value(), (byte)0), (byte)255);
                        blue = Math.Min(Math.Max((byte)(int)retValsColor[2].Value(), (byte)0), (byte)255);

                        switchInfo.Color = Color.FromArgb(red, green, blue);
                    }
                }
            }
        }
        //called when the lock is acquired
        private void ForgetCamera(VPort cameraPort)
        {
            cameraFriendlyNames.Remove(cameraPort.GetInfo().GetFriendlyName());

            //stop recording if we have a video make object
            StopRecording(cameraPort, true);

            registeredCameras.Remove(cameraPort);

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

        }
        //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);
        }
Example #18
0
        private void RemovePortFromConfig(VPort port , VModule module)
        {
            PortInfo targetPortInfo = new PortInfo(port.GetInfo().ModuleFacingName(), module.GetInfo());
            PortInfo matchingPortInfo = config.GetMatchingPortInfo(targetPortInfo);
            if (matchingPortInfo == null)
            {
            }
            else
            {
                config.RemovePort(matchingPortInfo);
            } 

        }
        private void WriteObjectImage(VPort cameraPort, Bitmap image, Rectangle rectSrc, bool center)
        {
            Rectangle rectTarget = rectSrc;
            int srcPixelShiftX = 0;
            int srcPixelShiftY = 0;

            if (rectSrc.Width == 0 && rectSrc.Height == 0)
            {
                logger.Log("Write Object Image Called with Rect with zero height and width!");
                return;
            }

            if (center)
            {
                rectTarget.X = (int)((image.Width - rectSrc.Width) / 2.0);
                rectTarget.Y = (int)((image.Height - rectSrc.Height) / 2.0);
                srcPixelShiftX = rectTarget.X - rectSrc.X;
                srcPixelShiftY = rectTarget.Y - rectSrc.Y;
            }

            // create the destination based upon layer one
            BitmapData bmpData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            int stride = bmpData.Stride;
            image.UnlockBits(bmpData);

            WriteableBitmap composite = new WriteableBitmap(image.Width, image.Height, 96, 96, System.Windows.Media.PixelFormats.Rgb24, null);
            Int32Rect sourceRect = new Int32Rect(0, 0, (int)image.Width, (int)image.Height);
            byte[] pixels = new byte[stride * image.Height];

            for (int x = 0; x < image.Width; ++x)
            {
                for (int y = 0; y < image.Height; ++y)
                {
                    if (rectSrc.Contains(x, y))
                    {
                        Color clr = image.GetPixel(x, y);
                        pixels[stride * (y + srcPixelShiftY) + 3 * (x + srcPixelShiftX)] = clr.R;
                        pixels[stride * (y + srcPixelShiftY) + 3 * (x + srcPixelShiftX) + 1] = clr.G;
                        pixels[stride * (y + srcPixelShiftY) + 3 * (x + srcPixelShiftX) + 2] = clr.B;
                    }
                    else if (!rectTarget.Contains(x, y))
                    {
                        pixels[stride * y + 3 * x] = 0x00;
                        pixels[stride * y + 3 * x + 1] = 0x00;
                        pixels[stride * y + 3 * x + 2] = 0x00;
                    }
                }
            }
            composite.WritePixels(sourceRect, pixels, stride, 0);

            // encode the bitmap to the output file
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(composite));
            string filepath = GetMediaFileName(cameraPort.GetInfo().GetFriendlyName(), MediaType.MediaType_Image_JPEG);

            if (null == filepath)
            {
                logger.Log("GetMediaFileName failed to get a file name, are there more than 10 files of the same name?");
                return;
            }

            using (var stream = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
            {
                encoder.Save(stream);
            }
        }
Example #20
0
        /// <summary>
        /// Issues a capability
        /// </summary>
        /// <param name="module">The module that is asking for the capability</param>
        /// <param name="targetPort">The port for which the capability is being requested</param>
        /// <param name="userName">The name of the user on behalf of which the capability is being requested</param>
        /// <param name="password">The password of the user</param>
        /// <returns>The issued capability</returns>
        public VCapability GetCapability(VModule requestingModule, VPort targetPort, string username, string password)
        {
            // ....
            //check if the user is valid, the module exists, and the port is registered
            // ....
            if (!username.Equals(Constants.UserSystem.Name) && 
                config.ValidUserUntil(username, password) < DateTime.Now)
                return null;

            if (!runningModules.ContainsKey(requestingModule))
                return null;

            //if (!config.allPorts.ContainsKey(targetPort.GetInfo()))
            //    return null;

            if (!registeredPorts.ContainsKey(targetPort))
                return null;

            string portName = targetPort.GetInfo().GetFriendlyName();

            DateTime allowedUntil = (Settings.EnforcePolicies) ? 
                                        policyEngine.AllowAccess(portName, requestingModule.GetInfo().FriendlyName(), username) : 
                                        DateTime.MaxValue;

            if (allowedUntil < DateTime.Now)
            {
                return null;
            }

            Capability capability = new Capability("platform", allowedUntil);

            if (RemoteInstallCapability(capability, targetPort))
            {
                return capability;
            }
            else
            {
                return null;
            }
        }
Example #21
0
        public override void OnNotification(string roleName, string opName, IList<VParamType> retVals, VPort senderPort)
        {
            string message;
            string sensorData;
            string sensorTag = senderPort.GetInfo().GetFriendlyName() + roleName;

            lock (this)
            {
                if (roleName.Contains(RoleSensor.RoleName) && opName.Equals(RoleSensor.OpGetName))
                {
                    byte rcvdNum = (byte)(int)retVals[0].Value();
                    sensorData = rcvdNum.ToString();

                }
                else if (roleName.Contains(RoleSensorMultiLevel.RoleName) && opName.Equals(RoleSensorMultiLevel.OpGetName))
                {
                    double rcvdNum = (double)retVals[0].Value();
                    sensorData = rcvdNum.ToString();
                }
                else
                {
                    sensorData = String.Format("Sensor: Invalid role->op {0}->{1} from {2}", roleName, opName, sensorTag);
                }

                try
                {
                    //Write to the stream
                    WriteToStream(sensorTag, sensorData);
                }
                catch (Exception exp)
                {
                    logger.Log("Sensor:{0}: WriteToStream failed. Exception caught.");
                    logger.Log(exp.ToString());
                }
            }

            //Create local list of alerts for display
            message = String.Format("{0}\t{1}\t{2}", DateTime.Now, sensorTag, sensorData);
            this.receivedMessageList.Enqueue(message);
            if (this.receivedMessageList.Count > gNumStringstoShow)
                this.receivedMessageList.Dequeue();
            logger.Log("Sensor\t{0}", message);

            //if applicable write the information to a local file.
            if (this.writingToLocalFile)
                WriteToLocalFile(message);

            //record latest data in our monitoring dictionary
            if (monitorDataValues.ContainsKey(sensorTag))
            {
                (monitorDataValues[sensorTag]).UpdateDataVal(sensorData);
            }
            else
            {
                monitorDataValues.Add(sensorTag, new MonitorDataVal(sensorData));
            }
        }
        /// <summary>
        ///  Called when a new port is registered with the platform
        /// </summary>
        /// <param name="port"></param>
        public override void PortRegistered(VPort port)
        {
            lock (this)
            {
                if (Role.ContainsRole(port, RoleCamera.RoleName))
                {
                    if (!registeredCameras.ContainsKey(port))
                    {
                        InitCamera(port);
                    }
                    else
                    {
                        //the friendly name of the port might have changed. update that.
                        string oldFriendlyName = null;

                        foreach (var pair in cameraFriendlyNames)
                        {
                            if (pair.Value.Equals(port) &&
                                !pair.Key.Equals(port.GetInfo().GetFriendlyName()))
                            {
                                oldFriendlyName = pair.Key;
                                break;
                            }
                        }

                        if (oldFriendlyName != null)
                        {
                            cameraFriendlyNames.Remove(oldFriendlyName);
                            cameraFriendlyNames.Add(port.GetInfo().GetFriendlyName(), port);
                        }
                    }

                }
            }
        }
Example #23
0
        public override void OnNotification(string roleName, string opName, IList<VParamType> retVals, VPort senderPort)
        {
            string message;
            string sensorData;
            string sensorTag = senderPort.GetInfo().GetFriendlyName() + roleName;

            lock (this)
            {
                if (roleName.Contains(RoleSensor.RoleName) && opName.Equals(RoleSensor.OpGetName))
                {
                    byte rcvdNum = (byte)(int)retVals[0].Value();
                    sensorData = rcvdNum.ToString();

                }
                else if (roleName.Contains(RoleSensorMultiLevel.RoleName) && opName.Equals(RoleSensorMultiLevel.OpGetName))
                {
                    double rcvdNum = (double)retVals[0].Value();
                    sensorData = rcvdNum.ToString();
                }
                else
                {
                    sensorData = String.Format("Sensor: Invalid role->op {0}->{1} from {2}", roleName, opName, sensorTag);
                }

                     //log to the azure sql db via azure mobile services
                    WriteToDB(sensorTag, Convert.ToSingle(sensorData));

              }

            //Create local list of alerts for display
            message = String.Format("{0}\t{1}\t{2}", DateTime.Now, sensorTag, sensorData);
            this.receivedMessageList.Enqueue(message);
            if (this.receivedMessageList.Count > gNumStringstoShow)
                this.receivedMessageList.Dequeue();
            logger.Log("Sensor\t{0}", message);

            //if applicable write the information to a local file.
            if (this.writingToLocalFile)
                WriteToLocalFile(message);

 
        }
 /// <summary>
 /// Get device Type from VPort
 /// </summary>
 /// <param name="vp"></param>
 /// <returns></returns>
 private String getDeviceTypeFromPort(VPort vp) {
     VPortInfo vpi = vp.GetInfo();
     string address = vpi.ModuleFacingName();
     string[] split = address.Split('|');
     address = split[split.Length - 3].ToString();
     address = address.Trim();
     return address;
 }