Пример #1
0
        private static async void GattServerOnChararteristicWrite(object myObject, CharacteristicEventArgs myArgs)
        {
            try
            {
                if (_terminal == null)
                {
                    await InitializeDeviceNet();
                }

                await _logger.LogMessageAsync($"Characteristic with Guid: {myArgs.Characteristic.ToString()} changed: {myArgs.Value.ToString()}");

                //We recieved a message. According to the code myObject is a string converted from data
                //Assuming that this data is a 6-digit PIN code and we want to relay a message "OPEN" to the terminal

                //First we need to check the local state to ensure the PIN code is valid
                var isValid = await pinChecker.IsValid((string)myObject);

                if (!isValid)
                {
                    await _logger.LogMessageAsync("Invalid PIN attempt");

                    return;
                }

                //This assumes that the Terminal want to get the message "OPEN"
                var data = Encoding.UTF8.GetBytes("OPEN");

                //Send the open message
                await _terminal.WriteAsync(data);
            }
            catch (Exception ex)
            {
                await _logger.LogMessageAsync(ex.ToString());

                //Atempt to reconnect
                await InitializeDeviceNet();
            }
        }
Пример #2
0
        private void Value_Notification(object sender, CharacteristicEventArgs e)
        {
            var data      = e.Value.ToArray();
            var hexstring = UTF8Encoding.UTF8.GetString(data, 0, data.Count());
            var bytes     = StringToByteArray(hexstring);


            switch (bytes[0])
            {
            case 0x82:     //GetCurrentMIPGameMode
                GameMode = (GameModeEmum)Enum.Parse(typeof(GameModeEmum), bytes[1].ToString());
                OnGameModeRecieved(GameMode);
                break;

            case 0x03:     //Receive IR Dongle code
                OnIRDongleCodeRecievedEvent(bytes.ToList());
                break;

            case 0x79:     //Request MIP status
                BatteryLevel = bytes[1];
                Status       = (StatusEnum)Enum.Parse(typeof(StatusEnum), bytes[2].ToString());
                OnMiPStatusRecieved(new MiPStatus()
                {
                    BatteryLevel = BatteryLevel, Status = Status
                });
                break;

            case 0x81:     //Request weight update
                Weight = bytes[1];
                OnWeightUpdateRecieved(Weight);
                break;

            case 0x83:     //RequestChestLED
                ChestLEDColor = Color.FromArgb(255, bytes[1], bytes[2], bytes[3]);
                if (bytes.Count() >= 5)
                {
                    ChestLEDTimeOn = bytes[4];
                }
                if (bytes.Count() == 6)
                {
                    ChestLEDTimeOff = bytes[5];
                }

                OnChestLEDRecieved(new ChestLED()
                {
                    Color = ChestLEDColor, TimeOn = ChestLEDTimeOn, TimeOff = ChestLEDTimeOff
                });
                break;

            case 0x8b:    // RequestHeadLED
                Light1 = (LightEnum)Enum.Parse(typeof(LightEnum), bytes[1].ToString());
                Light2 = (LightEnum)Enum.Parse(typeof(LightEnum), bytes[2].ToString());
                Light3 = (LightEnum)Enum.Parse(typeof(LightEnum), bytes[3].ToString());
                Light4 = (LightEnum)Enum.Parse(typeof(LightEnum), bytes[4].ToString());
                OnHeadLEDRecieved(new HeadLED()
                {
                    Light1 = Light1, Light2 = Light2, Light3 = Light3, Light4 = Light4
                });
                break;

            case 0x0A:     //GestureDetect
                var detectedGesture = (GestureEnum)Enum.Parse(typeof(GestureEnum), bytes[1].ToString());
                OnGestureRecieved(detectedGesture);
                break;

            case 0x0D:     //RadarMode
                RadarMode = (GestureRadarEnum)Enum.Parse(typeof(GestureRadarEnum), bytes[1].ToString());
                OnRadarModeRecieved(RadarMode);
                break;

            case 0x0C:     //Radar Response
                byte response = bytes[1];
                OnRadarRecieved(response);
                break;

            case 0x0F:     //Mip Detection Status
                MiPDetectionMode      = bytes[1];
                MiPDetectionIRTxPower = bytes[2];
                OnMiPDetectionStatusRecieved(new MiPDetectionStatus()
                {
                    Mode = MiPDetectionMode, IRTxPower = MiPDetectionIRTxPower
                });
                break;

            case 0x04:     //Mip Detected
                var mipsettingNumber = bytes[1];
                OnMipDetectedRecieved(mipsettingNumber);
                break;

            case 0x1A:     //Shake Detected
                OnShakeDetectedRecieved();
                break;

            case 0x11:     //IR Control Status
                IRControlStatus = bytes[1];
                OnIRControlStatusRecieved(IRControlStatus);
                break;

            case 0xFA:     //Sleep
                OnSleepRecieved();
                break;

            case 0x13:     //MIP User Or Other Eeprom Data
                var address        = bytes[1];
                var userEepromdata = bytes[2];
                OnEePromDataRecieved(new EepromData()
                {
                    Address = address, Data = userEepromdata
                });
                break;

            case 0x14:     //Mip Software Version
                MiPVersion = string.Format("{0}.{1}.{2}.{3}", Convert.ToInt32(bytes[1]), Convert.ToInt32(bytes[2]), Convert.ToInt32(bytes[3]), Convert.ToInt32(bytes[4]));
                OnSoftwareVersionRecieved(MiPVersion);
                break;

            case 0x19:     //Mip Hardware Info
                VoiceChipVersion = bytes[1];
                HardwareVersion  = bytes[2];
                OnMiPHardwareVersionRecieved(new MiPHardwareVersion()
                {
                    VoiceChipVersion = VoiceChipVersion, HardwareVersion = HardwareVersion
                });
                break;

            case 0x16:     //GetVolume
                Volume = bytes[1];
                OnVolumeRecieved(Volume);
                break;

            case 0x1d:     //Clap times
                var times = bytes[1];
                OnClapTimesRecieved(times);
                break;

            case 0x1f:     //Clap Status
                var isEnabled = bytes[1] == 0x00 ? false : true;
                int delayTime = (bytes[2] << 8) & bytes[3];
                OnClapStatusRecieved(new ClapStatus()
                {
                    Enabled = isEnabled, DelayTime = delayTime
                });
                break;

            default:
                //Value_Notification(bytes);
                break;
            }
        }
 private async void _gattServer_OnChararteristicWrite(object myObject, CharacteristicEventArgs myArgs)
 {
     await _logger.LogMessageAsync($"Characteristic with Guid: {myArgs.Characteristic.ToString()} changed: {myArgs.Value.ToString()}");
 }