@iotile/iotile-device
Version:
A typescript library for interfacing with IOTile BLE devices
77 lines (76 loc) • 3.74 kB
TypeScript
import { SignedListReport, RawReading } from "./iotile-reports";
export interface UTCAssignerOptions {
allowImprecise: boolean;
}
export interface AnchorPoint {
readingId: number;
uptime: number | null;
utcTime: Date | null;
isBreak: boolean;
}
/**
* A conversion function that takes the value of a reading in a given stream and turns it into a utc date.
*/
export declare type AnchorValueProcessor = (streamID: number, readingID: number, uptime: number, value: number) => Date;
/**
* This class assigns UTC dates to readings based on their sequential reading ID and local time.
*
* Internally it works based on the following principle. It knows that the sequential reading ID
* is monotonically increasing but the local time can reset to 0 every time there is a timeBreak
* in the data (e.g. the device reboots). So it constructs a series of TimeSegments which are
* ranges of reading IDs where there are not breaks. This means that each break ends the previous
* segment and starts the next segment.
*
* Inside each segment there needs to be at least one anchor point where the UTC time and local time
* are both known. Once there is an anchor point in a given segment, all other readings in that
* segment can be assigned UTC times by looking at their local time offset from the anchor point.
*
* If allowImprecise is passed, this class should still try to assign a UTC time to a reading even
* if it occurs in a segment with no anchor points by finding the first subsequent segment that does
* have an anchor point and back-calculating from there assuming that all breaks between segments
* were infinitely short.
*/
export declare class UTCAssigner {
private imprecise;
private anchorPoints;
private addedIDSet;
private anchorPointsSorted;
private logger;
private anchorStreams;
private breakStreams;
constructor(options: UTCAssignerOptions);
private initBreakStreams;
/**
* Assign a UTC date to a reading based on all previously added anchor and break
* points. If a UTC time cannot be assigned because there is insufficient data
* or because one of the options passed to the constructor does not allow it,
* throw an ArgumentError.
*/
assignUTCTimestamp(readingID: number, uptime: number | null): Date;
private bisectLeftAnchors;
private ensureAnchorPointsSorted;
addAnchorPoint(readingID: number, uptime: number | null, utc: Date | null, isBreak?: boolean): void;
addReading(reading: RawReading): void;
/**
* Inform the UTCAssigner that whenever it sees a value in the given
* stream, it can infer that its value is explicitly a UTC timestamp.
* This allows the UTCAssigner to automatically call addAnchorPoint
* when it processes a streamer report that contains streams that could
* act as anchors.
*
* If the value of the stream cannot be directly interpreted as the number
* of seconds since the year 2000, you can pass an optional callable that
* will be called to determine the correct UTC timestamp.
*
* You can pass a literal string "rtc" or "epoch" for valueProcessor if you want
* the value to be treated as seconds since 1/1/2000 or seconds since 1/1/1970 respectively.
*
* Alternatively, you can pass a function that returns a Date.
*/
markAnchorStream(streamID: number, valueProcessor?: AnchorValueProcessor | "rtc" | "epoch"): void;
/**
* Automatically call addAnchorPoint for all values found in
* a stream previously passed to markAnchorStream.
*/
addAnchorsFromReport(report: SignedListReport): void;
}