Example #1
0
        public void Main(DeviceModel device, DeviceEventHandler deviceEvent, CancellationToken token)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }
            if (string.IsNullOrWhiteSpace(device.Source))
            {
                throw new ArgumentException(nameof(device.Source));
            }
            if (deviceEvent == null)
            {
                throw new ArgumentNullException(nameof(deviceEvent));
            }
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            try
            {
                Run(device, deviceEvent, token);
            }
            finally
            {
                // notify client
                deviceEvent(this, new DeviceEventArgs(null));
            }
        }
        public void Main(DeviceModel device, DeviceEventHandler deviceEvent, CancellationToken token)
        {
            if (device != null)
            {
                throw new ArgumentNullException(nameof(device));
            }
            if (string.IsNullOrWhiteSpace(device.Source))
            {
                throw new ArgumentException(nameof(device.Source));
            }
            if (deviceEvent == null)
            {
                throw new ArgumentNullException(nameof(deviceEvent));
            }
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            try
            {
                using Bitmap bitmap = new Bitmap(Image.FromFile(device.Source));
                deviceEvent(this, new DeviceEventArgs(bitmap));
                token.WaitHandle.WaitOne();
            }
            finally
            {
                // notify client
                deviceEvent(this, new DeviceEventArgs(null));
            }
        }
Example #3
0
 /// <summary>
 /// Initialize for key event.
 /// </summary>
 /// <param name="keys">Keys.</param>
 /// <param name="deviceEvent">Device event.</param>
 /// <param name="eventHandler">Event handler.</param>
 public EventListener(Keys[] keys, DeviceEvent deviceEvent, DeviceEventHandler eventHandler)
 {
     this.keys          = keys;
     this.deviceEvent   = deviceEvent;
     this.eventHandler += eventHandler;
     this.keysEnabled   = deviceEvent == DeviceEvent.KeyPress;
 }
Example #4
0
 /// <summary>
 /// Listen for mouse event.
 /// </summary>
 /// <param name="name">Name.</param>
 /// <param name="deviceEvent">Device event.</param>
 /// <param name="eventHandler">Event handler.</param>
 /// <param name="forSystem">The event is for system or not.</param>
 private static void Listen(string name, DeviceEvent deviceEvent, DeviceEventHandler eventHandler, bool forSystem)
 {
     name = GetName(name, forSystem);
     lock (listenerLocker){
         NotListening(name);
         listeners.Add(name, new EventListener(deviceEvent, eventHandler));
     }
 }
Example #5
0
 public Page1()
 {
     this.InitializeComponent();
     ResultCollection = new ObservableCollection <Btdevice>();
     current          = this;
     this.Loaded     += Page1_Loaded;
     DeviceEventHandler.CreateNewDeviceEventHandler();
 }
Example #6
0
        public void OnDeviceEvent(DeviceEventArgs eventargs)
        {
            DeviceEventHandler handler = DeviceEvent;

            if (handler != null)
            {
                handler(this, eventargs);
            }
        }
        private static void FireEvent(DeviceEventHandler eventHandler, string serial, int drawerId)
        {
            var handler = eventHandler;

            if (handler != null)
            {
                var evt = new DrawerEventArgs(serial, drawerId);
                eventHandler(Device, evt);
            }
        }
Example #8
0
        public Device()
        {
            //для сериализации (при сохранении в XML) необходим конструктор без параметров
            Registers.ListChanged += new ListChangedEventHandler(Registers_ListChanged);

            Register.Delete     += new Register.RegisterEventHandler(RegisterDeleted);
            Register.Create     += new Register.RegisterEventHandler(RegisterAdded);       //подписываемся на событие Создания
            Register.ChangeType += new Register.RegisterEventHandler(RegisterTypeChanged); //подписываемся на событие ИзмененияТипа

            Delete    += new DeviceEventHandler(DeviceDeleted);
            DeleteAll += new DeviceEventHandler(RegistersDeleted);//подписываемся на событие Удаления
        }
Example #9
0
#pragma warning disable CA1822 // Mark members as static
        public DeviceModel Run(DeviceModel deviceModel, DeviceEventHandler deviceEvent, CancellationToken token)
#pragma warning restore CA1822 // Mark members as static
        {
            if (deviceModel == null)
            {
                throw new ArgumentNullException(nameof(deviceModel));
            }

            IDevice device = CreateProvider((string)deviceModel.Format);

            if (device == null)
            {
                deviceModel.Active = false;
                return(deviceModel);
            }

            device.Main(deviceModel, deviceEvent, token);
            return(deviceModel);
        }
Example #10
0
        ////////////////////////////////////////////////////////////////////////////   
        ////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Adds a component or a control to the manager.
        /// </summary>
        /// <param name="component">
        /// The component or control being added.
        /// </param>
        public virtual void Add(Component component)
        {
            if (component != null)
            {
                if (component is Control && !controls.Contains(component as Control))
                {
                    Control c = (Control)component;

                    if (c.Parent != null) c.Parent.Remove(c);

                    controls.Add(c);
                    c.Manager = this;
                    c.Parent = null;
                    if (focusedControl == null) c.Focused = true;

                    DeviceSettingsChanged += new DeviceEventHandler((component as Control).OnDeviceSettingsChanged);
                    SkinChanging += new SkinEventHandler((component as Control).OnSkinChanging);
                    SkinChanged += new SkinEventHandler((component as Control).OnSkinChanged);
                }
                else if (!(component is Control) && !components.Contains(component))
                {
                    components.Add(component);
                    component.Manager = this;
                }
            }
        }
Example #11
0
        private void Run(DeviceModel device, DeviceEventHandler deviceEvent, CancellationToken token)
        {
            byte[] buffer = new byte[_bufSize];  // buffer to read stream
            while (!token.IsCancellationRequested)
            {
                byte[] delimiter  = null;
                byte[] delimiter2 = null;
                byte[] boundary   = null;
                int    boundaryLen;
                int    delimiterLen  = 0;
                int    delimiter2Len = 0;
                int    read;
                int    todo  = 0;
                int    total = 0;
                int    pos   = 0;
                int    align = 1;
                int    start = 0;
                int    stop  = 0;

                // align
                //  1 = searching for image start
                //  2 = searching for image end
                // create request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(device.Source));

                // set login and password
                if ((device.Login != null) && (device.Password != null) && (!string.IsNullOrEmpty(device.Login)))
                {
                    request.Credentials = new NetworkCredential(device.Login, device.Password);
                }

                // set connection group name
                if (_useSeparateConnectionGroup)
                {
                    request.ConnectionGroupName = GetHashCode().ToString(CultureInfo.InvariantCulture);
                }

                // get response
                using WebResponse resp = request.GetResponse();

                // check content type
                string ct = resp.ContentType;
                if (!ct.Contains("multipart/x-mixed-replace"))
                {
                    throw new ApplicationException($"Invalid content type: {ct}");
                }

                // get boundary
                ASCIIEncoding encoding = new ASCIIEncoding();
                boundary    = encoding.GetBytes(ct.Substring(ct.IndexOf("boundary=", 0, StringComparison.InvariantCultureIgnoreCase) + 9));
                boundaryLen = boundary.Length;

                // get response stream
                using Stream stream = resp.GetResponseStream();

                // loop
                while (!token.IsCancellationRequested)
                {
                    // check total read
                    if (total > _bufSize - _readSize)
                    {
                        total = pos = todo = 0;
                    }

                    // read next portion from stream
                    if ((read = stream.Read(buffer, total, _readSize)) == 0)
                    {
                        break;
                    }

                    total += read;
                    todo  += read;

                    // increment received bytes counter
                    BytesReceived += read;

                    // does we know the delimiter ?
                    if (delimiter == null)
                    {
                        // find boundary
                        pos = ByteArrayUtils.Find(buffer, boundary, pos, todo);

                        if (pos == -1)
                        {
                            // was not found
                            todo = boundaryLen - 1;
                            pos  = total - todo;
                            continue;
                        }

                        Debug.Assert(pos >= 0);
                        todo = total - pos;

                        if (todo < 2)
                        {
                            continue;
                        }

                        // check new line delimiter type
                        if (buffer[pos + boundaryLen] == 10)
                        {
                            delimiterLen = 2;
                            delimiter    = new byte[2] {
                                10, 10
                            };
                            delimiter2Len = 1;
                            delimiter2    = new byte[1] {
                                10
                            };
                        }
                        else
                        {
                            delimiterLen = 4;
                            delimiter    = new byte[4] {
                                13, 10, 13, 10
                            };
                            delimiter2Len = 2;
                            delimiter2    = new byte[2] {
                                13, 10
                            };
                        }

                        pos += boundaryLen + delimiter2Len;
                        todo = total - pos;
                    }

                    if (pos < 0)
                    {
                        continue;
                    }

                    // search for image
                    if (align == 1)
                    {
                        start = ByteArrayUtils.Find(buffer, delimiter, pos, todo);
                        if (start != -1)
                        {
                            // found delimiter
                            start += delimiterLen;
                            pos    = start;
                            todo   = total - pos;
                            align  = 2;
                        }
                        else
                        {
                            // delimiter not found
                            todo = delimiterLen - 1;
                            pos  = total - todo;
                        }
                    }

                    // search for image end
                    while ((align == 2) && (todo >= boundaryLen))
                    {
                        stop = ByteArrayUtils.Find(buffer, boundary, pos, todo);
                        if (stop != -1)
                        {
                            pos  = stop;
                            todo = total - pos;

                            // increment frames counter
                            FramesReceived++;

                            // image at stop
                            using var ms = new MemoryStream(buffer, start, stop - start);
                            Bitmap bitmap = System.Drawing.Image.FromStream(ms) as Bitmap;

                            // notify client
                            deviceEvent(this, new DeviceEventArgs(bitmap));

                            // release the image
                            bitmap.Dispose();
                            bitmap = null;

                            // shift array
                            pos  = stop + boundaryLen;
                            todo = total - pos;
                            Array.Copy(buffer, pos, buffer, 0, todo);

                            total = todo;
                            pos   = 0;
                            align = 1;
                        }
                        else
                        {
                            // delimiter not found
                            todo = boundaryLen - 1;
                            pos  = total - todo;
                        }
                    }
                }
            }
        }
Example #12
0
 /// <summary>
 /// Listen for mouse tilt.
 /// </summary>
 /// <param name="name">Name.</param>
 /// <param name="eventHandler">Event handler.</param>
 /// <param name="forSystem">The event is for system or not.</param>
 public static void MouseTilt(string name, DeviceEventHandler eventHandler, bool forSystem = false)
 {
     Listen(name, DeviceEvent.MouseTilt, eventHandler, forSystem);
 }
Example #13
0
 /// <summary>
 /// Listen for key press.
 /// </summary>
 /// <param name="name">Name.</param>
 /// <param name="keys">Keys.</param>
 /// <param name="eventHandler">Event handler.</param>
 /// <param name="forSystem">The event is for system or not.</param>
 public static void KeyPress(string name, Keys[] keys, DeviceEventHandler eventHandler, bool forSystem = false)
 {
     Listen(name, keys, DeviceEvent.KeyPress, eventHandler, forSystem);
 }
Example #14
0
        /// <summary>
        /// Listen for key event.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="keys">Keys.</param>
        /// <param name="deviceEvent">Device event.</param>
        /// <param name="eventHandler">Event handler.</param>
        /// <param name="forSystem">The event is for system or not.</param>
        private static void Listen(string name, Keys[] keys, DeviceEvent deviceEvent, DeviceEventHandler eventHandler, bool forSystem)
        {
            if (keys.Length == 0)
            {
                throw new ArgumentException("Keys are not specified");
            }

            name = GetName(name, forSystem);
            lock (listenerLocker){
                NotListening(name);
                listeners.Add(name, new EventListener(keys, deviceEvent, eventHandler));
            }
        }
Example #15
0
        /// <summary>
        /// Adds a component or a control to the manager.
        /// </summary>
        /// <param name="component">
        /// The component or control being added.
        /// </param>
        public virtual void Add(Component component)
        {
            // Component exists?
            if (component != null)
            {
                // Component is a Control not already in the list?
                if (component is Control && !controls.Contains(component as Control))
                {
                    Control c = (Control)component;

                    // Remove control from parent list because it's under new management now.
                    if (c.Parent != null) c.Parent.Remove(c);

                    controls.Add(c);
                    c.Manager = this;
                    c.Parent = null;

                    // New control gains focus unless another control already has it.
                    if (focusedControl == null) c.Focused = true;

                    DeviceSettingsChanged += new DeviceEventHandler((component as Control).OnDeviceSettingsChanged);
                    SkinChanging += new SkinEventHandler((component as Control).OnSkinChanging);
                    SkinChanged += new SkinEventHandler((component as Control).OnSkinChanged);
                }

                // Component is not a control and doesn't already exist in the component list?
                else if (!(component is Control) && !components.Contains(component))
                {
                    components.Add(component);
                    component.Manager = this;
                }
            }
        }
Example #16
0
 /// <summary>
 /// Initialize for mouse event.
 /// </summary>
 /// <param name="deviceEvent">Device event.</param>
 /// <param name="eventHandler">Event handler.</param>
 public EventListener(DeviceEvent deviceEvent, DeviceEventHandler eventHandler)
 {
     this.deviceEvent   = deviceEvent;
     this.eventHandler += eventHandler;
 }