@signalk/assemblyscript-plugin-sdk 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +40 -0
- package/asconfig.json +25 -0
- package/assembly/api.ts +287 -0
- package/assembly/index.ts +15 -0
- package/assembly/network.ts +53 -0
- package/assembly/plugin.ts +49 -0
- package/assembly/resources.ts +157 -0
- package/assembly/signalk.ts +153 -0
- package/build/plugin.d.ts +104 -0
- package/build/plugin.js +232 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [dirkwa] [Dirk Wahrheit]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Signal K AssemblyScript Plugin SDK
|
|
2
|
+
|
|
3
|
+
Build WASM plugins for Signal K Server using TypeScript-like syntax.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- TypeScript-like syntax (strict subset)
|
|
8
|
+
- Compiles directly to WASM
|
|
9
|
+
- Small binaries (3-10 KB typical)
|
|
10
|
+
- Good performance (80-90% of Rust)
|
|
11
|
+
- Familiar tooling (npm, TypeScript)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @signalk/assemblyscript-plugin-sdk
|
|
17
|
+
npm install --save-dev assemblyscript
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Documentation
|
|
21
|
+
|
|
22
|
+
For complete documentation including:
|
|
23
|
+
|
|
24
|
+
- Step-by-step tutorial
|
|
25
|
+
- API reference
|
|
26
|
+
- Resource providers
|
|
27
|
+
- Troubleshooting
|
|
28
|
+
|
|
29
|
+
See the [AssemblyScript Plugin Guide](../../docs/develop/plugins/wasm/assemblyscript.md) in the Signal K Server documentation.
|
|
30
|
+
|
|
31
|
+
## Examples
|
|
32
|
+
|
|
33
|
+
See [examples/wasm-plugins/](../../examples/wasm-plugins/) for working examples:
|
|
34
|
+
|
|
35
|
+
- `example-hello-assemblyscript` - Basic plugin
|
|
36
|
+
- `example-weather-plugin` - Resource provider with network requests
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
Apache-2.0
|
package/asconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"targets": {
|
|
3
|
+
"release": {
|
|
4
|
+
"outFile": "build/plugin.wasm",
|
|
5
|
+
"sourceMap": false,
|
|
6
|
+
"optimize": true,
|
|
7
|
+
"shrinkLevel": 2,
|
|
8
|
+
"converge": true,
|
|
9
|
+
"noAssert": true,
|
|
10
|
+
"runtime": "stub",
|
|
11
|
+
"use": "abort="
|
|
12
|
+
},
|
|
13
|
+
"debug": {
|
|
14
|
+
"outFile": "build/plugin.debug.wasm",
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"debug": true,
|
|
17
|
+
"runtime": "stub"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"options": {
|
|
21
|
+
"bindings": "esm",
|
|
22
|
+
"exportRuntime": false,
|
|
23
|
+
"transform": ["as-fetch/transform"]
|
|
24
|
+
}
|
|
25
|
+
}
|
package/assembly/api.ts
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal K Server API functions for AssemblyScript plugins
|
|
3
|
+
*
|
|
4
|
+
* These functions provide the FFI bridge to the Signal K server
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Delta } from './signalk'
|
|
8
|
+
|
|
9
|
+
// ===== FFI Declarations =====
|
|
10
|
+
// These functions are provided by the Signal K server
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
* Emit delta to Signal K server
|
|
15
|
+
* @param deltaPtr - Pointer to delta JSON string
|
|
16
|
+
* @param deltaLen - Length of delta JSON string
|
|
17
|
+
* @param version - Signal K version: 0 = v1 (default), 1 = v2
|
|
18
|
+
*/
|
|
19
|
+
@external("env", "sk_handle_message")
|
|
20
|
+
declare function sk_handle_message_ffi(deltaPtr: usize, deltaLen: usize, version: i32): void
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
* Set plugin status message
|
|
25
|
+
*/
|
|
26
|
+
@external("env", "sk_set_status")
|
|
27
|
+
declare function sk_set_status_ffi(msgPtr: usize, msgLen: usize): void
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @internal
|
|
31
|
+
* Set plugin error message
|
|
32
|
+
*/
|
|
33
|
+
@external("env", "sk_set_error")
|
|
34
|
+
declare function sk_set_error_ffi(msgPtr: usize, msgLen: usize): void
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
* Debug logging
|
|
39
|
+
*/
|
|
40
|
+
@external("env", "sk_debug")
|
|
41
|
+
declare function sk_debug_ffi(msgPtr: usize, msgLen: usize): void
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
* Get value from vessel.self path
|
|
46
|
+
*/
|
|
47
|
+
@external("env", "sk_get_self_path")
|
|
48
|
+
declare function sk_get_self_path_ffi(
|
|
49
|
+
pathPtr: usize,
|
|
50
|
+
pathLen: usize,
|
|
51
|
+
bufPtr: usize,
|
|
52
|
+
bufLen: usize
|
|
53
|
+
): i32
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @internal
|
|
57
|
+
* Get value from any context path
|
|
58
|
+
*/
|
|
59
|
+
@external("env", "sk_get_path")
|
|
60
|
+
declare function sk_get_path_ffi(
|
|
61
|
+
pathPtr: usize,
|
|
62
|
+
pathLen: usize,
|
|
63
|
+
bufPtr: usize,
|
|
64
|
+
bufLen: usize
|
|
65
|
+
): i32
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @internal
|
|
69
|
+
* Read plugin configuration
|
|
70
|
+
*/
|
|
71
|
+
@external("env", "sk_read_config")
|
|
72
|
+
declare function sk_read_config_ffi(bufPtr: usize, bufLen: usize): i32
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @internal
|
|
76
|
+
* Save plugin configuration
|
|
77
|
+
*/
|
|
78
|
+
@external("env", "sk_save_config")
|
|
79
|
+
declare function sk_save_config_ffi(configPtr: usize, configLen: usize): i32
|
|
80
|
+
|
|
81
|
+
// ===== Public API Functions =====
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Signal K version for delta emission
|
|
85
|
+
*/
|
|
86
|
+
export const SK_VERSION_V1: i32 = 1
|
|
87
|
+
export const SK_VERSION_V2: i32 = 2
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Emit a delta message to the Signal K server
|
|
91
|
+
*
|
|
92
|
+
* @param delta The delta message to emit
|
|
93
|
+
* @param skVersion Signal K version: SK_VERSION_V1 (default) or SK_VERSION_V2
|
|
94
|
+
*
|
|
95
|
+
* Use SK_VERSION_V1 (default) for regular navigation data.
|
|
96
|
+
* Use SK_VERSION_V2 for Course API paths and other v2-specific data to prevent
|
|
97
|
+
* v2 data from being mixed into the v1 full data model.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* // Emit v1 delta (default - for regular navigation data)
|
|
102
|
+
* const delta = createSimpleDelta('my-plugin', 'environment.temperature', '25.5')
|
|
103
|
+
* emit(delta)
|
|
104
|
+
*
|
|
105
|
+
* // Emit v2 delta (for Course API and v2-specific paths)
|
|
106
|
+
* const courseDelta = createSimpleDelta('my-plugin', 'navigation.course.nextPoint', positionJson)
|
|
107
|
+
* emit(courseDelta, SK_VERSION_V2)
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function emit(delta: Delta, skVersion: i32 = SK_VERSION_V1): void {
|
|
111
|
+
const json = delta.toJSON()
|
|
112
|
+
const buffer = String.UTF8.encode(json)
|
|
113
|
+
const ptr = changetype<usize>(buffer)
|
|
114
|
+
sk_handle_message_ffi(ptr, buffer.byteLength, skVersion)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Set plugin status message (shown in admin UI)
|
|
119
|
+
*
|
|
120
|
+
* @param message Status message
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* setStatus('Running normally')
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export function setStatus(message: string): void {
|
|
128
|
+
const buffer = String.UTF8.encode(message)
|
|
129
|
+
const ptr = changetype<usize>(buffer)
|
|
130
|
+
sk_set_status_ffi(ptr, buffer.byteLength)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Set plugin error message (shown in admin UI)
|
|
135
|
+
*
|
|
136
|
+
* @param message Error message
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* setError('Failed to connect to sensor')
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export function setError(message: string): void {
|
|
144
|
+
const buffer = String.UTF8.encode(message)
|
|
145
|
+
const ptr = changetype<usize>(buffer)
|
|
146
|
+
sk_set_error_ffi(ptr, buffer.byteLength)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Log debug message to server logs
|
|
151
|
+
*
|
|
152
|
+
* @param message Debug message
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* debug('Processing data: ' + value.toString())
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export function debug(message: string): void {
|
|
160
|
+
const buffer = String.UTF8.encode(message)
|
|
161
|
+
const ptr = changetype<usize>(buffer)
|
|
162
|
+
sk_debug_ffi(ptr, buffer.byteLength)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Get value from vessel.self path
|
|
167
|
+
*
|
|
168
|
+
* @param path Signal K path (e.g., 'navigation.speedOverGround')
|
|
169
|
+
* @returns JSON-encoded value or null if not found
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const speedJson = getSelfPath('navigation.speedOverGround')
|
|
174
|
+
* if (speedJson !== null) {
|
|
175
|
+
* const speed = parseFloat(speedJson)
|
|
176
|
+
* debug('Current speed: ' + speed.toString())
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export function getSelfPath(path: string): string | null {
|
|
181
|
+
const pathBuffer = String.UTF8.encode(path)
|
|
182
|
+
const pathPtr = changetype<usize>(pathBuffer)
|
|
183
|
+
|
|
184
|
+
// Allocate buffer for result
|
|
185
|
+
const resultBuffer = new ArrayBuffer(1024)
|
|
186
|
+
const resultPtr = changetype<usize>(resultBuffer)
|
|
187
|
+
|
|
188
|
+
const len = sk_get_self_path_ffi(
|
|
189
|
+
pathPtr,
|
|
190
|
+
pathBuffer.byteLength,
|
|
191
|
+
resultPtr,
|
|
192
|
+
1024
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
if (len === 0) {
|
|
196
|
+
return null
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Decode result
|
|
200
|
+
const bytes = Uint8Array.wrap(resultBuffer, 0, len)
|
|
201
|
+
return String.UTF8.decode(bytes.buffer)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get value from any context path
|
|
206
|
+
*
|
|
207
|
+
* @param path Full Signal K path (e.g., 'vessels.urn:mrn:imo:mmsi:123456789.navigation.position')
|
|
208
|
+
* @returns JSON-encoded value or null if not found
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```typescript
|
|
212
|
+
* const posJson = getPath('vessels.self.navigation.position')
|
|
213
|
+
* if (posJson !== null) {
|
|
214
|
+
* debug('Position: ' + posJson)
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export function getPath(path: string): string | null {
|
|
219
|
+
const pathBuffer = String.UTF8.encode(path)
|
|
220
|
+
const pathPtr = changetype<usize>(pathBuffer)
|
|
221
|
+
|
|
222
|
+
// Allocate buffer for result
|
|
223
|
+
const resultBuffer = new ArrayBuffer(1024)
|
|
224
|
+
const resultPtr = changetype<usize>(resultBuffer)
|
|
225
|
+
|
|
226
|
+
const len = sk_get_path_ffi(
|
|
227
|
+
pathPtr,
|
|
228
|
+
pathBuffer.byteLength,
|
|
229
|
+
resultPtr,
|
|
230
|
+
1024
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
if (len === 0) {
|
|
234
|
+
return null
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Decode result
|
|
238
|
+
const bytes = Uint8Array.wrap(resultBuffer, 0, len)
|
|
239
|
+
return String.UTF8.decode(bytes.buffer)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Read plugin configuration
|
|
244
|
+
*
|
|
245
|
+
* @returns JSON string with configuration
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* const configJson = readConfig()
|
|
250
|
+
* const config = JSON.parse<MyConfig>(configJson)
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
export function readConfig(): string {
|
|
254
|
+
// Allocate buffer for result
|
|
255
|
+
const resultBuffer = new ArrayBuffer(4096)
|
|
256
|
+
const resultPtr = changetype<usize>(resultBuffer)
|
|
257
|
+
|
|
258
|
+
const len = sk_read_config_ffi(resultPtr, 4096)
|
|
259
|
+
|
|
260
|
+
if (len === 0) {
|
|
261
|
+
return '{}'
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Decode result
|
|
265
|
+
const bytes = Uint8Array.wrap(resultBuffer, 0, len)
|
|
266
|
+
return String.UTF8.decode(bytes.buffer)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Save plugin configuration
|
|
271
|
+
*
|
|
272
|
+
* @param config Configuration object (will be JSON-serialized)
|
|
273
|
+
* @returns 0 on success, non-zero on error
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* const result = saveConfig(JSON.stringify(myConfig))
|
|
278
|
+
* if (result !== 0) {
|
|
279
|
+
* setError('Failed to save configuration')
|
|
280
|
+
* }
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
export function saveConfig(configJson: string): i32 {
|
|
284
|
+
const buffer = String.UTF8.encode(configJson)
|
|
285
|
+
const ptr = changetype<usize>(buffer)
|
|
286
|
+
return sk_save_config_ffi(ptr, buffer.byteLength)
|
|
287
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal K AssemblyScript Plugin SDK
|
|
3
|
+
*
|
|
4
|
+
* Provides TypeScript-like API for building WASM plugins
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Export all public types and functions
|
|
8
|
+
export * from './plugin'
|
|
9
|
+
export * from './signalk'
|
|
10
|
+
export * from './api'
|
|
11
|
+
export * from './network'
|
|
12
|
+
export * from './resources'
|
|
13
|
+
|
|
14
|
+
// Re-export JSON parsing library for plugin authors
|
|
15
|
+
export { JSON } from 'assemblyscript-json/assembly'
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network API for AssemblyScript plugins
|
|
3
|
+
*
|
|
4
|
+
* Provides capability checking for network access
|
|
5
|
+
* Requires 'network' capability in plugin manifest
|
|
6
|
+
*
|
|
7
|
+
* For HTTP requests, use as-fetch directly:
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { fetchSync } from 'as-fetch/sync'
|
|
12
|
+
* import { Response } from 'as-fetch/assembly'
|
|
13
|
+
* import { hasNetworkCapability } from 'signalk-assemblyscript-plugin-sdk'
|
|
14
|
+
*
|
|
15
|
+
* if (!hasNetworkCapability()) {
|
|
16
|
+
* setError('Network capability not granted')
|
|
17
|
+
* return 1
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* const response = fetchSync('https://api.example.com/data')
|
|
21
|
+
* if (response && response.status === 200) {
|
|
22
|
+
* const data = response.text()
|
|
23
|
+
* // Process data...
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
* Check if network capability is granted
|
|
31
|
+
*/
|
|
32
|
+
@external("env", "sk_has_capability")
|
|
33
|
+
declare function sk_has_capability_ffi(capPtr: usize, capLen: usize): i32
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check if network capability is available
|
|
37
|
+
*
|
|
38
|
+
* @returns true if plugin has network capability
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* if (!hasNetworkCapability()) {
|
|
43
|
+
* setError('Network capability not granted')
|
|
44
|
+
* return 1
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function hasNetworkCapability(): boolean {
|
|
49
|
+
const capName = 'network'
|
|
50
|
+
const buffer = String.UTF8.encode(capName)
|
|
51
|
+
const ptr = changetype<usize>(buffer)
|
|
52
|
+
return sk_has_capability_ffi(ptr, buffer.byteLength) === 1
|
|
53
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Plugin class that all AssemblyScript plugins must extend
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract base class for Signal K WASM plugins
|
|
7
|
+
*
|
|
8
|
+
* Plugins must implement these abstract methods:
|
|
9
|
+
* - name(): Human-readable plugin name
|
|
10
|
+
* - schema(): JSON schema for configuration UI
|
|
11
|
+
* - start(): Initialize plugin with configuration
|
|
12
|
+
* - stop(): Clean shutdown
|
|
13
|
+
*
|
|
14
|
+
* Note: Plugin ID is automatically derived from your package.json name.
|
|
15
|
+
* For example: "@signalk/example-weather-plugin" → "_signalk_example-weather-plugin"
|
|
16
|
+
*/
|
|
17
|
+
export abstract class Plugin {
|
|
18
|
+
/**
|
|
19
|
+
* Return human-readable plugin name for display in Admin UI
|
|
20
|
+
*/
|
|
21
|
+
abstract name(): string
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Return JSON schema for configuration UI
|
|
25
|
+
* Must be valid JSON Schema draft-07
|
|
26
|
+
*/
|
|
27
|
+
abstract schema(): string
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Initialize plugin with configuration
|
|
31
|
+
* @param config JSON string with configuration
|
|
32
|
+
* @returns 0 for success, non-zero for error
|
|
33
|
+
*/
|
|
34
|
+
abstract start(config: string): i32
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Stop plugin and clean up resources
|
|
38
|
+
* @returns 0 for success, non-zero for error
|
|
39
|
+
*/
|
|
40
|
+
abstract stop(): i32
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Plugin configuration interface
|
|
45
|
+
* Extend this for type-safe configuration
|
|
46
|
+
*/
|
|
47
|
+
export class PluginConfig {
|
|
48
|
+
enabled: bool = true
|
|
49
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal K Resource Provider API for AssemblyScript plugins
|
|
3
|
+
*
|
|
4
|
+
* Allows WASM plugins to act as resource providers (routes, waypoints, weather, etc.)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { JSON } from 'assemblyscript-json/assembly'
|
|
8
|
+
|
|
9
|
+
// ===== FFI Declarations =====
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
* Register this plugin as a resource provider for a given type
|
|
14
|
+
*/
|
|
15
|
+
@external("env", "sk_register_resource_provider")
|
|
16
|
+
declare function sk_register_resource_provider_ffi(typePtr: usize, typeLen: usize): i32
|
|
17
|
+
|
|
18
|
+
// ===== Public API Functions =====
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Register this plugin as a resource provider for a given resource type.
|
|
22
|
+
*
|
|
23
|
+
* After calling this, the plugin must export the following handler functions:
|
|
24
|
+
* - resources_list_resources(queryJson: string): string - List resources matching query
|
|
25
|
+
* - resources_get_resource(requestJson: string): string - Get a single resource
|
|
26
|
+
* - resources_set_resource(requestJson: string): void - Create/update a resource
|
|
27
|
+
* - resources_delete_resource(requestJson: string): void - Delete a resource
|
|
28
|
+
*
|
|
29
|
+
* @param resourceType The type of resources to provide (e.g., "weather", "routes", "waypoints")
|
|
30
|
+
* @returns true if registration succeeded, false otherwise
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { registerResourceProvider } from 'signalk-assemblyscript-plugin-sdk/assembly/resources'
|
|
35
|
+
*
|
|
36
|
+
* // In plugin start():
|
|
37
|
+
* if (!registerResourceProvider("weather-forecasts")) {
|
|
38
|
+
* setError("Failed to register as resource provider")
|
|
39
|
+
* return 1
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // Export handler functions:
|
|
43
|
+
* export function resources_list_resources(queryJson: string): string {
|
|
44
|
+
* // Return JSON object of resources
|
|
45
|
+
* return '{"forecast-1": {"name": "Current Weather"}}'
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* export function resources_get_resource(requestJson: string): string {
|
|
49
|
+
* // requestJson: {"id": "forecast-1", "property": null}
|
|
50
|
+
* return '{"name": "Current Weather", "temperature": 20.5}'
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function registerResourceProvider(resourceType: string): bool {
|
|
55
|
+
const buffer = String.UTF8.encode(resourceType)
|
|
56
|
+
const ptr = changetype<usize>(buffer)
|
|
57
|
+
const result = sk_register_resource_provider_ffi(ptr, buffer.byteLength)
|
|
58
|
+
return result === 1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Check if this plugin has the resourceProvider capability granted.
|
|
63
|
+
*
|
|
64
|
+
* The capability must be declared in package.json:
|
|
65
|
+
* ```json
|
|
66
|
+
* {
|
|
67
|
+
* "wasmCapabilities": {
|
|
68
|
+
* "resourceProvider": true
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @returns true if resourceProvider capability is granted
|
|
74
|
+
*/
|
|
75
|
+
export function hasResourceProviderCapability(): bool {
|
|
76
|
+
// Try to check capability via sk_has_capability if available
|
|
77
|
+
// For now, we'll just return true and let registration fail if not granted
|
|
78
|
+
return true
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ===== Helper Types for Resource Handlers =====
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Request format for resources_get_resource handler
|
|
85
|
+
*/
|
|
86
|
+
export class ResourceGetRequest {
|
|
87
|
+
id: string = ''
|
|
88
|
+
property: string | null = null
|
|
89
|
+
|
|
90
|
+
static parse(jsonStr: string): ResourceGetRequest {
|
|
91
|
+
const req = new ResourceGetRequest()
|
|
92
|
+
const parsed = JSON.parse(jsonStr)
|
|
93
|
+
|
|
94
|
+
if (parsed.isObj) {
|
|
95
|
+
const obj = parsed as JSON.Obj
|
|
96
|
+
const idValue = obj.getString('id')
|
|
97
|
+
if (idValue !== null) {
|
|
98
|
+
req.id = idValue.valueOf()
|
|
99
|
+
}
|
|
100
|
+
const propValue = obj.getString('property')
|
|
101
|
+
if (propValue !== null) {
|
|
102
|
+
req.property = propValue.valueOf()
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return req
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Request format for resources_set_resource handler
|
|
112
|
+
*/
|
|
113
|
+
export class ResourceSetRequest {
|
|
114
|
+
id: string = ''
|
|
115
|
+
value: string = '{}' // JSON string of the value
|
|
116
|
+
|
|
117
|
+
static parse(jsonStr: string): ResourceSetRequest {
|
|
118
|
+
const req = new ResourceSetRequest()
|
|
119
|
+
const parsed = JSON.parse(jsonStr)
|
|
120
|
+
|
|
121
|
+
if (parsed.isObj) {
|
|
122
|
+
const obj = parsed as JSON.Obj
|
|
123
|
+
const idValue = obj.getString('id')
|
|
124
|
+
if (idValue !== null) {
|
|
125
|
+
req.id = idValue.valueOf()
|
|
126
|
+
}
|
|
127
|
+
const valueObj = obj.getObj('value')
|
|
128
|
+
if (valueObj !== null) {
|
|
129
|
+
req.value = valueObj.stringify()
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return req
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Request format for resources_delete_resource handler
|
|
139
|
+
*/
|
|
140
|
+
export class ResourceDeleteRequest {
|
|
141
|
+
id: string = ''
|
|
142
|
+
|
|
143
|
+
static parse(jsonStr: string): ResourceDeleteRequest {
|
|
144
|
+
const req = new ResourceDeleteRequest()
|
|
145
|
+
const parsed = JSON.parse(jsonStr)
|
|
146
|
+
|
|
147
|
+
if (parsed.isObj) {
|
|
148
|
+
const obj = parsed as JSON.Obj
|
|
149
|
+
const idValue = obj.getString('id')
|
|
150
|
+
if (idValue !== null) {
|
|
151
|
+
req.id = idValue.valueOf()
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return req
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal K data model types for AssemblyScript
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Position with latitude and longitude
|
|
7
|
+
*/
|
|
8
|
+
export class Position {
|
|
9
|
+
latitude: f64
|
|
10
|
+
longitude: f64
|
|
11
|
+
|
|
12
|
+
constructor(latitude: f64, longitude: f64) {
|
|
13
|
+
this.latitude = latitude
|
|
14
|
+
this.longitude = longitude
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toJSON(): string {
|
|
18
|
+
return `{"latitude":${this.latitude},"longitude":${this.longitude}}`
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Path-value pair for delta updates
|
|
24
|
+
*/
|
|
25
|
+
export class PathValue {
|
|
26
|
+
path: string
|
|
27
|
+
value: string // JSON-encoded value
|
|
28
|
+
|
|
29
|
+
constructor(path: string, value: string) {
|
|
30
|
+
this.path = path
|
|
31
|
+
this.value = value
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
toJSON(): string {
|
|
35
|
+
return `{"path":"${this.path}","value":${this.value}}`
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Delta update containing values
|
|
41
|
+
*
|
|
42
|
+
* Note: Plugins should NOT include source or timestamp when emitting deltas.
|
|
43
|
+
* The server automatically sets $source to the plugin ID and fills in
|
|
44
|
+
* timestamp with the current time.
|
|
45
|
+
*/
|
|
46
|
+
export class Update {
|
|
47
|
+
values: PathValue[]
|
|
48
|
+
|
|
49
|
+
constructor(values: PathValue[]) {
|
|
50
|
+
this.values = values
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
toJSON(): string {
|
|
54
|
+
let valuesJson = '['
|
|
55
|
+
for (let i = 0; i < this.values.length; i++) {
|
|
56
|
+
if (i > 0) valuesJson += ','
|
|
57
|
+
valuesJson += this.values[i].toJSON()
|
|
58
|
+
}
|
|
59
|
+
valuesJson += ']'
|
|
60
|
+
|
|
61
|
+
return `{"values":${valuesJson}}`
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Delta message with context and updates
|
|
67
|
+
*/
|
|
68
|
+
export class Delta {
|
|
69
|
+
context: string
|
|
70
|
+
updates: Update[]
|
|
71
|
+
|
|
72
|
+
constructor(context: string, updates: Update[]) {
|
|
73
|
+
this.context = context
|
|
74
|
+
this.updates = updates
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
toJSON(): string {
|
|
78
|
+
let updatesJson = '['
|
|
79
|
+
for (let i = 0; i < this.updates.length; i++) {
|
|
80
|
+
if (i > 0) updatesJson += ','
|
|
81
|
+
updatesJson += this.updates[i].toJSON()
|
|
82
|
+
}
|
|
83
|
+
updatesJson += ']'
|
|
84
|
+
|
|
85
|
+
return `{"context":"${this.context}","updates":${updatesJson}}`
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Notification state
|
|
91
|
+
*/
|
|
92
|
+
export enum NotificationState {
|
|
93
|
+
normal = 0,
|
|
94
|
+
alert = 1,
|
|
95
|
+
warn = 2,
|
|
96
|
+
alarm = 3,
|
|
97
|
+
emergency = 4
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Notification method
|
|
102
|
+
*/
|
|
103
|
+
export enum NotificationMethod {
|
|
104
|
+
visual = 0,
|
|
105
|
+
sound = 1
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Signal K notification
|
|
110
|
+
*/
|
|
111
|
+
export class Notification {
|
|
112
|
+
state: NotificationState
|
|
113
|
+
method: NotificationMethod[]
|
|
114
|
+
message: string
|
|
115
|
+
|
|
116
|
+
constructor(state: NotificationState, message: string) {
|
|
117
|
+
this.state = state
|
|
118
|
+
this.method = [NotificationMethod.visual]
|
|
119
|
+
this.message = message
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
toJSON(): string {
|
|
123
|
+
let methodStr = '['
|
|
124
|
+
for (let i = 0; i < this.method.length; i++) {
|
|
125
|
+
if (i > 0) methodStr += ','
|
|
126
|
+
methodStr += this.method[i] == NotificationMethod.visual ? '"visual"' : '"sound"'
|
|
127
|
+
}
|
|
128
|
+
methodStr += ']'
|
|
129
|
+
|
|
130
|
+
let stateStr = 'normal'
|
|
131
|
+
if (this.state == NotificationState.alert) stateStr = 'alert'
|
|
132
|
+
else if (this.state == NotificationState.warn) stateStr = 'warn'
|
|
133
|
+
else if (this.state == NotificationState.alarm) stateStr = 'alarm'
|
|
134
|
+
else if (this.state == NotificationState.emergency) stateStr = 'emergency'
|
|
135
|
+
|
|
136
|
+
return `{"state":"${stateStr}","method":${methodStr},"message":"${this.message}"}`
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Helper to create a simple delta with single value
|
|
142
|
+
*
|
|
143
|
+
* The server automatically adds $source (plugin ID) and timestamp.
|
|
144
|
+
* Plugins should not include these fields.
|
|
145
|
+
*
|
|
146
|
+
* @param path Signal K path (e.g., 'environment.outside.temperature')
|
|
147
|
+
* @param value JSON-encoded value (e.g., '288.15' or '{"latitude":60,"longitude":24}')
|
|
148
|
+
*/
|
|
149
|
+
export function createSimpleDelta(path: string, value: string): Delta {
|
|
150
|
+
const pathValue = new PathValue(path, value)
|
|
151
|
+
const update = new Update([pathValue])
|
|
152
|
+
return new Delta('vessels.self', [update])
|
|
153
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** Exported memory */
|
|
2
|
+
export declare const memory: WebAssembly.Memory;
|
|
3
|
+
/** assembly/signalk/NotificationState */
|
|
4
|
+
export declare enum NotificationState {
|
|
5
|
+
/** @type `i32` */
|
|
6
|
+
normal,
|
|
7
|
+
/** @type `i32` */
|
|
8
|
+
alert,
|
|
9
|
+
/** @type `i32` */
|
|
10
|
+
warn,
|
|
11
|
+
/** @type `i32` */
|
|
12
|
+
alarm,
|
|
13
|
+
/** @type `i32` */
|
|
14
|
+
emergency,
|
|
15
|
+
}
|
|
16
|
+
/** assembly/signalk/NotificationMethod */
|
|
17
|
+
export declare enum NotificationMethod {
|
|
18
|
+
/** @type `i32` */
|
|
19
|
+
visual,
|
|
20
|
+
/** @type `i32` */
|
|
21
|
+
sound,
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* assembly/signalk/createSimpleDelta
|
|
25
|
+
* @param path `~lib/string/String`
|
|
26
|
+
* @param value `~lib/string/String`
|
|
27
|
+
* @returns `assembly/signalk/Delta`
|
|
28
|
+
*/
|
|
29
|
+
export declare function createSimpleDelta(path: string, value: string): __Internref4;
|
|
30
|
+
/** assembly/api/SK_VERSION_V1 */
|
|
31
|
+
export declare const SK_VERSION_V1: {
|
|
32
|
+
/** @type `i32` */
|
|
33
|
+
get value(): number
|
|
34
|
+
};
|
|
35
|
+
/** assembly/api/SK_VERSION_V2 */
|
|
36
|
+
export declare const SK_VERSION_V2: {
|
|
37
|
+
/** @type `i32` */
|
|
38
|
+
get value(): number
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* assembly/api/emit
|
|
42
|
+
* @param delta `assembly/signalk/Delta`
|
|
43
|
+
* @param skVersion `i32`
|
|
44
|
+
*/
|
|
45
|
+
export declare function emit(delta: __Internref4, skVersion?: number): void;
|
|
46
|
+
/**
|
|
47
|
+
* assembly/api/setStatus
|
|
48
|
+
* @param message `~lib/string/String`
|
|
49
|
+
*/
|
|
50
|
+
export declare function setStatus(message: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* assembly/api/setError
|
|
53
|
+
* @param message `~lib/string/String`
|
|
54
|
+
*/
|
|
55
|
+
export declare function setError(message: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* assembly/api/debug
|
|
58
|
+
* @param message `~lib/string/String`
|
|
59
|
+
*/
|
|
60
|
+
export declare function debug(message: string): void;
|
|
61
|
+
/**
|
|
62
|
+
* assembly/api/getSelfPath
|
|
63
|
+
* @param path `~lib/string/String`
|
|
64
|
+
* @returns `~lib/string/String | null`
|
|
65
|
+
*/
|
|
66
|
+
export declare function getSelfPath(path: string): string | null;
|
|
67
|
+
/**
|
|
68
|
+
* assembly/api/getPath
|
|
69
|
+
* @param path `~lib/string/String`
|
|
70
|
+
* @returns `~lib/string/String | null`
|
|
71
|
+
*/
|
|
72
|
+
export declare function getPath(path: string): string | null;
|
|
73
|
+
/**
|
|
74
|
+
* assembly/api/readConfig
|
|
75
|
+
* @returns `~lib/string/String`
|
|
76
|
+
*/
|
|
77
|
+
export declare function readConfig(): string;
|
|
78
|
+
/**
|
|
79
|
+
* assembly/api/saveConfig
|
|
80
|
+
* @param configJson `~lib/string/String`
|
|
81
|
+
* @returns `i32`
|
|
82
|
+
*/
|
|
83
|
+
export declare function saveConfig(configJson: string): number;
|
|
84
|
+
/**
|
|
85
|
+
* assembly/network/hasNetworkCapability
|
|
86
|
+
* @returns `bool`
|
|
87
|
+
*/
|
|
88
|
+
export declare function hasNetworkCapability(): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* assembly/resources/registerResourceProvider
|
|
91
|
+
* @param resourceType `~lib/string/String`
|
|
92
|
+
* @returns `bool`
|
|
93
|
+
*/
|
|
94
|
+
export declare function registerResourceProvider(resourceType: string): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* assembly/resources/hasResourceProviderCapability
|
|
97
|
+
* @returns `bool`
|
|
98
|
+
*/
|
|
99
|
+
export declare function hasResourceProviderCapability(): boolean;
|
|
100
|
+
/** assembly/signalk/Delta */
|
|
101
|
+
declare class __Internref4 extends Number {
|
|
102
|
+
private __nominal4: symbol;
|
|
103
|
+
private __nominal0: symbol;
|
|
104
|
+
}
|
package/build/plugin.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
async function instantiate(module, imports = {}) {
|
|
2
|
+
const adaptedImports = {
|
|
3
|
+
env: Object.assign(Object.create(globalThis), imports.env || {}, {
|
|
4
|
+
sk_handle_message(deltaPtr, deltaLen, version) {
|
|
5
|
+
// assembly/api/sk_handle_message_ffi(usize, usize, i32) => void
|
|
6
|
+
deltaPtr = deltaPtr >>> 0;
|
|
7
|
+
deltaLen = deltaLen >>> 0;
|
|
8
|
+
sk_handle_message(deltaPtr, deltaLen, version);
|
|
9
|
+
},
|
|
10
|
+
sk_set_status(msgPtr, msgLen) {
|
|
11
|
+
// assembly/api/sk_set_status_ffi(usize, usize) => void
|
|
12
|
+
msgPtr = msgPtr >>> 0;
|
|
13
|
+
msgLen = msgLen >>> 0;
|
|
14
|
+
sk_set_status(msgPtr, msgLen);
|
|
15
|
+
},
|
|
16
|
+
sk_set_error(msgPtr, msgLen) {
|
|
17
|
+
// assembly/api/sk_set_error_ffi(usize, usize) => void
|
|
18
|
+
msgPtr = msgPtr >>> 0;
|
|
19
|
+
msgLen = msgLen >>> 0;
|
|
20
|
+
sk_set_error(msgPtr, msgLen);
|
|
21
|
+
},
|
|
22
|
+
sk_debug(msgPtr, msgLen) {
|
|
23
|
+
// assembly/api/sk_debug_ffi(usize, usize) => void
|
|
24
|
+
msgPtr = msgPtr >>> 0;
|
|
25
|
+
msgLen = msgLen >>> 0;
|
|
26
|
+
sk_debug(msgPtr, msgLen);
|
|
27
|
+
},
|
|
28
|
+
sk_get_self_path(pathPtr, pathLen, bufPtr, bufLen) {
|
|
29
|
+
// assembly/api/sk_get_self_path_ffi(usize, usize, usize, usize) => i32
|
|
30
|
+
pathPtr = pathPtr >>> 0;
|
|
31
|
+
pathLen = pathLen >>> 0;
|
|
32
|
+
bufPtr = bufPtr >>> 0;
|
|
33
|
+
bufLen = bufLen >>> 0;
|
|
34
|
+
return sk_get_self_path(pathPtr, pathLen, bufPtr, bufLen);
|
|
35
|
+
},
|
|
36
|
+
sk_get_path(pathPtr, pathLen, bufPtr, bufLen) {
|
|
37
|
+
// assembly/api/sk_get_path_ffi(usize, usize, usize, usize) => i32
|
|
38
|
+
pathPtr = pathPtr >>> 0;
|
|
39
|
+
pathLen = pathLen >>> 0;
|
|
40
|
+
bufPtr = bufPtr >>> 0;
|
|
41
|
+
bufLen = bufLen >>> 0;
|
|
42
|
+
return sk_get_path(pathPtr, pathLen, bufPtr, bufLen);
|
|
43
|
+
},
|
|
44
|
+
sk_read_config(bufPtr, bufLen) {
|
|
45
|
+
// assembly/api/sk_read_config_ffi(usize, usize) => i32
|
|
46
|
+
bufPtr = bufPtr >>> 0;
|
|
47
|
+
bufLen = bufLen >>> 0;
|
|
48
|
+
return sk_read_config(bufPtr, bufLen);
|
|
49
|
+
},
|
|
50
|
+
sk_save_config(configPtr, configLen) {
|
|
51
|
+
// assembly/api/sk_save_config_ffi(usize, usize) => i32
|
|
52
|
+
configPtr = configPtr >>> 0;
|
|
53
|
+
configLen = configLen >>> 0;
|
|
54
|
+
return sk_save_config(configPtr, configLen);
|
|
55
|
+
},
|
|
56
|
+
sk_has_capability(capPtr, capLen) {
|
|
57
|
+
// assembly/network/sk_has_capability_ffi(usize, usize) => i32
|
|
58
|
+
capPtr = capPtr >>> 0;
|
|
59
|
+
capLen = capLen >>> 0;
|
|
60
|
+
return sk_has_capability(capPtr, capLen);
|
|
61
|
+
},
|
|
62
|
+
sk_register_resource_provider(typePtr, typeLen) {
|
|
63
|
+
// assembly/resources/sk_register_resource_provider_ffi(usize, usize) => i32
|
|
64
|
+
typePtr = typePtr >>> 0;
|
|
65
|
+
typeLen = typeLen >>> 0;
|
|
66
|
+
return sk_register_resource_provider(typePtr, typeLen);
|
|
67
|
+
},
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
const { exports } = await WebAssembly.instantiate(module, adaptedImports);
|
|
71
|
+
const memory = exports.memory || imports.env.memory;
|
|
72
|
+
const adaptedExports = Object.setPrototypeOf({
|
|
73
|
+
NotificationState: (values => (
|
|
74
|
+
// assembly/signalk/NotificationState
|
|
75
|
+
values[values.normal = exports["NotificationState.normal"].valueOf()] = "normal",
|
|
76
|
+
values[values.alert = exports["NotificationState.alert"].valueOf()] = "alert",
|
|
77
|
+
values[values.warn = exports["NotificationState.warn"].valueOf()] = "warn",
|
|
78
|
+
values[values.alarm = exports["NotificationState.alarm"].valueOf()] = "alarm",
|
|
79
|
+
values[values.emergency = exports["NotificationState.emergency"].valueOf()] = "emergency",
|
|
80
|
+
values
|
|
81
|
+
))({}),
|
|
82
|
+
NotificationMethod: (values => (
|
|
83
|
+
// assembly/signalk/NotificationMethod
|
|
84
|
+
values[values.visual = exports["NotificationMethod.visual"].valueOf()] = "visual",
|
|
85
|
+
values[values.sound = exports["NotificationMethod.sound"].valueOf()] = "sound",
|
|
86
|
+
values
|
|
87
|
+
))({}),
|
|
88
|
+
createSimpleDelta(path, value) {
|
|
89
|
+
// assembly/signalk/createSimpleDelta(~lib/string/String, ~lib/string/String) => assembly/signalk/Delta
|
|
90
|
+
path = __retain(__lowerString(path) || __notnull());
|
|
91
|
+
value = __lowerString(value) || __notnull();
|
|
92
|
+
try {
|
|
93
|
+
return __liftInternref(exports.createSimpleDelta(path, value) >>> 0);
|
|
94
|
+
} finally {
|
|
95
|
+
__release(path);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
emit(delta, skVersion) {
|
|
99
|
+
// assembly/api/emit(assembly/signalk/Delta, i32?) => void
|
|
100
|
+
delta = __lowerInternref(delta) || __notnull();
|
|
101
|
+
exports.__setArgumentsLength(arguments.length);
|
|
102
|
+
exports.emit(delta, skVersion);
|
|
103
|
+
},
|
|
104
|
+
setStatus(message) {
|
|
105
|
+
// assembly/api/setStatus(~lib/string/String) => void
|
|
106
|
+
message = __lowerString(message) || __notnull();
|
|
107
|
+
exports.setStatus(message);
|
|
108
|
+
},
|
|
109
|
+
setError(message) {
|
|
110
|
+
// assembly/api/setError(~lib/string/String) => void
|
|
111
|
+
message = __lowerString(message) || __notnull();
|
|
112
|
+
exports.setError(message);
|
|
113
|
+
},
|
|
114
|
+
debug(message) {
|
|
115
|
+
// assembly/api/debug(~lib/string/String) => void
|
|
116
|
+
message = __lowerString(message) || __notnull();
|
|
117
|
+
exports.debug(message);
|
|
118
|
+
},
|
|
119
|
+
getSelfPath(path) {
|
|
120
|
+
// assembly/api/getSelfPath(~lib/string/String) => ~lib/string/String | null
|
|
121
|
+
path = __lowerString(path) || __notnull();
|
|
122
|
+
return __liftString(exports.getSelfPath(path) >>> 0);
|
|
123
|
+
},
|
|
124
|
+
getPath(path) {
|
|
125
|
+
// assembly/api/getPath(~lib/string/String) => ~lib/string/String | null
|
|
126
|
+
path = __lowerString(path) || __notnull();
|
|
127
|
+
return __liftString(exports.getPath(path) >>> 0);
|
|
128
|
+
},
|
|
129
|
+
readConfig() {
|
|
130
|
+
// assembly/api/readConfig() => ~lib/string/String
|
|
131
|
+
return __liftString(exports.readConfig() >>> 0);
|
|
132
|
+
},
|
|
133
|
+
saveConfig(configJson) {
|
|
134
|
+
// assembly/api/saveConfig(~lib/string/String) => i32
|
|
135
|
+
configJson = __lowerString(configJson) || __notnull();
|
|
136
|
+
return exports.saveConfig(configJson);
|
|
137
|
+
},
|
|
138
|
+
hasNetworkCapability() {
|
|
139
|
+
// assembly/network/hasNetworkCapability() => bool
|
|
140
|
+
return exports.hasNetworkCapability() != 0;
|
|
141
|
+
},
|
|
142
|
+
registerResourceProvider(resourceType) {
|
|
143
|
+
// assembly/resources/registerResourceProvider(~lib/string/String) => bool
|
|
144
|
+
resourceType = __lowerString(resourceType) || __notnull();
|
|
145
|
+
return exports.registerResourceProvider(resourceType) != 0;
|
|
146
|
+
},
|
|
147
|
+
hasResourceProviderCapability() {
|
|
148
|
+
// assembly/resources/hasResourceProviderCapability() => bool
|
|
149
|
+
return exports.hasResourceProviderCapability() != 0;
|
|
150
|
+
},
|
|
151
|
+
}, exports);
|
|
152
|
+
function __liftString(pointer) {
|
|
153
|
+
if (!pointer) return null;
|
|
154
|
+
const
|
|
155
|
+
end = pointer + new Uint32Array(memory.buffer)[pointer - 4 >>> 2] >>> 1,
|
|
156
|
+
memoryU16 = new Uint16Array(memory.buffer);
|
|
157
|
+
let
|
|
158
|
+
start = pointer >>> 1,
|
|
159
|
+
string = "";
|
|
160
|
+
while (end - start > 1024) string += String.fromCharCode(...memoryU16.subarray(start, start += 1024));
|
|
161
|
+
return string + String.fromCharCode(...memoryU16.subarray(start, end));
|
|
162
|
+
}
|
|
163
|
+
function __lowerString(value) {
|
|
164
|
+
if (value == null) return 0;
|
|
165
|
+
const
|
|
166
|
+
length = value.length,
|
|
167
|
+
pointer = exports.__new(length << 1, 2) >>> 0,
|
|
168
|
+
memoryU16 = new Uint16Array(memory.buffer);
|
|
169
|
+
for (let i = 0; i < length; ++i) memoryU16[(pointer >>> 1) + i] = value.charCodeAt(i);
|
|
170
|
+
return pointer;
|
|
171
|
+
}
|
|
172
|
+
class Internref extends Number {}
|
|
173
|
+
const registry = new FinalizationRegistry(__release);
|
|
174
|
+
function __liftInternref(pointer) {
|
|
175
|
+
if (!pointer) return null;
|
|
176
|
+
const sentinel = new Internref(__retain(pointer));
|
|
177
|
+
registry.register(sentinel, pointer);
|
|
178
|
+
return sentinel;
|
|
179
|
+
}
|
|
180
|
+
function __lowerInternref(value) {
|
|
181
|
+
if (value == null) return 0;
|
|
182
|
+
if (value instanceof Internref) return value.valueOf();
|
|
183
|
+
throw TypeError("internref expected");
|
|
184
|
+
}
|
|
185
|
+
const refcounts = new Map();
|
|
186
|
+
function __retain(pointer) {
|
|
187
|
+
if (pointer) {
|
|
188
|
+
const refcount = refcounts.get(pointer);
|
|
189
|
+
if (refcount) refcounts.set(pointer, refcount + 1);
|
|
190
|
+
else refcounts.set(exports.__pin(pointer), 1);
|
|
191
|
+
}
|
|
192
|
+
return pointer;
|
|
193
|
+
}
|
|
194
|
+
function __release(pointer) {
|
|
195
|
+
if (pointer) {
|
|
196
|
+
const refcount = refcounts.get(pointer);
|
|
197
|
+
if (refcount === 1) exports.__unpin(pointer), refcounts.delete(pointer);
|
|
198
|
+
else if (refcount) refcounts.set(pointer, refcount - 1);
|
|
199
|
+
else throw Error(`invalid refcount '${refcount}' for reference '${pointer}'`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function __notnull() {
|
|
203
|
+
throw TypeError("value must not be null");
|
|
204
|
+
}
|
|
205
|
+
return adaptedExports;
|
|
206
|
+
}
|
|
207
|
+
export const {
|
|
208
|
+
memory,
|
|
209
|
+
NotificationState,
|
|
210
|
+
NotificationMethod,
|
|
211
|
+
createSimpleDelta,
|
|
212
|
+
SK_VERSION_V1,
|
|
213
|
+
SK_VERSION_V2,
|
|
214
|
+
emit,
|
|
215
|
+
setStatus,
|
|
216
|
+
setError,
|
|
217
|
+
debug,
|
|
218
|
+
getSelfPath,
|
|
219
|
+
getPath,
|
|
220
|
+
readConfig,
|
|
221
|
+
saveConfig,
|
|
222
|
+
hasNetworkCapability,
|
|
223
|
+
registerResourceProvider,
|
|
224
|
+
hasResourceProviderCapability,
|
|
225
|
+
} = await (async url => instantiate(
|
|
226
|
+
await (async () => {
|
|
227
|
+
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
|
|
228
|
+
if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
|
|
229
|
+
else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
|
|
230
|
+
})(), {
|
|
231
|
+
}
|
|
232
|
+
))(new URL("plugin.wasm", import.meta.url));
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@signalk/assemblyscript-plugin-sdk",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "AssemblyScript SDK for developing Signal K WASM plugins",
|
|
5
|
+
"main": "assembly/index.ts",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./assembly/index.ts",
|
|
10
|
+
"types": "./index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./assembly": {
|
|
13
|
+
"import": "./assembly/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"./assembly/*": {
|
|
16
|
+
"import": "./assembly/*.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"ascMain": "assembly/index.ts",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"signalk",
|
|
22
|
+
"assemblyscript",
|
|
23
|
+
"wasm",
|
|
24
|
+
"plugin",
|
|
25
|
+
"sdk"
|
|
26
|
+
],
|
|
27
|
+
"author": "Signal K",
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"scripts": {
|
|
30
|
+
"asbuild:debug": "asc assembly/index.ts --target debug --disableWarning 235",
|
|
31
|
+
"asbuild:release": "asc assembly/index.ts --target release --disableWarning 235",
|
|
32
|
+
"build": "npm run asbuild:release",
|
|
33
|
+
"test": "npm run asbuild:debug"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"assemblyscript": "^0.27.0"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"as-wasi": "^0.6.0",
|
|
40
|
+
"as-fetch": "^2.1.4",
|
|
41
|
+
"assemblyscript-json": "^1.1.0"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/SignalK/signalk-server.git",
|
|
46
|
+
"directory": "packages/assemblyscript-plugin-sdk"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|