/// <summary> /// Class redirecting sniffer output to a remote client. /// </summary> /// <param name="FullJID">Full JID of remote client.</param> /// <param name="Expires">When the sniffer should automatically expire.</param> /// <param name="Node">Node being sniffed.</param> /// <param name="ConcentratorServer">Concentrator server managing nodes.</param> public RemoteSniffer(string FullJID, DateTime Expires, ISniffable Node, ConcentratorServer ConcentratorServer) { this.id = Guid.NewGuid().ToString().Replace("-", string.Empty); this.fullJID = FullJID; this.expires = Expires; this.node = Node; this.concentratorServer = ConcentratorServer; }
public WaitHandle Start() { ConcentratorServer ConcentratorServer = null; ProvisioningClient ProvisioningClient = null; if (Types.TryGetModuleParameter("Concentrator", out object Obj)) { ConcentratorServer = Obj as ConcentratorServer; } if (Types.TryGetModuleParameter("Provisioning", out Obj)) { ProvisioningClient = Obj as ProvisioningClient; } this.bobClient = new BobClient(Gateway.XmppClient, Path.Combine(Gateway.AppDataFolder, "BoB")); this.chatServer = new ChatServer(Gateway.XmppClient, this.bobClient, ConcentratorServer, ProvisioningClient); return(null); }
/// <summary> /// Adds the extension to the client. /// </summary> /// <param name="Instance">Actor instance.</param> /// <param name="Client">XMPP Client</param> /// <returns>Extension object.</returns> public override Task <object> Add(IActor Instance, Waher.Networking.XMPP.XmppClient Client) { ConcentratorServer Extension; if (!Client.TryGetTag("ProvisioningClient", out object Obj) || !(Obj is ProvisioningClient ProvisioningClient)) { ProvisioningClient = null; } if (!Client.TryGetTag("ThingRegistryClient", out Obj) || !(Obj is ThingRegistryClient ThingRegistryClient)) { ThingRegistryClient = null; } Extension = new ConcentratorServer(Client, ThingRegistryClient, ProvisioningClient); Client.SetTag("ConcentratorServer", Extension); return(Task.FromResult <object>(Extension)); }
//存储集中器信息 private void SavaConcentrator(Socket con, byte[] message) { //保存集中器IP、编号 string ConcerIP = ((IPEndPoint)(con.RemoteEndPoint)).Address.ToString(); Concentrator concer = new Concentrator(); concer.Unit_Id = (message[0] * 256 + message[1]).ToString(); concer.Unit_IpAddress = ConcerIP; concer.Unit_Status = "1"; concer.Unit_Latitude = ""; concer.Unit_Longitude = ""; ConcentratorServer SaveConcen = new ConcentratorServer(); string sql = "select * from concentrator where UnitID = " + concer.Unit_Id; if (db.Select(sql) != null) { string sqlSave = string.Format("insert into sensordata values('{0}','{1}','{2}','{3}','{4}')", concer.Unit_Id, concer.Unit_IpAddress, concer.Unit_Status, concer.Unit_Longitude, concer.Unit_Latitude); db.Insert(sqlSave); } }
/// <summary> /// Stops the gateway. /// </summary> public static void Stop() { IDisposable Disposable; Log.Informational("Server shutting down."); /* * if (databaseProvider != null) * File.WriteAllText(appDataFolder + "Stop.xml", databaseProvider.ExportXml(true).Result); */ if (gatewayRunning != null) { gatewayRunning.Release(); gatewayRunning.Dispose(); gatewayRunning = null; } if (ibbClient != null) { ibbClient.Dispose(); ibbClient = null; } if (httpxServer != null) { httpxServer.Dispose(); httpxServer = null; } if (provisioningClient != null) { provisioningClient.Dispose(); provisioningClient = null; } if (thingRegistryClient != null) { thingRegistryClient.Dispose(); thingRegistryClient = null; } if (concentratorServer != null) { concentratorServer.Dispose(); concentratorServer = null; } if (xmppClient != null) { using (ManualResetEvent OfflineSent = new ManualResetEvent(false)) { xmppClient.SetPresence(Availability.Offline, string.Empty, (sender, e) => OfflineSent.Set()); OfflineSent.WaitOne(1000); } foreach (ISniffer Sniffer in xmppClient.Sniffers) { XmppClient.Remove(Sniffer); Disposable = Sniffer as IDisposable; if (Disposable != null) { Disposable.Dispose(); } } xmppClient.Dispose(); xmppClient = null; } if (connectionTimer != null) { connectionTimer.Dispose(); connectionTimer = null; } if (coapEndpoint != null) { coapEndpoint.Dispose(); coapEndpoint = null; } if (webServer != null) { foreach (ISniffer Sniffer in webServer.Sniffers) { webServer.Remove(Sniffer); Disposable = Sniffer as IDisposable; if (Disposable != null) { Disposable.Dispose(); } } webServer.Dispose(); webServer = null; } clientEvents = null; }
/// <summary> /// Starts the gateway. /// </summary> /// <param name="ConsoleOutput">If console output is permitted.</param> public static bool Start(bool ConsoleOutput) { gatewayRunning = new Semaphore(1, 1, "Waher.IoTGateway.Running"); if (!gatewayRunning.WaitOne(1000)) { return(false); // Is running in another process. } Semaphore StartingServer = new Semaphore(1, 1, "Waher.IoTGateway.Starting"); if (!StartingServer.WaitOne(1000)) { gatewayRunning.Release(); gatewayRunning.Dispose(); gatewayRunning = null; StartingServer.Dispose(); return(false); // Being started in another process. } try { Initialize(); appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); if (!appDataFolder.EndsWith(new string(Path.DirectorySeparatorChar, 1))) { appDataFolder += Path.DirectorySeparatorChar; } appDataFolder += "IoT Gateway" + Path.DirectorySeparatorChar; Log.Register(new XmlFileEventSink("XML File Event Sink", appDataFolder + "Events" + Path.DirectorySeparatorChar + "Event Log %YEAR%-%MONTH%-%DAY%T%HOUR%.xml", appDataFolder + "Transforms" + Path.DirectorySeparatorChar + "EventXmlToHtml.xslt", 7)); Log.Informational("Server starting up."); beforeUninstallCommandNr = Gateway.RegisterServiceCommand(BeforeUninstall); rootFolder = appDataFolder + "Root" + Path.DirectorySeparatorChar; if (!Directory.Exists(rootFolder)) { appDataFolder = string.Empty; rootFolder = "Root" + Path.DirectorySeparatorChar; } Types.SetModuleParameter("AppData", appDataFolder); Types.SetModuleParameter("Root", rootFolder); scheduler = new Scheduler(); rnd = RandomNumberGenerator.Create(); Task.Run(() => CodeContent.GraphViz.Init()); XmlDocument Config = new XmlDocument(); string GatewayConfigFileName = appDataFolder + "Gateway.config"; if (!File.Exists(GatewayConfigFileName)) { GatewayConfigFileName = "Gateway.config"; } Config.Load(GatewayConfigFileName); XSL.Validate("Gateway.config", Config, "GatewayConfiguration", "http://waher.se/Schema/GatewayConfiguration.xsd", XSL.LoadSchema(typeof(Gateway).Namespace + ".Schema.GatewayConfiguration.xsd", typeof(Gateway).Assembly)); domain = Config.DocumentElement["Domain"].InnerText; XmlElement DatabaseConfig = Config.DocumentElement["Database"]; if (!CommonTypes.TryParse(DatabaseConfig.Attributes["encrypted"].Value, out bool Encrypted)) { Encrypted = true; } databaseProvider = new FilesProvider(appDataFolder + DatabaseConfig.Attributes["folder"].Value, DatabaseConfig.Attributes["defaultCollectionName"].Value, int.Parse(DatabaseConfig.Attributes["blockSize"].Value), int.Parse(DatabaseConfig.Attributes["blocksInCache"].Value), int.Parse(DatabaseConfig.Attributes["blobBlockSize"].Value), Encoding.UTF8, int.Parse(DatabaseConfig.Attributes["timeoutMs"].Value), Encrypted, false); Database.Register(databaseProvider); PersistedEventLog PersistedEventLog = new PersistedEventLog(7, new TimeSpan(4, 15, 0)); Log.Register(PersistedEventLog); PersistedEventLog.Queue(new Event(EventType.Informational, "Server starting up.", string.Empty, string.Empty, string.Empty, EventLevel.Minor, string.Empty, string.Empty, string.Empty)); xmppConfigFileName = Config.DocumentElement["XmppClient"].Attributes["configFileName"].Value; if (!File.Exists(xmppConfigFileName)) { xmppConfigFileName = appDataFolder + xmppConfigFileName; } if (ConsoleOutput) { xmppConfiguration = SimpleXmppConfiguration.GetConfigUsingSimpleConsoleDialog(xmppConfigFileName, Guid.NewGuid().ToString().Replace("-", string.Empty), // Default user name. Guid.NewGuid().ToString().Replace("-", string.Empty), // Default password. FormSignatureKey, FormSignatureSecret, typeof(Gateway).Assembly); } else if (File.Exists(xmppConfigFileName)) { xmppConfiguration = new SimpleXmppConfiguration(xmppConfigFileName); RuntimeSettings.Set("XMPP.CONFIG", xmppConfiguration.ExportSimpleXmppConfiguration()); } else { string XmppConfig = RuntimeSettings.Get("XMPP.CONFIG", string.Empty); XmlDocument Doc = new XmlDocument(); Doc.LoadXml(XmppConfig); xmppConfiguration = new SimpleXmppConfiguration(Doc); } xmppClient = xmppConfiguration.GetClient("en", typeof(Gateway).Assembly, false); xmppClient.AllowRegistration(FormSignatureKey, FormSignatureSecret); xmppClient.OnValidateSender += XmppClient_OnValidateSender; Types.SetModuleParameter("XMPP", xmppClient); if (xmppConfiguration.Sniffer) { ISniffer Sniffer; if (ConsoleOutput) { Sniffer = new ConsoleOutSniffer(BinaryPresentationMethod.ByteCount, LineEnding.PadWithSpaces); xmppClient.Add(Sniffer); } Sniffer = new XmlFileSniffer(appDataFolder + "XMPP" + Path.DirectorySeparatorChar + "XMPP Log %YEAR%-%MONTH%-%DAY%T%HOUR%.xml", appDataFolder + "Transforms" + Path.DirectorySeparatorChar + "SnifferXmlToHtml.xslt", 7, BinaryPresentationMethod.ByteCount); xmppClient.Add(Sniffer); } if (!string.IsNullOrEmpty(xmppConfiguration.Events)) { Log.Register(new XmppEventSink("XMPP Event Sink", xmppClient, xmppConfiguration.Events, false)); } if (!string.IsNullOrEmpty(xmppConfiguration.ThingRegistry)) { thingRegistryClient = new ThingRegistryClient(xmppClient, xmppConfiguration.ThingRegistry); thingRegistryClient.Claimed += ThingRegistryClient_Claimed; thingRegistryClient.Disowned += ThingRegistryClient_Disowned; thingRegistryClient.Removed += ThingRegistryClient_Removed; } if (!string.IsNullOrEmpty(xmppConfiguration.Provisioning)) { provisioningClient = new ProvisioningClient(xmppClient, xmppConfiguration.Provisioning); } DateTime Now = DateTime.Now; int MsToNext = 60000 - (Now.Second * 1000 + Now.Millisecond); connectionTimer = new Timer(CheckConnection, null, MsToNext, 60000); xmppClient.OnStateChanged += XmppClient_OnStateChanged; xmppClient.OnPresenceSubscribe += XmppClient_OnPresenceSubscribe; xmppClient.OnPresenceUnsubscribe += XmppClient_OnPresenceUnsubscribe; xmppClient.OnRosterItemUpdated += XmppClient_OnRosterItemUpdated; ibbClient = new Networking.XMPP.InBandBytestreams.IbbClient(xmppClient, MaxChunkSize); Types.SetModuleParameter("IBB", ibbClient); socksProxy = new Networking.XMPP.P2P.SOCKS5.Socks5Proxy(xmppClient); Types.SetModuleParameter("SOCKS5", socksProxy); string CertificateLocalFileName = Config.DocumentElement["Certificate"].Attributes["configFileName"].Value; string CertificateFileName; string CertificateXml; string CertificatePassword; byte[] CertificateRaw; try { CertificateRaw = Convert.FromBase64String(RuntimeSettings.Get("CERTIFICATE.BASE64", string.Empty)); CertificatePassword = RuntimeSettings.Get("CERTIFICATE.PWD", string.Empty); certificate = new X509Certificate2(CertificateRaw, CertificatePassword); } catch (Exception) { certificate = null; } if (File.Exists(CertificateFileName = appDataFolder + CertificateLocalFileName)) { CertificateXml = File.ReadAllText(CertificateFileName); } else if (File.Exists(CertificateFileName = CertificateLocalFileName) && certificate == null) { CertificateXml = File.ReadAllText(CertificateFileName); } else { CertificateFileName = null; CertificateXml = null; } if (CertificateXml != null) { XmlDocument CertificateConfig = new XmlDocument(); CertificateConfig.LoadXml(CertificateXml); XSL.Validate(CertificateLocalFileName, CertificateConfig, "CertificateConfiguration", "http://waher.se/Schema/CertificateConfiguration.xsd", XSL.LoadSchema(typeof(Gateway).Namespace + ".Schema.CertificateConfiguration.xsd", typeof(Gateway).Assembly)); CertificateLocalFileName = CertificateConfig.DocumentElement["FileName"].InnerText; if (File.Exists(appDataFolder + CertificateLocalFileName)) { CertificateLocalFileName = appDataFolder + CertificateLocalFileName; } CertificateRaw = File.ReadAllBytes(CertificateLocalFileName); CertificatePassword = CertificateConfig.DocumentElement["Password"].InnerText; certificate = new X509Certificate2(CertificateRaw, CertificatePassword); RuntimeSettings.Set("CERTIFICATE.BASE64", Convert.ToBase64String(CertificateRaw)); RuntimeSettings.Set("CERTIFICATE.PWD", CertificatePassword); if (CertificateLocalFileName != "certificate.pfx" || CertificatePassword != "testexamplecom") { try { File.Delete(CertificateLocalFileName); } catch (Exception) { Log.Warning("Unable to delete " + CertificateLocalFileName + " after importing it into the encrypted database."); } try { File.Delete(CertificateFileName); } catch (Exception) { Log.Warning("Unable to delete " + CertificateFileName + " after importing it into the encrypted database."); } } } foreach (XmlNode N in Config.DocumentElement["Ports"].ChildNodes) { if (N.LocalName == "Port") { XmlElement E = (XmlElement)N; string Protocol = XML.Attribute(E, "protocol"); if (!string.IsNullOrEmpty(Protocol) && int.TryParse(E.InnerText, out int Port)) { ports.AddLast(new KeyValuePair <string, int>(Protocol, Port)); } } } webServer = new HttpServer(GetConfigPorts("HTTP"), GetConfigPorts("HTTPS"), certificate); Types.SetModuleParameter("HTTP", webServer); StringBuilder sb = new StringBuilder(); foreach (int Port in webServer.OpenPorts) { sb.AppendLine(Port.ToString()); } try { File.WriteAllText(appDataFolder + "Ports.txt", sb.ToString()); } catch (Exception ex) { Log.Critical(ex); } HttpFolderResource HttpFolderResource; HttpxProxy HttpxProxy; webServer.Register(new HttpFolderResource("/Graphics", "Graphics", false, false, true, false)); // TODO: Add authentication mechanisms for PUT & DELETE. webServer.Register(new HttpFolderResource("/highlight", "Highlight", false, false, true, false)); // Syntax highlighting library, provided by http://highlightjs.org webServer.Register(HttpFolderResource = new HttpFolderResource(string.Empty, rootFolder, false, false, true, true)); // TODO: Add authentication mechanisms for PUT & DELETE. webServer.Register(HttpxProxy = new HttpxProxy("/HttpxProxy", xmppClient, MaxChunkSize)); webServer.Register("/", (req, resp) => { throw new TemporaryRedirectException(Config.DocumentElement["DefaultPage"].InnerText); }); webServer.Register(clientEvents = new ClientEvents()); HttpFolderResource.AllowTypeConversion(); MarkdownToHtmlConverter.EmojiSource = Emoji1_24x24; XmlElement FileFolders = Config.DocumentElement["FileFolders"]; if (FileFolders != null) { foreach (XmlNode N in FileFolders.ChildNodes) { if (N is XmlElement E && E.LocalName == "FileFolder") { string WebFolder = XML.Attribute(E, "webFolder"); string FolderPath = XML.Attribute(E, "folderPath"); webServer.Register(new HttpFolderResource(WebFolder, FolderPath, false, false, true, true)); } } } httpxServer = new HttpxServer(xmppClient, webServer, MaxChunkSize); Types.SetModuleParameter("HTTPX", HttpxProxy); Types.SetModuleParameter("HTTPXS", httpxServer); HttpxProxy.IbbClient = ibbClient; httpxServer.IbbClient = ibbClient; HttpxProxy.Socks5Proxy = socksProxy; httpxServer.Socks5Proxy = socksProxy; if (xmppConfiguration.Sniffer) { ISniffer Sniffer; Sniffer = new XmlFileSniffer(appDataFolder + "HTTP" + Path.DirectorySeparatorChar + "HTTP Log %YEAR%-%MONTH%-%DAY%T%HOUR%.xml", appDataFolder + "Transforms" + Path.DirectorySeparatorChar + "SnifferXmlToHtml.xslt", 7, BinaryPresentationMethod.ByteCount); webServer.Add(Sniffer); } coapEndpoint = new CoapEndpoint(); Types.SetModuleParameter("CoAP", coapEndpoint); concentratorServer = new ConcentratorServer(xmppClient, provisioningClient, new MeteringTopology()); Types.SetModuleParameter("Concentrator", concentratorServer); Types.SetModuleParameter("Sensor", concentratorServer.SensorServer); Types.SetModuleParameter("Control", concentratorServer.ControlServer); } catch (Exception ex) { Log.Critical(ex); gatewayRunning.Release(); gatewayRunning.Dispose(); gatewayRunning = null; StartingServer.Release(); StartingServer.Dispose(); ExceptionDispatchInfo.Capture(ex).Throw(); } Task.Run(async() => { try { try { string BinaryFolder = AppDomain.CurrentDomain.BaseDirectory; string[] LanguageFiles = Directory.GetFiles(BinaryFolder, "*.lng", SearchOption.AllDirectories); string FileName; if (LanguageFiles.Length > 0) { XmlSchema Schema = XSL.LoadSchema(Translator.SchemaResource, typeof(Translator).Assembly); foreach (string LanguageFile in LanguageFiles) { try { FileName = LanguageFile; if (FileName.StartsWith(BinaryFolder)) { FileName = FileName.Substring(BinaryFolder.Length); } DateTime LastWriteTime = File.GetLastWriteTime(LanguageFile); DateTime LastImportedTime = await RuntimeSettings.GetAsync(FileName, DateTime.MinValue); if (LastWriteTime > LastImportedTime) { Log.Informational("Importing language file.", FileName); string Xml = File.ReadAllText(LanguageFile); XmlDocument Doc = new XmlDocument(); Doc.LoadXml(Xml); XSL.Validate(FileName, Doc, Translator.SchemaRoot, Translator.SchemaNamespace, Schema); using (XmlReader r = new XmlNodeReader(Doc)) { await Translator.ImportAsync(r); } RuntimeSettings.Set(FileName, LastWriteTime); } } catch (Exception ex) { Log.Critical(ex, LanguageFile); } } } Types.StartAllModules(int.MaxValue); } finally { StartingServer.Release(); StartingServer.Dispose(); } } catch (Exception ex) { Log.Critical(ex); } finally { xmppClient.Connect(); } }); return(true); }
/// <summary> /// Sets parameters from a data form in an object. /// </summary> /// <param name="e">IQ Event Arguments describing the request.</param> /// <param name="EditableObject">Object whose parameters will be set.</param> /// <param name="Form">Data Form.</param> /// <param name="OnlySetChanged">If only changed parameters are to be set.</param> /// <returns>Any errors encountered, or null if parameters was set properly.</returns> public static async Task <SetEditableFormResult> SetEditableForm(IqEventArgs e, object EditableObject, DataForm Form, bool OnlySetChanged) { Type T = EditableObject.GetType(); string DefaultLanguageCode = GetDefaultLanguageCode(T); List <KeyValuePair <string, string> > Errors = null; PropertyInfo PropertyInfo; FieldInfo FieldInfo; Language Language = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode); Namespace Namespace = null; Namespace ConcentratorNamespace = await Language.GetNamespaceAsync(typeof(ConcentratorServer).Namespace); LinkedList <Tuple <PropertyInfo, FieldInfo, object> > ToSet = null; ValidationMethod ValidationMethod; OptionAttribute OptionAttribute; RegularExpressionAttribute RegularExpressionAttribute; RangeAttribute RangeAttribute; DataType DataType; Type PropertyType; string NamespaceStr; string LastNamespaceStr = null; object ValueToSet; object ValueToSet2; object[] Parsed; bool ReadOnly; bool Alpha; bool DateOnly; bool HasHeader; bool HasOptions; bool ValidOption; bool Nullable; if (Namespace is null) { Namespace = await Language.CreateNamespaceAsync(T.Namespace); } if (ConcentratorNamespace is null) { ConcentratorNamespace = await Language.CreateNamespaceAsync(typeof(ConcentratorServer).Namespace); } foreach (Field Field in Form.Fields) { PropertyInfo = T.GetRuntimeProperty(Field.Var); FieldInfo = PropertyInfo is null?T.GetRuntimeField(Field.Var) : null; if (PropertyInfo is null && FieldInfo is null) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(1, "Property not found.")); continue; } if (PropertyInfo != null && (!PropertyInfo.CanRead || !PropertyInfo.CanWrite)) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(2, "Property not editable.")); continue; } NamespaceStr = (PropertyInfo?.DeclaringType ?? FieldInfo.DeclaringType).Namespace; if (Namespace is null || NamespaceStr != LastNamespaceStr) { Namespace = await Language.GetNamespaceAsync(NamespaceStr); LastNamespaceStr = NamespaceStr; } ValidationMethod = null; ReadOnly = Alpha = DateOnly = HasHeader = HasOptions = ValidOption = false; foreach (Attribute Attr in (PropertyInfo?.GetCustomAttributes() ?? FieldInfo.GetCustomAttributes())) { if (Attr is HeaderAttribute) { HasHeader = true; } else if ((OptionAttribute = Attr as OptionAttribute) != null) { HasOptions = true; if (Field.ValueString == OptionAttribute.Option.ToString()) { ValidOption = true; } } else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null) { ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern); } else if ((RangeAttribute = Attr as RangeAttribute) != null) { ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max); } else if (Attr is OpenAttribute) { ValidationMethod = new OpenValidation(); } else if (Attr is ReadOnlyAttribute) { ReadOnly = true; } else if (Attr is AlphaChannelAttribute) { Alpha = true; } else if (Attr is DateOnlyAttribute) { DateOnly = true; } } if (!HasHeader) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(2, "Property not editable.")); continue; } if (ReadOnly) { if (Field.ValueString != (PropertyInfo?.GetValue(EditableObject) ?? FieldInfo?.GetValue(EditableObject))?.ToString()) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(3, "Property is read-only.")); } continue; } if (HasOptions && !ValidOption) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(4, "Select a valid option.")); continue; } PropertyType = PropertyInfo?.PropertyType ?? FieldInfo.FieldType; ValueToSet = null; ValueToSet2 = null; Parsed = null; DataType = null; Nullable = false; if (PropertyType.GetTypeInfo().IsGenericType) { Type GT = PropertyType.GetGenericTypeDefinition(); if (GT == typeof(Nullable <>)) { Nullable = true; PropertyType = PropertyType.GenericTypeArguments[0]; } } if (Nullable && string.IsNullOrEmpty(Field.ValueString)) { ValueToSet2 = null; } else { if (PropertyType == typeof(string[])) { if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } ValueToSet = ValueToSet2 = Parsed = Field.ValueStrings; DataType = StringDataType.Instance; } else if (PropertyType.GetTypeInfo().IsEnum) { if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } try { ValueToSet = ValueToSet2 = Enum.Parse(PropertyType, Field.ValueString); } catch (Exception) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(4, "Select a valid option.")); continue; } } else if (PropertyType == typeof(bool)) { if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } if (!CommonTypes.TryParse(Field.ValueString, out bool b)) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(5, "Invalid boolean value.")); continue; } DataType = BooleanDataType.Instance; ValueToSet = ValueToSet2 = b; } else { if (PropertyType == typeof(string)) { DataType = StringDataType.Instance; } else if (PropertyType == typeof(sbyte)) { DataType = ByteDataType.Instance; } else if (PropertyType == typeof(short)) { DataType = ShortDataType.Instance; } else if (PropertyType == typeof(int)) { DataType = IntDataType.Instance; } else if (PropertyType == typeof(long)) { DataType = LongDataType.Instance; } else if (PropertyType == typeof(byte)) { DataType = ShortDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(byte.MinValue.ToString(), byte.MaxValue.ToString()); } } else if (PropertyType == typeof(ushort)) { DataType = IntDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString()); } } else if (PropertyType == typeof(uint)) { DataType = LongDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString()); } } else if (PropertyType == typeof(ulong)) { DataType = IntegerDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString()); } } else if (PropertyType == typeof(DateTime)) { if (DateOnly) { DataType = DateDataType.Instance; } else { DataType = DateTimeDataType.Instance; } } else if (PropertyType == typeof(decimal)) { DataType = DecimalDataType.Instance; } else if (PropertyType == typeof(double)) { DataType = DoubleDataType.Instance; } else if (PropertyType == typeof(float)) { DataType = DoubleDataType.Instance; // Use xs:double anyway } else if (PropertyType == typeof(TimeSpan)) { DataType = TimeDataType.Instance; } else if (PropertyType == typeof(Uri)) { DataType = AnyUriDataType.Instance; } else if (PropertyType == typeof(SKColor)) { if (Alpha) { DataType = ColorAlphaDataType.Instance; } else { DataType = ColorDataType.Instance; } } else { DataType = null; } if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } try { if (DataType is null) { ValueToSet = Field.ValueString; ValueToSet2 = Activator.CreateInstance(PropertyType, ValueToSet); } else { ValueToSet = DataType.Parse(Field.ValueString); if (ValueToSet.GetType() == PropertyType) { ValueToSet2 = ValueToSet; } else { ValueToSet2 = Convert.ChangeType(ValueToSet, PropertyType); } } } catch (Exception) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(6, "Invalid value.")); continue; } } if (Parsed is null) { Parsed = new object[] { ValueToSet } } ; ValidationMethod.Validate(Field, DataType, Parsed, Field.ValueStrings); if (Field.HasError) { AddError(ref Errors, Field.Var, Field.Error); continue; } } if (ToSet is null) { ToSet = new LinkedList <Tuple <PropertyInfo, FieldInfo, object> >(); } ToSet.AddLast(new Tuple <PropertyInfo, FieldInfo, object>(PropertyInfo, FieldInfo, ValueToSet2)); } if (Errors is null) { SetEditableFormResult Result = new SetEditableFormResult() { Errors = null, Tags = new List <KeyValuePair <string, object> >() }; foreach (Tuple <PropertyInfo, FieldInfo, object> P in ToSet) { try { if (OnlySetChanged) { object Current = P.Item1?.GetValue(EditableObject) ?? P.Item2?.GetValue(EditableObject); if (Current is null) { if (P.Item3 is null) { continue; } } else if (P.Item3 != null && Current.Equals(P.Item3)) { continue; } } if (P.Item1 != null) { P.Item1.SetValue(EditableObject, P.Item3); Result.Tags.Add(new KeyValuePair <string, object>(P.Item1.Name, P.Item3)); } else { P.Item2.SetValue(EditableObject, P.Item3); Result.Tags.Add(new KeyValuePair <string, object>(P.Item2.Name, P.Item3)); } } catch (Exception ex) { AddError(ref Errors, P.Item1?.Name ?? P.Item2.Name, ex.Message); } } return(Result); } else { return(new SetEditableFormResult() { Errors = Errors.ToArray(), Tags = null }); } }
/// <summary> /// Gets a data form containing editable parameters from an object. /// </summary> /// <param name="Client">Client</param> /// <param name="e">IQ Event Arguments describing the request.</param> /// <param name="EditableObject">Object whose parameters will be edited.</param> /// <param name="Title">Title of form.</param> /// <returns>Data form containing editable parameters.</returns> public static async Task <DataForm> GetEditableForm(XmppClient Client, IqEventArgs e, object EditableObject, string Title) { Type T = EditableObject.GetType(); string DefaultLanguageCode = GetDefaultLanguageCode(T); DataForm Parameters = new DataForm(Client, FormType.Form, e.To, e.From); Language Language = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode); Namespace Namespace = null; List <Field> Fields = new List <Field>(); List <Page> Pages = new List <Page>(); Dictionary <string, Page> PageByLabel = new Dictionary <string, Page>(); Dictionary <string, Section> SectionByPageAndSectionLabel = null; List <KeyValuePair <string, string> > Options = null; string NamespaceStr; string LastNamespaceStr = null; string Header; string ToolTip; string PageLabel; string SectionLabel; string s; int StringId; int PagePriority; int PageOrdinal = 0; int FieldPriority; int FieldOrdinal = 0; bool Required; bool ReadOnly; bool Masked; bool Alpha; bool DateOnly; bool Nullable; HeaderAttribute HeaderAttribute; ToolTipAttribute ToolTipAttribute; PageAttribute PageAttribute; SectionAttribute SectionAttribute; OptionAttribute OptionAttribute; TextAttribute TextAttribute; RegularExpressionAttribute RegularExpressionAttribute; LinkedList <TextAttribute> TextAttributes; RangeAttribute RangeAttribute; ValidationMethod ValidationMethod; Type PropertyType; Field Field; Page DefaultPage = null; Page Page; if (Namespace is null) { Namespace = await Language.CreateNamespaceAsync(T.Namespace); } LinkedList <KeyValuePair <PropertyInfo, FieldInfo> > Properties = new LinkedList <KeyValuePair <PropertyInfo, FieldInfo> >(); foreach (PropertyInfo PI in T.GetRuntimeProperties()) { if (PI.CanRead) { Properties.AddLast(new KeyValuePair <PropertyInfo, FieldInfo>(PI, null)); } } foreach (FieldInfo FI in T.GetRuntimeFields()) { Properties.AddLast(new KeyValuePair <PropertyInfo, FieldInfo>(null, FI)); } foreach (KeyValuePair <PropertyInfo, FieldInfo> Rec in Properties) { PropertyInfo PropertyInfo = Rec.Key; FieldInfo FieldInfo = Rec.Value; NamespaceStr = (PropertyInfo?.DeclaringType ?? FieldInfo.DeclaringType).Namespace; if (Namespace is null || NamespaceStr != LastNamespaceStr) { Namespace = await Language.GetNamespaceAsync(NamespaceStr); LastNamespaceStr = NamespaceStr; } Header = ToolTip = PageLabel = SectionLabel = null; TextAttributes = null; ValidationMethod = null; Options = null; Required = Masked = Alpha = DateOnly = false; ReadOnly = PropertyInfo != null && !PropertyInfo.CanWrite; PagePriority = PageAttribute.DefaultPriority; FieldPriority = HeaderAttribute.DefaultPriority; foreach (Attribute Attr in (PropertyInfo?.GetCustomAttributes() ?? FieldInfo.GetCustomAttributes())) { if ((HeaderAttribute = Attr as HeaderAttribute) != null) { Header = HeaderAttribute.Header; StringId = HeaderAttribute.StringId; FieldPriority = HeaderAttribute.Priority; if (StringId > 0) { Header = await Namespace.GetStringAsync(StringId, Header); } } else if ((ToolTipAttribute = Attr as ToolTipAttribute) != null) { ToolTip = ToolTipAttribute.ToolTip; StringId = ToolTipAttribute.StringId; if (StringId > 0) { ToolTip = await Namespace.GetStringAsync(StringId, ToolTip); } } else if ((PageAttribute = Attr as PageAttribute) != null) { PageLabel = PageAttribute.Label; StringId = PageAttribute.StringId; PagePriority = PageAttribute.Priority; if (StringId > 0) { PageLabel = await Namespace.GetStringAsync(StringId, PageLabel); } } else if ((SectionAttribute = Attr as SectionAttribute) != null) { SectionLabel = SectionAttribute.Label; StringId = SectionAttribute.StringId; if (StringId > 0) { SectionLabel = await Namespace.GetStringAsync(StringId, SectionLabel); } } else if ((TextAttribute = Attr as TextAttribute) != null) { if (TextAttributes is null) { TextAttributes = new LinkedList <TextAttribute>(); } TextAttributes.AddLast(TextAttribute); } else if ((OptionAttribute = Attr as OptionAttribute) != null) { if (Options is null) { Options = new List <KeyValuePair <string, string> >(); } StringId = OptionAttribute.StringId; if (StringId > 0) { Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(), await Namespace.GetStringAsync(StringId, OptionAttribute.Label))); } else { Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(), OptionAttribute.Label)); } } else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null) { ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern); } else if ((RangeAttribute = Attr as RangeAttribute) != null) { ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max); } else if (Attr is OpenAttribute) { ValidationMethod = new OpenValidation(); } else if (Attr is RequiredAttribute) { Required = true; } else if (Attr is ReadOnlyAttribute) { ReadOnly = true; } else if (Attr is MaskedAttribute) { Masked = true; } else if (Attr is AlphaChannelAttribute) { Alpha = true; } else if (Attr is DateOnlyAttribute) { DateOnly = true; } } if (Header is null) { continue; } PropertyType = PropertyInfo?.PropertyType ?? FieldInfo.FieldType; Field = null; Nullable = false; if (PropertyType.GetTypeInfo().IsGenericType) { Type GT = PropertyType.GetGenericTypeDefinition(); if (GT == typeof(Nullable <>)) { Nullable = true; PropertyType = PropertyType.GenericTypeArguments[0]; } } string PropertyName = PropertyInfo?.Name ?? FieldInfo.Name; object PropertyValue = (PropertyInfo?.GetValue(EditableObject) ?? FieldInfo.GetValue(EditableObject)); if (PropertyType == typeof(string[])) { if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } if (Options is null) { Field = new TextMultiField(Parameters, PropertyName, Header, Required, (string[])PropertyValue, null, ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } else { Field = new ListMultiField(Parameters, PropertyName, Header, Required, (string[])PropertyValue, Options.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } } else if (PropertyType.GetTypeInfo().IsEnum) { if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } if (Options is null) { Options = new List <KeyValuePair <string, string> >(); foreach (string Option in Enum.GetNames(PropertyType)) { Options.Add(new KeyValuePair <string, string>(Option, Option)); } } s = PropertyValue?.ToString(); if (Nullable && s is null) { s = string.Empty; } Field = new ListSingleField(Parameters, PropertyName, Header, Required, new string[] { s }, Options.ToArray(), ToolTip, null, ValidationMethod, string.Empty, false, ReadOnly, false); } else if (PropertyType == typeof(bool)) { if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } if (Nullable && PropertyValue is null) { s = string.Empty; } else { s = CommonTypes.Encode((bool)PropertyValue); } Field = new BooleanField(Parameters, PropertyName, Header, Required, new string[] { s }, Options?.ToArray(), ToolTip, BooleanDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } else { DataType DataType; if (PropertyType == typeof(string)) { DataType = StringDataType.Instance; } else if (PropertyType == typeof(sbyte)) { DataType = ByteDataType.Instance; } else if (PropertyType == typeof(short)) { DataType = ShortDataType.Instance; } else if (PropertyType == typeof(int)) { DataType = IntDataType.Instance; } else if (PropertyType == typeof(long)) { DataType = LongDataType.Instance; } else if (PropertyType == typeof(byte)) { DataType = ShortDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(byte.MinValue.ToString(), byte.MaxValue.ToString()); } } else if (PropertyType == typeof(ushort)) { DataType = IntDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString()); } } else if (PropertyType == typeof(uint)) { DataType = LongDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString()); } } else if (PropertyType == typeof(ulong)) { DataType = IntegerDataType.Instance; if (ValidationMethod is null) { ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString()); } } else if (PropertyType == typeof(DateTime)) { if (DateOnly) { DataType = DateDataType.Instance; } else { DataType = DateTimeDataType.Instance; } } else if (PropertyType == typeof(decimal)) { DataType = DecimalDataType.Instance; } else if (PropertyType == typeof(double)) { DataType = DoubleDataType.Instance; } else if (PropertyType == typeof(float)) { DataType = DoubleDataType.Instance; // Use xs:double anyway } else if (PropertyType == typeof(TimeSpan)) { DataType = TimeDataType.Instance; } else if (PropertyType == typeof(Uri)) { DataType = AnyUriDataType.Instance; } else if (PropertyType == typeof(SKColor)) { if (Alpha) { DataType = ColorAlphaDataType.Instance; } else { DataType = ColorDataType.Instance; } } else { DataType = StringDataType.Instance; } if (ValidationMethod is null) { ValidationMethod = new BasicValidation(); } s = PropertyValue?.ToString(); if (Nullable && s is null) { s = string.Empty; } if (Masked) { Field = new TextPrivateField(Parameters, PropertyName, Header, Required, new string[] { s }, Options?.ToArray(), ToolTip, DataType, ValidationMethod, string.Empty, false, ReadOnly, false); } else if (Options is null) { Field = new TextSingleField(Parameters, PropertyName, Header, Required, new string[] { s }, null, ToolTip, DataType, ValidationMethod, string.Empty, false, ReadOnly, false); } else { Field = new ListSingleField(Parameters, PropertyName, Header, Required, new string[] { s }, Options.ToArray(), ToolTip, DataType, ValidationMethod, string.Empty, false, ReadOnly, false); } } if (Field is null) { continue; } if (string.IsNullOrEmpty(PageLabel)) { if (DefaultPage is null) { DefaultPage = new Page(Parameters, string.Empty) { Priority = PageAttribute.DefaultPriority, Ordinal = PageOrdinal++ }; Pages.Add(DefaultPage); PageByLabel[string.Empty] = DefaultPage; } Page = DefaultPage; PageLabel = string.Empty; } else { if (!PageByLabel.TryGetValue(PageLabel, out Page)) { Page = new Page(Parameters, PageLabel) { Priority = PagePriority, Ordinal = PageOrdinal++ }; Pages.Add(Page); PageByLabel[PageLabel] = Page; } } Field.Priority = FieldPriority; Field.Ordinal = FieldOrdinal++; Fields.Add(Field); if (string.IsNullOrEmpty(SectionLabel)) { if (TextAttributes != null) { foreach (TextAttribute TextAttr in TextAttributes) { if (TextAttr.Position == TextPosition.BeforeField) { StringId = TextAttr.StringId; if (StringId > 0) { Page.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label))); } else { Page.Add(new TextElement(Parameters, TextAttr.Label)); } } } } Page.Add(new FieldReference(Parameters, Field.Var)); if (TextAttributes != null) { foreach (TextAttribute TextAttr in TextAttributes) { if (TextAttr.Position == TextPosition.AfterField) { StringId = TextAttr.StringId; if (StringId > 0) { Page.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label))); } else { Page.Add(new TextElement(Parameters, TextAttr.Label)); } } } } } else { if (SectionByPageAndSectionLabel is null) { SectionByPageAndSectionLabel = new Dictionary <string, Section>(); } s = PageLabel + " \xa0 " + SectionLabel; if (!SectionByPageAndSectionLabel.TryGetValue(s, out Section Section)) { Section = new Section(Parameters, SectionLabel); SectionByPageAndSectionLabel[s] = Section; Page.Add(Section); } if (TextAttributes != null) { foreach (TextAttribute TextAttr in TextAttributes) { if (TextAttr.Position == TextPosition.BeforeField) { StringId = TextAttr.StringId; if (StringId > 0) { Section.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label))); } else { Section.Add(new TextElement(Parameters, TextAttr.Label)); } } } } Section.Add(new FieldReference(Parameters, Field.Var)); if (TextAttributes != null) { foreach (TextAttribute TextAttr in TextAttributes) { if (TextAttr.Position == TextPosition.AfterField) { StringId = TextAttr.StringId; if (StringId > 0) { Section.Add(new TextElement(Parameters, await Namespace.GetStringAsync(StringId, TextAttr.Label))); } else { Section.Add(new TextElement(Parameters, TextAttr.Label)); } } } } } } if (EditableObject is IPropertyFormAnnotation PropertyFormAnnotation) { FormState State = new FormState() { Form = Parameters, PageByLabel = PageByLabel, SectionByPageAndSectionLabel = SectionByPageAndSectionLabel, DefaultPage = DefaultPage, LanguageCode = Language.Code, Fields = Fields, Pages = Pages, FieldOrdinal = FieldOrdinal, PageOrdinal = PageOrdinal }; await PropertyFormAnnotation.AnnotatePropertyForm(State); } Fields.Sort(OrderFields); Parameters.Title = Title; Parameters.Fields = Fields.ToArray(); if (Pages != null) { Pages.Sort(OrderPages); foreach (Page Page2 in Pages) { Page2.Sort(); } } Parameters.Pages = Pages.ToArray(); return(Parameters); }
/// <summary> /// Sets parameters from a data form in an object. /// </summary> /// <param name="e">IQ Event Arguments describing the request.</param> /// <param name="EditableObject">Object whose parameters will be set.</param> /// <param name="Form">Data Form.</param> /// <returns>Any errors encountered, or null if parameters was set properly.</returns> public static async Task <KeyValuePair <string, string>[]> SetEditableForm(IqEventArgs e, object EditableObject, DataForm Form) { Type T = EditableObject.GetType(); string DefaultLanguageCode = GetDefaultLanguageCode(T); List <KeyValuePair <string, string> > Errors = null; PropertyInfo PI; Language Language = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode); Namespace Namespace = await Language.GetNamespaceAsync(T.Namespace); Namespace ConcentratorNamespace = await Language.GetNamespaceAsync(typeof(ConcentratorServer).Namespace); LinkedList <KeyValuePair <PropertyInfo, object> > ToSet = null; ValidationMethod ValidationMethod; OptionAttribute OptionAttribute; RegularExpressionAttribute RegularExpressionAttribute; RangeAttribute RangeAttribute; DataType DataType; Type PropertyType; string Header; object ValueToSet; object[] Parsed; bool ReadOnly; bool Alpha; bool DateOnly; bool HasHeader; bool HasOptions; bool ValidOption; if (Namespace == null) { Namespace = await Language.CreateNamespaceAsync(T.Namespace); } if (ConcentratorNamespace == null) { ConcentratorNamespace = await Language.CreateNamespaceAsync(typeof(ConcentratorServer).Namespace); } foreach (Field Field in Form.Fields) { PI = T.GetRuntimeProperty(Field.Var); if (PI == null) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(1, "Property not found.")); continue; } if (!PI.CanRead || !PI.CanWrite) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(2, "Property not editable.")); continue; } Header = null; ValidationMethod = null; ReadOnly = Alpha = DateOnly = HasHeader = HasOptions = ValidOption = false; foreach (Attribute Attr in PI.GetCustomAttributes()) { if (Attr is HeaderAttribute) { HasHeader = true; } else if ((OptionAttribute = Attr as OptionAttribute) != null) { HasOptions = true; if (Field.ValueString == OptionAttribute.Option.ToString()) { ValidOption = true; } } else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null) { ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern); } else if ((RangeAttribute = Attr as RangeAttribute) != null) { ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max); } else if (Attr is OpenAttribute) { ValidationMethod = new OpenValidation(); } else if (Attr is ReadOnlyAttribute) { ReadOnly = true; } else if (Attr is AlphaChannelAttribute) { Alpha = true; } else if (Attr is DateOnlyAttribute) { DateOnly = true; } } if (Header == null) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(2, "Property not editable.")); continue; } if (ReadOnly) { if (Field.ValueString != PI.GetValue(EditableObject).ToString()) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(3, "Property is read-only.")); } continue; } if (HasOptions && !ValidOption) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(4, "Select a valid option.")); continue; } PropertyType = PI.PropertyType; ValueToSet = null; Parsed = null; DataType = null; if (PropertyType == typeof(string[])) { if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } ValueToSet = Parsed = Field.ValueStrings; DataType = StringDataType.Instance; } else if (PropertyType == typeof(Enum)) { if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } try { ValueToSet = Enum.Parse(PropertyType, Field.ValueString); } catch (Exception) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(4, "Select a valid option.")); continue; } } else if (PropertyType == typeof(bool)) { if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } if (!CommonTypes.TryParse(Field.ValueString, out bool b)) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(5, "Invalid boolean value.")); continue; } DataType = BooleanDataType.Instance; } else { if (PropertyType == typeof(string)) { DataType = StringDataType.Instance; } else if (PropertyType == typeof(byte)) { DataType = ByteDataType.Instance; } else if (PropertyType == typeof(short)) { DataType = ShortDataType.Instance; } else if (PropertyType == typeof(int)) { DataType = IntDataType.Instance; } else if (PropertyType == typeof(long)) { DataType = LongDataType.Instance; } else if (PropertyType == typeof(sbyte)) { DataType = ShortDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(sbyte.MinValue.ToString(), sbyte.MaxValue.ToString()); } } else if (PropertyType == typeof(ushort)) { DataType = IntDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString()); } } else if (PropertyType == typeof(uint)) { DataType = LongDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString()); } } else if (PropertyType == typeof(ulong)) { DataType = IntegerDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString()); } } else if (PropertyType == typeof(DateTime)) { if (DateOnly) { DataType = DateDataType.Instance; } else { DataType = DateTimeDataType.Instance; } } else if (PropertyType == typeof(decimal)) { DataType = DecimalDataType.Instance; } else if (PropertyType == typeof(double)) { DataType = DoubleDataType.Instance; } else if (PropertyType == typeof(float)) { DataType = DoubleDataType.Instance; // Use xs:double anyway } else if (PropertyType == typeof(TimeSpan)) { DataType = TimeDataType.Instance; } else if (PropertyType == typeof(Uri)) { DataType = AnyUriDataType.Instance; } else if (PropertyType == typeof(SKColor)) { if (Alpha) { DataType = ColorAlphaDataType.Instance; } else { DataType = ColorDataType.Instance; } } else { DataType = null; } if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } try { ValueToSet = DataType.Parse(Field.ValueString); } catch (Exception) { AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(6, "Invalid value.")); continue; } } if (Parsed == null) { Parsed = new object[] { ValueToSet } } ; ValidationMethod.Validate(Field, DataType, Parsed, Field.ValueStrings); if (Field.HasError) { AddError(ref Errors, Field.Var, Field.Error); continue; } if (ToSet == null) { ToSet = new LinkedList <KeyValuePair <PropertyInfo, object> >(); } ToSet.AddLast(new KeyValuePair <PropertyInfo, object>(PI, ValueToSet)); } if (Errors == null) { foreach (KeyValuePair <PropertyInfo, object> P in ToSet) { try { P.Key.SetValue(EditableObject, P.Value); } catch (Exception ex) { AddError(ref Errors, P.Key.Name, ex.Message); } } } if (Errors == null) { return(null); } else { return(Errors.ToArray()); } }
/// <summary> /// Gets a data form containing editable parameters from an object. /// </summary> /// <param name="Client">Client</param> /// <param name="e">IQ Event Arguments describing the request.</param> /// <param name="EditableObject">Object whose parameters will be edited.</param> /// <param name="Title">Title of form.</param> /// <returns>Data form containing editable parameters.</returns> public static async Task <DataForm> GetEditableForm(XmppClient Client, IqEventArgs e, object EditableObject, string Title) { Type T = EditableObject.GetType(); string DefaultLanguageCode = GetDefaultLanguageCode(T); DataForm Parameters = new DataForm(Client, FormType.Form, e.To, e.From); Language Language = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode); Namespace Namespace = await Language.GetNamespaceAsync(T.Namespace); List <Field> Fields = new List <Field>(); List <Page> Pages = new List <Page>(); Dictionary <string, Page> PageByLabel = new Dictionary <string, Page>(); Dictionary <string, Section> SectionByPageAndSectionLabel = null; List <KeyValuePair <string, string> > Options = null; string Header; string ToolTip; string PageLabel; string SectionLabel; string s; int StringId; bool Required; bool ReadOnly; bool Masked; bool Alpha; bool DateOnly; HeaderAttribute HeaderAttribute; ToolTipAttribute ToolTipAttribute; PageAttribute PageAttribute; SectionAttribute SectionAttribute; OptionAttribute OptionAttribute; TextAttribute TextAttribute; RegularExpressionAttribute RegularExpressionAttribute; LinkedList <string> TextAttributes; RangeAttribute RangeAttribute; ValidationMethod ValidationMethod; Type PropertyType; Field Field; Page DefaultPage = null; Page Page; if (Namespace == null) { Namespace = await Language.CreateNamespaceAsync(T.Namespace); } foreach (PropertyInfo PI in T.GetRuntimeProperties()) { if (!PI.CanRead || !PI.CanWrite) { continue; } Header = ToolTip = PageLabel = SectionLabel = null; TextAttributes = null; ValidationMethod = null; Required = ReadOnly = Masked = Alpha = DateOnly = false; foreach (Attribute Attr in PI.GetCustomAttributes()) { if ((HeaderAttribute = Attr as HeaderAttribute) != null) { Header = HeaderAttribute.Header; StringId = HeaderAttribute.StringId; if (StringId > 0) { Header = await Namespace.GetStringAsync(StringId, Header); } } else if ((ToolTipAttribute = Attr as ToolTipAttribute) != null) { ToolTip = ToolTipAttribute.ToolTip; StringId = ToolTipAttribute.StringId; if (StringId > 0) { ToolTip = await Namespace.GetStringAsync(StringId, ToolTip); } } else if ((PageAttribute = Attr as PageAttribute) != null) { PageLabel = PageAttribute.Label; StringId = PageAttribute.StringId; if (StringId > 0) { PageLabel = await Namespace.GetStringAsync(StringId, PageLabel); } } else if ((SectionAttribute = Attr as SectionAttribute) != null) { SectionLabel = SectionAttribute.Label; StringId = SectionAttribute.StringId; if (StringId > 0) { SectionLabel = await Namespace.GetStringAsync(StringId, SectionLabel); } } else if ((TextAttribute = Attr as TextAttribute) != null) { if (TextAttributes == null) { TextAttributes = new LinkedList <string>(); } StringId = TextAttribute.StringId; if (StringId > 0) { TextAttributes.AddLast(await Namespace.GetStringAsync(StringId, TextAttribute.Label)); } else { TextAttributes.AddLast(TextAttribute.Label); } } else if ((OptionAttribute = Attr as OptionAttribute) != null) { if (Options == null) { Options = new List <KeyValuePair <string, string> >(); } StringId = OptionAttribute.StringId; if (StringId > 0) { Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(), await Namespace.GetStringAsync(StringId, TextAttribute.Label))); } else { Options.Add(new KeyValuePair <string, string>(OptionAttribute.Option.ToString(), OptionAttribute.Label)); } } else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null) { ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern); } else if ((RangeAttribute = Attr as RangeAttribute) != null) { ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max); } else if (Attr is OpenAttribute) { ValidationMethod = new OpenValidation(); } else if (Attr is RequiredAttribute) { Required = true; } else if (Attr is ReadOnlyAttribute) { ReadOnly = true; } else if (Attr is MaskedAttribute) { Masked = true; } else if (Attr is AlphaChannelAttribute) { Alpha = true; } else if (Attr is DateOnlyAttribute) { DateOnly = true; } } if (Header == null) { continue; } PropertyType = PI.PropertyType; Field = null; if (PropertyType == typeof(string[])) { if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } if (Options == null) { Field = new TextMultiField(Parameters, PI.Name, Header, Required, (string[])PI.GetValue(EditableObject), null, ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } else { Field = new ListMultiField(Parameters, PI.Name, Header, Required, (string[])PI.GetValue(EditableObject), Options.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } } else if (PropertyType == typeof(Enum)) { if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } if (Options == null) { Options = new List <KeyValuePair <string, string> >(); foreach (string Option in Enum.GetNames(PropertyType)) { Options.Add(new KeyValuePair <string, string>(Option, Option)); } } Field = new ListSingleField(Parameters, PI.Name, Header, Required, new string[] { PI.GetValue(EditableObject).ToString() }, Options.ToArray(), ToolTip, null, ValidationMethod, string.Empty, false, ReadOnly, false); } else if (PropertyType == typeof(bool)) { if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } Field = new BooleanField(Parameters, PI.Name, Header, Required, new string[] { CommonTypes.Encode((bool)PI.GetValue(EditableObject)) }, Options?.ToArray(), ToolTip, BooleanDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } else { DataType DataType; if (PropertyType == typeof(string)) { DataType = StringDataType.Instance; } else if (PropertyType == typeof(byte)) { DataType = ByteDataType.Instance; } else if (PropertyType == typeof(short)) { DataType = ShortDataType.Instance; } else if (PropertyType == typeof(int)) { DataType = IntDataType.Instance; } else if (PropertyType == typeof(long)) { DataType = LongDataType.Instance; } else if (PropertyType == typeof(sbyte)) { DataType = ShortDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(sbyte.MinValue.ToString(), sbyte.MaxValue.ToString()); } } else if (PropertyType == typeof(ushort)) { DataType = IntDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString()); } } else if (PropertyType == typeof(uint)) { DataType = LongDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString()); } } else if (PropertyType == typeof(ulong)) { DataType = IntegerDataType.Instance; if (ValidationMethod == null) { ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString()); } } else if (PropertyType == typeof(DateTime)) { if (DateOnly) { DataType = DateDataType.Instance; } else { DataType = DateTimeDataType.Instance; } } else if (PropertyType == typeof(decimal)) { DataType = DecimalDataType.Instance; } else if (PropertyType == typeof(double)) { DataType = DoubleDataType.Instance; } else if (PropertyType == typeof(float)) { DataType = DoubleDataType.Instance; // Use xs:double anyway } else if (PropertyType == typeof(TimeSpan)) { DataType = TimeDataType.Instance; } else if (PropertyType == typeof(Uri)) { DataType = AnyUriDataType.Instance; } else if (PropertyType == typeof(SKColor)) { if (Alpha) { DataType = ColorAlphaDataType.Instance; } else { DataType = ColorDataType.Instance; } } else { DataType = null; } if (ValidationMethod == null) { ValidationMethod = new BasicValidation(); } if (Masked) { Field = new TextPrivateField(Parameters, PI.Name, Header, Required, new string[] { (string)PI.GetValue(EditableObject) }, Options?.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } else if (Options == null) { Field = new TextSingleField(Parameters, PI.Name, Header, Required, new string[] { (string)PI.GetValue(EditableObject) }, null, ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } else { Field = new ListSingleField(Parameters, PI.Name, Header, Required, new string[] { (string)PI.GetValue(EditableObject) }, Options.ToArray(), ToolTip, StringDataType.Instance, ValidationMethod, string.Empty, false, ReadOnly, false); } } if (Field == null) { continue; } Fields.Add(Field); if (string.IsNullOrEmpty(PageLabel)) { if (DefaultPage == null) { DefaultPage = new Page(Parameters, string.Empty); Pages.Add(DefaultPage); PageByLabel[string.Empty] = DefaultPage; } Page = DefaultPage; PageLabel = string.Empty; } else { if (!PageByLabel.TryGetValue(PageLabel, out Page)) { Page = new Page(Parameters, PageLabel); Pages.Add(Page); PageByLabel[PageLabel] = Page; } } if (string.IsNullOrEmpty(SectionLabel)) { if (TextAttributes != null) { foreach (string Text in TextAttributes) { Page.Add(new TextElement(Parameters, Text)); } } Page.Add(new FieldReference(Parameters, Field.Var)); } else { if (SectionByPageAndSectionLabel == null) { SectionByPageAndSectionLabel = new Dictionary <string, Section>(); } s = PageLabel + " \xa0 " + SectionLabel; if (!SectionByPageAndSectionLabel.TryGetValue(s, out Section Section)) { Section = new Section(Parameters, SectionLabel); SectionByPageAndSectionLabel[s] = Section; Page.Add(Section); } if (TextAttributes != null) { foreach (string Text in TextAttributes) { Section.Add(new TextElement(Parameters, Text)); } } Section.Add(new FieldReference(Parameters, Field.Var)); } } Parameters.Title = Title; Parameters.Fields = Fields.ToArray(); Parameters.Pages = Pages.ToArray(); return(Parameters); }