-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrndi.ext.jsfuncs.pp
215 lines (183 loc) · 5.41 KB
/
trndi.ext.jsfuncs.pp
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
(*
* This file is part of Trndi (https://github.com/slicke/trndi).
* Copyright (c) 2021-2025 Björn Lindh.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------
*
* GitHub: https://github.com/slicke/trndi
*)
// Handle JS functions
unit trndi.ext.jsfuncs;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, trndi.ext.functions, trndi.types, slicke.ux.alert, trndi.api, trndi.ext.engine,
Trndi.Native,
dialogs, math;
type
TJSFuncs = class(TObject)
public
function asyncget(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
function bgDump(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
function setLimits(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
function querySvc(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
function runCMD(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
constructor Create(cgm: TrndiAPI);
private
tapi: TrndiAPI;
procedure ShowMsg(const str: string);
end;
implementation
// Create this function JS handler
constructor TJSFuncs.Create(cgm: TrndiAPI);
begin
tapi := cgm;
with TTrndiExtEngine.Instance do
begin
// Register the functions in JS
AddPromise('bgDump', JSCallbackFunction(@bgDump));
AddPromise('asyncGet', JSCallbackFunction(@asyncGet));
AddPromise('runCMD', JSCallbackFunction(@asyncGet));
AddPromise('querySvc', JSCallbackFunction(@querySvc));
AddPromise('setLimits', JSCallbackFunction(@setLimits), 2, 5);
AddPromise('setLevelColor', JSCallbackFunction(@setLimits), 3, 6);
end;
end;
procedure TJSFuncs.ShowMsg(const str: string);
begin
ExtLog('Message from Extension','An extension triggered a message', str, widechar($274F));
end;
// Blood Glucose dump, from JS
function TJSFuncs.bgDump(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
var
i: integer;
r: BGResults;
begin
if params[1]^.data.match <> JD_INT then
begin
ShowMsg('Unknown paramter #1');
Exit(false);
end;
if params[2]^.data.match <> JD_INT then
begin
ShowMsg('Unknown paramter #2');
Exit(false);
end;
tapi.getReadings(params[0]^.data.Int32Val, params[1]^.data.Int32Val);
//@fixme not done
result := true;
end;
// backend for asyncGet in JS
// Fetches a file from the web
function TJSFuncs.asyncget(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
var
s,r: string;
v: JSValueVal;
begin
v := params[0]^;
if not v.mustbe(JD_STR, func, 0) then
begin
result := false;
r := 'Wrong data type for URL';
v := StringToValueVal(r);
Exit(false);
end;
if not TrndiNative.getURL(v.data.StrVal, s) then
begin
result := false;
r := 'Cannot fetch URL ' + v.data.strval;
end
else
begin
r := s;
result := true;
end;
v := StringToValueVal(r);
res := v;
end;
// Set high/low values
function TJSFuncs.setLimits(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
var
v: JSValueVal; // Return data
times: single; // Unit multiplier
f: boolean;
begin
// Values has to be int and might have a bool
if checkJSParams(params, [JD_INT, JD_INT, JD_INT, JD_INT], [JD_INT, JD_INT]) = JS_PARAM_OK then
begin
times := 1;
f := false;
end
else
if checkJSParams(params, [JD_F64, JD_F64, JD_F64, JD_F64], [JD_F64, JD_F64]) = JS_PARAM_OK then
begin
times := 18.0182;
f := true;
end
else
begin
result := false;
res.data.Int32Val := -1;
Exit(false);
end;
tapi.cgmLo := round(params[0]^.floatify * times);
tapi.cgmHi := round(params[1]^.floatify * times);
if params.Count = 4 then
begin
tapi.cgmRangeLo := round(params[2]^.floatify * times);
tapi.cgmRangeHi := round(params[3]^.floatify * times);
end;
v := IntToValueVal(tapi.cgmHi);
res := v;
result := true;
end;
// Query the backend via JS
function TJSFuncs.querySvc(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
const
QUERY = 0;
var
sd: string;
v: JSValueVal;
begin
//params[QUERY].mustbe(JD_STR, func);
//params^[QUERY].data.StrVal;
//params^[PROPS].mustbe(JD_ARRAY, func);
//res := params[1][2][0];
end;
// Query the backend via JS
function TJSFuncs.runCMD(ctx: pointer; const func: string; const params: JSParameters; out res:
JSValueVal): boolean;
begin
if not checkJSParams(params, [JD_STR], [JD_STR, JD_STR, JD_STR]) = JS_PARAM_OK then
begin
result := false;
res.data.Int32Val := -1;
Exit(false);
end;
if params.count = 2 then
res.data.Int32Val := ExecuteProcess(params[0]^.stringify, [])
else
res.data.Int32Val := ExecuteProcess(params[0]^.stringify, params[1]^.stringify.Split(params[2]^.stringify));
result := true;
end;
end.