-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cls.xml
215 lines (195 loc) · 6.21 KB
/
Main.cls.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="WebSockets.Main">
<Super>%CSP.WebSocket</Super>
<TimeCreated>63900,30560.372348</TimeCreated>
<Parameter name="Timeout">
<Description>
How long to wait for user when connection established</Description>
<Default>3600</Default>
</Parameter>
<Parameter name="AuthorizationTimeout">
<Description>
How long to wait for authorization key when connection established</Description>
<Default>3600</Default>
</Parameter>
<Parameter name="Buffer">
<Description>
Buffer size in bytes</Description>
<Default>8192</Default>
</Parameter>
<Method name="OnPreServer">
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[ Quit $$$OK
]]></Implementation>
</Method>
<Method name="OnPostServer">
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[ Quit $$$OK
]]></Implementation>
</Method>
<Method name="RequireAuthorization">
<Description><![CDATA[
Holds process and expects only one package from the client,<br>
containing Username and password in JSON format. <br>
Example : {User:"_SYSTEM",Password:"sys"}]]></Description>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set status=..Write("Wait auth "_$ZDT($H))
Set authObj = ..Read(,.status,..#AuthorizationTimeout) // wait for package
Set loginStatus = ##class(%SYSTEM.Security).Login(authObj.User,authObj.Password) // login user
Quit loginStatus
]]></Implementation>
</Method>
<Method name="Read">
<Description>
Reads up to len characters from the client.
If the call is successful the status (sc) will be returned as $$$OK, otherwise an error code of $$$CSPWebSocketTimeout
indicates a timeout and $$$CSPWebSocketClosed indicates that the client has terminated the WebSocket.
Converts results into json
Closes connection if received incorrect json</Description>
<FormalSpec><![CDATA[&len:%Integer=32656,&sc:%Status,timeout:%Integer=86400]]></FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
Set json = ##super(len, .sc, timeout)
Do:$$$ISERR(sc) ..EndServer()
Set st = ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(json,,.obj,1)
Do:$$$ISERR(st) ..EndServer()
Quit obj
]]></Implementation>
</Method>
<Method name="Write">
<Description><![CDATA[
Write to the client.
If <var>data</var> is object - outputs JSON.
If <var>data</var> is string - wraps it as JSON { "Data" : data }.]]></Description>
<FormalSpec>data:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
if $isobject(data) {
If data.%IsA("%ZEN.proxyObject") {
Set data = ..OutputToStr(data,"%ToJSON")
} Else {
Set data = ..OutputToStr("%ZEN.Auxiliary.jsonProvider","%ObjectToJSON",data)
}
} Else {
//#include %ZEN.Utils
Set data = "{""Data"":"""_$Replace(data,"\'","'")_"""}"
}
Quit ##super(data)
]]></Implementation>
</Method>
<Method name="Server">
<Description><![CDATA[
Main method. <br>
Authenticates user, checks that he has required resource.
If all goes well, initialises sensors and forwards users' input to Choose Action]]></Description>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set status=..Write("Server start at "_$ZDT($H))
If $$$ISOK(..RequireAuthorization())
{
Set status=..Write("Auth success at "_$ZDT($H))
Do ..ClientLoop()
} Else {
Set status=..EndServer()
Quit $$$OK
}
]]></Implementation>
</Method>
<Method name="ClientLoop">
<Implementation><![CDATA[
For {
Set data=..Read(, .st, ..#Timeout)
Set st=..Action(data)
If $$$ISERR(st) {
Set st=..Write("Error "_$System.Status.GetErrorText(st))
}
}
]]></Implementation>
</Method>
<Method name="Action">
<Description><![CDATA[
Execute singular action
<var>obj</var> is an object with Action property and additional arguments in Arguments property]]></Description>
<FormalSpec>obj:%ZEN.proxyObject</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set action =obj.Action
If action = "Echo" {
Set st=..Write(obj.Arguments)
} Elseif action = "SetVar" {
Set st=..SetVar(obj.Arguments.Var, obj.Arguments.Val)
} Elseif action = "GetVar" {
Set st=..GetVar(obj.Arguments.Var)
} Else {
Set st=..Write("Unknown action")
}
Quit st
]]></Implementation>
</Method>
<Method name="SetVar">
<Description><![CDATA[
Set <var>var</var> to <var>val</var>]]></Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>var,val</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set @var = val
Quit $$$OK
]]></Implementation>
</Method>
<Method name="GetVar">
<Description><![CDATA[
Get <var>Var</var> value]]></Description>
<FormalSpec>var</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[ Quit ..Write($g(@var))
]]></Implementation>
</Method>
<Method name="OutputToStr">
<Description><![CDATA[
Executes actions and returns device output <br>
pObj - OREF or class<br>
pMethod - instance or class method to execute respectively<br>
pArgs - additional arguments]]></Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>obj,method,args...</FormalSpec>
<ProcedureBlock>0</ProcedureBlock>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
Set str=""
//Redirect IO to the current routine - makes use of the labels defined below
Use $io::("^"_$ZNAME)
//Enable redirection
Do ##class(%Device).ReDirectIO(1)
if $isobject(obj) {
Do $Method(obj,method,args...)
} elseif $$$comClassDefined(obj) {
Do $ClassMethod(obj,method,args...)
}
//Disable redirection
Do ##class(%Device).ReDirectIO(0)
Quit str
//Labels that allow for IO redirection
//Read Character - we Don't care about reading
rchr(c) quit
//Read a string - we Don't care about reading
rstr(sz,to) quit
//Write a character - call the output label
wchr(s) Do output($char(s)) quit
//Write a form feed - call the output label
wff() Do output($char(12)) quit
//Write a newline - call the output label
wnl() Do output($char(13,10)) quit
//Write a string - call the output label
wstr(s) Do output(s) quit
//Write a tab - call the output label
wtab(s) Do output($char(9)) quit
//Output label - this is where you would handle what you actually want to Do.
// in our case, we want to Write to str
output(s) Set str=str_s quit
]]></Implementation>
</Method>
</Class>
</Export>