/// <summary> /// Persists changes to the node, and generates a node updated event. /// </summary> protected virtual async Task NodeUpdated() { this.updated = DateTime.Now; await Database.Update(this); await MeteringTopology.NewEvent(new NodeUpdated() { Parameters = await this.GetDisplayableParameterAraryAsync(await Translator.GetDefaultLanguageAsync(), RequestOrigin.Empty), HasChildren = this.HasChildren, ChildrenOrdered = this.ChildrenOrdered, IsReadable = this.IsReadable, IsControllable = this.IsControllable, HasCommands = this.HasCommands, ParentId = this.NodeId, ParentPartition = this.Partition, Updated = this.Updated, State = this.State, NodeId = this.NodeId, OldId = this.oldId, Partition = this.Partition, LogId = MeteringTopology.EmptyIfSame(this.LogId, this.NodeId), LocalId = MeteringTopology.EmptyIfSame(this.LocalId, this.NodeId), SourceId = this.SourceId, Timestamp = DateTime.Now }); this.oldId = this.nodeId; }
/// <summary> /// Adds a new child to the node. /// </summary> /// <param name="Child">New child to add.</param> public virtual async Task AddAsync(INode Child) { MeteringNode Node = Child as MeteringNode; if (Node == null) { throw new Exception("Child must be a metering node."); } if (this.objectId == Guid.Empty) { throw new Exception("Parent node must be persisted before you can add nodes to it."); } if (!this.childrenLoaded) { await this.LoadChildren(); } Node.parentId = this.objectId; MeteringNode After = null; int c; lock (this.synchObject) { if (this.children == null) { this.children = new List <MeteringNode>(); } else if ((c = this.children.Count) > 0) { After = this.children[c - 1]; } this.children.Add(Node); Node.parent = this; } if (Node.objectId == Guid.Empty) { await Database.Insert(Node); MeteringTopology.RegisterNode(Node); Language Language = await Translator.GetDefaultLanguageAsync(); NodeAdded Event = new NodeAdded() { Parameters = await Node.GetDisplayableParameterAraryAsync(Language, RequestOrigin.Empty), NodeType = Node.GetType().FullName, Sniffable = this is ISniffable, DisplayName = await Node.GetTypeNameAsync(Language), HasChildren = Node.HasChildren, ChildrenOrdered = Node.ChildrenOrdered, IsReadable = Node.IsReadable, IsControllable = Node.IsControllable, HasCommands = Node.HasCommands, ParentId = this.NodeId, ParentPartition = this.Partition, Updated = Node.Updated, State = Node.State, NodeId = Node.NodeId, Partition = Node.Partition, LogId = MeteringTopology.EmptyIfSame(Node.LogId, Node.NodeId), LocalId = MeteringTopology.EmptyIfSame(Node.LocalId, Node.NodeId), SourceId = Node.SourceId, Timestamp = DateTime.Now }; if (this.ChildrenOrdered && After != null) { Event.AfterNodeId = After.nodeId; Event.AfterPartition = After.Partition; } await MeteringTopology.NewEvent(Event); } else { await Node.NodeUpdated(); } this.RaiseUpdate(); }