UNPKG

dwh-fetch

Version:

fetching data from DigiWEB devices

136 lines (103 loc) 3.55 kB
# Typescript service for Digitronic Dwh interface ### Usage ```bash yarn add dwh-fetch ``` **Start coding!** ### Features - fetch DigiWEB variables as strings, or integers or as arrays - map results of DigiWeb variables to objects using the `@Expression` decorator ### Importing library ```typescript import DwhFetch from 'dwh-fetch' ``` ### Examples ```typescript const dwh = new DwhFetch(); // fetch a simple variable to promise dwh.fetchString('##000187').then(value => console.log(value)); (async () => { // or with await ... const value = await dwh.fetchString('##000187'); })(); ``` ```typescript const dwh = new DwhFetch(); (async () => { // fetch as Number to convert the result to number before returning const aNumber = await dwh.fetchNumber('##000187'); const isNumber = aNumber instanceof Number; // true })(); ``` ```typescript const dwh = new DwhFetch(); (async () => { // fetch as Number to convert the result to number before returning const result = await dwh.fetch(['##A', '##B']); console.log(result.accessRights); //current accessRights from the request console.log(result.lines[0].success); //boolean indicates that request has been successful processed or not console.log(result.lines[0].value); //value of ##A or errorCode if success is false console.log(result.lines[1].success); //boolean indicates that request has been successful processed or not console.log(result.lines[1].value); //value of ##B or errorCode if success is false })(); ``` It's also possible to map values to objects and convert the results directly ```typescript interface Test { timer: number; firmware: string; } const expressions: ExpressionDefinitions<Test> = { timer: Expression.integer('##000187'), firmware: Expression.string('#$000199') }; (async () => { const dwhFetch = new DwhFetch(); const values: Test = await fetchExpressions(dwhFetch, expressions); console.log(values); })(); ``` This is also possible with array like expressions and using G= requests ```typescript import { Expression, ExpressionDefinitions, fetchExpressions, fetchIndexed } from '../fetch-indexed'; import { DwhFetch } from '../dwh-fetch'; interface Test { num: number; str: string; } const expressions: ExpressionDefinitions<Test> = { num: Expression.integer('##Module[##X].num'), str: Expression.string('##Module[##X].str') }; (async () => { const dwhFetch = new DwhFetch(); const values: Test[] = await fetchIndexed(dwhFetch, '##Module.Count', expressions); console.log(values); })(); ``` This is also possible with multiple `ExpressionDefinitions` in one call ```typescript //expression store holds all the requests that should be sent in one request const store = new ExpressionStore(); // add the first expressions. Note that this won't resolve util fetchExpressionStore has been called and awaited const first = store.add({ a: Expression.string('#$str[$(index)]'), b: Expression.integer('##num[$(index)]'), }); // add the first expressions. Note that this won't resolve util fetchExpressionStore has been called and awaited const second = store.add({ c: Expression.string('#$str'), d: Expression.integer('##num'), e: Expression.string('#$str'), }); // fetch data from dwh await fetchExpressionStore(dwhFetch, store); // now the promises from the expressionstore can be awaited console.log(await first); // { a: 'str', b: 7 } console.log(await second); // { c: 'str', d: 7, e: 'str' } ```