public async void Notification_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
            {
                var        uuid = sender.Service.Uuid;
                string     st   = uuid.ToString();
                SensorData data = null;
                long       incr = 0;

                SensorServicesCls.SensorIndexes sensor = SensorServicesCls.GetSensor(st);

                if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                {
                    byte[] bArray = new byte[eventArgs.CharacteristicValue.Length];
                    DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(bArray);
                    if ((bArray.Length == SensorServicesCls.DataLength[(int)sensor]) ||
                        ((sensor == SensorServicesCls.SensorIndexes.REGISTERS) && ((bArray.Length > 0) && (bArray.Length < 5)))) //Can be 1 to 4 for Registers
                    {
                        //if (sensor != SensorUUIDs.SensorIndexes.REGISTERS)
                        //    System.Diagnostics.Debug.WriteLine(st);


                        long nm = 32;
                        incr = increments[sensor];

                        switch (sensor)
                        {
                        case SensorServicesCls.SensorIndexes.REGISTERS:
                            data = Registers_Handler(sensor, bArray);
                            //incr = 1;
                            break;

                        case SensorServicesCls.SensorIndexes.IR_SENSOR:
                            data = IR_Sensor_Handler(sensor, bArray);
                            //incr = 100;
                            break;

                        case SensorServicesCls.SensorIndexes.HUMIDITY:
                            data = Humidity_Handler(sensor, bArray);
                            //incr = 10000;
                            break;

                        case SensorServicesCls.SensorIndexes.BAROMETRIC_PRESSURE:
                            data = Pressure_Handler(sensor, bArray);
                            //incr = 1000000;
                            break;

                        case SensorServicesCls.SensorIndexes.OPTICAL:
                            data = Optical_Handler(sensor, bArray);
                            //incr = 100000000;
                            break;

                        case SensorServicesCls.SensorIndexes.IO_SENSOR:
                            data = IO_Sensor_Handler(sensor, bArray);
                            //incr = 10000000000;
                            break;

                        case SensorServicesCls.SensorIndexes.KEYS:
                            data = Keys(sensor, bArray);
                            //incr = 1000000000000;
                            break;

                        case SensorServicesCls.SensorIndexes.MOVEMENT:
                            data = Movement_Handler(sensor, bArray);
                            //incr = 100000000000000;
                            break;

                        case SensorServicesCls.SensorIndexes.NOTFOUND:
                            data = this.NotFound_Handler(sensor, bArray);
                            //incr = 10000000000000000;
                            //9223372036854775807
                            break;
                        }
                    }
                }
                else
                {
                    //PropertyServiceCls.SensorTagProperties property = PropertyServices.GetProperty(st);
                    //if (property != PropertyServiceCls.SensorTagProperties.NOTFOUND)
                    //{
                    //    byte[] bArray2 = new byte[eventArgs.CharacteristicValue.Length];
                    //    DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(bArray2);
                    //    if (true)
                    //    //((bArray.Length == CC2650SensorTag.DataLength[(int)sensor]) ||
                    //    //((sensor == SensorIndexes.REGISTERS) && ((bArray.Length > 0) && (bArray.Length < 5)))) //Can be 1 to 4 for Registers
                    //    {
                    //        //if (sensor != SensorUUIDs.SensorIndexes.REGISTERS)
                    //        //    System.Diagnostics.Debug.WriteLine(st);

                    //        data = null;
                    //        long nm = 32;

                    //        switch (property)
                    //        {
                    //        }
                    //    }
                    //}
                    //else
                    //{
                    //    data = NotFound_Handler(sensor, bArray);
                    //    incr = 10000000000000000;
                    //}
                }
                long res;

                res = System.Threading.Interlocked.Increment(ref Logging.AllEventCount);
                if (!Logging.KeepCounting)
                {
                    res = System.Threading.Interlocked.Add(ref Logging.EventCount, incr);
                }

                if (data != null)
                {
                    // Debug.WriteLine("{0} ", data.Sensor_Index);
                    //Debug.Write("{0} ", data.Sensor_Index.ToString());
                    //for (int i = 0; i < data.Values.Length; i++)
                    //    Debug.Write("{0} ", data.Values[i].ToString());
                    //Debug.WriteLine("");
                    if (doCallback)
                    {
                        if (CallMeBack != null)
                        {
                            await CallMeBack(data);
                        }
                    }
                }
            }
Exemplo n.º 2
0
            public async Task InterogateServices(IReadOnlyList <GattDeviceService> svcs)
            {
                //With event data event an increment is added.
                //The increment is 100^(sensor)  so each sensor can be counted up to 100 times in each logging interval
                //ie Max of 1 minute 30 seconds to be safe.
                //Each sensor then uses 2 digits in this logged value.

                foreach (var gattService in svcs)
                {
                    var    uuid = gattService.Uuid;
                    string st   = uuid.ToString();
                    System.Diagnostics.Debug.WriteLine("Service: {0}\r\n", st);
                    await CC2650SensorTag.PrependTextStatic(string.Format("Service: {0}\r\n", st));

                    SensorChars sensorCharacteristics = null;

                    PropertyServiceCls.SensorTagProperties property = PropertyServiceCls.SensorTagProperties.NOTFOUND;

                    SensorServicesCls.SensorIndexes sensor = SensorServicesCls.SensorIndexes.NOTFOUND;
                    sensor = SensorServicesCls.GetSensor(st);
                    if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                    {
                        sensorCharacteristics = new SensorChars(sensor);
                        System.Diagnostics.Debug.WriteLine("Sensor: {0}", sensor);
                        await CC2650SensorTag.PrependTextStatic(string.Format("Sensor: {0}", sensor));
                    }
                    else
                    {
                        property = PropertyServices.GetProperty(st);
                        if (property == PropertyServiceCls.SensorTagProperties.NOTFOUND)
                        {
                            System.Diagnostics.Debug.WriteLine("Service Not Found: {0}", st);
                            await CC2650SensorTag.PrependTextStatic(string.Format("Service Not Found: {0}", st));

                            continue;
                        }
                        else
                        {
                            sensorCharacteristics = new SensorChars(property);
                            System.Diagnostics.Debug.WriteLine("Service: {0}", property);
                            await CC2650SensorTag.PrependTextStatic(string.Format("Service: {0}", property));
                        }
                    }


                    //if (sensor == CC2650SensorTag.SensorIndexes.REGISTERS)
                    //{
                    //    System.Diagnostics.Debug.WriteLine("Service Ignored: {0}", st);
                    //    continue;
                    //}



                    var res = await gattService.GetCharacteristicsAsync();

                    if (res.Status != GattCommunicationStatus.Success)
                    {
                        System.Diagnostics.Debug.WriteLine("Error getting Characteristics in {0}/{1}. Status: {2}", sensor, property, res.Status);


                        if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                        {
                            await CC2650SensorTag.PrependTextStatic(string.Format("Error getting Characteristics in {0}", sensor.ToString()));
                        }
                        else
                        {
                            await CC2650SensorTag.PrependTextStatic(string.Format("Error getting Characteristics in {0}", property.ToString()));
                        }

                        continue;
                    }
                    int count = res.Characteristics.Count();
                    var sensorCharacteristicList = res.Characteristics;
                    if (count == 0)
                    {
                        System.Diagnostics.Debug.WriteLine("No Characteristics in {0}/{1}", sensor, property);
                        if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                        {
                            await CC2650SensorTag.PrependTextStatic(string.Format("No Characteristics in {0}", sensor.ToString()));
                        }
                        else
                        {
                            await CC2650SensorTag.PrependTextStatic(string.Format("No Characteristics in {0}", property.ToString()));
                        }

                        continue;
                    }
                    var characteristic1 = sensorCharacteristicList.First();

                    foreach (var characteristic in sensorCharacteristicList)
                    {
                        SensorServicesCls.CharacteristicTypes  charType  = SensorServicesCls.CharacteristicTypes.NOTFOUND;
                        PropertyServiceCls.SensorTagProperties charPType = PropertyServiceCls.SensorTagProperties.NOTFOUND;

                        if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                        {
                            charType = SensorServicesCls.GetSensorCharacteristicType(characteristic.Uuid.ToString());
                            System.Diagnostics.Debug.WriteLine("{0} {1}", characteristic.Uuid, charType);
                            await CC2650SensorTag.PrependTextStatic(string.Format("{0} {1}", characteristic.Uuid, charType));
                        }
                        else
                        {
                            charPType = PropertyServices.GetPropertyCharacteristicType(characteristic.Uuid.ToString());
                            System.Diagnostics.Debug.WriteLine("{0} {1}", characteristic.Uuid, charPType);
                            await CC2650SensorTag.PrependTextStatic(string.Format("{0} {1}", characteristic.Uuid, charPType));
                        }
                        if (characteristic.CharacteristicProperties.HasFlag(flagNotify))
                        {
                            GattCharacteristic CharacteristicNotification = characteristic;
                            if (CharacteristicNotification != null)
                            {
                                CharacteristicNotification.ValueChanged += SensorEvents.Notification_ValueChanged;
                                if (CharacteristicNotification.CharacteristicProperties.HasFlag(flagNotify))
                                {
                                    await CharacteristicNotification.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                                }
                            }
                        }

                        if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                        {
                            if (!sensorCharacteristics.Charcteristics.Keys.Contains(charType))
                            {
                                switch (charType)
                                {
                                case SensorServicesCls.CharacteristicTypes.Notify:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Notify, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.Enable:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Enable, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.Period:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Period, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.Data:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Data, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.Configuration:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Configuration, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.Registers_Address:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Registers_Address, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.Registers_Device_Id:
                                    sensorCharacteristics.Charcteristics.Add(CC2650SensorTag.SensorServicesCls.CharacteristicTypes.Registers_Device_Id, characteristic);
                                    break;

                                case SensorServicesCls.CharacteristicTypes.NOTFOUND:
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (property != PropertyServiceCls.SensorTagProperties.NOTFOUND)
                            {
                                if (!sensorCharacteristics.CharcteristicsP.Keys.Contains(charPType))
                                {
                                    sensorCharacteristics.CharcteristicsP.Add(charPType, characteristic);
                                }
                            }
                        }
                    }

                    if (sensor != SensorServicesCls.SensorIndexes.NOTFOUND)
                    {
                        if (!Sensors.Keys.Contains(sensor))
                        {
                            Sensors.Add(sensor, sensorCharacteristics);
                        }
                    }
                    else if (property != PropertyServiceCls.SensorTagProperties.NOTFOUND)
                    {
                        if (!PropertyServices.Properties.Keys.Contains(property))
                        {
                            PropertyServices.Properties.Add(property, sensorCharacteristics);
                        }
                    }
                }
            }