public int AddOperationOnHomeDeviceProgram(int idHomeDevice, int idHomeDeviceDestination, string operationName, object[] args) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDeviceDestination = repository.HomeDeviceRespository.GetById(idHomeDeviceDestination); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("HomeDevice id doesn't exist"); } if (homeDeviceDestination == null) { throw new ArgumentException("HomeDevice destination id doesn't exist"); } Operation operation = new Operation { DestionationHomeDevice = homeDeviceDestination, OperationName = operationName, Args = args }; homeDevice.Operations.Add(operation); int idRes = repository.OperationRepository.Insert(operation).Id; repository.Commit(); return(idRes); }
/// <summary> /// Connect without checks the connector with the homeDevice /// </summary> /// <param name="connector"></param> /// <param name="homeDevice"></param> public static void LinkHomeDevice(this Connector connector, HomeDevice homeDevice) { connector.HomeDevices = new List <HomeDevice> { homeDevice }; connector.Product = null; }
void statusModule_StateRefreshed(object sender, HomeDevice e) { if (this.pendingRequests.ContainsKey(e)) { this.pendingRequests[e].SetResult(true); } }
private void OnStateRefresh(HomeDevice hd) { if (StateRefreshed != null) { StateRefreshed(hd, hd); } }
public static List <PropertyParam> GetStateValue(this HomeDevice homeDevice) { List <PropertyParam> propiertyValues = new List <PropertyParam>(); var filterProperties = homeDevice.HomeDeviceType.GetProperties().Where(p => p.GetCustomAttributes(true) .OfType <PropertyAttribute>() .Where(a => !a.Internal).Count() > 0); foreach (var item in filterProperties) { Type type = item.PropertyType; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { type = type.GetGenericArguments()[0]; } propiertyValues.Add(new PropertyParam { Name = item.Name, Type = type, Value = item.GetValue(homeDevice, null) }); } return(propiertyValues); }
public OperationDefinitionDTO GetDefinitionOperation(int idHomeDevice, string nameOperation) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("HomeDevice id doesn't exist"); } MethodInfo method = homeDevice.GetArgsOperation(nameOperation); // For extension method. Remove the frist parameter (extension parameter) IEnumerable <ParameterInfo> methodParameters = method.IsDefined(typeof(ExtensionAttribute), true) ? method.GetParameters().Skip(1) : method.GetParameters(); OperationDefinitionDTO operationResult = new OperationDefinitionDTO { NameOperation = method.Name, ReturnValueType = method.ReturnType.ToString(), Args = methodParameters .Select(p => new ParamDTO { Name = p.Name, Type = p.ParameterType.ToString(), Value = p.DefaultValue }) .ToArray(), }; return(operationResult); }
public static bool IsCapable(this Connector connector, HomeDevice homeDevice) { if (connector.ConnectorType == ConnectorTypes.DimmerPassZero) { return(false); } return(connector.ConnectorType == homeDevice.ConnectorCapable); }
public static OperationMessage GetAddressableOperation(this HomeDevice homeDevice, OperationMessage message) { if (homeDevice.InUse) { message.DestinationAddress = (ushort)homeDevice.Connector.Node.Address; } return(message); }
public static MethodInfo[] GetHomeDeviceMethodOperations(this HomeDevice homeDevice) { Type homeDeviceType = homeDevice.HomeDeviceType; if (homeDeviceType == null || !typeof(HomeDevice).IsAssignableFrom(homeDeviceType)) { throw new ArgumentException("This homeDevice is not a valid home Device"); } return(HomeDeviceOperations[homeDeviceType]); }
/// <summary> /// Alias over the <see cref="AsyncContext.Until{TAbortTrigger}"/> specialized for <see cref="HomeDevice{TDevice}"/> /// </summary> /// <typeparam name="T">Type of the device</typeparam> /// <param name="device">The home device</param> /// <param name="predicate">The predicate</param> /// <param name="assumeIntialState">Determines if the <paramref name="predicate" /> result should be validated or not (cf Remarks)</param> /// <remarks> /// If the flag <see cref="assumeIntialState"/> is set, all values of the device will be ignored until the predicates returns `true`. /// This is useful to filter out initial states until a state is effectively applied. /// </remarks> /// <returns>An async context that will be cancelled (cf. <see cref="AsyncContext.Cancel"/>) if the <paramref name="predicate" /> returns `false`.</returns> public static AsyncContext While <T>(this HomeDevice <T> device, Predicate <T> predicate, bool assumeIntialState = true) { var isValid = device.Select(dev => predicate(dev)); if (assumeIntialState) { isValid = isValid.SkipWhile(state => !state); } return(AsyncContext.Until(isValid.Where(state => !state))); }
public static void Set <T>(this HomeDevice <T> device, DeviceState state) { if (device.Host is TestDeviceHost testHost) { testHost.Publish(state); } else { throw new InvalidOperationException("This device was not initialized from a TestDeviceHost, you cannot set its state."); } }
public static Nullable <T> ReadProperty <T>(this HomeDevice homeDevice, string propertyName) where T : struct { string propKey = homeDevice.GetPropertyKey(propertyName); if (statusMap.ContainsKey(propKey)) { return((T)statusMap[propKey]); } return(null); }
public IEnumerable <OperationProgrammedDTO> GetHomeDeviceOperationProgram(int idHomeDevice) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("HomeDevice id doesn't exist"); } return(Mapper.Map <IEnumerable <OperationProgrammedDTO> >(homeDevice.Operations)); }
public string[] GetExecutableHomeDeviceNameOperations(int idHomeDevice) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("HomeDevice id doesn't exist"); } return(homeDevice.GetHomeDeviceNameOperations()); }
public async Task <bool> RefreshHomeDevice(HomeDevice homeDevice, TimeSpan timeout) { //TODO: To be implemented OperationMessage refreshMessage = homeDevice.RefreshState(); //If the home device can't be updated. Return true. if (refreshMessage == null) { return(true); } if (!this.pendingRequests.ContainsKey(homeDevice)) { this.pendingRequests.Add(homeDevice, new TaskCompletionSource <bool>()); bool sent = await this.SendMessage(refreshMessage); if (sent) { var pendingConfirmationTask = this.pendingRequests[homeDevice]; //Await for a confirmation with timeout Task delayTask = Task.Delay((int)timeout.TotalMilliseconds); Task firstTask = await Task.WhenAny(pendingConfirmationTask.Task, delayTask); bool result = false; if (firstTask == delayTask) { //Timeout this.PrintLog(true, "TIMEOUT during HD update"); pendingConfirmationTask.SetCanceled(); this.pendingRequests.Remove(homeDevice); } else { result = pendingConfirmationTask.Task.Result; this.pendingRequests.Remove(homeDevice); } return(result); } } else { return(await this.pendingRequests[homeDevice].Task); } return(false); }
public List <HomeDevice> GetInstanceProducts() { List <HomeDevice> homeDeviceResult = new List <HomeDevice>(); for (int i = 0; i < MapProduct.Count; i++) { HomeDevice homeDev = BusinessHomeDevice.BusinessHomeDevice.CreateHomeDevice(MapProduct[i].Item1.Name); homeDev.ProductTag = i; homeDeviceResult.Add(homeDev); } return(homeDeviceResult); }
public void SetNameHomeDevice(int idHomeDevice, string newName) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("Home device id doesn't exist"); } homeDevice.Name = newName; repository.Commit(); }
private byte GetLogicConfiguration(Connector connector, HomeDevice homeDevice) { PinPortConfiguration ppc = connector.GetPinPortConfiguration(homeDevice); byte res = 0x00; if (ppc.Output) { res |= (byte)(0x01 << 3); } if (ppc.DefaultValueD) { res |= (byte)(0x01 << 2); } res |= (byte)ppc.ChangeTypeD; return(res); }
public static PinPort[] GetPinPorts(this HomeDevice homeDevice) { if (!homeDevice.InUse) { return(null); } if (!homeDevice.ProductTag.HasValue)//no es un producto { return(homeDevice.Connector.GetPinPort()); } //Is Product List <PinPort> pinPorts = new List <PinPort>(); //TODO return(pinPorts.ToArray()); }
public static OperationMessage RefreshState(this HomeDevice homeDevice) { if (homeDevice is WallPlug) { return((homeDevice as WallPlug).RefreshState()); } if (homeDevice is Light) { return((homeDevice as Light).RefreshState()); } if (homeDevice is Dimmable) { return((homeDevice as Dimmable).RefreshState()); } if (homeDevice is HumiditySensor) { return((homeDevice as HumiditySensor).RefreshState()); } if (homeDevice is PowerSensor) { return((homeDevice as PowerSensor).RefreshState()); } if (homeDevice is PresenceSensor) { return((homeDevice as PresenceSensor).RefreshState()); } if (homeDevice is RGBLight) { return((homeDevice as RGBLight).RefreshState()); } if (homeDevice is SwitchButton) { return((homeDevice as SwitchButton).RefreshState()); } if (homeDevice is TemperatureSensor) { return((homeDevice as TemperatureSensor).RefreshState()); } if (homeDevice is Button) { return(null); } throw new ArgumentException("HomeDevice not valid"); }
private void ProcessLogicReading(ushort nodeAddress, ushort deviceAddress, int value) { if (value == 0xFF) //Invalid Reading { PrintLog(true, string.Format("LOGIC READING FROM 0x{0:X4} DEVICE {1}: UNKNOWN", nodeAddress, deviceAddress)); } else { bool status = (value != 0); PrintLog(false, string.Format("LOGIC READING FROM 0x{0:X4} DEVICE {1}: {2}", nodeAddress, deviceAddress, status ? "HIGH" : "LOW")); HomeDevice logicHD = CheckHomeDevice <HomeDevice>(nodeAddress, deviceAddress); if (logicHD is WallPlug) { (logicHD as WallPlug).IsOn = status; } else if (logicHD is Light) { (logicHD as Light).IsOn = status; } else if (logicHD is Button) { //TODO: Raise Button Pressed Event } else if (logicHD is SwitchButton) { (logicHD as SwitchButton).Open = status; } else { PrintLog(true, "HD type not spected!"); } if (logicHD != null) { logicHD.LastStatusUpdate = DateTime.Now; OnStateRefresh(logicHD); } } }
public static PinPortConfiguration GetPinPortConfiguration(HomeDevice homeDevice) { PinPortConfiguration configuration = DefaultPinPortConfiguration(); if (homeDevice is Button) { configuration.Output = false; configuration.Digital = true; configuration.ChangeTypeD = PinPortConfiguration.Trigger.FallingEdge; } else if (homeDevice is SwitchButton) { configuration.Output = false; configuration.Digital = true; configuration.ChangeTypeD = PinPortConfiguration.Trigger.Both; } else if (homeDevice is Light || homeDevice is WallPlug) { configuration.Output = true; configuration.Digital = true; configuration.DefaultValueD = false; } else if (homeDevice is Dimmable) { configuration.Output = true; configuration.Digital = false; configuration.DefaultValueA = 0x00; } else if (homeDevice is PresenceSensor) { configuration.Output = false; configuration.Digital = true; configuration.ChangeTypeD = PinPortConfiguration.Trigger.RisingEdge; } else if (homeDevice is DoorLock) { configuration.Output = true; configuration.Digital = true; } return(configuration); }
public static void StoreProperty(this HomeDevice homeDevice, string propertyName, object value) { string propKey = homeDevice.GetPropertyKey(propertyName); if (statusMap.ContainsKey(propKey)) { if (value == null) { statusMap.Remove(propKey); } else { statusMap[propKey] = value; } } else if (value != null) { statusMap.Add(propKey, value); } }
public void UnlinkHomeDevice(int idHomeDevice) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("HomeDevice Id doesn't exist"); } Connector connector = homeDevice.Connector; homeDevice.Connector.UnlinkHomeDevice(); //UPDATE CHECKSUM connector.Node.UpdateChecksum(null); repository.Commit(); }
public IEnumerable <ConnectorDTO> GetConnectorsCapable(int idHomeDevice, int idNode) { UnitOfWork repository = UnitOfWork.GetInstance(); Node node = repository.NodeRespository.GetById(idNode); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (node == null) { throw new ArgumentException("Node Id doesn't exist"); } if (homeDevice == null) { throw new ArgumentException("HomeDevice Id doesn't exist"); } var connectorsResult = node.Connectors.Where(c => c.IsCapable(homeDevice) && c.InUse == false); return(Mapper.Map <IEnumerable <ConnectorDTO> >(connectorsResult)); }
public void RemoveHomeDevice(int idHomeDevice) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); if (homeDevice == null) { throw new ArgumentException("Home Device id doesn't exist"); } if (homeDevice.InUse) { //UPDATE CHECKSUM homeDevice.Connector.Node.UpdateChecksum(null); Services.HomeDeviceService.UnlinkHomeDevice(idHomeDevice); } repository.HomeDeviceRespository.Delete(homeDevice); repository.Commit(); }
public int AddScheduler(byte weekDays, TimeSpan time, string name, int idHomeDeviceDestination, string operation, object[] args = null) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDeviceDestination); if (homeDevice == null) { throw new ArgumentException("HomeDevice id doesn't exist"); } Operation operationInternal = new Operation { Args = args, Name = name, OperationName = operation, DestionationHomeDevice = homeDevice }; TimeOperation timeOperation = new TimeOperation { MaskWeekDays = weekDays, Time = time, Operation = operationInternal }; int idRes = repository.TimeOperationRepository.Insert(timeOperation).Id; if (homeDevice.InUse) { //UPDATE CHECKSUM homeDevice.Connector.Node.UpdateChecksum(null); } repository.Commit(); return(idRes); }
public void LinkHomeDevice(int idConnector, int idHomeDevice) { UnitOfWork repository = UnitOfWork.GetInstance(); Connector connector = repository.ConnectorRepository.GetById(idConnector); HomeDevice homeDevice = repository.HomeDeviceRespository.GetById(idHomeDevice); //TODO: falta chequear que el homedevice sea compatible con el conector if (connector == null) { throw new ArgumentException("Connector Id doesn't exist"); } if (homeDevice == null) { throw new ArgumentException("HomeDevice Id doesn't exist"); } if (connector.InUse) { throw new ArgumentException("The connector has been occuped by other home device"); } if (homeDevice.InUse) { throw new ArgumentException("The HomeDevice has been occuped by other connector"); } //UPDATE CHECKSUM connector.Node.UpdateChecksum(null); connector.LinkHomeDevice(homeDevice); repository.Commit(); }
public void ExecuteOperation(int idHomeDeviceDestination, string operationName, object[] args) { UnitOfWork repository = UnitOfWork.GetInstance(); HomeDevice homeDeviceDestination = repository.HomeDeviceRespository.GetById(idHomeDeviceDestination); if (homeDeviceDestination == null) { throw new ArgumentException("HomeDevice destination id doesn't exist"); } if (!homeDeviceDestination.GetHomeDeviceNameOperations().Contains(operationName)) { throw new ArgumentException("OperationName is not available on this HomeDevice"); } Operation operation = new Operation { DestionationHomeDevice = homeDeviceDestination, OperationName = operationName, Args = new object[] { homeDeviceDestination }.Concat(args).ToArray(), }; operation.Execute(); }
public static MethodInfo GetArgsOperation(this HomeDevice homeDevice, string nameOperation) { return(GetHomeDeviceMethodOperations(homeDevice).First(m => m.Name == nameOperation)); }