4using System.Threading.Tasks;
23 [CollectionName(
"Groups")]
27 [Index(
"ParentId",
"NodeId")]
30 private Guid objectId = Guid.Empty;
31 private Guid parentId = Guid.Empty;
33 private string nodeId =
string.Empty;
34 private string oldId =
null;
36 private List<GroupNode> children =
null;
37 private int siblingOrdinal = 0;
38 private bool childrenLoaded =
false;
39 private readonly
object synchObject =
new object();
40 private DateTime created = DateTime.Now;
41 private DateTime updated = DateTime.MinValue;
59 return Node.thingReference;
67 public override bool Equals(
object obj)
72 return this.nodeId == Ref.NodeId && this.SourceId == Ref.SourceId && this.Partition == Ref.Partition;
81 return this.nodeId.GetHashCode() ^
93 set => this.objectId = value;
101 get => this.parentId;
102 set => this.parentId = value;
111 set => this.created = value;
117 [DefaultValueDateTimeMinValue]
121 set => this.updated = value;
130 get => this.siblingOrdinal;
131 set => this.siblingOrdinal = value;
137 [Header(2,
"Group ID:", 0)]
138 [Page(3,
"Group", 0)]
139 [ToolTip(4,
"Group identity in the collection of groups.")]
147 this.thingReference =
null;
149 if (this.oldId is
null && !
string.IsNullOrEmpty(value))
169 StringBuilder sb =
new StringBuilder();
172 sb.Append(this.nodeId);
185 return sb.ToString();
263 if (this.objectId == Guid.Empty)
274 Message.Updated = DateTime.Now;
296 if (this.state <
NodeState.ErrorUnsigned)
300 await this.RaiseUpdate();
305 if (this.state <
NodeState.WarningUnsigned)
309 await this.RaiseUpdate();
318 await this.RaiseUpdate();
338 await this.NodeStateChanged();
341 internal async Task NodeStateChanged()
350 Timestamp = DateTime.UtcNow
421 if (this.objectId == Guid.Empty)
424 bool Removed =
false;
447 bool ErrorsFound =
false;
448 bool WarningsFound =
false;
449 bool InformationFound =
false;
460 WarningsFound =
true;
464 InformationFound =
true;
475 NewStateUnsigned =
NodeState.ErrorUnsigned;
477 else if (WarningsFound)
479 NewStateSigned =
NodeState.WarningSigned;
480 NewStateUnsigned =
NodeState.WarningUnsigned;
482 else if (InformationFound)
485 NewStateUnsigned =
NodeState.Information;
497 if (this.state != NewStateSigned)
499 this.state = NewStateSigned;
501 await this.RaiseUpdate();
506 if (this.state != NewStateUnsigned)
508 this.state = NewStateUnsigned;
510 await this.RaiseUpdate();
515 await this.NodeStateChanged();
526 internal Task RaiseUpdate()
528 return this.OnUpdate.Raise(
this, EventArgs.Empty);
539 string Suffix =
string.Empty;
553 Suffix =
" (" + i.ToString() +
")";
586 if (!this.childrenLoaded)
587 this.LoadChildren().Wait();
589 return !(this.children is
null) && this.children.Count > 0;
620 [Obsolete(
"Use the asynchronous GetParent() method instead.")]
630 if (!(this.parent is
null))
633 if (this.parentId == Guid.Empty)
636 this.parent = await this.LoadParent();
637 if (this.parent is
null)
638 throw new Exception(
"Parent not found.");
652 while (!(Loop is
null))
654 if (Loop is T Ancestor)
673 if (this.updated == DateTime.MinValue)
687 set => this.state = value;
698 return this.GetChildNodes();
702 private async Task<IEnumerable<INode>> GetChildNodes()
704 if (!this.childrenLoaded)
705 await this.LoadChildren();
707 lock (this.synchObject)
709 if (this.children is
null)
710 return Array.Empty<
INode>();
712 return this.children.ToArray();
716 private async Task LoadChildren()
718 List<GroupNode> Children =
new List<GroupNode>();
727 lock (this.synchObject)
729 this.children =
null;
731 if (Children.Count > 0)
737 this.children = Children;
740 this.childrenLoaded =
true;
743 if (!(ToUpdate is
null))
754 if (this.ChildrenOrdered)
756 Children.Sort((n1, n2) => n1.siblingOrdinal.CompareTo(n2.siblingOrdinal));
761 Children.Sort((n1, n2) => n1.nodeId.CompareTo(n2.nodeId));
773 if (this.ChildrenOrdered)
784 Child.siblingOrdinal = Expected;
789 return ToUpdate?.ToArray();
795 internal async Task<GroupNode> LoadParent()
797 if (!(this.parent is
null))
800 if (this.parentId == Guid.Empty)
860 LinkedList<Parameter> Result =
new LinkedList<Parameter>();
864 if (!(this.parent is
null))
867 if (!this.childrenLoaded)
868 await this.LoadChildren();
870 if (!(this.children is
null))
874 lock (this.synchObject)
876 i = this.children.Count;
882 string s = this.state
switch
892 if (!
string.IsNullOrEmpty(s))
897 if (this.updated != DateTime.MinValue)
924 LinkedList<Message> Result =
new LinkedList<Message>();
938 List<Message> Result =
new List<Message>();
943 return Result.ToArray();
980 if (!this.ChildrenOrdered)
983 if (!this.childrenLoaded)
984 await this.LoadChildren();
986 if (this.children is
null)
996 int i = this.children.IndexOf(Child);
1000 Child2 = this.children[i - 1];
1002 this.children.RemoveAt(i);
1003 this.children.Insert(i - 1, Child);
1005 i = Child.siblingOrdinal;
1006 Child.siblingOrdinal = Child2.siblingOrdinal;
1007 Child2.siblingOrdinal = i;
1017 Timestamp = DateTime.UtcNow
1031 if (!this.ChildrenOrdered)
1034 if (!this.childrenLoaded)
1035 await this.LoadChildren();
1037 if (this.children is
null)
1045 lock (this.children)
1047 int c = this.children.Count;
1048 int i = this.children.IndexOf(Child);
1049 if (i < 0 || i + 1 >= c)
1052 Child2 = this.children[i + 1];
1054 this.children.RemoveAt(i);
1055 this.children.Insert(i + 1, Child);
1057 i = Child.siblingOrdinal;
1058 Child.siblingOrdinal = Child2.siblingOrdinal;
1059 Child2.siblingOrdinal = i;
1069 Timestamp = DateTime.UtcNow
1096 throw new Exception(
"Child must be a group node.");
1098 if (this.objectId == Guid.Empty)
1099 throw new Exception(
"Parent node must be persisted before you can add nodes to it.");
1101 if (!this.childrenLoaded)
1102 await this.LoadChildren();
1104 Node.parentId = this.objectId;
1110 lock (this.synchObject)
1112 if (this.children is
null)
1113 this.children =
new List<GroupNode>();
1114 else if ((c = this.children.Count) > 0)
1115 After = this.children[c - 1];
1119 Node.siblingOrdinal = this.children.Count;
1122 this.children.Add(Node);
1125 if (!(ToUpdate is
null))
1128 if (Node.objectId == Guid.Empty)
1137 NodeType = Node.GetType().FullName,
1139 DisplayName = await Node.GetTypeNameAsync(
Language),
1154 Timestamp = DateTime.UtcNow
1157 if (this.ChildrenOrdered && !(After is
null))
1159 Event.AfterNodeId = After.nodeId;
1166 await Node.NodeUpdated();
1168 await this.RaiseUpdate();
1176 this.updated = DateTime.Now;
1188 ParentPartition = this.Partition,
1197 Timestamp = DateTime.UtcNow
1200 if (this.oldId != this.nodeId)
1203 this.oldId = this.nodeId;
1212 if (this.objectId != Guid.Empty)
1215 await this.RaiseUpdate();
1226 throw new Exception(
"Child must be a group node.");
1228 if (!this.childrenLoaded)
1229 await this.LoadChildren();
1234 lock (this.synchObject)
1236 if (!(this.children is
null))
1238 i = this.children.IndexOf(Node);
1241 this.children.RemoveAt(i);
1242 if (i == 0 && this.children.Count == 0)
1243 this.children =
null;
1252 if (!(ToUpdate is
null))
1255 Node.parentId = Guid.Empty;
1258 if (Node.objectId != Guid.Empty)
1261 await this.RaiseUpdate();
1268 Timestamp = DateTime.UtcNow
1282 if (this.parent.childrenLoaded)
1286 lock (this.parent.synchObject)
1288 if (!(this.parent.children is
null))
1290 if (this.parent.children.Remove(
this))
1292 if (this.parent.children.Count == 0)
1293 this.parent.children =
null;
1300 if (!(ToUpdate is
null))
1305 if (!this.childrenLoaded)
1306 await this.LoadChildren();
1308 if (!(this.children is
null))
1310 List<GroupNode> Children = this.children;
1311 this.children =
null;
1315 Child.parent =
null;
1316 Child.parentId = Guid.Empty;
1321 this.children =
null;
1324 if (this.objectId != Guid.Empty)
1327 this.objectId = Guid.Empty;
1341 return Task.FromResult<IEnumerable<ICommand>>(
new ICommand[]
Class representing an event.
Static class managing the application event log. Applications and services log events on this static ...
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Clears all messages for a group.
Logs a message on a group.
Defines a message logged on a group node.
string EventId
Optional Event ID.
MessageType Type
Message Type
DateTime Created
When node was created.
Base class for all group nodes.
EventHandlerAsync OnUpdate
Event raised when node has been updated.
virtual async Task LogMessageAsync(MessageType Type, string EventId, string Body)
Logs a message on the node.
virtual Task< bool > RemoveWarningAsync()
Removes warning messages with an empty event ID from the node.
virtual Task LogMessageAsync(MessageType Type, string Body)
Logs a message on the node.
virtual GroupNode[] CheckOrderLocked(List< GroupNode > Children)
Checks the ordering of children.
virtual GroupNode[] SortChildrenAfterLoadLocked(List< GroupNode > Children)
Method that allows the node to sort its children, after they have been loaded.
virtual Task< bool > RemoveInformationAsync(string EventId)
Removes an informational message on the node.
virtual Task LogWarningAsync(string Body)
Logs an warning message on the node.
virtual async Task AddAsync(INode Child)
Adds a new child to the node.
virtual async Task< bool > MoveDownAsync(GroupNode Child, RequestOrigin Caller)
Tries to move the child node down.
abstract Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
virtual Task< bool > CanViewAsync(RequestOrigin Caller)
If the node is visible to the caller.
virtual Task< bool > RemoveMessageAsync(MessageType Type)
Removes messages with empty event IDs from the node.
NodeState State
Current overall state of the node.
override int GetHashCode()
Serves as the default hash function.
async Task< T > GetAncestor< T >()
Tries to get an ancestor node of a given type, if one exists.
abstract Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a presumptive parent, i.e. can be added to that parent (if that parent accepts th...
virtual Task LogErrorAsync(string Body)
Logs an error message on the node.
virtual Task< bool > RemoveInformationAsync()
Removes warning messages with an empty event ID from the node.
async Task< INode > GetParent()
Gets the parent of the node.
virtual async Task< bool > MoveDownAsync(RequestOrigin Caller)
Tries to move the node down.
virtual bool IsControllable
If the node can be controlled.
virtual async Task< bool > RemoveMessageAsync(MessageType Type, string EventId)
Logs a message on the node.
virtual string LocalId
If provided, an ID for the node, but unique locally between siblings. Can be null,...
INode Parent
Parent Node, or null if a root node.
async Task< Message[]> GetMessageArrayAsync(RequestOrigin Caller)
Gets messages logged on the node.
virtual Task< bool > CanAddAsync(RequestOrigin Caller)
If the node can be added to by the caller.
virtual bool IsReadable
If the node can be read.
string Partition
Optional partition in which the Node ID is unique.
virtual bool HasCommands
If the node has registered commands or not.
override string ToString()
virtual async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
virtual Task< bool > RemoveWarningAsync(string EventId)
Removes warning messages with a given event ID from the node.
DateTime LastChanged
When the node was last updated.
virtual Task< bool > RemoveErrorAsync(string EventId)
Removes error messages with a given event ID from the node.
Guid ParentId
Object ID of parent node in persistence layer.
string SourceId
Optional ID of source containing node.
DateTime Created
When node was created.
virtual bool ChildrenOrdered
If the children of the node have an intrinsic order (true), or if the order is not important (false).
virtual async Task< bool > MoveUpAsync(RequestOrigin Caller)
Tries to move the node up.
virtual Task< bool > CanEditAsync(RequestOrigin Caller)
If the node can be edited by the caller.
virtual async Task< bool > RemoveAsync(INode Child)
Removes a child from the node.
virtual async Task< IEnumerable< Message > > GetMessagesAsync(RequestOrigin Caller)
Gets messages logged on the node.
Guid ObjectId
Object ID in persistence layer.
virtual Task LogErrorAsync(string EventId, string Body)
Logs an error message on the node.
GroupNode()
Base class for all group nodes.
virtual Task< IEnumerable< ICommand > > Commands
Available command objects. If no commands are available, null is returned.
abstract Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node ...
virtual async Task< bool > MoveUpAsync(GroupNode Child, RequestOrigin Caller)
Tries to move the child node up.
virtual async Task NodeUpdated()
Persists changes to the node, and generates a node updated event.
static async Task< string > GetUniqueGroupId(string GroupId)
Gets a Group ID, based on GroupId that is not already available in the database.
virtual string LogId
If provided, an ID for the node, as it would appear or be used in system logs. Can be null,...
virtual Task< bool > RemoveErrorAsync()
Removes error messages with an empty event ID from the node.
virtual async Task DestroyAsync()
Destroys the node. If it is a child to a parent node, it is removed from the parent first.
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
virtual Task LogInformationAsync(string EventId, string Body)
Logs an informational message on the node.
virtual Task< bool > CanDestroyAsync(RequestOrigin Caller)
If the node can be destroyed to by the caller.
virtual async Task UpdateAsync()
Updates the node (in persisted storage).
DateTime Updated
When node was last updated. If it has not been updated, value will be DateTime.MinValue.
int SiblingOrdinal
Sibling ordinal, used to order siblings when ordered.
virtual Task LogWarningAsync(string EventId, string Body)
Logs an warning message on the node.
async Task< Parameter[]> GetDisplayableParameterAraryAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
override bool Equals(object obj)
Determines whether the specified object is equal to the current object.
bool HasChildren
If the source has any child sources.
virtual Task LogInformationAsync(string Body)
Logs an informational message on the node.
Defines the Groups data source. This data source contains a tree structure of groups of nodes
const string SourceID
Source ID for the groups data source.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
static async Task Update(object Object)
Updates an object in the database.
static async Task Delete(object Object)
Deletes an object in the database.
static async Task< object > LoadObject(string CollectionName, object ObjectId)
Loads an object given its Object ID ObjectId and its collection name CollectionName .
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that conform to all child-filters provided.
This filter selects objects that have a named field equal to a given value.
A chunked list is a linked list of chunks of objects of type T .
void AddRange(IEnumerable< T > Collection)
Adds a range of elements (last) to the list.
T[] ToArray()
Returns an array containing all elements of the collection.
Contains information about a language.
async Task< Namespace > GetNamespaceAsync(string Name)
Gets the namespace object, given its name, if available.
async Task< Namespace > CreateNamespaceAsync(string Name)
Creates a new language namespace, or updates an existing language namespace, if one exist with the sa...
Contains information about a namespace in a language.
Task< LanguageString > GetStringAsync(int Id)
Gets the string object, given its ID, if available.
Basic access point for runtime language localization.
static async Task< Language > GetDefaultLanguageAsync()
Gets the default language.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Static class of application-wide semaphores that can be used to order access to editable objects.
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
DateTime-valued parameter.
Contains information about a message logged on a node.
Base class for all node parameters.
string Name
Parameter Name.
virtual object StringValue
String representation of parameter value
Tokens available in request.
static readonly RequestOrigin Empty
Empty request origin.
bool HasPrivilege(string Privilege)
If the origin has a given privilege.
static string EmptyIfSame(string Id1, string Id2)
Returns Id1 if different, string.Empty if the same.
Node status changed event.
Contains a reference to a thing
Base Interface for all metering nodes.
Interface for actuator nodes.
Interface for nodes that are published through the concentrator interface.
Task< bool > MoveUpAsync(RequestOrigin Caller)
Tries to move the node up.
INode Parent
Parent Node, or null if a root node.
Task< bool > MoveDownAsync(RequestOrigin Caller)
Tries to move the node down.
Interface for sensor nodes.
Interface for thing references.
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.
TypeNameSerialization
How the type name should be serialized.
MessageType
Type of message.
NodeState
State of a node.