Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send bp last observation #104

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/controller/ObservationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ var activeDevices = [];
var lastRequestData = {};
var logData = [];
var statusData = [];

// [{
// time: new Date(),
// data: {
// device_id: status
// }
// }]
var lastObservationData = {};

// start updating after 1 minutes of starting the middleware
let lastUpdatedToCare = new Date() - 59 * 60 * 1000;
Expand Down Expand Up @@ -112,6 +106,27 @@ const addLogData = (newData) => {
];
};

const updateLastObservationData = (flattenedObservations, skipEmpty = true) => {
flattenedObservations.forEach((observation) => {
const observationId =
observation.observation_id === "waveform"
? `waveform_${observation["wave-name"]}`
: observation.observation_id;

if (
skipEmpty &&
!observation.value &&
!observation.data &&
observation.status !== "final"
) {
return;
}

lastObservationData[observationId] ??= {};
lastObservationData[observationId][observation.device_id] = observation;
});
};

const updateObservationsToCare = async () => {
// console.log(dailyRoundTag() + "updateObservationsToCare called")
const now = new Date();
Expand Down Expand Up @@ -472,13 +487,31 @@ export class ObservationController {

const flattenedObservations = flattenObservations(observations);

updateLastObservationData(flattenedObservations);
this.latestObservation.set(flattenedObservations);

filterClients(req.wsInstance.getWss(), "/observations").forEach(
(client) => {
const filteredObservations = flattenedObservations?.filter(
(observation) => observation?.device_id === client?.params?.ip
);

if (
lastObservationData["blood-pressure"]?.[client?.params?.ip] &&
dayjs().diff(
dayjs(
lastObservationData["blood-pressure"]?.[client?.params?.ip][
"date-time"
]
),
"minutes"
) < 30
) {
filteredObservations?.push(
lastObservationData["blood-pressure"][client?.params?.ip]
);
}

if (filteredObservations.length) {
client.send(JSON.stringify(filteredObservations));
}
Expand Down
Loading