UNPKG

sksim

Version:

Signal K data stream generator.

123 lines (118 loc) 3.68 kB
"use strict"; /* * Copyright 2019 Adrian Panazzolo <panaaj@hotmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const CONFIG_SCHEMA = { properties: {}, }; const CONFIG_UISCHEMA = {}; module.exports = (server) => { // ****************************************** let config; // ** applied configuration settings let subscriptions = []; // ******** REQUIRED PLUGIN DEFINITION ******* let plugin = { id: "sksim", name: "SKsim: Data stream generator.", schema: () => CONFIG_SCHEMA, uiSchema: () => CONFIG_UISCHEMA, start: (options, restart) => { doStartup(options, restart); }, stop: () => { doShutdown(); }, }; // ************************************ const doStartup = (options, restart) => { config = options; try { server.debug("** starting up **"); subscriptions.push( // ** handle navigation.position. calc bearingTrue server.streambundle.getSelfBus("navigation.position").onValue((v) => { server.debug(`*** ${JSON.stringify(v.value)} ***`); }) ); initRoutes(); let msg = "Started"; if (typeof server.setPluginStatus === "function") { server.setPluginStatus(msg); } else { server.setProviderStatus(msg); } } catch (err) { let msg = "Started with errors!"; if (typeof server.setPluginError === "function") { server.setPluginError(msg); } else { server.setProviderError(msg); } server.error("** EXCEPTION: **"); server.error(err.stack); return err; } }; const doShutdown = () => { server.debug("** shutting down **"); subscriptions.forEach((b) => b()); let msg = `Stopped`; if (typeof server.setPluginStatus === "function") { server.setPluginStatus(msg); } else { server.setProviderStatus(msg); } }; const sendEndpoint = `/skServer/plugins/${plugin.id}/api`; const initRoutes = () => { server.debug(`** Registering HTTP paths **`); // ** accept submitted resources ** server.post(`${sendEndpoint}`, (req, res) => { if (!processUIPost(req.body)) { res.status(400).json({ error: true, message: `${plugin.id}: Invalid request data received!`, }); } else { res.status(200).json({ error: false, message: `${plugin.id}: OK` }); } }); // ** return plugin information ** server.get(`/signalk/${plugin.id}/about`, (req, res) => { res.status(200).json({ id: plugin.id, name: plugin.name, version: plugin.version, description: plugin.description, url: `${sendEndpoint}`, }); }); }; // ** process posted data ** const processUIPost = (data) => { if (!Array.isArray(data)) { server.debug(`** ERROR: ** Invalid payload data!!`); return false; } let idx = 0; while (idx < data.length) { const v = data.slice(idx, idx + 2); server.handleMessage(null, { updates: [{ values: v }] }); idx += 2; } return true; }; return plugin; };