Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateReport.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
9using Waher.Script;
15
17{
21 public enum ReportType
22 {
26 Present,
27
31 History,
32
36 StateDiagram,
37
41 Profiling
42 }
43
48 {
52 public CreateReport()
53 : base("StateMachines/CreateReport",
54 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
55 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
56 {
57 }
58
59 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(CreateReport).Namespace + ".JSON.CreateReport.req");
60 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(CreateReport).Namespace + ".XML.CreateReport.req");
61
70 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
71 {
73 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
74 string ReportTypeStr = (string)Parameters["PReportType"].AssociatedObjectValue;
75 string ReportFormatStr = (string)Parameters["PReportFormat"].AssociatedObjectValue;
76
77 if (!Enum.TryParse(ReportTypeStr, out ReportType ReportType))
78 throw new BadRequestException("Invalid report type.");
79
80 if (!Enum.TryParse(ReportFormatStr, out ReportFormat ReportFormat))
81 throw new BadRequestException("Invalid report format.");
82
83 string BareJid = User.UserName + "@" + Gateway.Domain;
84
85 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
86 ?? throw new NotFoundException("Token not found.");
87
88 await GetToken.AssertAuthorization(Token, new XmppAddress(BareJid));
89
90 StateMachineProcessor.CacheRecord _ = await StateMachineProcessor.GetStateMachine(Token.MachineId)
91 ?? throw new NotFoundException("State-Machine not found.");
92
93 string Report;
94
95 switch (ReportType)
96 {
97 case ReportType.Present:
98 Report = await StateMachineProcessor.GetPresentReportAsync(Token, ReportFormat);
99 break;
100
101 case ReportType.History:
102 Report = await StateMachineProcessor.GetHistoryReportAsync(Token, ReportFormat);
103 break;
104
105 case ReportType.StateDiagram:
106 Report = await StateMachineProcessor.GetStateDiagramAsync(Token, ReportFormat);
107 break;
108
109 case ReportType.Profiling:
110 Report = await StateMachineProcessor.GetProfilingReportAsync(TokenId, ReportFormat);
111 break;
112
113 default:
114 throw new BadRequestException("Unrecognized report type.");
115 }
116
117 await Response.Return(new NamedDictionary<string, object>("ReportResult", AgentNamespace)
118 {
119 { "report", Report }
120 });
121 }
122 }
123}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
Class managing a script expression.
Definition: Expression.cs:39
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
CaseInsensitiveString MachineId
State Machine ID, if any
Definition: Token.cs:157
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: CreateReport.cs:70
ReportFormat
Desired report format
Definition: ReportFormat.cs:7