public static async Task HandleAttributeInitialization(this INetDaemonAppBase netDaemonApp, INetDaemon daemon) { _ = daemon ?? throw new NetDaemonArgumentNullException(nameof(daemon)); _ = netDaemonApp ?? throw new NetDaemonArgumentNullException(nameof(netDaemonApp)); var netDaemonAppType = netDaemonApp.GetType(); foreach (var method in netDaemonAppType.GetMethods()) { foreach (var attr in method.GetCustomAttributes(false)) { if (netDaemonApp is NetDaemonRxApp daemonRxApp) { switch (attr) { case HomeAssistantServiceCallAttribute: await HandleServiceCallAttribute(daemon, daemonRxApp, method, false).ConfigureAwait(false); break; } } } } }
public static async Task HandleAttributeInitialization(this INetDaemonAppBase netDaemonApp, INetDaemon _daemon) { var netDaemonAppType = netDaemonApp.GetType(); foreach (var method in netDaemonAppType.GetMethods()) { foreach (var attr in method.GetCustomAttributes(false)) { if (netDaemonApp is NetDaemonApp daemonApp) { switch (attr) { case HomeAssistantServiceCallAttribute hasstServiceCallAttribute: await HandleServiceCallAttribute(_daemon, daemonApp, method, true).ConfigureAwait(false); break; case HomeAssistantStateChangedAttribute hassStateChangedAttribute: HandleStateChangedAttribute(_daemon, hassStateChangedAttribute, daemonApp, method); break; } } else if (netDaemonApp is NetDaemonRxApp daemonRxApp) { switch (attr) { case HomeAssistantServiceCallAttribute hasstServiceCallAttribute: await HandleServiceCallAttribute(_daemon, daemonRxApp, method, false).ConfigureAwait(false); break; } } } } }
/// <summary> /// Intended for internal use by fakes only /// </summary> public static ApplicationContext CreateFromAppInstanceForTest(INetDaemonAppBase applicationInstance, IServiceProvider serviceProvider) { if (applicationInstance == null) { throw new ArgumentNullException(nameof(applicationInstance)); } var applicationContext = new AppBaseApplicationContext(applicationInstance, serviceProvider); return(applicationContext); }
public AppBaseApplicationContext(Type applicationType, string id, IServiceProvider serviceProvider) : base(applicationType, id, serviceProvider) { _appInstance = (INetDaemonAppBase)ActivatorUtilities.GetServiceOrCreateInstance(ServiceProvider, applicationType); _appInstance.Id = id; if (_appInstance is NetDaemonAppBase appBase) { appBase.ServiceProvider = ServiceProvider; } ApplicationInstance = _appInstance; Id = id; }
/// <summary> /// Constructor from an already instantiated app (used for unit testing) /// </summary> /// <param name="appInstance"></param> /// <param name="serviceProvider"></param> public AppBaseApplicationContext(INetDaemonAppBase appInstance, IServiceProvider serviceProvider) : base(appInstance.GetType(), appInstance.Id !, serviceProvider)
public static object?ToObject(this YamlScalarNode node, Type valueType, INetDaemonAppBase deamonApp) { _ = valueType ?? throw new NetDaemonArgumentNullException(nameof(valueType)); _ = node ?? throw new NetDaemonArgumentNullException(nameof(node)); Type?underlyingNullableType = Nullable.GetUnderlyingType(valueType); if (underlyingNullableType != null) { // It is nullable type valueType = underlyingNullableType; } switch (valueType.Name) { case "String": return(node.Value); case "Int32": if (int.TryParse(node.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out int i32Value)) { return(i32Value); } break; case "Int64": if (long.TryParse(node.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out long i64Value)) { return(i64Value); } break; case "Decimal": if (decimal.TryParse(node.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out decimal decimalValue)) { return(decimalValue); } break; case "Single": if (float.TryParse(node.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out float floatValue)) { return(floatValue); } break; case "Double": if (double.TryParse(node.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out double doubleValue)) { return(doubleValue); } break; case "Boolean": if (bool.TryParse(node.Value, out bool boolValue)) { return(boolValue); } break; } if (valueType.IsAssignableTo(typeof(RxEntityBase))) { return(Activator.CreateInstance(valueType, deamonApp, new[] { node.Value })); } return(null); }
/// <summary> /// Constructor /// </summary> /// <param name="logger">A ILogger instance</param> /// <param name="app">App being tracked</param> public ObservableBase(ILogger logger, INetDaemonAppBase app) { _logger = logger; _app = app; }
[SuppressMessage("", "CA1508")] // Weird bug that this should not warn! private object?InstanceProperty(INetDaemonAppBase deamonApp, object?parent, Type instanceType, YamlNode node) { if (node.NodeType == YamlNodeType.Scalar) { var scalarNode = (YamlScalarNode)node; ReplaceSecretIfExists(scalarNode); return(((YamlScalarNode)node).ToObject(instanceType, deamonApp)); } else if (node.NodeType == YamlNodeType.Sequence) { if (instanceType.IsGenericType && instanceType?.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { Type listType = instanceType?.GetGenericArguments()[0] ?? throw new NetDaemonNullReferenceException($"The property {instanceType?.Name} of Class {parent?.GetType().Name} is not compatible with configuration"); IList list = listType.CreateListOfPropertyType() ?? throw new NetDaemonNullReferenceException("Failed to create listtype, please check {prop.Name} of Class {app.GetType().Name}"); foreach (YamlNode item in ((YamlSequenceNode)node).Children) { var instance = InstanceProperty(deamonApp, null, listType, item) ?? throw new NotSupportedException($"The class {parent?.GetType().Name} has wrong type in items"); list.Add(instance); } return(list); } } else if (node.NodeType == YamlNodeType.Mapping) { var instance = Activator.CreateInstance(instanceType); foreach (KeyValuePair <YamlNode, YamlNode> entry in ((YamlMappingNode)node).Children) { string?scalarPropertyName = ((YamlScalarNode)entry.Key).Value; // Just continue to next configuration if null or class declaration if (scalarPropertyName == null) { continue; } var childProp = instanceType.GetYamlProperty(scalarPropertyName) ?? throw new MissingMemberException($"{scalarPropertyName} is missing from the type {instanceType}"); var valueType = entry.Value.NodeType; object?result = null; switch (valueType) { case YamlNodeType.Sequence: result = InstanceProperty(deamonApp, instance, childProp.PropertyType, (YamlSequenceNode)entry.Value); break; case YamlNodeType.Scalar: result = InstanceProperty(deamonApp, instance, childProp.PropertyType, (YamlScalarNode)entry.Value); break; case YamlNodeType.Mapping: // Maps are not currently supported (var map = (YamlMappingNode)entry.Value;) break; } childProp.SetValue(instance, result); } return(instance); } return(null); }