void OnCameraChanged(CameraInfo oldCameara, CameraInfo newCamera)
        {
            if (device != null)
            {
                oldCameara.PropertyChanged -= OnCameraPropertyChanged;
                device.Error          -= OnDeviceError;
                device.FrameAvailable -= OnFrameAvailable;
                device.StopStream();
            }

            if (newCamera != null)
            {
                device = new FoscamDevice()
                {
                    CameraInfo = newCamera
                };
                device.Error              += OnDeviceError;
                device.FrameAvailable     += OnFrameAvailable;
                newCamera.PropertyChanged += OnCameraPropertyChanged;
                StartUpdateSnapshot(true);
                OnRotationChanged();
                if (device.CameraInfo.LastFrame != null)
                {
                    CameraImage.Source = device.CameraInfo.LastFrame as ImageSource;
                }
            }
        }
        void StartSearch()
        {
            if (store.Cameras.Count == 0)
            {
                Prompt.Text          = StringResources.SearchingPrompt;
                searchTimer          = new DispatcherTimer();
                searchTimer.Interval = TimeSpan.FromSeconds(10);
                searchTimer.Tick    += OnSearchTick;
                searchTimer.Start();
            }
            else
            {
                foreach (var cam in store.Cameras)
                {
                    cam.PropertyChanged += OnCameraPropertyChanged;
                }

                store.Cameras.CollectionChanged += Cameras_CollectionChanged;
            }

            try
            {
                FoscamDevice.StartFindDevices();
                FoscamDevice.DeviceAvailable -= OnDeviceAvailable;
                FoscamDevice.DeviceAvailable += OnDeviceAvailable;
            }
            catch (Exception ex)
            {
                Prompt.Text = "Error: " + ex.Message;
            }
        }
 async void OnDeviceAvailable(object sender, FoscamDevice e)
 {
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
     {
         SetupNewCamera(e);
     }));
 }
        private void CheckFirmwareVersion(FoscamDevice device)
        {
            App app    = (App)(App.Current);
            var info   = device.CameraInfo;
            var update = app.Firmware.FindUpdate(info.SystemVersion);

            if (update != null)
            {
                info.UpdateAvailable = update;
            }
        }
        private async void SetupNewCamera(FoscamDevice device)
        {
            var newCam = store.MergeNewCamera(device.CameraInfo);

            if (newCam.IsNew || newCam.LastPingTime + 10000 < Environment.TickCount)
            {
                // get the device properties
                device = new FoscamDevice()
                {
                    CameraInfo = newCam
                };
                var properties = await device.GetStatus();

                if (properties.HasValue("Error"))
                {
                    device.CameraInfo.StaticError = properties.GetValue <string>("Error");
                }
                else
                {
                    var realName = properties.GetValue <string>("alias");
                    if (!string.IsNullOrEmpty(realName))
                    {
                        newCam.Name = realName;
                    }

                    var sysver = properties.GetValue <string>("sys_ver");
                    if (!string.IsNullOrEmpty(sysver))
                    {
                        newCam.SystemVersion = sysver;
                    }

                    var webver = properties.GetValue <string>("app_ver");
                    if (!string.IsNullOrEmpty(webver))
                    {
                        newCam.WebUiVersion = webver;
                    }

                    CheckFirmwareVersion(device);
                    newCam.IsNew = false;
                }
            }

            Prompt.Text             = "";
            newCam.LastPingTime     = Environment.TickCount;
            newCam.Rebooting        = false;
            newCam.UpdatingFirmware = false;
            newCam.PropertyChanged -= OnCameraPropertyChanged;
            newCam.PropertyChanged += OnCameraPropertyChanged;

            if (store.Cameras.Count > 1 && noCamera != null && store.Cameras.Contains(noCamera))
            {
                this.RemoveNoCameraMessage();
            }
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // bring up camera details page...
            Log.WriteLine("FoscamDetailsPage OnNavigatedTo");

            CameraInfo camera = (CameraInfo)e.Parameter;

            device = new FoscamDevice()
            {
                CameraInfo = camera
            };

            device.Error           += OnDeviceError;
            device.FrameAvailable  += OnFrameAvailable;
            camera.PropertyChanged += OnCameraPropertyChanged;

            if (camera.UpdateAvailable != null)
            {
                //ButtonUpdate.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }

            this.Reconnect();

            this.DataContext = camera;

            if (camera.IpAddress != null)
            {
                deviceParams = await device.GetParams();

                ShowParameters(deviceParams);
            }

            OnRotationChanged();

            //SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;

            if (camera.IpAddress != null)
            {
                var p = await device.GetCameraParams();

                if (p.HasValue("brightness"))
                {
                    device.CameraInfo.Brightness = p.GetValue <byte>("brightness");
                }
                if (p.HasValue("contrast"))
                {
                    device.CameraInfo.Contrast = p.GetValue <byte>("contrast");
                }
            }
        }
        private async void OnZoomChanged(object sender, double zoomDirection)
        {
            FoscamDevice d = this.device;

            if (d != null)
            {
                if (zoomDirection < 0)
                {
                    await d.ZoomOut();
                }
                else
                {
                    await d.ZoomIn();
                }
            }
        }
        internal void Disconnect()
        {
            Log.WriteLine("MainPage disconnecting");

            FoscamDevice.StopFindingDevices();
            FoscamDevice.DeviceAvailable -= OnDeviceAvailable;

            foreach (var cam in store.Cameras)
            {
                cam.PropertyChanged -= OnCameraPropertyChanged;
            }

            store.Cameras.CollectionChanged -= Cameras_CollectionChanged;

            CameraGrid.ItemsSource = null;
        }
Exemple #9
0
 private async Task UpdateWifiOnAllCameras(WifiNetworkInfo info)
 {
     foreach (var camera in DataStore.Instance.Cameras)
     {
         if (camera != this.device.CameraInfo)
         {
             FoscamDevice temp = new FoscamDevice()
             {
                 CameraInfo = camera
             };
             if (camera.WifiNetwork == null || camera.WifiNetwork.SSID != info.SSID || camera.WifiPassword != device.CameraInfo.WifiPassword)
             {
                 camera.WifiPassword         = device.CameraInfo.WifiPassword;
                 temp.CameraInfo.WifiNetwork = info;
                 await temp.UpdateWifiSettings();
             }
         }
     }
 }
        private async Task <bool> UpdateAccountInfo(FoscamDevice device, string userName, string password, bool showError)
        {
            var result = await device.ChangeUserPassword(userName, password);

            if (!string.IsNullOrEmpty(result))
            {
                if (showError)
                {
                    ShowError(result);
                }
                return(false);
            }
            else
            {
                device.CameraInfo.UserName = userName;
                device.CameraInfo.Password = password;
                return(true);
            }
        }
 private async Task ChangeUserNamePassword(string userName, string password, bool updateAll)
 {
     if (await UpdateAccountInfo(device, userName, password, true))
     {
         if (updateAll)
         {
             foreach (var camera in DataStore.Instance.Cameras)
             {
                 if (camera != this.device.CameraInfo)
                 {
                     FoscamDevice temp = new FoscamDevice()
                     {
                         CameraInfo = camera
                     };
                     await UpdateAccountInfo(device, userName, password, false);
                 }
             }
         }
     }
 }
        async void OnMoveTick(object sender, object e)
        {
            int             id    = this.moveTimerId;
            DispatcherTimer timer = this.moveTimer;

            if (timer != null)
            {
                int now  = Environment.TickCount;
                int diff = now - lastMove;
                if (diff < msPerMove)
                {
                    return;
                }
                lastMove = now;

                timer.Stop();

                // try and make movedSoFar match moveDirection,
                // this is similar to a pixel line drawing algorithm.

                FoscamDevice d = this.device;
                if (d != null)
                {
                    CameraDirection direction = CameraDirection.Right;

                    steps++;

                    // this is where we should have moved to.
                    Point projected = new Point(moveDirection.X * steps, moveDirection.Y * steps);

                    if (projected.X.IsAlmost(0))
                    {
                        // vertical
                        direction = (projected.Y < 0) ? CameraDirection.Down : CameraDirection.Up;
                    }
                    else if (projected.Y.IsAlmost(0))
                    {
                        // horizontal
                        direction = (projected.X < 0) ? CameraDirection.Left : CameraDirection.Right;
                    }
                    else if (Math.Abs(projected.Y) > Math.Abs(projected.X))
                    {
                        // favor the vertical
                        if (Math.Abs(movedSoFar.Y) > Math.Abs(projected.Y))
                        {
                            // then time to step in the X direction
                            direction = (projected.X < 0) ? CameraDirection.Left : CameraDirection.Right;
                        }
                        else
                        {
                            // step in the Y direction.
                            direction = (projected.Y < 0) ? CameraDirection.Down : CameraDirection.Up;
                        }
                    }
                    else
                    {
                        // favor the horizontal
                        if (Math.Abs(movedSoFar.X) > Math.Abs(projected.X))
                        {
                            // then time to step in the Y direction
                            direction = (projected.Y < 0) ? CameraDirection.Down : CameraDirection.Up;
                        }
                        else
                        {
                            // step in the X direction.
                            direction = (projected.X < 0) ? CameraDirection.Left : CameraDirection.Right;
                        }
                    }
                    switch (direction)
                    {
                    case CameraDirection.Up:
                        movedSoFar.Y--;
                        break;

                    case CameraDirection.Down:
                        movedSoFar.Y++;
                        break;

                    case CameraDirection.Left:
                        movedSoFar.X--;
                        break;

                    case CameraDirection.Right:
                        movedSoFar.X++;
                        break;
                    }
                    var result = await d.Move(direction);

                    var error = result.GetValue <string>("error");
                    if (!string.IsNullOrEmpty(error))
                    {
                        Log.WriteLine("Error moving camera: " + error);
                    }
                }
                else
                {
                    StopMoving();
                }


                if (this.moveTimer != null && id == this.moveTimerId)
                {
                    timer.Start();
                }
            }
        }