Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MongoDBDatabase.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
10{
15 {
20 {
21 }
22
28 public string Name(Language Language)
29 {
30 return "MongoDB Database";
31 }
32
38 {
39 return new MongoDBSettings();
40 }
41
46 public Task ConfigureSettings(object Settings)
47 {
49 {
50 object MongoClientSettings = Types.CreateObject("MongoDB.Driver.MongoClientSettings");
51 Types.SetProperty(MongoClientSettings, "ApplicationName", Gateway.ApplicationName);
52
53 if (!string.IsNullOrEmpty(MongoDBSettings.Host))
54 {
55 object MongoServerAddress;
56
57 if (MongoDBSettings.Port.HasValue)
58 MongoServerAddress = Types.CreateObject("MongoDB.Driver.MongoServerAddress", MongoDBSettings.Host, MongoDBSettings.Port.Value);
59 else
60 MongoServerAddress = Types.CreateObject("MongoDB.Driver.MongoServerAddress", MongoDBSettings.Host);
61
62 Types.SetProperty(MongoClientSettings, "Server", MongoServerAddress);
63 }
64
65 if (!string.IsNullOrEmpty(MongoDBSettings.UserName))
66 {
67 object MongoCredential = Types.CallStatic("MongoDB.Driver.MongoCredential", "CreateCredential", MongoDBSettings.Database,
69
70 Types.SetProperty(MongoClientSettings, "Credential", MongoCredential);
71 }
72
73 IDatabaseProvider MongoDBProvider = Types.CreateObject("Waher.Persistence.MongoDB.MongoDBProvider", MongoClientSettings,
75
76 Database.Register(MongoDBProvider, true);
77 }
78
79 return Task.CompletedTask;
80 }
81
85 public string SettingsResource => "/Settings/Database/MongoDB.md";
86
94 public async Task Test(Dictionary<string, object> Form, bool Save, object Settings)
95 {
96 if (!Form.TryGetValue("HostName", out object Obj) ||
97 !(Obj is string HostName) ||
98 !Form.TryGetValue("DatabaseName", out Obj) ||
99 !(Obj is string DatabaseName) ||
100 !Form.TryGetValue("DefaultCollection", out Obj) ||
101 !(Obj is string DefaultCollection) ||
102 !Form.TryGetValue("UserName", out Obj) ||
103 !(Obj is string UserName) ||
104 !Form.TryGetValue("Password", out Obj) ||
105 !(Obj is string Password) ||
106 !Form.TryGetValue("PortNumber", out Obj) ||
107 !(Obj is string PortNumberString) ||
108 !(Settings is MongoDBSettings MongoDBSettings))
109 {
110 throw new BadRequestException();
111 }
112
113 (string Message, string _, string _) = await this.Test(HostName, DatabaseName, DefaultCollection, UserName, Password,
114 PortNumberString, Save, MongoDBSettings);
115
116 if (!string.IsNullOrEmpty(Message))
117 throw new BadRequestException(Message);
118 }
119
123 public const string MONGO_DB_HOST = nameof(MONGO_DB_HOST);
124
128 public const string MONGO_DB_NAME = nameof(MONGO_DB_NAME);
129
133 public const string MONGO_DB_DEFAULT = nameof(MONGO_DB_DEFAULT);
134
138 public const string MONGO_DB_USER = nameof(MONGO_DB_USER);
139
143 public const string MONGO_DB_PASSWORD = nameof(MONGO_DB_PASSWORD);
144
148 public const string MONGO_DB_PORT = nameof(MONGO_DB_PORT);
149
150 private async Task<(string, string, string)> Test(string HostName, string DatabaseName, string DefaultCollection, string UserName, string Password,
151 string PortNumberString, bool Save, MongoDBSettings Settings)
152 {
153 try
154 {
155 int? PortNumber;
156
157 if (string.IsNullOrEmpty(PortNumberString))
158 PortNumber = null;
159 else if (!int.TryParse(PortNumberString, out int i) || i <= 0 || i > 65535)
160 return ("Invalid Port Number.", MONGO_DB_PORT, PortNumberString);
161 else
162 PortNumber = i;
163
164 object MongoClientSettings = Types.CreateObject("MongoDB.Driver.MongoClientSettings");
165 Types.SetProperty(MongoClientSettings, "ApplicationName", Gateway.ApplicationName);
166 Types.SetProperty(MongoClientSettings, "ConnectTimeout", TimeSpan.FromSeconds(5));
167
168 if (!string.IsNullOrEmpty(HostName))
169 {
170 try
171 {
172 object MongoServerAddress;
173
174 if (PortNumber.HasValue)
175 MongoServerAddress = Types.CreateObject("MongoDB.Driver.MongoServerAddress", HostName, PortNumber.Value);
176 else
177 MongoServerAddress = Types.CreateObject("MongoDB.Driver.MongoServerAddress", HostName);
178
179 Types.SetProperty(MongoClientSettings, "Server", MongoServerAddress);
180 }
181 catch (Exception ex)
182 {
183 return (ex.Message, MONGO_DB_HOST, HostName);
184 }
185 }
186
187 if (!string.IsNullOrEmpty(Settings.UserName))
188 {
189 try
190 {
191 object MongoCredential = Types.CallStatic("MongoDB.Driver.MongoCredential", "CreateCredential",
192 DatabaseName, UserName, Password);
193
194 Types.SetProperty(MongoClientSettings, "Credential", MongoCredential);
195 }
196 catch (Exception ex)
197 {
198 return (ex.Message, MONGO_DB_USER, UserName);
199 }
200 }
201
202 object Client = Types.CreateObject("MongoDB.Driver.MongoClient", MongoClientSettings);
203
204 Task Task = Types.Call(Client, "StartSessionAsync") as Task;
205 await Task;
206
207 Types.Call(Client, "GetDatabase", DatabaseName);
208
209 if (Save)
210 {
211 Settings.Host = HostName;
212 Settings.Port = PortNumber;
213 Settings.UserName = UserName;
214 Settings.Password = Password;
215 Settings.Database = DatabaseName;
216 Settings.DefaultCollection = DefaultCollection;
217 }
218 }
219 catch (Exception ex)
220 {
221 return (ex.Message, MONGO_DB_HOST, HostName);
222 }
223
224 return (null, null, null);
225 }
226
233 public async Task<bool> TestEnvironmentVariables(DatabaseConfiguration Configuration, object Settings)
234 {
235 if (!(Settings is MongoDBSettings MongoDBSettings))
236 return false;
237
238 if (!Configuration.TryGetEnvironmentVariable(MONGO_DB_HOST, false, out string Host))
239 return false;
240
241 if (!Configuration.TryGetEnvironmentVariable(MONGO_DB_NAME, true, out string Name) ||
242 !Configuration.TryGetEnvironmentVariable(MONGO_DB_USER, true, out string User) ||
243 !Configuration.TryGetEnvironmentVariable(MONGO_DB_PASSWORD, true, out string Password) ||
244 !Configuration.TryGetEnvironmentVariable(MONGO_DB_PORT, true, out string Port))
245 {
246 return false;
247 }
248
249 if (!Configuration.TryGetEnvironmentVariable(MONGO_DB_DEFAULT, false, out string Default))
250 Default = "Default";
251
252 (string Message, string Parameter, string Value) = await this.Test(Host, Name, Default, User, Password, Port, true, MongoDBSettings);
253
254 if (!string.IsNullOrEmpty(Message))
255 {
256 Configuration.LogEnvironmentError(Message, Parameter, Value);
257 return false;
258 }
259
260 return true;
261 }
262
263 }
264}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static string ApplicationName
Application Name.
Definition: Gateway.cs:2390
DatabaseSettings CreateNewSettings()
Creates a new settings object.
const string MONGO_DB_NAME
Environment variable name for MongoDB Database Name.
const string MONGO_DB_PASSWORD
Environment variable name for MongoDB Password.
const string MONGO_DB_PORT
Environment variable name for MongoDB Port number.
const string MONGO_DB_HOST
Environment variable name for MongoDB Host Name.
string Name(Language Language)
Displayable name of database plug-in
async Task Test(Dictionary< string, object > Form, bool Save, object Settings)
Tests database connection parameters.
const string MONGO_DB_USER
Environment variable name for MongoDB User Name.
const string MONGO_DB_DEFAULT
Environment variable name for MongoDB default collection.
Task ConfigureSettings(object Settings)
Configures a database connection using the provided settings object.
async Task< bool > TestEnvironmentVariables(DatabaseConfiguration Configuration, object Settings)
Tests database connection parameters available via environment variables.
MongoDBDatabase()
Plugin for MongoDB databases.
string SettingsResource
Resource name pointing to settings form.
string Host
Name of database host, or empty if running on local machine.
int? Port
Port number, or null if standard MongoDB service port.
void LogEnvironmentError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value contains an error.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static void Register(IDatabaseProvider DatabaseProvider)
Registers a database provider for use from the static Database class, throughout the lifetime of the ...
Definition: Database.cs:31
static bool Locked
If the datbase provider has been locked for the rest of the run-time of the application.
Definition: Database.cs:87
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static void SetProperty(object Object, string PropertyName, object Value)
Sets a property value (or field value) in an object.
Definition: Types.cs:944
static object CreateObject(string TypeName, params object[] Parameters)
Creates an object of a given type, given its full name.
Definition: Types.cs:893
static object Call(object Object, string MethodName, params object[] Arguments)
Calls a method on an object.
Definition: Types.cs:982
static object CallStatic(string TypeName, string MethodName, params object[] Arguments)
Calls a static method on a class.
Definition: Types.cs:1062
Contains information about a language.
Definition: Language.cs:17
Interface for database providers that can be plugged into the static Database class.