Gregw - I'm trying to write my first plugin and...
I'm trying to write my first plugin and I'm struggling with understanding resources from the plugin point of view. I can see the resources API that is available via HTTP GET/POST methods, but I cannot work out how to access it via the app API within the plugin. If I subscribe to context: 'vessels.' + app.selfId with path: 'resources.waypoints.*', then I see deltas when a waypoint is edited (eg in FreeboardSK) and I see it has a path like:
resources.waypoints.602ee562-c6dc-42c3-a775-512597063ca4
with meaningful data. But if I then do an app.getPath or app.getSelfPath for that path, I get nothing. Yet the edited waypoint is now visible in the databrowser with my vessel context selected? And obviously FreeboardSK can see all the waypoints. Do I need to do GET requests to my own server from the plugin or is there a plugin API to get these resources. If so, where is it document... or at least where is the code?21 Replies
Resources API is accessible to plugins via the following methods
https://demo.signalk.org/documentation/_signalk/server-api/ResourcesApi.html
The documentation covers the rest APIs.
That looks like what I want. Sorry not sure how I missed that whole section at the bottom of the doco.... but now I need a registered provider for waypoints... obviously there is one in the server, but I'm guessing my plugin need to select/activate it in some way? Sorry, these first steps are the hard ones
The documentation outlines how the plugin can register as a provider.
The server comes with one that will service routes, waypoints, notes and regions.
There can be multiple providers for the same resource type e.g. active captain plugin is a notes provider and the API is smart enough to handle this.
See the section on registering as a provider.
Here is a link to the relevant section of the built-in resource provider plugin...https://github.com/SignalK/signalk-server/blob/master/packages%2Fresources-provider-plugin%2Fsrc%2Findex.ts#L237
@AdrianP But my plugin does not want to be a provider, it wants to use a provider already installed in the server. I think it is a timing issue. I just moved my code calling the resourcesApi from my start method to a method called by a delta subscription. It now works. So it is like the resourcesApi provider is not yet instantiated when my start method is called. Is there a way I can say my plugin depends on a resourceProvider plugin, and thus not be started until the proivider is?
There is currently no way to know when all plugins are initialised so calling other plugin methods in the startup function is risky.
There is a PR that should be merged soon that should address this.
It addresses the ability for a plugin to use the
getFeatures
method during startup to determine what plugins and APIs are available.So is there a well know item that I can subscribe to in start, that will always receive a delta after all the plugins are initialised? Or do I just need to try and backoff until a provider is there?
I.e. if I subscribe to the boats position, will I only get a delta once everything is started and the boat is moving?
If you subscribe to a path you will get a value whenever the value becomes available and after subsequent changes.
Clearly the server needs to have initialised the source of that data (data connection or plugin) to start receiving the values.
IS there any guarantee that all plugins are started before any data connections?
I don't believe so.
What specifically are you trying to achieve?
I'm writing a plugin to do start line stuff. I have configured the names of waypoints for the boat and pin. I want my plugin to look those up in the waypoints to find their true path, so I can subscribe and see if they change. I was doing that in start, but the resources provider is not started when my plugin is called. So rather than just sleep waiting until the provider is installed, I'm looking for some other event I can subscribe to that will tell me everything is up and running in the server and thus I can start doing my start line lookups and calculations
I guess I can just subscribe to the boats position. Each time I get a delta I can check if I have the line way points, if not I can try the API to see if a provider is available or not. That should work, at least to get me going. I need the boats position anyway
These paths might be of interest https://github.com/SignalK/specification/blob/master/schemas%2Fgroups%2Fnavigation.json#L267
Freeboard-SK recognises start line paths
I've seen that schema, but no plugins that set them. Hence I was having a go at it myself.
So my intention is for my plugin to publish some of those values in that schema
OK thanks for the info, I'm now getting waypoint data, so I think I have enough to get something working... thanks
Let me know how you go.
So far I've made a good start. I'm now setting the racing.startLinePort and racing.startLineStb from waypoints. Everything else is just trig from here :). Anybody interested can follow at https://github.com/gregw/signalk-racer
It would be good at some point to look at how the route schema may be extended to accommodate a race course... Again most likely just additional metadata.
One of the things I want my plugin to do is to calculate the TWA (and AWA if polars are available) of each leg of the course and to make the TWA of the next leg easily available. My intention was do add that as something under the racing object. I was wondering if having port/starboard roundings in the course, but I don't have an immediate need for that info, let alone a UI to enter it.
The twa and awa calculations should be easy enough if you reference a route with the course marks.... Mark roundings could be part of the route point metadata object.
Could make the editing part of Freeboard-SK
Progress. Line length and being calculated. However I don't appear to have the units right with my deltas, as they do not display and the lat/long are all sorts of wrong if I try. The DTL and line length both updates as the boat moves or if you move a mark. Once I have the units sorted out, I'll do line bias and laylines . After that, I'll look at doing a webapp to make it easier to set the names of the line marks and to list the course with TWA etc. However, weather forecast is OK, so I might go sailing instead of programming 🙂

The DTL line calculation is the perpendicular to the line distance, unless you our inside the triangle extending 45 degrees either side of the line, in which case the distance is the distance to the closest end of the mark. Is the schema the right place to document that? or is there a better way of getting text onto the plugin config page....
.... and geeeeeeze my javascript is rusty!!!!!
@AdrianP so how are paths outside of the standard schema treated? They appear to be handled ok? at what point is the json actually validated against the schema?
The schema is generally the way to do it.
It's kind of a cut down version of react forms... If you look at https://github.com/SignalK/freeboard-sk/blob/master/helper%2Findex.ts#L16 you can see how it's done in Freeboard-SK.
Paths you send are just emitted.
I don't believe there is any validation against the schema.
DOH! I'm and idiot... many of my functions are asynchronous... so I've been getting strange results as I've written all kinds of races. Restructuring for the good. This is fun... but slowwwww