Beispiel #1
0
        private void RawDiscoveryReceived(byte[] payload)
        {
            MemoryStream stream = new MemoryStream(payload);

            String          name = STRING_ENCODING.GetString(stream.ReadFully(12));
            PhysicalAddress mac  = new PhysicalAddress(new byte[] {
                Convert.ToByte(STRING_ENCODING.GetString(stream.ReadFully(2)), 16),
                Convert.ToByte(STRING_ENCODING.GetString(stream.ReadFully(2)), 16),
                Convert.ToByte(STRING_ENCODING.GetString(stream.ReadFully(2)), 16),
                Convert.ToByte(STRING_ENCODING.GetString(stream.ReadFully(2)), 16),
                Convert.ToByte(STRING_ENCODING.GetString(stream.ReadFully(2)), 16),
                Convert.ToByte(STRING_ENCODING.GetString(stream.ReadFully(2)), 16)
            });
            IPAddress ip      = new IPAddress(stream.ReadFully(4));
            IPAddress mask    = new IPAddress(stream.ReadFully(4));
            IPAddress gateway = new IPAddress(stream.ReadFully(4));

            if (Discovered != null)
            {
                Discovered(name, mac, ip, mask, gateway);
            }
        }
Beispiel #2
0
        internal async Task InitializeAsync()
        {
            this.ReceiveLoopTask = ReceiveLoopAsync();

            await SendMessageAndWaitResponseAsync(COMMAND_ID_REQUEST, COMMAND_ID_RESPONSE, new byte[0],
                                                  (id) => {
                this.Id = STRING_ENCODING.GetString(id);
                return(this.Id);
            }
                                                  );

            await SendMessageAndWaitResponseAsync(COMMAND_FEATURES_REQUEST, COMMAND_FEATURES_RESPONSE, new byte[0],
                                                  (raw) => {
                this.Features = new Features(this, raw);


                List <DigitalInput> digitalInputs = new List <DigitalInput> ();
                for (int i = 0; i < this.Features.NumDigitalInputs; i++)
                {
                    digitalInputs.Add(new DigitalInput(this, i));
                }
                this.DigitalInputs = digitalInputs.AsReadOnly();
                this.RawListeners[COMMAND_INPUT_CHANGED] += (payload) => {
                    Stream stream = new MemoryStream(payload);
                    int index     = stream.ReadInt();
                    bool value    = stream.ReadInt() != 0;
                    this.DigitalInputs[index].Value = value;
                };

                this.RawListeners[COMMAND_KEY_TYPED] += (payload) => {
                    Stream stream = new MemoryStream(payload);
                    int key       = stream.ReadInt();
                    if (this.KeyTyped != null)
                    {
                        this.KeyTyped((char)key);
                    }
                };

                this.RawListeners[COMMAND_IMAGE_ACQUIRED] += (payload) => {
                    Stream stream   = new MemoryStream(payload);
                    int width       = stream.ReadInt();
                    int height      = stream.ReadInt();
                    int resX        = stream.ReadInt();
                    int resY        = stream.ReadInt();
                    byte[] rawImage = stream.ReadFully(width *height);
                    String scanner  = STRING_ENCODING.GetString(stream.ReadFully((int)(stream.Length - stream.Position)));

                    //Create a grayscale bitmap -- DotNet sucks, therefore it uses a pallete
                    Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    bitmap.SetResolution(resX, resY);

                    int i = 0;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            byte v = rawImage[i++];
                            bitmap.SetPixel(x, y, Color.FromArgb(v, v, v));
                        }
                    }

                    if (FingerprintCaptured != null)
                    {
                        FingerprintCaptured(scanner, bitmap);
                    }
                };


                List <DigitalOutput> relays = new List <DigitalOutput> ();
                for (int i = 0; i < this.Features.NumRelays; i++)
                {
                    relays.Add(new DigitalOutput(this, DigitalOutput.DigitalOutputType.Relay, i));
                }
                this.Relays = relays.AsReadOnly();


                List <DigitalOutput> leds = new List <DigitalOutput> ();
                for (int i = 0; i < this.Features.NumLeds; i++)
                {
                    leds.Add(new DigitalOutput(this, DigitalOutput.DigitalOutputType.Led, i));
                }
                this.Leds = leds.AsReadOnly();


                List <SerialPort> serials = new List <SerialPort> ();
                for (int i = 0; i < this.Features.NumSerialPorts; i++)
                {
                    serials.Add(new SerialPort(this, i));
                }
                this.SerialPorts = serials.AsReadOnly();


                this.Buzzer = this.Features.HasBuzzer ? new DigitalOutput(this, DigitalOutput.DigitalOutputType.Buzzer, 0) : null;


                this.Display = this.Features.HasDisplay ? new Display(this) : null;

                return(this.Features);
            }
                                                  );

            if (Connected != null)
            {
                Connected();
            }
            await ReceiveLoopTask;

            if (Disconnected != null)
            {
                Disconnected();
            }
        }