public static HomeKitCondition CreateCharacteristic (HMCharacteristic characteristic, INSCopying value)
		{
			return new HomeKitCondition {
				CharacteristicData = new Tuple<HMCharacteristic, INSCopying> (characteristic, value),
				Type = HomeKitConditionType.Characteristic
			};
		}
        // Generates a characteristic cell based on the type of characteristic located at the specified index path.
        UITableViewCell GetCellForCharacteristicCell(UITableView tableView, NSIndexPath indexPath)
        {
            HMCharacteristic characteristic = Service.Characteristics [indexPath.Row];

            var reuseIdentifier = characteristicCell;

            if ((characteristic.IsReadOnly() || characteristic.IsWriteOnly()) && !allowsAllWrites)
            {
                reuseIdentifier = characteristicCell;
            }
            else if (characteristic.IsBoolean())
            {
                reuseIdentifier = switchCharacteristicCell;
            }
            else if (characteristic.HasPredeterminedValueDescriptions())
            {
                reuseIdentifier = segmentedControlCharacteristicCell;
            }
            else if (characteristic.IsNumeric())
            {
                reuseIdentifier = sliderCharacteristicCell;
            }
            else if (characteristic.IsTextWritable())
            {
                reuseIdentifier = textCharacteristicCell;
            }

            var cell = (CharacteristicCell)tableView.DequeueReusableCell(reuseIdentifier, indexPath);

            cell.ShowsFavorites = showsFavorites;
            cell.Delegate       = Delegate;
            cell.Characteristic = characteristic;

            return(cell);
        }
        // Evaluates whether or not an `HMCharacteristic` is a favorite.
        public bool IsFavorite(HMCharacteristic characteristic)
        {
            var service = characteristic.Service;

            if (service == null)
            {
                return(false);
            }

            var accessory = service.Accessory;

            if (accessory == null)
            {
                return(false);
            }

            List <NSUuid> characteristicIdentifiers;
            var           accessoryIdentifier = accessory.UniqueIdentifier;

            if (!accessoryToCharacteristicIdentifiers.TryGetValue(accessoryIdentifier, out characteristicIdentifiers))
            {
                return(false);
            }

            return(characteristicIdentifiers.Contains(characteristic.UniqueIdentifier));
        }
        // Searches through the target value map and existing `HMCharacteristicWriteActions`
        // to find the target value for the characteristic in question.
        public NSObject TargetValueForCharacteristic(HMCharacteristic characteristic)
        {
            NSObject value;

            if (targetValueMap.TryGetValue(characteristic, out value))
            {
                return(value);
            }

            var actionSet = ActionSet;

            if (actionSet != null)
            {
                foreach (var action in actionSet.Actions)
                {
                    var writeAction = action as HMCharacteristicWriteAction;
                    if (writeAction != null && writeAction.Characteristic == characteristic)
                    {
                        return(writeAction.TargetValue);
                    }
                }
            }

            return(null);
        }
        public static int[] AllPossibleValues(this HMCharacteristic self)
        {
            if (!self.IsInteger())
            {
                return(null);
            }

            var metadata = self.Metadata;

            if (metadata == null)
            {
                return(null);
            }

            var stepValue = metadata.StepValue;

            if (stepValue == null)
            {
                return(null);
            }

            var step = stepValue.DoubleValue;

            return(Enumerable.Range(0, self.NumberOfChoices())
                   .Select(i => (int)(i * step))
                   .ToArray());
        }
Exemple #6
0
 public static HomeKitCondition CreateCharacteristic(HMCharacteristic characteristic, INSCopying value)
 {
     return(new HomeKitCondition {
         CharacteristicData = new Tuple <HMCharacteristic, INSCopying> (characteristic, value),
         Type = HomeKitConditionType.Characteristic
     });
 }
        public static string DescriptionForValue(this HMCharacteristic self, int value)
        {
            if (self.IsBoolean())
            {
                return(Convert.ToBoolean(value) ? "On" : "Off");
            }

            var predeterminedValueString = self.PredeterminedValueDescriptionForNumber(value);

            if (predeterminedValueString != null)
            {
                return(predeterminedValueString);
            }

            var metadata = self.Metadata;

            if (metadata != null)
            {
                var stepValue = metadata.StepValue;
                if (stepValue != null)
                {
                    formatter.MaximumFractionDigits = (int)Math.Log10(1f / stepValue.DoubleValue);
                    var str = formatter.StringFromNumber(value);
                    if (!string.IsNullOrEmpty(str))
                    {
                        return(str + self.LocalizedUnitDescription());
                    }
                }
            }

            return(value.ToString());
        }
        static int CharacteristicOrderedBefore(HMCharacteristic characteristic1, HMCharacteristic characteristic2)
        {
            var type1 = characteristic1.LocalizedCharacteristicType();
            var type2 = characteristic2.LocalizedCharacteristicType();

            return(type1.CompareTo(type2));
        }
        // Sets the cell's text to represent a characteristic and target value.
        // For example, "Brightness → 60%"
        // Sets the subtitle to the service and accessory that this characteristic represents.
        public void SetCharacteristic(HMCharacteristic characteristic, NSNumber targetValue)
        {
            var targetDescription = string.Format ("{0} → {1}", characteristic.LocalizedDescription, characteristic.DescriptionForValue (targetValue));
            TextLabel.Text = targetDescription;

            HMService service = characteristic.Service;
            if (service != null && service.Accessory != null)
                TrySetDetailText (string.Format ("{0} in {1}", service.Name, service.Accessory.Name));
            else
                TrySetDetailText ("Unknown Characteristic");
        }
        // Returns the description for a provided value, taking the characteristic's metadata and possible values into account.
        public static string DescriptionForValue(this HMCharacteristic self, NSObject value)
        {
            if (self.IsWriteOnly())
            {
                return("Write-Only Characteristic");
            }

            var number = value as NSNumber;

            return(number != null?self.DescriptionForValue(number.Int32Value) : value.ToString());
        }
 public static void IsFavorite(this HMCharacteristic self, bool newValue)
 {
     if (newValue)
     {
         FavoritesManager.SharedManager.FavoriteCharacteristic(self);
     }
     else
     {
         FavoritesManager.SharedManager.UnfavoriteCharacteristic(self);
     }
 }
		// Reads the characteristic's value and calls the completion with the characteristic's value.
		// If there is a pending write request on the same characteristic, the read is ignored to prevent "UI glitching".
		public void CharacteristicCellReadInitialValueForCharacteristic (CharacteristicCell cell, HMCharacteristic characteristic, Action<NSObject, NSError> completion)
		{
			characteristic.ReadValue (error => updateQueue.DispatchSync (() => {
				NSObject sentValue;
				if (sentWrites.TryGetValue (characteristic, out sentValue)) {
					completion (sentValue, null);
					return;
				}
				DispatchQueue.MainQueue.DispatchAsync (() => completion (characteristic.Value, error));
			}));
		}
		// Tries to use the value from the condition-value map, but falls back
		// to reading the characteristic's value from HomeKit.
		public virtual void CharacteristicCellReadInitialValueForCharacteristic (CharacteristicCell cell, HMCharacteristic characteristic, Action<NSObject, NSError> completion)
		{
			if (TryReadValue (characteristic, completion))
				return;

			characteristic.ReadValue (error => {
				// The user may have updated the cell value while the
				// read was happening. We check the map one more time.
				if (!TryReadValue (characteristic, completion))
					completion (characteristic.Value, error);
			});
		}
Exemple #14
0
        bool TryReadValue(HMCharacteristic characteristic, Action <NSObject, NSError> completion)
        {
            NSObject value;

            if (conditionValueMap.TryGetValue(characteristic, out value))
            {
                completion(value, null);
                return(true);
            }

            return(false);
        }
Exemple #15
0
        // Generates predicates from the characteristic-value map and adds them to the pending conditions.
        public void UpdatePredicates()
        {
            foreach (var kvp  in conditionValueMap)
            {
                HMCharacteristic characteristic = kvp.Key;
                NSObject         value          = kvp.Value;
                NSPredicate      predicate      = HMEventTrigger.CreatePredicateForEvaluatingTrigger(characteristic, NSPredicateOperatorType.EqualTo, value);
                AddCondition(predicate);
            }

            conditionValueMap.Clear();
        }
        public static bool HasPredeterminedValueDescriptions(this HMCharacteristic self)
        {
            var number = self.Value as NSNumber;

            if (number == null)
            {
                return(false);
            }

            var num = number.Int32Value;

            return(self.PredeterminedValueDescriptionForNumber(num) != null);
        }
Exemple #17
0
 // If a characteristic is selected, and it is the 'Identify' characteristic, perform an identify on that accessory.
 void DidSelectCharacteristic(HMCharacteristic characteristic, NSIndexPath indexPath)
 {
     if (characteristic.IsIdentify())
     {
         var accessory = Service.Accessory;
         if (accessory != null)
         {
             accessory.Identify(error => {
                 if (error != null)
                 {
                     DisplayError(error);
                 }
             });
         }
     }
 }
Exemple #18
0
        // Saves a characteristic and value into the pending map of characteristic events.
        void UpdateEventValue(INSCopying value, HMCharacteristic characteristic)
        {
            for (int index = 0; index < removalCharacteristicEvents.Count; index++)
            {
                var e = removalCharacteristicEvents [index];

                // We have this event pending for deletion, but we are going to want to update it.
                // remove it from the removal array.
                if (e.Characteristic == characteristic)
                {
                    removalCharacteristicEvents.RemoveAt(index);
                    break;
                }
            }
            targetValueMap [characteristic] = value;
        }
Exemple #19
0
        // Sets the cell's text to represent a characteristic and target value.
        // For example, "Brightness → 60%"
        // Sets the subtitle to the service and accessory that this characteristic represents.
        public void SetCharacteristic(HMCharacteristic characteristic, NSNumber targetValue)
        {
            var targetDescription = string.Format("{0} → {1}", characteristic.LocalizedDescription, characteristic.DescriptionForValue(targetValue));

            TextLabel.Text = targetDescription;

            HMService service = characteristic.Service;

            if (service != null && service.Accessory != null)
            {
                TrySetDetailText(string.Format("{0} in {1}", service.Name, service.Accessory.Name));
            }
            else
            {
                TrySetDetailText("Unknown Characteristic");
            }
        }
        // Unfavorites a characteristic.
        public void UnfavoriteCharacteristic(HMCharacteristic characteristic)
        {
            var service = characteristic.Service;

            if (service == null)
            {
                return;
            }

            var accessory = service.Accessory;

            if (accessory == null)
            {
                return;
            }

            List <NSUuid> characteristicIdentifiers;
            var           accessoryIdentifier = accessory.UniqueIdentifier;

            if (!accessoryToCharacteristicIdentifiers.TryGetValue(accessoryIdentifier, out characteristicIdentifiers))
            {
                return;
            }

            var indexOfCharacteristic = characteristicIdentifiers.IndexOf(characteristic.UniqueIdentifier);

            if (indexOfCharacteristic < 0)
            {
                return;
            }

            // Remove the characteristic from the mapped collection.
            characteristicIdentifiers.RemoveAt(indexOfCharacteristic);

            // If that was the last characteristic for that accessory, remove the accessory from the internal array.
            if (characteristicIdentifiers.Count == 0)
            {
                accessoryToCharacteristicIdentifiers.Remove(accessoryIdentifier);
            }

            Save();
        }
        // First removes the characteristic from the `targetValueMap`.
        // Then removes any `HMCharacteristicWriteAction`s from the action set
        // which set the specified characteristic.
        public void RemoveTargetValueForCharacteristic(HMCharacteristic characteristic, Action completion)
        {
            /*
             *      We need to create a dispatch group here, because in many cases
             *      there will be one characteristic saved in the Action Set, and one
             *      in the target value map. We want to run the completion closure only one time,
             *      to ensure we've removed both.
             */
            var group = DispatchGroup.Create();

            if (targetValueMap.ContainsKey(characteristic))
            {
                // Remove the characteristic from the target value map.
                group.DispatchAsync(DispatchQueue.MainQueue, () => targetValueMap.Remove(characteristic));
            }

            var actionSet = ActionSet;

            if (actionSet != null)
            {
                var actions = actionSet.Actions.OfType <HMCharacteristicWriteAction> ();
                foreach (var action in actions)
                {
                    if (action.Characteristic == characteristic)
                    {
                        // Also remove the action, and only relinquish the dispatch group once the action set has finished.
                        group.Enter();
                        actionSet.RemoveAction(action, error => {
                            if (error != null)
                            {
                                Console.WriteLine(error.LocalizedDescription);
                            }
                            group.Leave();
                        });
                    }
                }
            }
            // Once we're positive both have finished, run the completion closure on the main queue.
            group.Notify(DispatchQueue.MainQueue, completion);
        }
        public static string LocalizedCharacteristicType(this HMCharacteristic self)
        {
            var type = self.LocalizedDescription;

            string description = null;

            if (self.IsReadOnly())
            {
                description = "Read Only";
            }
            else if (self.IsWriteOnly())
            {
                description = "Write Only";
            }

            if (description != null)
            {
                type = string.Format("{0} {1}", type, description);
            }

            return(type);
        }
        // Favorites a characteristic.
        public void FavoriteCharacteristic(HMCharacteristic characteristic)
        {
            if (IsFavorite(characteristic))
            {
                return;
            }

            var service = characteristic.Service;

            if (service == null)
            {
                return;
            }

            var accessory = service.Accessory;

            if (accessory == null)
            {
                return;
            }

            List <NSUuid> characteristicIdentifiers;
            var           aId             = accessory.UniqueIdentifier;
            var           cId             = characteristic.UniqueIdentifier;
            bool          alreadyFavorite = accessoryToCharacteristicIdentifiers.TryGetValue(aId, out characteristicIdentifiers);

            if (alreadyFavorite)
            {
                characteristicIdentifiers.Add(cId);
            }
            else
            {
                accessoryToCharacteristicIdentifiers [aId] = new List <NSUuid> {
                    cId
                }
            };

            Save();
        }
        static int NumberOfChoices(this HMCharacteristic self)
        {
            var metadata = self.Metadata;

            if (metadata == null)
            {
                return(0);
            }

            var minimumValue = metadata.MinimumValue;

            if (minimumValue == null)
            {
                return(0);
            }

            var maximumValue = metadata.MaximumValue;

            if (maximumValue == null)
            {
                return(0);
            }

            int minimum = minimumValue.Int32Value;
            int maximum = maximumValue.Int32Value;
            int range   = maximum - minimum;

            var stepValue = metadata.StepValue;

            if (stepValue != null)
            {
                range = (int)(range / stepValue.DoubleValue);
            }

            return(range + 1);
        }
        static string LocalizedUnitDescription(this HMCharacteristic self)
        {
            var metadata = self.Metadata;

            if (metadata != null)
            {
                HMCharacteristicMetadataUnits units = metadata.Units;
                switch (units)
                {
                case HMCharacteristicMetadataUnits.Celsius:
                    return("℃");

                case HMCharacteristicMetadataUnits.ArcDegree:
                    return("º");

                case HMCharacteristicMetadataUnits.Fahrenheit:
                    return("℉");

                case HMCharacteristicMetadataUnits.Percentage:
                    return("%");
                }
            }
            return(string.Empty);
        }
		// First removes the characteristic from the `targetValueMap`.
		// Then removes any `HMCharacteristicWriteAction`s from the action set
		// which set the specified characteristic.
		public void RemoveTargetValueForCharacteristic (HMCharacteristic characteristic, Action completion)
		{
			/*
				We need to create a dispatch group here, because in many cases
				there will be one characteristic saved in the Action Set, and one
				in the target value map. We want to run the completion closure only one time,
				to ensure we've removed both.
			*/
			var group = DispatchGroup.Create ();
			if (targetValueMap.ContainsKey (characteristic)) {
				// Remove the characteristic from the target value map.
				group.DispatchAsync (DispatchQueue.MainQueue, () => targetValueMap.Remove (characteristic));
			}

			var actionSet = ActionSet;
			if (actionSet != null) {
				var actions = actionSet.Actions.OfType<HMCharacteristicWriteAction> ();
				foreach (var action in actions) {
					if (action.Characteristic == characteristic) {
						// Also remove the action, and only relinquish the dispatch group once the action set has finished.
						group.Enter ();
						actionSet.RemoveAction (action, error => {
							if (error != null)
								Console.WriteLine (error.LocalizedDescription);
							group.Leave ();
						});
					}

				}
			}
			// Once we're positive both have finished, run the completion closure on the main queue.
			group.Notify (DispatchQueue.MainQueue, completion);
		}
		// Searches through the target value map and existing `HMCharacteristicWriteActions`
		// to find the target value for the characteristic in question.
		public NSObject TargetValueForCharacteristic (HMCharacteristic characteristic)
		{
			NSObject value;
			if (targetValueMap.TryGetValue (characteristic, out value))
				return value;

			var actionSet = ActionSet;
			if (actionSet != null) {
				foreach (var action in actionSet.Actions) {
					var writeAction = action as HMCharacteristicWriteAction;
					if (writeAction != null && writeAction.Characteristic == characteristic)
						return writeAction.TargetValue;
				}
			}

			return null;
		}
		static bool CharacteristicFormatEqualsTo(HMCharacteristic characteristic, HMCharacteristicMetadataFormat format)
		{
			var metadata = characteristic.Metadata;
			return metadata != null && metadata.Format == format;
		}
		// Receives a callback from a `CharacteristicCell` with a value change. Adds this value change into the targetValueMap, overwriting other value changes.
		public void CharacteristicCellDidUpdateValueForCharacteristic (CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
		{
			targetValueMap [characteristic] = value;
		}
Exemple #30
0
        // If the mode is event, update the event value. Otherwise, default to super implementation
        public override void CharacteristicCellDidUpdateValueForCharacteristic(CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
        {
            switch (Mode)
            {
            case CharacteristicTriggerCreatorMode.Event:
                UpdateEventValue((INSCopying)value, characteristic);
                break;

            default:
                base.CharacteristicCellDidUpdateValueForCharacteristic(cell, value, characteristic, immediate);
                break;
            }
        }
		// Responds to a cell change, and if the update was marked immediate, updates the characteristics.
		public void CharacteristicCellDidUpdateValueForCharacteristic (CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
		{
			pendingWrites [characteristic] = value;
			if (immediate)
				UpdateCharacteristics ();
		}
		// Evaluates whether or not an `HMCharacteristic` is a favorite.
		public bool IsFavorite (HMCharacteristic characteristic)
		{
			var service = characteristic.Service;
			if (service == null)
				return false;

			var accessory = service.Accessory;
			if (accessory == null)
				return false;

			List<NSUuid> characteristicIdentifiers;
			var accessoryIdentifier = accessory.UniqueIdentifier;
			if (!accessoryToCharacteristicIdentifiers.TryGetValue (accessoryIdentifier, out characteristicIdentifiers))
				return false;

			return characteristicIdentifiers.Contains (characteristic.UniqueIdentifier);
		}
Exemple #33
0
        public void DidUpdateValueForCharacteristic(HMAccessory accessory, HMService service, HMCharacteristic characteristic)
        {
            var s = characteristic.Service;

            if (s == null)
            {
                return;
            }

            var a = service.Accessory;

            if (a == null)
            {
                return;
            }

            var indexOfAccessory = Array.IndexOf(favoriteAccessories, accessory);

            if (indexOfAccessory < 0)
            {
                return;
            }

            var favoriteCharacteristics = FavoritesManager.SharedManager.FavoriteCharacteristicsForAccessory(accessory);
            var indexOfCharacteristic   = Array.IndexOf(favoriteCharacteristics, characteristic);

            if (indexOfCharacteristic < 0)
            {
                return;
            }

            var indexPath = NSIndexPath.FromRowSection(indexOfCharacteristic, indexOfAccessory);
            var cell      = (CharacteristicCell)TableView.CellAt(indexPath);

            cell.SetValue(characteristic.Value, false);
        }
		// Unfavorites a characteristic.
		public void UnfavoriteCharacteristic (HMCharacteristic characteristic)
		{
			var service = characteristic.Service;
			if (service == null)
				return;

			var accessory = service.Accessory;
			if (accessory == null)
				return;

			List<NSUuid> characteristicIdentifiers;
			var accessoryIdentifier = accessory.UniqueIdentifier;
			if (!accessoryToCharacteristicIdentifiers.TryGetValue (accessoryIdentifier, out characteristicIdentifiers))
				return;

			var indexOfCharacteristic = characteristicIdentifiers.IndexOf (characteristic.UniqueIdentifier);
			if (indexOfCharacteristic < 0)
				return;

			// Remove the characteristic from the mapped collection.
			characteristicIdentifiers.RemoveAt (indexOfCharacteristic);

			// If that was the last characteristic for that accessory, remove the accessory from the internal array.
			if (characteristicIdentifiers.Count == 0)
				accessoryToCharacteristicIdentifiers.Remove (accessoryIdentifier);

			Save ();
		}
		static int CharacteristicOrderedBefore (HMCharacteristic characteristic1, HMCharacteristic characteristic2)
		{
			var type1 = characteristic1.LocalizedCharacteristicType ();
			var type2 = characteristic2.LocalizedCharacteristicType ();

			return type1.CompareTo (type2);
		}
		// Favorites a characteristic.
		public void FavoriteCharacteristic (HMCharacteristic characteristic)
		{
			if (IsFavorite (characteristic))
				return;

			var service = characteristic.Service;
			if (service == null)
				return;

			var accessory = service.Accessory;
			if (accessory == null)
				return;

			List<NSUuid> characteristicIdentifiers;
			var aId = accessory.UniqueIdentifier;
			var cId = characteristic.UniqueIdentifier;
			bool alreadyFavorite = accessoryToCharacteristicIdentifiers.TryGetValue (aId, out characteristicIdentifiers);
			if (alreadyFavorite)
				characteristicIdentifiers.Add (cId);
			else
				accessoryToCharacteristicIdentifiers [aId] = new List<NSUuid> { cId };

			Save ();
		}
		bool TryReadValue (HMCharacteristic  characteristic, Action<NSObject, NSError> completion)
		{
			NSObject value;
			if (conditionValueMap.TryGetValue (characteristic, out value)) {
				completion (value, null);
				return true;
			}

			return false;
		}
Exemple #38
0
 // Synchronously removes the characteristic-value pair from the `sentWrites` map.
 void DidCompleteWrite(HMCharacteristic characteristic, NSObject value)
 {
     updateQueue.DispatchSync(() => sentWrites.Remove(characteristic));
 }
Exemple #39
0
 // Handles the value update and stores the value in the condition map.
 public virtual void CharacteristicCellDidUpdateValueForCharacteristic(CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
 {
     conditionValueMap [characteristic] = value;
 }
		// Synchronously adds the characteristic-value pair into the `sentWrites` map.
		void DidSendWrite (HMCharacteristic characteristic, NSObject  value)
		{
			updateQueue.DispatchSync (() => {
				sentWrites [characteristic] = value;
			});
		}
Exemple #41
0
        // Tries to use the value from the condition-value map, but falls back
        // to reading the characteristic's value from HomeKit.
        public virtual void CharacteristicCellReadInitialValueForCharacteristic(CharacteristicCell cell, HMCharacteristic characteristic, Action <NSObject, NSError> completion)
        {
            if (TryReadValue(characteristic, completion))
            {
                return;
            }

            characteristic.ReadValue(error => {
                // The user may have updated the cell value while the
                // read was happening. We check the map one more time.
                if (!TryReadValue(characteristic, completion))
                {
                    completion(characteristic.Value, error);
                }
            });
        }
		// Saves a characteristic and value into the pending map of characteristic events.
		void UpdateEventValue (INSCopying value, HMCharacteristic characteristic)
		{
			for (int index = 0; index < removalCharacteristicEvents.Count; index++) {
				var e = removalCharacteristicEvents [index];

				// We have this event pending for deletion, but we are going to want to update it.
				// remove it from the removal array.
				if (e.Characteristic == characteristic) {
					removalCharacteristicEvents.RemoveAt (index);
					break;
				}
			}
			targetValueMap [characteristic] = value;
		}
		public override void CharacteristicCellReadInitialValueForCharacteristic (CharacteristicCell cell, HMCharacteristic characteristic, Action<NSObject, NSError> completion)
		{
			// This is a condition, fall back to the `EventTriggerCreator` read.
			if (Mode == CharacteristicTriggerCreatorMode.Condition) {
				base.CharacteristicCellReadInitialValueForCharacteristic (cell, characteristic, completion);
				return;
			}

			INSCopying value;
			if (targetValueMap.TryGetValue (characteristic, out value)) {
				completion ((NSObject)value, null);
				return;
			}

			// The user may have updated the cell value while the read was happening. We check the map one more time.
			characteristic.ReadValue (error => {
				INSCopying v;
				if (targetValueMap.TryGetValue (characteristic, out v))
					completion ((NSObject)v, null);
				else
					completion (characteristic.Value, error);
			});
		}
		public void DidUpdateValueForCharacteristic (HMAccessory accessory, HMService service, HMCharacteristic characteristic)
		{
			var index = Array.IndexOf (service.Characteristics, characteristic);
			if (index >= 0) {
				var indexPath = NSIndexPath.FromRowSection (index, 0);
				var cell = (CharacteristicCell)TableView.CellAt (indexPath);
				cell.SetValue (characteristic.Value, false);
			}
		}
Exemple #45
0
        public override void CharacteristicCellReadInitialValueForCharacteristic(CharacteristicCell cell, HMCharacteristic characteristic, Action <NSObject, NSError> completion)
        {
            // This is a condition, fall back to the `EventTriggerCreator` read.
            if (Mode == CharacteristicTriggerCreatorMode.Condition)
            {
                base.CharacteristicCellReadInitialValueForCharacteristic(cell, characteristic, completion);
                return;
            }

            INSCopying value;

            if (targetValueMap.TryGetValue(characteristic, out value))
            {
                completion((NSObject)value, null);
                return;
            }

            // The user may have updated the cell value while the read was happening. We check the map one more time.
            characteristic.ReadValue(error => {
                INSCopying v;
                if (targetValueMap.TryGetValue(characteristic, out v))
                {
                    completion((NSObject)v, null);
                }
                else
                {
                    completion(characteristic.Value, error);
                }
            });
        }
		// Handles the value update and stores the value in the condition map.
		public virtual void CharacteristicCellDidUpdateValueForCharacteristic (CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
		{
			conditionValueMap [characteristic] = value;
		}
		// If the mode is event, update the event value. Otherwise, default to super implementation
		public override void CharacteristicCellDidUpdateValueForCharacteristic (CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
		{
			switch (Mode) {
			case CharacteristicTriggerCreatorMode.Event:
				UpdateEventValue ((INSCopying)value, characteristic);
				break;

			default:
				base.CharacteristicCellDidUpdateValueForCharacteristic (cell, value, characteristic, immediate);
				break;
			}
		}
Exemple #48
0
 // Responds to a cell change, and if the update was marked immediate, updates the characteristics.
 public void CharacteristicCellDidUpdateValueForCharacteristic(CharacteristicCell cell, NSObject value, HMCharacteristic characteristic, bool immediate)
 {
     pendingWrites [characteristic] = value;
     if (immediate)
     {
         UpdateCharacteristics();
     }
 }
		// Synchronously removes the characteristic-value pair from the `sentWrites` map.
		void DidCompleteWrite (HMCharacteristic characteristic, NSObject value)
		{
			updateQueue.DispatchSync (() => sentWrites.Remove (characteristic));
		}
		// Receives a callback from a `CharacteristicCell`, requesting an initial value for a given characteristic.
		// It checks to see if we have an action in this Action Set that matches the characteristic.
		// If so, calls the completion closure with the target value.
		public void CharacteristicCellReadInitialValueForCharacteristic (CharacteristicCell cell, HMCharacteristic characteristic, Action<NSObject, NSError> completion)
		{
			var value = TargetValueForCharacteristic (characteristic);
			if (value != null) {
				completion (value, null);
				return;
			}

			characteristic.ReadValue (error => {
				// The user may have updated the cell value while the
				// read was happening. We check the map one more time.
				var v = TargetValueForCharacteristic (characteristic);
				if (v != null)
					completion (v, null);
				else
					completion (characteristic.Value, error);
			});
		}
Exemple #51
0
 // Reads the characteristic's value and calls the completion with the characteristic's value.
 // If there is a pending write request on the same characteristic, the read is ignored to prevent "UI glitching".
 public void CharacteristicCellReadInitialValueForCharacteristic(CharacteristicCell cell, HMCharacteristic characteristic, Action <NSObject, NSError> completion)
 {
     characteristic.ReadValue(error => updateQueue.DispatchSync(() => {
         NSObject sentValue;
         if (sentWrites.TryGetValue(characteristic, out sentValue))
         {
             completion(sentValue, null);
             return;
         }
         DispatchQueue.MainQueue.DispatchAsync(() => completion(characteristic.Value, error));
     }));
 }
		// If a characteristic is selected, and it is the 'Identify' characteristic, perform an identify on that accessory.
		void DidSelectCharacteristic (HMCharacteristic characteristic, NSIndexPath indexPath)
		{
			if (characteristic.IsIdentify ()) {
				var accessory = Service.Accessory;
				if (accessory != null)
					accessory.Identify (error => {
						if (error != null)
							DisplayError (error);
					});
			}
		}
		public void DidUpdateValueForCharacteristic (HMAccessory accessory, HMService service, HMCharacteristic characteristic)
		{
			var s = characteristic.Service;
			if (s == null)
				return;

			var a = service.Accessory;
			if (a == null)
				return;

			var indexOfAccessory = Array.IndexOf (favoriteAccessories, accessory);
			if (indexOfAccessory < 0)
				return;

			var favoriteCharacteristics = FavoritesManager.SharedManager.FavoriteCharacteristicsForAccessory (accessory);
			var indexOfCharacteristic = Array.IndexOf (favoriteCharacteristics, characteristic);
			if (indexOfCharacteristic < 0)
				return;

			var indexPath = NSIndexPath.FromRowSection (indexOfCharacteristic, indexOfAccessory);
			var cell = (CharacteristicCell)TableView.CellAt (indexPath);
			cell.SetValue (characteristic.Value, false);
		}
Exemple #54
0
 // Synchronously adds the characteristic-value pair into the `sentWrites` map.
 void DidSendWrite(HMCharacteristic characteristic, NSObject value)
 {
     updateQueue.DispatchSync(() => {
         sentWrites [characteristic] = value;
     });
 }