/** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2021-05-13 07:49:23 * @modify date 2021-05-13 08:07:52 * @desc [description] */ import * as Tinkerforge from "tinkerforge"; import { NopeObservable } from "../../../lib/observables/nopeObservable"; import { INopeDispatcher } from "../../../lib/types/nope/nopeDispatcher.interface"; import { GenericBrick } from "../src/genericBrick.module"; import { BRICKS } from "../src/identifiers"; import { TinkerForgeConnector } from "../src/TinkerForgeConnector"; import { IDeviceConfiguration } from "../type/interfaces"; export default async function ( _dispatcher: INopeDispatcher, UID: string, ipcon: TinkerForgeConnector, config: IDeviceConfiguration ) { const sensor = new ThermoCoupleSensor(_dispatcher, UID, ipcon); sensor.identifier = config.name; await sensor.init(...config.params); return sensor; } export class ThermoCoupleSensor extends GenericBrick { public value = new NopeObservable(); private _instance: any; constructor( _dispatcher: INopeDispatcher, UID: string, ipcon: TinkerForgeConnector ) { super( _dispatcher, UID, ipcon, BRICKS.DISTANCE_IR_BRICKLET, "BrickletDistanceIR" ); } /** * Helper Function, to assign the Frequency of the Sensor. * As minimum Frequency we will use 10 Hz. * * @param {number} freq The Frequency of the Sensor, given in Hz * @return {*} {Promise} * @memberof ThermoCoupleSensor */ setFrequency(freq: number): Promise { // Adapt the Max Frequency const period = Math.round((1 / freq) * 1000 > 1 ? (1 / freq) * 1000 : 10); // Now return a Promise, which will perform the Action: const _this = this; return new Promise((resolve, reject) => { _this.instance.setDistanceCallbackPeriod(period, resolve, reject); }); } async init(freq = 50): Promise { await super.init(); await this.setFrequency(freq); const _factor = 1 / 1000; const _this = this; this.instance.on( Tinkerforge.BrickletDistanceIR.CALLBACK_DISTANCE, // Callback function for temperature callback (value) => { // Define the Distance in [m] _this.value.setContent(value * _factor); } ); } }