2using System.Collections.Generic;
3using System.Threading.Tasks;
32 public const string SourceID =
"MeteringTopology";
34 private static readonly Dictionary<string, MeteringNode> nodes =
new Dictionary<string, MeteringNode>();
35 private static Root root =
null;
38 private DateTime lastChanged;
55 string IDataSource.SourceID
81 get => this.lastChanged;
84 if (this.lastChanged != value)
86 this.lastChanged = value;
117 public static Task<MeteringNode>
GetNode(
string NodeId)
142 if (nodes.TryGetValue(NodeRef.NodeId, out
MeteringNode Node))
146 nodes[NodeRef.NodeId] = Node2;
165 nodes[Node.
NodeId] = Node;
181 if (!nodes.TryGetValue(OldId, out
MeteringNode Node2) || Node2 != Node)
184 if (nodes.TryGetValue(Node.
NodeId, out Node2))
188 nodes[Node.
NodeId] = Node;
201 nodes.Remove(Node.
NodeId);
213 return Task.FromResult(
true);
226 return new INode[] { root };
244 private static async Task LoadRoot()
272 NodeType = Result.GetType().FullName,
280 ParentId =
string.Empty,
281 ParentPartition =
string.Empty,
283 State = Result.
State,
289 Timestamp = DateTime.Now
295 nodes[Result.
NodeId] = Result;
308 if (MaxAge <= TimeSpan.Zero)
309 throw new ArgumentException(
"Age must be positive.", nameof(MaxAge));
311 DateTime Limit = DateTime.Now.Subtract(MaxAge);
330 KeyValuePair<string, object>[] Tags =
new KeyValuePair<string, object>[]
332 new KeyValuePair<string, object>(
"Limit", Limit),
333 new KeyValuePair<string, object>(
"NrEvents", NrEvents)
339 Log.
Informational(
"Deleting " + NrEvents.ToString() +
" metering topology events from the database.",
SourceID, Tags);
348 public event EventHandlerAsync<SourceEvent>
OnEvent =
null;
365 return Task.CompletedTask;
367 return h(Reference, Values);
389 if (ParentNode is
null)
396 nodes.Remove(Node.
NodeId);
Class representing an event.
Static class managing the application event log. Applications and services log events on this static ...
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.
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 InsertLazy(object Object)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
static async Task Delete(object Object)
Deletes an object in the database.
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.
static Task< object > TryLoadObject(string CollectionName, object ObjectId)
Tries to load an object given its Object ID ObjectId and its collection name CollectionName .
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.
This filter selects objects that have a named field lesser or equal to a given value.
Contains information about a language.
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Basic access point for runtime language localization.
static async Task< Language > GetDefaultLanguageAsync()
Gets the default language.
Static class managing persistent settings.
static string Get(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Base class for all metering nodes.
virtual bool IsControllable
If the node can be controlled.
async Task< Parameter[]> GetDisplayableParameterAraryAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
virtual bool IsReadable
If the node can be read.
bool HasChildren
If the source has any child sources.
NodeState State
Current overall state of the node.
virtual string LogId
If provided, an ID for the node, as it would appear or be used in system logs. Can be null,...
virtual string LocalId
If provided, an ID for the node, but unique locally between siblings. Can be null,...
virtual bool HasCommands
If the node has registered commands or not.
DateTime Updated
When node was last updated. If it has not been updated, value will be DateTime.MinValue.
string SourceId
Optional ID of source containing node.
string Partition
Optional partition in which the Node ID is unique.
virtual bool ChildrenOrdered
If the children of the node have an intrinsic order (true), or if the order is not important (false).
Guid ParentId
Object ID of parent node in persistence layer.
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
IEnumerable< IDataSource > ChildSources
Child sources. If no child sources are available, null is returned.
IEnumerable< INode > RootNodes
Root node references. If no root nodes are available, null is returned.
DateTime LastChanged
When the source was last updated.
static Task NewMomentaryValues(IThingReference Reference, IEnumerable< Field > Values)
Reports newly measured values.
async Task< INode > GetNodeAsync(IThingReference NodeRef)
Gets the node, given a reference to it.
static Task< MeteringNode > GetNode(string NodeId)
Gets a node from the Metering Topology
EventHandlerAsync< SourceEvent > OnEvent
Event raised when a data source event has been raised.
Task< bool > CanViewAsync(RequestOrigin Caller)
If the data source is visible to the caller.
static async Task< int > DeleteOrphans()
Deletes orphaned nodes in the metering topology source.
MeteringTopology()
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
static async Task< int > DeleteOldEvents(TimeSpan MaxAge)
Deletes old data source events.
bool HasChildren
If the source has any child sources.
const string SourceID
Source ID for the metering topology data source.
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
static NewMomentaryValuesHandler OnNewMomentaryValues
Event raised when a node in the metering topology reports a new momentary value.
static Root Root
Root node.
static async Task< MeteringNode > GetNode(IThingReference NodeRef)
Gets a node from the Metering Topology
Class for the root node of the Metering topology.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Tokens available in request.
static readonly RequestOrigin Empty
Empty request origin.
static string EmptyIfSame(string Id1, string Id2)
Returns Id1 if different, string.Empty if the same.
Abstract base class for all data source events.
Contains a reference to a thing
Interface for observable classes implementing communication protocols.
Interface for datasources that are published through the concentrator interface.
Interface for nodes that are published through the concentrator interface.
Interface for thing references.
string Partition
Optional partition in which the Node ID is unique.
string SourceId
Optional ID of source containing node.
delegate Task NewMomentaryValuesHandler(IThingReference Reference, IEnumerable< Field > Values)
Delegate for new momentary values event handlers.