/*! Copyright (c) 2018 Siemens AG. Licensed under the MIT License. */ import { Observable } from "rxjs"; import { Controller, TimeInterval, Uuid } from ".."; import { Observation, Sensor } from "./objects"; import { ISensorIoStatic, SensorIo } from "./sensor-io"; /** * Defines whether and how the observations should be automatically published * from SensorSourceController. * * This is used together with the samplingInterval value. */ export declare type ObservationPublicationType = /** * The observations should not be published automatically. */ "none" | /** * The observations are advertised. * * The value of the sensor is read continuously every `samplingInterval` * milliseconds. */ "advertise" | /** * The observations are channeled. * * The value of the sensor is read continuously every `samplingInterval` * milliseconds. */ "channel"; /** * Static definition of a sensor for use in SensorController. * * This is used in the options of the controller to register the sensors * directly at creation. The `sensors` option of the SensorController should * contain an array of SensorDefinitions. * * Each definition should contain the Sensor object, the hardware pin that it is * connected to, and the type of the IO communication that it has. */ export interface SensorDefinition { /** * Hardware-specific parameters necessary for the read and write methods. */ parameters?: any; /** * The Sensor object. * * This will be used for the main communication with other controllers and * applications. */ sensor: Sensor; /** * The type of the IO handling used for this sensor. * * You can use predefined ones such as `Aio`, `InputGpio`, `OuputGpio` or * define your own class. You can also use `MockSensorIo` if you don't have * any actual hardware connection. */ io: ISensorIoStatic; /** * The time interval in milliseconds with which the values of the sensor * should be read and published. * * This should only be used if the observationPublicationType is not set to * none. */ samplingInterval?: number; /** * Defines whether and how the observations should be automatically * published from the controller. * * This is used together with the samplingInterval value. If it is not set * to none, samplingInterval must be set to positive value. */ observationPublicationType: ObservationPublicationType; } /** * The registered sensors as they are used by the SensorController. * * This contains the Sensor object that is used for communication and the IO * interface that defines the hardware connection and how the pin is read and * written to. */ export interface SensorContainer { /** * The Sensor object. This will be used for the main communication * with other controllers and applications. */ sensor: Sensor; /** * The IO handling used for this sensor. */ io: SensorIo; } /** * Manages a set of registered Sensors and provides a source for both Sensors * and Sensor-related objects. * * This controller is designed to be used by a server as a counterpart of a * SensorObserverController. * * You can either register the Sensors manually by calling registerSensor method * or do it automatically from the controller options by defining your Sensors * in a SensorDefinition array given in the `sensors` option of the controller. * * SensorSourceController also takes some other options (they all default to * false): * - ignoreSensorDiscoverEvents: Ignores received discover events for registered * sensors. * - ignoreSensorQueryEvents: Ignores received query events for registered * sensors. * - skipSensorAdvertise: Does not advertise a sensor when registered. * - skipSensorDeadvertise: Does not deadvertise a sensor when unregistered. */ export declare class SensorSourceController extends Controller { private _sensors; private _observationPublishers; private _sensorValueObservables; private _querySubscription; private _discoverSubscription; onInit(): void; onCommunicationManagerStopping(): void; /** * Returns all the registered sensor containers in an array. */ get registeredSensorContainers(): SensorContainer[]; /** * Returns all the registered Sensors in an array. */ get registeredSensors(): Sensor[]; /** * Determines whether a Sensor with the given objectId is registered with * this controller. */ isRegistered(sensorId: Uuid): boolean; /** * Returns the sensor container associated with the given Sensor objectId. * * If no such container exists then `undefined` is returned. */ getSensorContainer(sensorId: Uuid): SensorContainer; /** * Returns the Sensor associated with the given Sensor objectId. * * If no such sensor exists then `undefined` is returned. */ getSensor(sensorId: Uuid): Sensor; /** * Returns the sensor IO interface associated with the given Sensor objectId. * If no such sensor IO exists then `undefined` is returned. */ getSensorIo(sensorId: Uuid): SensorIo; /** * Returns an observable that emits values read from the sensor. * * Only the values read with publishChanneledObservation or * publishAdvertisedObservation will emit new values. Reading the sensor * value manually with SensorIo.read will not emit any new value. * * The observable is created lazily and cached. Once the Sensor associated * with the observable is unregistered, all subscriptions to the observable * are also unsubscribed. * * If the Sensor is not registered, this method will throw an error. */ getSensorValueObservable(sensorId: Uuid): Observable; /** * Returns the first registered sensor where the given predicate is true, * `undefined` otherwise. * * @param predicate findSensor calls predicate once in arbitrary order for * each registered sensor, until it finds one where predicate returns true. * If such a sensor is found, that sensor is returned immediately. * Otherwise, undefined is returned * @returns the first sensor matching the predicate; otherwise `undefined` */ findSensor(predicate: (sensor: Sensor) => boolean): Sensor; /** * Registers a Sensor to this controller along with its IO handler * interface. * * You can call this function at runtime or register sensors automatically * at the beginning by passing them in the controller options under * `sensors` as a SensorDefinition array. * * If a Sensor with the given objectId already exists, this method is simply * ignored. * * When a sensor is registered, it is advertised to notify other listeners * (unless ignoreSensorAdvertise option is set). The controller class also * starts to listen on query and discover events for Sensors (unless * skipSensorQueryEvents or skipSensorDiscoverEvents options are set). * * If the observationPublicationType is not set to none, then the value of * the sensor is read every `samplingInterval` milliseconds and published as * an observation automatically. * * @param sensor Sensor to register to the controller. * @param io IO handler interface for the sensor. This could be an `Aio`, * `InputGpio`, `OutputGpio`, `MockSensorIo` or a custom handler. * @param observationPublicationType Whether and how the observations of the * sensor should be published. * @param samplingInterval If the observations are published automatically, * defines how often the publications should be sent. */ registerSensor(sensor: Sensor, io: SensorIo, observationPublicationType?: ObservationPublicationType, samplingInterval?: number): void; /** * Unregisters a previously registered Sensor. * * If no such Sensor is registered, then an error is thrown. * * This also sends a disadvertise event to notify the listeners (unless * ignoreSensorDeadvertise option is set). The query and discover events for * this Sensor are ignored from this point on. * * If a value observable for the Sensor exists, then all subscriptions to * that observable are unsubscribed. */ unregisterSensor(sensorId: Uuid): Error; /** * Publishes an observation for a registered Sensor. * * If no such registered Sensor exists then an error is thrown. The * publication is performed in the form of a channel event. By default the * channelId is the sensorId. However, subclasses can change it by * overriding getChannelId method. * * The observation value is read directly from the IO handler of the Sensor. * The observation time is recorded as this method is called. Subclasses can * change the final value of the observation object by overriding the * createObservation method. * * @param sensorId ObjectId of the Sensor to publish the observation. * @param resultQuality The quality of the result. (optional) * @param validTime The validity time of the observation. (optional) * @param parameters Extra parameters for the observation. (optional) * @param featureOfInterestId UUID of associated feature of interest. * (optional) */ publishChanneledObservation(sensorId: Uuid, resultQuality?: string[], validTime?: TimeInterval, parameters?: { [key: string]: any; }, featureOfInterestId?: Uuid): void; /** * Publishes an observation for a registered Sensor. * * If no such registered Sensor exists then an error is thrown. The * publication is performed in the form of an advertise event. * * The observation value is read directly from the IO handler of the Sensor. * The observation time is recorded as this method is called. Subclasses can * change the final value of the observation object by overriding the * createObservation method. * * @param sensorId ObjectId of the Sensor to publish the observation. * @param resultQuality The quality of the result. (optional) * @param validTime The validity time of the observation. (optional) * @param parameters Extra parameters for the observation. (optional) * @param featureOfInterestId UUID of associated feature of interest. * (optional) */ publishAdvertisedObservation(sensorId: Uuid, resultQuality?: string[], validTime?: TimeInterval, parameters?: { [key: string]: any; }, featureOfInterestId?: Uuid): void; /** * Creates an Observation object for a Sensor. * * Subclasses can override this method to provide their own logic. * * @param container Sensor container creating the observation. * @param value Value of the observation. * @param resultQuality The quality of the result. (optional) * @param validTime The validity time of the observation. (optional) * @param parameters Extra parameters for the observation. (optional) * @param featureOfInterestId UUID of associated feature of interest. * (optional) */ protected createObservation(container: SensorContainer, value: any, resultQuality?: string[], validTime?: TimeInterval, parameters?: { [key: string]: any; }, featureOfInterestId?: Uuid): Observation; /** * Returns the channelId associated with a sensor container. * * By default, it is the objectId of the Sensor. Subclasses can override * this method to provide their own logic. */ protected getChannelId(container: SensorContainer): string; /** * Lifecycle method called just before an observation is published for a * specific Sensor. * * Default implementation does nothing. */ protected onObservationWillPublish(container: SensorContainer, observation: Observation): void; /** * Lifecycle method called immediately after an observation is published for * a specific Sensor. Default implementation does nothing. */ protected onObservationDidPublish(container: SensorContainer, observation: Observation): void; private _observeSensorQueriesIfNeeded; private _observeSensorDiscoversIfNeeded; private _publishObservation; private _isSensorDefinition; private _isSensorDefinitionArray; }