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;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
16
18{
22 public enum ReportType
23 {
27 Present,
28
32 History,
33
37 StateDiagram,
38
42 Profiling
43 }
44
49 {
53 public CreateReport()
54 : base("StateMachines/CreateReport",
55 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
56 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
57 {
58 }
59
60 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(CreateReport).Namespace + ".JSON.CreateReport.req");
61 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(CreateReport).Namespace + ".XML.CreateReport.req");
62
71 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
72 {
74 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
75 string ReportTypeStr = (string)Parameters["PReportType"].AssociatedObjectValue;
76 string ReportFormatStr = (string)Parameters["PReportFormat"].AssociatedObjectValue;
77
78 if (!Enum.TryParse(ReportTypeStr, out ReportType ReportType))
79 throw new BadRequestException("Invalid report type.");
80
81 if (!Enum.TryParse(ReportFormatStr, out ReportFormat ReportFormat))
82 throw new BadRequestException("Invalid report format.");
83
84 string BareJid = User.UserName + "@" + Gateway.Domain;
85
86 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
87 ?? throw new NotFoundException("Token not found.");
88
89 await GetToken.AssertAuthorization(Request, Token, new XmppAddress(BareJid));
90
91 StateMachineProcessor.CacheRecord _ = await StateMachineProcessor.GetStateMachine(Token.MachineId, false)
92 ?? throw new NotFoundException("State-Machine not found.");
93
94 string Report = ReportType switch
95 {
96 ReportType.Present => await StateMachineProcessor.GetPresentReportAsync(Token, ReportFormat),
97 ReportType.History => await StateMachineProcessor.GetHistoryReportAsync(Token, ReportFormat),
98 ReportType.StateDiagram => await StateMachineProcessor.GetStateDiagramAsync(Token, ReportFormat),
99 ReportType.Profiling => await StateMachineProcessor.GetProfilingReportAsync(TokenId, ReportFormat),
100 _ => throw new BadRequestException("Unrecognized report type."),
101 };
102
103 await Response.Return(new NamedDictionary<string, object>("ReportResult", AgentNamespace)
104 {
105 { "report", Report }
106 });
107 }
108 }
109}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
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:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
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
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Class managing a script expression.
Definition: Expression.cs:41
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
CaseInsensitiveString MachineId
State Machine ID, if any
Definition: Token.cs:163
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:71
Definition: ImplTypes.g.cs:58
ReportFormat
Desired report format
Definition: ReportFormat.cs:7