Skip to content

Commit

Permalink
remove login test for alrams dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed Jan 9, 2025
1 parent 5d1a182 commit ebee620
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 60 deletions.
95 changes: 49 additions & 46 deletions helper/alarms/alarms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export const initAlarms = (app: FreeboardHelperApp, id: string) => {

server.debug(`** initAlarms() **`);

if (server.registerPutHandler) {
if (server.registerActionHandler) {
server.debug(`** Registering Alarm Action Handler(s) **`);
STANDARD_ALARMS.forEach((i) => {
server.debug(`** Registering ${i} Handler **`);
server.registerPutHandler(
'vessels.self',
`notifications.${i}`,
handlePutAlarmState
handleV1PutRequest
);
});
}
Expand All @@ -54,7 +54,9 @@ const initAlarmEndpoints = () => {
server.post(
`${ALARM_API_PATH}/:alarmType`,
(req: Request, res: Response, next: NextFunction) => {
server.debug(`** POST ${ALARM_API_PATH}/${req.params.alarmType}`);
server.debug(
`** ${req.method} ${ALARM_API_PATH}/${req.params.alarmType}`
);
if (!STANDARD_ALARMS.includes(req.params.alarmType)) {
next();
return;
Expand All @@ -64,15 +66,17 @@ const initAlarmEndpoints = () => {
? req.body.message
: (req.params.alarmType as string);

const r = handlePutAlarmState(
const r = handleAlarm(
'vessels.self',
`notifications.${req.params.alarmType}` as Path,
{
message: msg,
method: [ALARM_METHOD.sound, ALARM_METHOD.visual],
state: ALARM_STATE.emergency
state: ALARM_STATE.emergency,
data: buildAlarmData()
}
);

res.status(200).json(r);
} catch (e) {
res.status(400).json({
Expand All @@ -86,7 +90,7 @@ const initAlarmEndpoints = () => {
server.post(
`${ALARM_API_PATH}/:alarmType/silence`,
(req: Request, res: Response) => {
server.debug(`** POST ${req.path}`);
server.debug(`** ${req.method} ${req.path}`);
if (!STANDARD_ALARMS.includes(req.params.alarmType)) {
res.status(200).json({
state: 'COMPLETED',
Expand All @@ -102,7 +106,7 @@ const initAlarmEndpoints = () => {
if (al.value.method && al.value.method.includes('sound')) {
server.debug('Alarm has sound... silence!!!');
al.value.method = al.value.method.filter((i) => i !== 'sound');
const r = handlePutAlarmState(
const r = handleAlarm(
'vessels.self',
`notifications.${req.params.alarmType}` as Path,
al.value
Expand Down Expand Up @@ -133,13 +137,15 @@ const initAlarmEndpoints = () => {
server.delete(
`${ALARM_API_PATH}/:alarmType`,
(req: Request, res: Response, next: NextFunction) => {
server.debug(`** DELETE ${ALARM_API_PATH}/${req.params.alarmType}`);
server.debug(
`** ${req.method} ${ALARM_API_PATH}/${req.params.alarmType}`
);
if (!STANDARD_ALARMS.includes(req.params.alarmType)) {
next();
return;
}
try {
const r = handlePutAlarmState(
const r = handleAlarm(
'vessels.self',
`notifications.${req.params.alarmType}` as Path,
{
Expand All @@ -160,18 +166,43 @@ const initAlarmEndpoints = () => {
);
};

const handlePutAlarmState = (
const handleV1PutRequest = (
context: string,
path: Path,
value: any,
cb: (actionResult: any) => void
) => {
cb(handleAlarm(
context,
path,
value
));
}

const buildAlarmData = () => {
const pos: { value: Position } = server.getSelfPath('navigation.position');
return {
position: pos ? pos.value : null,
timestamp: new Date().toISOString()
};
};

const handleAlarm = (
context: string,
path: Path,
value: {
message: string;
state: ALARM_STATE;
method: ALARM_METHOD[];
data?: {
position: Position;
timestamp: string;
};
}
) => {
server.debug(context);
server.debug(path);
server.debug(JSON.stringify(value));
server.debug(`context: ${context}`);
server.debug(`path: ${path}`);
server.debug(`value: ${JSON.stringify(value)}`);
if (!path) {
server.debug('Error: no path provided!');
return {
Expand All @@ -184,32 +215,13 @@ const handlePutAlarmState = (

const pa = path.split('.');
const alarmType = pa[pa.length - 1];
server.debug(JSON.stringify(alarmType));
let noti: PathValue;
if (value) {
const alm = value.state === ALARM_STATE.normal ? null : buildAlarmData();
noti = {
path: `notifications.${alarmType}` as Path,
value: {
state: value.state ?? null,
method: value.method ?? null,
message: value.message
}
};
if (alm && alm.data) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(noti.value as any).data = alm.data;
}
} else {
noti = {
path,
value: null
};
}
server.debug(`alarmType: ${JSON.stringify(alarmType)}`);
if (STANDARD_ALARMS.includes(alarmType)) {
// ** send delta **
server.debug(`****** Sending Delta (Std Alarm Notification): ******`);
emitNotification(noti);
emitNotification({
path: path,
value: value ?? null
});
return { state: 'COMPLETED', resultStatus: 200, statusCode: 200 };
} else {
return {
Expand All @@ -221,15 +233,6 @@ const handlePutAlarmState = (
}
};

const buildAlarmData = () => {
const pos: { value: Position } = server.getSelfPath('navigation.position');
return {
data: {
position: pos ? pos.value : null
}
};
};

// ** send notification delta message **
const emitNotification = (msg: PathValue) => {
const delta = {
Expand Down
20 changes: 6 additions & 14 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,18 +1137,10 @@ export class AppComponent {
// ********** TOOLBAR ACTIONS **********

public openAlarmsDialog() {
if (this.app.data.loginRequired && !this.app.data.loggedIn) {
this.showLogin(null, false, false).subscribe((r) => {
if (r) {
this.openAlarmsDialog();
}
});
} else {
this.dialog
.open(AlarmsDialog, { disableClose: true })
.afterClosed()
.subscribe(() => this.focusMap());
}
this.dialog
.open(AlarmsDialog, { disableClose: true })
.afterClosed()
.subscribe(() => this.focusMap());
}

public toggleMoveMap(exit = false) {
Expand Down Expand Up @@ -1862,8 +1854,8 @@ export class AppComponent {
}

this.display.navDataPanel.apModeText = this.app.data.vessels.self.autopilot
.enabled
? 'Autopilot: ' + this.app.data.vessels.self.autopilot.mode
.default
? 'Autopilot: ' + this.app.data.vessels.self.autopilot.default
: '';
}
}

0 comments on commit ebee620

Please sign in to comment.