Skip to content

Commit

Permalink
Update format of weather resource entries
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed Jun 4, 2023
1 parent bcc18c3 commit 35af684
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 214 deletions.
29 changes: 17 additions & 12 deletions helper/alarms/alarms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ export const initAlarms = (app: FreeboardHelperApp, id: string) => {

initAlarmEndpoints();

/*
curl -H "Content-Type: application/json" -X PUT -d '{"message": "Man Overboard!"}'\
http://localhost:3000/signalk/v2/api/notifications/mob
curl -H "Content-Type: application/json" -X DELETE \
http://localhost:3000/signalk/v2/api/notifications/mob
*/
};
}

const initAlarmEndpoints = () => {
server.debug(`** Registering Alarm Action API endpoint(s) **`);
Expand All @@ -67,13 +60,15 @@ const initAlarmEndpoints = () => {
return;
}
try {
const msg = req.body.message
? req.body.message
: (req.params.alarmType as string);

const r = handlePutAlarmState(
'vessels.self',
`notifications.${req.params.alarmType}`,
{
message: req.body.message
? req.body.message
: (req.params.alarmType as string),
message: msg,
method: [ALARM_METHOD.sound, ALARM_METHOD.visual],
state: ALARM_STATE.emergency
}
Expand Down Expand Up @@ -145,7 +140,7 @@ const handlePutAlarmState = (
if (value) {
noti = new Notification(
alarmType,
value.message ?? '',
buildAlarmMessage(value.message, alarmType),
value.state ?? null,
value.method ?? null
);
Expand All @@ -170,6 +165,16 @@ const handlePutAlarmState = (
}
};

const buildAlarmMessage = (message: string, alarmType?: string): string => {
let msgAttrib = '';
if (['mob', 'sinking'].includes(alarmType)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pos: any = server.getSelfPath('navigation.position');
msgAttrib = pos ? JSON.stringify(pos?.value) : '';
}
return `${message}\n\r${msgAttrib}`;
};

// ** send notification delta message **
const emitNotification = (n: Notification | DeltaMessage) => {
const msg = n instanceof Notification ? n.message : n;
Expand Down
22 changes: 17 additions & 5 deletions helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ const CONFIG_UISCHEMA = {
};

interface SETTINGS {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
alarms: { [key: string]: any };
weather: WEATHER_CONFIG;
pypilot: PYPILOT_CONFIG;
}

interface OpenApiPlugin extends Plugin {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getOpenApi: () => any;
}

Expand All @@ -126,11 +128,14 @@ export interface FreeboardHelperApp
setProviderStatus: (providerId: string, status?: string) => void;
setProviderError: (providerId: string, status?: string) => void;
getSelfPath: (path: string) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
savePluginOptions: (options: any, callback: () => void) => void;
config: { configPath: string };

// eslint-disable-next-line @typescript-eslint/no-explicit-any
handleMessage: (id: string | null, msg: any, version?: string) => void;
streambundle: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getSelfBus: (path: string | void) => any;
};
registerPutHandler: (
Expand All @@ -139,6 +144,7 @@ export interface FreeboardHelperApp
callback: (
context: string,
path: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
actionResultCallback: (actionResult: ActionResult) => void
) => ActionResult
Expand Down Expand Up @@ -169,6 +175,7 @@ module.exports = (server: FreeboardHelperApp): OpenApiPlugin => {
name: 'Freeboard-SK',
schema: () => CONFIG_SCHEMA,
uiSchema: () => CONFIG_UISCHEMA,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
start: (settings: any) => {
doStartup(settings);
},
Expand Down Expand Up @@ -219,19 +226,22 @@ module.exports = (server: FreeboardHelperApp): OpenApiPlugin => {
initAlarms(server, plugin.id);
}

const result = registerProvider('weather');
const msg = `Started - ${
result.length !== 0 ? `${result} not registered!` : 'Providing: weather'
}`;

let msg = '';
if (settings.weather.enable) {
const result = registerProvider('weather');
msg = `Started - ${
result.length !== 0
? `${result} not registered!`
: 'Providing: weather'
}`;
initWeather(server, plugin.id, settings.weather);
}
if (settings.pypilot.enable) {
initPyPilot(server, plugin.id, settings.pypilot);
}

server.setPluginStatus(msg);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
const msg = 'Started with errors!';
server.setPluginError(msg);
Expand All @@ -256,12 +266,14 @@ module.exports = (server: FreeboardHelperApp): OpenApiPlugin => {
server.registerResourceProvider({
type: resType,
methods: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listResources: (params: object): any => {
return listWeather(params);
},
getResource: (path: string, property?: string) => {
return getWeather(path, property);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setResource: (id: string, value: any) => {
throw 'Not implemented!';
},
Expand Down
Loading

0 comments on commit 35af684

Please sign in to comment.