2022-03-16 16:35:09 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
import { debounce } from "lodash";
|
2022-03-16 16:35:09 +00:00
|
|
|
import {
|
|
|
|
Beacon,
|
2022-04-08 08:53:06 +00:00
|
|
|
BeaconIdentifier,
|
2022-03-16 16:35:09 +00:00
|
|
|
BeaconEvent,
|
|
|
|
MatrixEvent,
|
2022-04-11 08:28:43 +00:00
|
|
|
RelationType,
|
2022-03-16 16:35:09 +00:00
|
|
|
Room,
|
2022-03-29 16:18:34 +00:00
|
|
|
RoomMember,
|
|
|
|
RoomState,
|
|
|
|
RoomStateEvent,
|
2022-03-16 16:35:09 +00:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-03-18 13:38:41 +00:00
|
|
|
import {
|
2022-03-28 10:48:38 +00:00
|
|
|
BeaconInfoState, makeBeaconContent, makeBeaconInfoContent,
|
2022-03-18 13:38:41 +00:00
|
|
|
} from "matrix-js-sdk/src/content-helpers";
|
2022-03-28 10:48:38 +00:00
|
|
|
import { M_BEACON } from "matrix-js-sdk/src/@types/beacon";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
|
|
|
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
|
2022-03-28 10:48:38 +00:00
|
|
|
import { arrayDiff } from "../utils/arrays";
|
|
|
|
import {
|
|
|
|
ClearWatchCallback,
|
|
|
|
GeolocationError,
|
|
|
|
mapGeolocationPositionToTimedGeo,
|
2022-03-31 11:51:44 +00:00
|
|
|
sortBeaconsByLatestCreation,
|
2022-03-28 10:48:38 +00:00
|
|
|
TimedGeoUri,
|
|
|
|
watchPosition,
|
|
|
|
} from "../utils/beacon";
|
|
|
|
import { getCurrentPosition } from "../utils/beacon/geolocation";
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconInfoOwner === userId;
|
|
|
|
|
|
|
|
export enum OwnBeaconStoreEvent {
|
2022-03-22 13:57:12 +00:00
|
|
|
LivenessChange = 'OwnBeaconStore.LivenessChange',
|
2022-03-28 16:46:39 +00:00
|
|
|
MonitoringLivePosition = 'OwnBeaconStore.MonitoringLivePosition',
|
2022-03-30 14:01:44 +00:00
|
|
|
WireError = 'WireError',
|
2022-03-16 16:35:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
const MOVING_UPDATE_INTERVAL = 2000;
|
|
|
|
const STATIC_UPDATE_INTERVAL = 30000;
|
|
|
|
|
2022-03-30 14:01:44 +00:00
|
|
|
const BAIL_AFTER_CONSECUTIVE_ERROR_COUNT = 2;
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
type OwnBeaconStoreState = {
|
2022-04-08 08:53:06 +00:00
|
|
|
beacons: Map<BeaconIdentifier, Beacon>;
|
2022-03-30 12:31:19 +00:00
|
|
|
beaconWireErrors: Map<string, Beacon>;
|
2022-04-08 08:53:06 +00:00
|
|
|
beaconsByRoomId: Map<Room['roomId'], Set<BeaconIdentifier>>;
|
|
|
|
liveBeaconIds: BeaconIdentifier[];
|
2022-03-16 16:35:09 +00:00
|
|
|
};
|
|
|
|
export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
|
|
|
|
private static internalInstance = new OwnBeaconStore();
|
2022-03-22 13:57:12 +00:00
|
|
|
// users beacons, keyed by event type
|
2022-04-08 08:53:06 +00:00
|
|
|
public readonly beacons = new Map<BeaconIdentifier, Beacon>();
|
|
|
|
public readonly beaconsByRoomId = new Map<Room['roomId'], Set<BeaconIdentifier>>();
|
2022-03-30 12:31:19 +00:00
|
|
|
/**
|
2022-03-30 14:01:44 +00:00
|
|
|
* Track over the wire errors for published positions
|
|
|
|
* Counts consecutive wire errors per beacon
|
|
|
|
* Reset on successful publish of location
|
2022-03-30 12:31:19 +00:00
|
|
|
*/
|
2022-04-08 08:53:06 +00:00
|
|
|
public readonly beaconWireErrorCounts = new Map<BeaconIdentifier, number>();
|
2022-03-31 11:51:44 +00:00
|
|
|
/**
|
|
|
|
* ids of live beacons
|
|
|
|
* ordered by creation time descending
|
|
|
|
*/
|
2022-03-16 16:35:09 +00:00
|
|
|
private liveBeaconIds = [];
|
2022-03-28 10:48:38 +00:00
|
|
|
private locationInterval: number;
|
|
|
|
private geolocationError: GeolocationError | undefined;
|
|
|
|
private clearPositionWatch: ClearWatchCallback | undefined;
|
|
|
|
/**
|
|
|
|
* Track when the last position was published
|
|
|
|
* So we can manually get position on slow interval
|
|
|
|
* when the target is stationary
|
|
|
|
*/
|
|
|
|
private lastPublishedPositionTimestamp: number | undefined;
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
super(defaultDispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get instance(): OwnBeaconStore {
|
|
|
|
return OwnBeaconStore.internalInstance;
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
/**
|
|
|
|
* True when we have live beacons
|
|
|
|
* and geolocation.watchPosition is active
|
|
|
|
*/
|
|
|
|
public get isMonitoringLiveLocation(): boolean {
|
|
|
|
return !!this.clearPositionWatch;
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
protected async onNotReady() {
|
|
|
|
this.matrixClient.removeListener(BeaconEvent.LivenessChange, this.onBeaconLiveness);
|
|
|
|
this.matrixClient.removeListener(BeaconEvent.New, this.onNewBeacon);
|
2022-04-08 08:53:06 +00:00
|
|
|
this.matrixClient.removeListener(BeaconEvent.Update, this.onUpdateBeacon);
|
2022-03-29 16:18:34 +00:00
|
|
|
this.matrixClient.removeListener(RoomStateEvent.Members, this.onRoomStateMembers);
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
this.beacons.forEach(beacon => beacon.destroy());
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
this.stopPollingLocation();
|
2022-03-16 16:35:09 +00:00
|
|
|
this.beacons.clear();
|
|
|
|
this.beaconsByRoomId.clear();
|
|
|
|
this.liveBeaconIds = [];
|
2022-03-30 14:01:44 +00:00
|
|
|
this.beaconWireErrorCounts.clear();
|
2022-03-16 16:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async onReady(): Promise<void> {
|
|
|
|
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness);
|
|
|
|
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon);
|
2022-04-08 08:53:06 +00:00
|
|
|
this.matrixClient.removeListener(BeaconEvent.Update, this.onUpdateBeacon);
|
2022-03-29 16:18:34 +00:00
|
|
|
this.matrixClient.on(RoomStateEvent.Members, this.onRoomStateMembers);
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
this.initialiseBeaconState();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onAction(payload: ActionPayload): Promise<void> {
|
|
|
|
// we don't actually do anything here
|
|
|
|
}
|
|
|
|
|
2022-03-31 11:51:44 +00:00
|
|
|
public hasLiveBeacons = (roomId?: string): boolean => {
|
2022-03-16 16:35:09 +00:00
|
|
|
return !!this.getLiveBeaconIds(roomId).length;
|
2022-03-31 11:51:44 +00:00
|
|
|
};
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-03-31 08:57:12 +00:00
|
|
|
/**
|
|
|
|
* Some live beacon has a wire error
|
|
|
|
* Optionally filter by room
|
|
|
|
*/
|
2022-03-31 11:51:44 +00:00
|
|
|
public hasWireErrors = (roomId?: string): boolean => {
|
2022-03-31 08:57:12 +00:00
|
|
|
return this.getLiveBeaconIds(roomId).some(this.beaconHasWireError);
|
2022-03-31 11:51:44 +00:00
|
|
|
};
|
2022-03-31 08:57:12 +00:00
|
|
|
|
2022-03-30 14:01:44 +00:00
|
|
|
/**
|
|
|
|
* If a beacon has failed to publish position
|
|
|
|
* past the allowed consecutive failure count (BAIL_AFTER_CONSECUTIVE_ERROR_COUNT)
|
|
|
|
* Then consider it to have an error
|
|
|
|
*/
|
2022-03-31 08:57:12 +00:00
|
|
|
public beaconHasWireError = (beaconId: string): boolean => {
|
2022-03-30 14:01:44 +00:00
|
|
|
return this.beaconWireErrorCounts.get(beaconId) >= BAIL_AFTER_CONSECUTIVE_ERROR_COUNT;
|
2022-03-31 08:57:12 +00:00
|
|
|
};
|
2022-03-30 14:01:44 +00:00
|
|
|
|
2022-03-31 08:57:12 +00:00
|
|
|
public resetWireError = (beaconId: string): void => {
|
2022-03-30 14:01:44 +00:00
|
|
|
this.incrementBeaconWireErrorCount(beaconId, false);
|
|
|
|
|
|
|
|
// always publish to all live beacons together
|
|
|
|
// instead of just one that was changed
|
|
|
|
// to keep lastPublishedTimestamp simple
|
|
|
|
// and extra published locations don't hurt
|
|
|
|
this.publishCurrentLocationToBeacons();
|
2022-03-31 08:57:12 +00:00
|
|
|
};
|
2022-03-30 14:01:44 +00:00
|
|
|
|
2022-03-31 11:51:44 +00:00
|
|
|
public getLiveBeaconIds = (roomId?: string): string[] => {
|
2022-03-16 16:35:09 +00:00
|
|
|
if (!roomId) {
|
|
|
|
return this.liveBeaconIds;
|
|
|
|
}
|
|
|
|
return this.liveBeaconIds.filter(beaconId => this.beaconsByRoomId.get(roomId)?.has(beaconId));
|
2022-03-31 11:51:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public getLiveBeaconIdsWithWireError = (roomId?: string): string[] => {
|
|
|
|
return this.getLiveBeaconIds(roomId).filter(this.beaconHasWireError);
|
|
|
|
};
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-03-31 11:51:44 +00:00
|
|
|
public getBeaconById = (beaconId: string): Beacon | undefined => {
|
2022-03-22 13:57:12 +00:00
|
|
|
return this.beacons.get(beaconId);
|
2022-03-31 11:51:44 +00:00
|
|
|
};
|
2022-03-22 13:57:12 +00:00
|
|
|
|
2022-04-08 08:53:06 +00:00
|
|
|
public stopBeacon = async (beaconIdentifier: string): Promise<void> => {
|
|
|
|
const beacon = this.beacons.get(beaconIdentifier);
|
2022-03-18 13:38:41 +00:00
|
|
|
// if no beacon, or beacon is already explicitly set isLive: false
|
|
|
|
// do nothing
|
|
|
|
if (!beacon?.beaconInfo?.live) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.updateBeaconEvent(beacon, { live: false });
|
|
|
|
};
|
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
/**
|
|
|
|
* Listeners
|
|
|
|
*/
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
private onNewBeacon = (_event: MatrixEvent, beacon: Beacon): void => {
|
|
|
|
if (!isOwnBeacon(beacon, this.matrixClient.getUserId())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.addBeacon(beacon);
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
2022-04-08 08:53:06 +00:00
|
|
|
/**
|
|
|
|
* This will be called when a beacon is replaced
|
|
|
|
*/
|
|
|
|
private onUpdateBeacon = (_event: MatrixEvent, beacon: Beacon): void => {
|
|
|
|
if (!isOwnBeacon(beacon, this.matrixClient.getUserId())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
private onBeaconLiveness = (isLive: boolean, beacon: Beacon): void => {
|
|
|
|
// check if we care about this beacon
|
2022-03-22 13:57:12 +00:00
|
|
|
if (!this.beacons.has(beacon.identifier)) {
|
2022-03-16 16:35:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:38:41 +00:00
|
|
|
// beacon expired, update beacon to un-alive state
|
|
|
|
if (!isLive) {
|
2022-03-22 13:57:12 +00:00
|
|
|
this.stopBeacon(beacon.identifier);
|
2022-03-18 13:38:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
this.checkLiveness();
|
2022-03-18 13:38:41 +00:00
|
|
|
|
2022-03-22 13:57:12 +00:00
|
|
|
this.emit(OwnBeaconStoreEvent.LivenessChange, this.getLiveBeaconIds());
|
2022-03-16 16:35:09 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
/**
|
|
|
|
* Check for changes in membership in rooms with beacons
|
|
|
|
* and stop monitoring beacons in rooms user is no longer member of
|
|
|
|
*/
|
|
|
|
private onRoomStateMembers = (_event: MatrixEvent, roomState: RoomState, member: RoomMember): void => {
|
|
|
|
// no beacons for this room, ignore
|
|
|
|
if (
|
|
|
|
!this.beaconsByRoomId.has(roomState.roomId) ||
|
|
|
|
member.userId !== this.matrixClient.getUserId()
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check powerlevels here
|
|
|
|
// in PSF-797
|
|
|
|
|
|
|
|
// stop watching beacons in rooms where user is no longer a member
|
|
|
|
if (member.membership === 'leave' || member.membership === 'ban') {
|
|
|
|
this.beaconsByRoomId.get(roomState.roomId).forEach(this.removeBeacon);
|
|
|
|
this.beaconsByRoomId.delete(roomState.roomId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* State management
|
|
|
|
*/
|
|
|
|
|
2022-03-30 14:01:44 +00:00
|
|
|
/**
|
|
|
|
* Live beacon ids that do not have wire errors
|
|
|
|
*/
|
|
|
|
private get healthyLiveBeaconIds() {
|
2022-03-31 08:57:12 +00:00
|
|
|
return this.liveBeaconIds.filter(beaconId => !this.beaconHasWireError(beaconId));
|
2022-03-30 14:01:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
private initialiseBeaconState = () => {
|
|
|
|
const userId = this.matrixClient.getUserId();
|
|
|
|
const visibleRooms = this.matrixClient.getVisibleRooms();
|
|
|
|
|
|
|
|
visibleRooms
|
|
|
|
.forEach(room => {
|
|
|
|
const roomState = room.currentState;
|
|
|
|
const beacons = roomState.beacons;
|
|
|
|
const ownBeaconsArray = [...beacons.values()].filter(beacon => isOwnBeacon(beacon, userId));
|
|
|
|
ownBeaconsArray.forEach(beacon => this.addBeacon(beacon));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
|
|
|
private addBeacon = (beacon: Beacon): void => {
|
2022-03-22 13:57:12 +00:00
|
|
|
this.beacons.set(beacon.identifier, beacon);
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
if (!this.beaconsByRoomId.has(beacon.roomId)) {
|
|
|
|
this.beaconsByRoomId.set(beacon.roomId, new Set<string>());
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:57:12 +00:00
|
|
|
this.beaconsByRoomId.get(beacon.roomId).add(beacon.identifier);
|
2022-03-18 13:38:41 +00:00
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
beacon.monitorLiveness();
|
|
|
|
};
|
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
/**
|
|
|
|
* Remove listeners for a given beacon
|
|
|
|
* remove from state
|
|
|
|
* and update liveness if changed
|
|
|
|
*/
|
|
|
|
private removeBeacon = (beaconId: string): void => {
|
|
|
|
if (!this.beacons.has(beaconId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.beacons.get(beaconId).destroy();
|
|
|
|
this.beacons.delete(beaconId);
|
|
|
|
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
private checkLiveness = (): void => {
|
2022-03-22 13:57:12 +00:00
|
|
|
const prevLiveBeaconIds = this.getLiveBeaconIds();
|
2022-03-16 16:35:09 +00:00
|
|
|
this.liveBeaconIds = [...this.beacons.values()]
|
|
|
|
.filter(beacon => beacon.isLive)
|
2022-03-31 11:51:44 +00:00
|
|
|
.sort(sortBeaconsByLatestCreation)
|
2022-03-22 13:57:12 +00:00
|
|
|
.map(beacon => beacon.identifier);
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
const diff = arrayDiff(prevLiveBeaconIds, this.liveBeaconIds);
|
|
|
|
|
|
|
|
if (diff.added.length || diff.removed.length) {
|
2022-03-22 13:57:12 +00:00
|
|
|
this.emit(OwnBeaconStoreEvent.LivenessChange, this.liveBeaconIds);
|
2022-03-16 16:35:09 +00:00
|
|
|
}
|
2022-03-28 10:48:38 +00:00
|
|
|
|
|
|
|
// publish current location immediately
|
|
|
|
// when there are new live beacons
|
|
|
|
// and we already have a live monitor
|
|
|
|
// so first position is published quickly
|
|
|
|
// even when target is stationary
|
|
|
|
//
|
|
|
|
// when there is no existing live monitor
|
|
|
|
// it will be created below by togglePollingLocation
|
|
|
|
// and publish first position quickly
|
|
|
|
if (diff.added.length && this.isMonitoringLiveLocation) {
|
|
|
|
this.publishCurrentLocationToBeacons();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if overall liveness changed
|
|
|
|
if (!!prevLiveBeaconIds?.length !== !!this.liveBeaconIds.length) {
|
|
|
|
this.togglePollingLocation();
|
|
|
|
}
|
2022-03-16 16:35:09 +00:00
|
|
|
};
|
2022-03-18 13:38:41 +00:00
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
/**
|
|
|
|
* Geolocation
|
|
|
|
*/
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
private togglePollingLocation = () => {
|
2022-03-28 10:48:38 +00:00
|
|
|
if (!!this.liveBeaconIds.length) {
|
2022-03-28 16:46:39 +00:00
|
|
|
this.startPollingLocation();
|
|
|
|
} else {
|
|
|
|
this.stopPollingLocation();
|
2022-03-28 10:48:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private startPollingLocation = async () => {
|
|
|
|
// clear any existing interval
|
|
|
|
this.stopPollingLocation();
|
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
try {
|
|
|
|
this.clearPositionWatch = await watchPosition(
|
|
|
|
this.onWatchedPosition,
|
|
|
|
this.onGeolocationError,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
this.onGeolocationError(error?.message);
|
|
|
|
// don't set locationInterval if geolocation failed to setup
|
|
|
|
return;
|
|
|
|
}
|
2022-03-28 10:48:38 +00:00
|
|
|
|
|
|
|
this.locationInterval = setInterval(() => {
|
|
|
|
if (!this.lastPublishedPositionTimestamp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if position was last updated STATIC_UPDATE_INTERVAL ms ago or more
|
|
|
|
// get our position and publish it
|
|
|
|
if (this.lastPublishedPositionTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
|
|
|
|
this.publishCurrentLocationToBeacons();
|
|
|
|
}
|
|
|
|
}, STATIC_UPDATE_INTERVAL);
|
2022-03-28 16:46:39 +00:00
|
|
|
|
|
|
|
this.emit(OwnBeaconStoreEvent.MonitoringLivePosition);
|
2022-03-28 10:48:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private stopPollingLocation = () => {
|
|
|
|
clearInterval(this.locationInterval);
|
|
|
|
this.locationInterval = undefined;
|
|
|
|
this.lastPublishedPositionTimestamp = undefined;
|
|
|
|
this.geolocationError = undefined;
|
|
|
|
|
|
|
|
if (this.clearPositionWatch) {
|
|
|
|
this.clearPositionWatch();
|
|
|
|
this.clearPositionWatch = undefined;
|
|
|
|
}
|
2022-03-28 16:46:39 +00:00
|
|
|
|
|
|
|
this.emit(OwnBeaconStoreEvent.MonitoringLivePosition);
|
2022-03-28 10:48:38 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
private onWatchedPosition = (position: GeolocationPosition) => {
|
|
|
|
const timedGeoPosition = mapGeolocationPositionToTimedGeo(position);
|
|
|
|
|
|
|
|
// if this is our first position, publish immediateley
|
|
|
|
if (!this.lastPublishedPositionTimestamp) {
|
|
|
|
this.publishLocationToBeacons(timedGeoPosition);
|
|
|
|
} else {
|
|
|
|
this.debouncedPublishLocationToBeacons(timedGeoPosition);
|
|
|
|
}
|
2022-03-28 10:48:38 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
private onGeolocationError = async (error: GeolocationError): Promise<void> => {
|
|
|
|
this.geolocationError = error;
|
|
|
|
logger.error('Geolocation failed', this.geolocationError);
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
// other errors are considered non-fatal
|
|
|
|
// and self recovering
|
|
|
|
if (![
|
|
|
|
GeolocationError.Unavailable,
|
|
|
|
GeolocationError.PermissionDenied,
|
|
|
|
].includes(error)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stopPollingLocation();
|
|
|
|
// kill live beacons when location permissions are revoked
|
|
|
|
// TODO may need adjustment when PSF-797 is done
|
|
|
|
await Promise.all(this.liveBeaconIds.map(this.stopBeacon));
|
2022-03-28 10:48:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current location
|
|
|
|
* (as opposed to using watched location)
|
|
|
|
* and publishes it to all live beacons
|
|
|
|
*/
|
|
|
|
private publishCurrentLocationToBeacons = async () => {
|
2022-03-28 16:46:39 +00:00
|
|
|
try {
|
|
|
|
const position = await getCurrentPosition();
|
|
|
|
this.publishLocationToBeacons(mapGeolocationPositionToTimedGeo(position));
|
|
|
|
} catch (error) {
|
|
|
|
this.onGeolocationError(error?.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
/**
|
|
|
|
* MatrixClient api
|
|
|
|
*/
|
2022-03-28 16:46:39 +00:00
|
|
|
|
2022-03-29 16:18:34 +00:00
|
|
|
private updateBeaconEvent = async (beacon: Beacon, update: Partial<BeaconInfoState>): Promise<void> => {
|
|
|
|
const { description, timeout, timestamp, live, assetType } = {
|
|
|
|
...beacon.beaconInfo,
|
|
|
|
...update,
|
|
|
|
};
|
2022-03-28 16:46:39 +00:00
|
|
|
|
2022-04-11 08:28:43 +00:00
|
|
|
const newContent = makeBeaconInfoContent(timeout,
|
2022-03-29 16:18:34 +00:00
|
|
|
live,
|
|
|
|
description,
|
|
|
|
assetType,
|
2022-04-11 08:28:43 +00:00
|
|
|
timestamp,
|
|
|
|
);
|
|
|
|
const updateContent = {
|
|
|
|
...newContent,
|
|
|
|
"m.new_content": newContent,
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationType.Replace,
|
|
|
|
"event_id": beacon.beaconInfoId,
|
|
|
|
},
|
|
|
|
};
|
2022-03-29 16:18:34 +00:00
|
|
|
|
2022-04-08 08:53:06 +00:00
|
|
|
await this.matrixClient.unstable_setLiveBeacon(beacon.roomId, updateContent);
|
2022-03-29 16:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends m.location events to all live beacons
|
|
|
|
* Sets last published beacon
|
|
|
|
*/
|
|
|
|
private publishLocationToBeacons = async (position: TimedGeoUri) => {
|
|
|
|
this.lastPublishedPositionTimestamp = Date.now();
|
2022-03-30 14:01:44 +00:00
|
|
|
await Promise.all(this.healthyLiveBeaconIds.map(beaconId =>
|
2022-03-29 16:18:34 +00:00
|
|
|
this.sendLocationToBeacon(this.beacons.get(beaconId), position)),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private debouncedPublishLocationToBeacons = debounce(this.publishLocationToBeacons, MOVING_UPDATE_INTERVAL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends m.location event to referencing given beacon
|
|
|
|
*/
|
|
|
|
private sendLocationToBeacon = async (beacon: Beacon, { geoUri, timestamp }: TimedGeoUri) => {
|
|
|
|
const content = makeBeaconContent(geoUri, timestamp, beacon.beaconInfoId);
|
2022-03-30 12:31:19 +00:00
|
|
|
try {
|
|
|
|
await this.matrixClient.sendEvent(beacon.roomId, M_BEACON.name, content);
|
2022-03-30 14:01:44 +00:00
|
|
|
this.incrementBeaconWireErrorCount(beacon.identifier, false);
|
2022-03-30 12:31:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2022-03-30 14:01:44 +00:00
|
|
|
this.incrementBeaconWireErrorCount(beacon.identifier, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage beacon wire error count
|
|
|
|
* - clear count for beacon when not error
|
|
|
|
* - increment count for beacon when is error
|
|
|
|
* - emit if beacon error count crossed threshold
|
|
|
|
*/
|
|
|
|
private incrementBeaconWireErrorCount = (beaconId: string, isError: boolean): void => {
|
2022-03-31 08:57:12 +00:00
|
|
|
const hadError = this.beaconHasWireError(beaconId);
|
2022-03-30 14:01:44 +00:00
|
|
|
|
|
|
|
if (isError) {
|
|
|
|
// increment error count
|
|
|
|
this.beaconWireErrorCounts.set(
|
|
|
|
beaconId,
|
|
|
|
(this.beaconWireErrorCounts.get(beaconId) ?? 0) + 1,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// clear any error count
|
|
|
|
this.beaconWireErrorCounts.delete(beaconId);
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:57:12 +00:00
|
|
|
if (this.beaconHasWireError(beaconId) !== hadError) {
|
2022-03-30 14:01:44 +00:00
|
|
|
this.emit(OwnBeaconStoreEvent.WireError, beaconId);
|
2022-03-30 12:31:19 +00:00
|
|
|
}
|
2022-03-28 10:48:38 +00:00
|
|
|
};
|
2022-03-16 16:35:09 +00:00
|
|
|
}
|