From 4fc4c8eda417e25d39868f38893b4b1f70e7752f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C5=BEe=20Vovk?= Date: Thu, 30 Jan 2020 11:14:52 +0100 Subject: [PATCH] Update code base to ES6 **Includes ADS indexOffset fix caused 'read' 'write' and 'notify' to return values of 0 or false Replaced all 'var' types to 'const' and 'let' respectively Replaced all 'function' types to 'const' arrow functions Replaced all function inheritences of 'this' to pass as first parameter instead Reduced code size by about 350 lines and structural imrpovements Code examples must be updated becuase 'this' has to be replaced with your 'client' object Example: ```js let client = ads.connect(amsConfig, () => { console.log(`Trying to read symbol:`, sym) //* // Read symbol client.read(sym, (err, handle) => { if (err) console.log(err) console.log('Response:', handle.value) client.end() })//*/ /* // Add symbol to notify client.notify(sym) client.on('notification', handle => { console.log('Response:', handle.value); });//*/ }) client.on('error', e => { console.log(e) }) ``` --- lib/ads.js | 1814 +++++++++++++++++++++------------------------------- 1 file changed, 728 insertions(+), 1086 deletions(-) diff --git a/lib/ads.js b/lib/ads.js index 794de63..a1ce8e3 100755 --- a/lib/ads.js +++ b/lib/ads.js @@ -18,860 +18,697 @@ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +// @ts-check 'use strict' -var debug = require('debug')('node-ads') -var net = require('net') -var events = require('events') -var Buffer = require('safe-buffer').Buffer +const debug = require('debug')('node-ads') +const net = require('net') +const events = require('events') +const Buffer = require('safe-buffer').Buffer +// @ts-ignore Buffer.INSPECT_MAX_BYTES = 200 -exports.connect = function (options, cb) { - var adsClient = getAdsObject(options) +const exportedConnect = (options, cb) => { + const adsClient = getAdsObject(options) adsClient.connect(cb) return adsClient } -var getAdsObject = function (options) { - var ads = {} - ads.connected = false - ads.options = parseOptions(options) - ads.invokeId = 0 - ads.pending = {} - ads.symHandlesToRelease = [] - ads.notificationsToRelease = [] - ads.notifications = {} - ads.dataStream = null - ads.tcpHeaderSize = 6 - ads.amsHeaderSize = 32 - - var emitter = new events.EventEmitter() - ads.adsClient = Object.create(emitter) - - ads.adsClient.connect = function (cb) { - return connect.call(ads, cb) - } - - ads.adsClient.end = function (cb) { - return end.call(ads, cb) - } - - ads.adsClient.readDeviceInfo = function (cb) { - return readDeviceInfo.call(ads, cb) - } - - ads.adsClient.read = function (handle, cb) { - return read.call(ads, handle, cb) - } - - ads.adsClient.write = function (handle, cb) { - return write.call(ads, handle, cb) - } - - ads.adsClient.readState = function (cb) { - return readState.call(ads, cb) - } - - ads.adsClient.notify = function (handle, cb) { - return notify.call(ads, handle, cb) - } +exports.connect = exportedConnect - ads.adsClient.releaseNotificationHandles = function (cb) { - return releaseNotificationHandles.call(ads, cb) +const getAdsObject = options => { + const ads = { + connected: false, + options: parseOptions(options), + invokeId: 0, + pending: {}, + symHandlesToRelease: [], + notificationsToRelease: [], + notifications: {}, + dataStream: null, + tcpHeaderSize: 6, + amsHeaderSize: 32 } - - ads.adsClient.releaseNotificationHandle = function (handle, cb) { - return releaseNotificationHandle.call(ads, handle, cb) - } - - ads.adsClient.writeRead = function (handle, cb) { - return writeReadCommand.call(ads, handle, cb) - } - - ads.adsClient.getSymbols = function (cb, raw) { - return getSymbols.call(ads, cb, raw) - } - - ads.adsClient.getDatatyps = function (cb) { - return getDatatyps.call(ads, cb) - } - - ads.adsClient.multiRead = function (handles, cb) { - return multiRead.call(ads, handles, cb) - } - - ads.adsClient.multiWrite = function (handles, cb) { - return multiWrite.call(ads, handles, cb) - } - - ads.adsClient.getHandles = function (handles, cb) { - return getHandles.call(ads, handles, cb) - } - + const emitter = new events.EventEmitter() + ads.adsClient = Object.create(emitter) + ads.adsClient.connect = cb => connect(ads, cb) + ads.adsClient.end = cb => end(ads, cb) + ads.adsClient.readDeviceInfo = cb => readDeviceInfo(ads, cb) + ads.adsClient.read = (handle, cb) => read(ads, handle, cb) + ads.adsClient.write = (handle, cb) => write(ads, handle, cb) + ads.adsClient.readState = cb => readState(ads, cb) + ads.adsClient.notify = (handle, cb) => notify(ads, handle, cb) + ads.adsClient.releaseNotificationHandles = cb => releaseNotificationHandles(ads, cb) + ads.adsClient.releaseNotificationHandle = (handle, cb) => releaseNotificationHandle(ads, handle, cb) + ads.adsClient.writeRead = (handle, cb) => writeReadCommand(ads, handle, cb) + ads.adsClient.getSymbols = (cb, raw) => getSymbols(ads, cb, raw) + ads.adsClient.getDatatypes = cb => getDatatypes(ads, cb) + ads.adsClient.multiRead = (handles, cb) => multiRead(ads, handles, cb) + ads.adsClient.multiWrite = (handles, cb) => multiWrite(ads, handles, cb) + ads.adsClient.getHandles = (handles, cb) => getHandles(ads, handles, cb) Object.defineProperty(ads.adsClient, 'options', { - get options () { return ads.options }, - set options (v) { ads.options = v } + // @ts-ignore + get options() { return ads.options }, + set options(v) { ads.options = v } }) - return ads.adsClient } -var connect = function (cb) { - var ads = this - - ads.tcpClient = net.connect( - ads.options.port, - ads.options.host, - function () { - ads.connected = true - cb.apply(ads.adsClient) - } - ) - +const connect = (ads, cb) => { + ads.tcpClient = net.connect(ads.options.port, ads.options.host, onConenct => { + ads.connected = true + cb.apply(ads.adsClient) + }) // ads.tcpClient.setKeepAlive(true) ads.tcpClient.setNoDelay(true) - - ads.tcpClient.on('data', function (data) { - if (ads.dataStream === null) { - ads.dataStream = data - } else { - ads.dataStream = Buffer.concat([ads.dataStream, data]) - } - checkResponseStream.call(ads) + ads.tcpClient.on('data', data => { + if (ads.dataStream === null) ads.dataStream = data + else ads.dataStream = Buffer.concat([ads.dataStream, data]) + checkResponseStream(ads) }) - - ads.tcpClient.on('timeout', function (data) { + ads.tcpClient.on('timeout', data => { ads.connected = false; ads.adsClient.emit('timeout', data) ads.tcpClient.end() }) - - ads.dataCallback = function (data) { + ads.tcpClient.on('disconnect', () => { ads.connected = false; - ads.adsClient.emit('error', data) + ads.adsClient.emit('disconnect') + ads.tcpClient.end() + }) + ads.dataCallback = data => { + ads.connected = false; + ads.adsClient.emit('error_', data) ads.tcpClient.end() } - ads.tcpClient.on('error', ads.dataCallback) debug('Hello') } -var end = function (cb) { - var ads = this +const end = (ads, cb) => { ads.tcpClient.removeListener('data', ads.dataCallback) - releaseSymHandles.call(ads, function () { - releaseNotificationHandles.call(ads, function () { + releaseSymHandles(ads, () => { + releaseNotificationHandles(ads, done => { if (ads.tcpClient) { ads.connected = false ads.tcpClient.destroy() } - if (typeof cb !== 'undefined') cb.call(ads) + if (typeof cb !== 'undefined') cb() }) }) } -var ID_READ_DEVICE_INFO = 1 -var ID_READ = 2 -var ID_WRITE = 3 -var ID_READ_STATE = 4 -var ID_WRITE_CONTROL = 5 -var ID_ADD_NOTIFICATION = 6 -var ID_DEL_NOTIFICATION = 7 -var ID_NOTIFICATION = 8 -var ID_READ_WRITE = 9 -var processDataByte = function (inByte) { - var ads = this +const ID_READ_DEVICE_INFO = 1 +const ID_READ = 2 +const ID_WRITE = 3 +const ID_READ_STATE = 4 +const ID_WRITE_CONTROL = 5 +const ID_ADD_NOTIFICATION = 6 +const ID_DEL_NOTIFICATION = 7 +const ID_NOTIFICATION = 8 +const ID_READ_WRITE = 9 +const processDataByte = (ads, inByte) => { ads._buffer = ads._buffer || [] ads._buffer.push(inByte) - var headerSize = ads.tcpHeaderSize + ads.amsHeaderSize + const headerSize = ads.tcpHeaderSize + ads.amsHeaderSize if (ads._buffer.length > headerSize) { - var length = ads._buffer.readUInt32LE(26) + const length = ads._buffer.readUInt32LE(26) if (ads._buffer.length >= headerSize + length) { ads.dataStream = Buffer.from(ads._buffer) debug('ads:', ads.dataStream) ads._buffer = [] - analyseResponse.call(ads) + analyseResponse(ads) } } } -var checkResponseStream = function () { - var ads = this +const checkResponseStream = ads => { if (ads.dataStream !== null) { - var headerSize = ads.tcpHeaderSize + ads.amsHeaderSize + const headerSize = ads.tcpHeaderSize + ads.amsHeaderSize if (ads.dataStream.length > headerSize) { - var length = ads.dataStream.readUInt32LE(26) - if (ads.dataStream.length >= headerSize + length) { - analyseResponse.call(ads) - } + const length = ads.dataStream.readUInt32LE(26) + if (ads.dataStream.length >= headerSize + length) analyseResponse(ads) } } } -var analyseResponse = function () { - var ads = this - var commandId = ads.dataStream.readUInt16LE(22) - var length = ads.dataStream.readUInt32LE(26) - var errorId = ads.dataStream.readUInt32LE(30) - var invokeId = ads.dataStream.readUInt32LE(34) - - logPackage.call(ads, 'receiving', ads.dataStream, commandId, invokeId) - - emitAdsError.call(ads, errorId) - - var totHeadSize = ads.tcpHeaderSize + ads.amsHeaderSize - var data = Buffer.alloc(length) +const analyseResponse = ads => { + const commandId = ads.dataStream.readUInt16LE(22) + const length = ads.dataStream.readUInt32LE(26) + const errorId = ads.dataStream.readUInt32LE(30) + const invokeId = ads.dataStream.readUInt32LE(34) + logPackage(ads, 'receiving', ads.dataStream, commandId, invokeId) + emitAdsError(ads, errorId) + const totHeadSize = ads.tcpHeaderSize + ads.amsHeaderSize + const data = Buffer.alloc(length) ads.dataStream.copy(data, 0, totHeadSize, totHeadSize + length) if (ads.dataStream.length > totHeadSize + length) { - var nextdata = Buffer.alloc(ads.dataStream.length - totHeadSize - length) + const nextdata = Buffer.alloc(ads.dataStream.length - totHeadSize - length) ads.dataStream.copy(nextdata, 0, totHeadSize + length) ads.dataStream = nextdata } else ads.dataStream = null - if (commandId === ID_NOTIFICATION) { // Special case: Notifications are initialised from the server socket - getNotificationResult.call(ads, data) + getNotificationResult(ads, data) } else if (ads.pending[invokeId]) { - var cb = ads.pending[invokeId].cb + const cb = ads.pending[invokeId].cb clearTimeout(ads.pending[invokeId].timeout) delete ads.pending[invokeId] - if (!cb) { debug(ads.dataStream, invokeId, commandId) throw new Error("Received a response, but I can't find the request") } - switch (commandId) { - case ID_READ_DEVICE_INFO: - getDeviceInfoResult.call(ads, data, cb) - break - case ID_READ: - getReadResult.call(ads, data, cb) - break - case ID_WRITE: - getWriteResult.call(ads, data, cb) - break - case ID_READ_STATE: - getReadStateResult.call(ads, data, cb) - break - case ID_WRITE_CONTROL: - // writeControl.call(ads, data, cb) - break - case ID_ADD_NOTIFICATION: - getAddDeviceNotificationResult.call(ads, data, cb) - break - case ID_DEL_NOTIFICATION: - getDeleteDeviceNotificationResult.call(ads, data, cb) - break - case ID_READ_WRITE: - getWriteReadResult.call(ads, data, cb) - break - default: - throw new Error('Unknown command') + case ID_READ_DEVICE_INFO: getDeviceInfoResult(ads, data, cb); break + case ID_READ: getReadResult(ads, data, cb); break + case ID_WRITE: getWriteResult(ads, data, cb); break + case ID_READ_STATE: getReadStateResult(ads, data, cb); break + case ID_WRITE_CONTROL: /* writeControl(ads, data, cb); */ break + case ID_ADD_NOTIFICATION: getAddDeviceNotificationResult(ads, data, cb); break + case ID_DEL_NOTIFICATION: getDeleteDeviceNotificationResult(ads, data, cb); break + case ID_READ_WRITE: getWriteReadResult(ads, data, cb); break + default: throw new Error('Unknown command') } } - checkResponseStream.call(ads) + checkResponseStream(ads) } /// //////////////////// ADS FUNCTIONS /////////////////////// -var readDeviceInfo = function (cb) { - var ads = this - var buf = Buffer.alloc(0) - - var options = { +const readDeviceInfo = (ads, cb) => { + const buf = Buffer.alloc(0) + const options = { commandId: ID_READ_DEVICE_INFO, data: buf, cb: cb } - runCommand.call(ads, options) + runCommand(ads, options) } -var readState = function (cb) { - var ads = this - var buf = Buffer.alloc(0) - - var options = { +const readState = (ads, cb) => { + const buf = Buffer.alloc(0) + const options = { commandId: ID_READ_STATE, data: buf, cb: cb } - runCommand.call(ads, options) + runCommand(ads, options) } -var multiRead = function (handles, cb) { - var ads = this - getHandles.call(ads, handles, function (err, handles) { +const multiRead = (ads, handles, cb) => { + getHandles(ads, handles, (err, handles) => { if (!err) { - var countreads = 0 - var readlen = 0 - handles.forEach(function(handle) { - if (!handle.err){ + let countreads = 0 + let readlen = 0 + handles.forEach(handle => { + if (!handle.err) { countreads++ - readlen += handle.totalByteLength+4 + readlen += handle.totalByteLength + 4 } }) - if (countreads>0) { - var buf = Buffer.alloc(12*countreads) - var index = 0 - handles.forEach(function(handle) { - if (!handle.err){ - buf.writeUInt32LE(handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE,index) - buf.writeUInt32LE(handle.symhandle,index+4) - buf.writeUInt32LE(handle.totalByteLength,index+8) - index+=12 + if (countreads > 0) { + let buf = Buffer.alloc(12 * countreads) + let index = 0 + handles.forEach(handle => { + if (!handle.err) { + buf.writeUInt32LE(handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE, index) + buf.writeUInt32LE(handle.symhandle, index + 4) + buf.writeUInt32LE(handle.totalByteLength, index + 8) + index += 12 } }) - var commandOptions = { + const commandOptions = { indexGroup: ADSIGRP.SUMUP_READ, indexOffset: countreads, writeBuffer: buf, readLength: readlen, symname: 'multiRead' } - writeReadCommand.call(ads, commandOptions, function (err, result) { + writeReadCommand(ads, commandOptions, (err, result) => { if (err) { - if (typeof cb !== 'undefined') cb.call(ads.adsClient, err) + if (typeof cb !== 'undefined') cb(err) } else { if (result.length > 0) { - var resultpos = 0 - var handlespos = countreads*4 - handles.forEach( function (handle){ - if (!handle.err){ - var adsError = result.readUInt32LE(resultpos) - resultpos+=4 - if (adsError!=0) { - handle.err = adsError - } - if (handle.totalByteLength>0) { - var integrate = Buffer.alloc(handle.totalByteLength) - result.copy(integrate,0,handlespos,handlespos+handle.totalByteLength) + let resultpos = 0 + let handlespos = countreads * 4 + handles.forEach(handle => { + if (!handle.err) { + const adsError = result.readUInt32LE(resultpos) + resultpos += 4 + if (adsError != 0) handle.err = adsError + if (handle.totalByteLength > 0) { + let integrate = Buffer.alloc(handle.totalByteLength) + result.copy(integrate, 0, handlespos, handlespos + handle.totalByteLength) integrateResultInHandle(handle, integrate) } - handlespos+=handle.totalByteLength + handlespos += handle.totalByteLength } }) } - cb.call(ads.adsClient, null, handles) + if (typeof cb !== 'undefined') cb(null, handles) } }) - } else { - cb.call(ads.adsClient, null, handles) - } - } else { - cb.call(ads.adsClient, err) - } + } else if (typeof cb !== 'undefined') cb(null, handles) + } else if (typeof cb !== 'undefined') cb(err) }) } -var multiWrite = function (handles, cb) { - var ads = this - getHandles.call(ads, handles, function (err, handles) { +const multiWrite = (ads, handles, cb) => { + getHandles(ads, handles, (err, handles) => { if (!err) { - var countwrites = 0 - var writelen = 0 - handles.forEach(function(handle) { - if (!handle.err){ + let countwrites = 0 + let writelen = 0 + handles.forEach(handle => { + if (!handle.err) { countwrites++ - writelen+=12+handle.totalByteLength + writelen += 12 + handle.totalByteLength } }) - if (countwrites>0) { - var buf = Buffer.alloc(writelen) - var index = 0 - var valindex = 12*countwrites - handles.forEach(function(handle) { - if (!handle.err){ - buf.writeUInt32LE(handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE,0) - buf.writeUInt32LE(handle.symhandle,4) - buf.writeUInt32LE(handle.totalByteLength,8) - index+=12 + if (countwrites > 0) { + let buf = Buffer.alloc(writelen) + let index = 0 + let valindex = 12 * countwrites + handles.forEach(handle => { + if (!handle.err) { + buf.writeUInt32LE(handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE, 0) + buf.writeUInt32LE(handle.symhandle, 4) + buf.writeUInt32LE(handle.totalByteLength, 8) + index += 12 getBytesFromHandle(handle) - handle.bytes.copy(buf,valindex,0,handle.bytes.length) - valindex+=handle.totalByteLength + handle.bytes.copy(buf, valindex, 0, handle.bytes.length) + valindex += handle.totalByteLength } }) - var commandOptions = { + const commandOptions = { indexGroup: ADSIGRP.SUMUP_WRITE, indexOffset: countwrites, writeBuffer: buf, - readLength: countwrites*4, + readLength: countwrites * 4, symname: 'multiWrite' } - writeReadCommand.call(ads, commandOptions, function (err, result) { + writeReadCommand(ads, commandOptions, (err, result) => { if (err) { - cb.call(ads.adsClient, err) + if (typeof cb !== 'undefined') cb(err) } else { if (result.length > 0) { - var resultpos = 0 - handles.forEach( function (handle){ - if (!handle.err){ - var adsError = result.readUInt32LE(resultpos) - resultpos+=4 - if (adsError!=0) { - handle.err = adsError - } + let resultpos = 0 + handles.forEach(handle => { + if (!handle.err) { + const adsError = result.readUInt32LE(resultpos) + resultpos += 4 + if (adsError != 0) handle.err = adsError } }) } - cb.call(ads.adsClient, null, handles) + if (typeof cb !== 'undefined') cb(null, handles) } }) - } else { - cb.call(ads.adsClient, null, handles) - } - } else { - cb.call(ads.adsClient, err) - } + } else if (typeof cb !== 'undefined') cb(null, handles) + } else if (typeof cb !== 'undefined') cb(err) }) } -var getHandles = function (handles, cb) { - var ads = this - var countsymnames = 0 - var buflength = 0 - handles.forEach( function (handle){ +const getHandles = (ads, handles, cb) => { + let countsymnames = 0 + let buflength = 0 + handles.forEach(handle => { handle = parseHandle(handle) if (typeof handle.symname !== 'undefined') { countsymnames++ - buflength+=17+handle.symname.length + buflength += 17 + handle.symname.length } }) - if (countsymnames>0){ - var buf = Buffer.alloc(buflength) - var index = 0 - var indexsynname = countsymnames*16 - handles.forEach( function (handle){ + if (countsymnames > 0) { + let buf = Buffer.alloc(buflength) + let index = 0 + let indexsynname = countsymnames * 16 + handles.forEach(handle => { if (typeof handle.symname === 'undefined') { handle.symname = handle.indexOffset } else { - var bufsymname = stringToBuffer(handle.symname) - bufsymname.copy(buf,indexsynname,0,bufsymname.length) - indexsynname+=bufsymname.length - buf.writeUInt32LE(ADSIGRP.GET_SYMHANDLE_BYNAME,index+0) - buf.writeUInt32LE(0x00000000,index+4) - buf.writeUInt32LE(0x00000004,index+8) - buf.writeUInt32LE(bufsymname.length,index+12) - index+=16 + let bufsymname = stringToBuffer(handle.symname) + bufsymname.copy(buf, indexsynname, 0, bufsymname.length) + indexsynname += bufsymname.length + buf.writeUInt32LE(ADSIGRP.GET_SYMHANDLE_BYNAME, index + 0) + buf.writeUInt32LE(0x00000000, index + 4) + buf.writeUInt32LE(0x00000004, index + 8) + buf.writeUInt32LE(bufsymname.length, index + 12) + index += 16 } }) - var commandOptions = { + const commandOptions = { indexGroup: ADSIGRP.SUMUP_READWRITE, indexOffset: countsymnames, writeBuffer: buf, - readLength: countsymnames*16, + readLength: countsymnames * 16, symname: 'getMultiHandle' } - writeReadCommand.call(ads, commandOptions, function (err, result) { + writeReadCommand(ads, commandOptions, (err, result) => { if (err) { - cb.call(ads.adsClient, err) + if (typeof cb !== 'undefined') cb(err) } else { if (result.length > 0) { - var resultpos = 0 - var handlespos = countsymnames*8 - handles.forEach( function (handle){ + let resultpos = 0 + let handlespos = countsymnames * 8 + handles.forEach(handle => { if (typeof handle.symname !== 'undefined') { - var adsError = result.readUInt32LE(resultpos) - if (adsError) { - handle.err = adsError - } - var symhandlebyte = result.readUInt32LE(resultpos+4) - resultpos+=8 - if (symhandlebyte==4) { + const adsError = result.readUInt32LE(resultpos) + if (adsError) handle.err = adsError + const symhandlebyte = result.readUInt32LE(resultpos + 4) + resultpos += 8 + if (symhandlebyte == 4) handle.symhandle = result.readUInt32LE(handlespos) - } - handlespos+=symhandlebyte - var symHandleToRelease = Buffer.alloc(4) - symHandleToRelease.writeUInt32LE(handle.symhandle,0) + handlespos += symhandlebyte + let symHandleToRelease = Buffer.alloc(4) + symHandleToRelease.writeUInt32LE(handle.symhandle, 0) ads.symHandlesToRelease.push(symHandleToRelease) } }) } - cb.call(ads.adsClient, null, handles) + if (typeof cb !== 'undefined') cb(null, handles) } }) - } else cb.call(ads.adsClient, null, handles) + } else if (typeof cb !== 'undefined') cb(null, handles) } -var read = function (handle, cb) { - var ads = this - getHandle.call(ads, handle, function (err, handle) { +const read = (ads, handle, cb) => { + getHandle(ads, handle, (err, handle) => { if (!err) { - var commandOptions = { + const commandOptions = { indexGroup: handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE, - indexOffset: handle.symhandle, + indexOffset: handle.indexOffset || handle.symhandle, bytelength: handle.totalByteLength, symname: handle.symnane } - - readCommand.call(ads, commandOptions, function (err, result) { + readCommand(ads, commandOptions, (err, result) => { if (result) integrateResultInHandle(handle, result) - cb.call(ads.adsClient, err, handle) + //if (typeof cb !== 'undefined') cb(err, result) + if (typeof cb !== 'undefined') cb(err, handle) }) - } else { - cb.call(ads.adsClient, err) - } + } else if (typeof cb !== 'undefined') cb(err) }) } -var write = function (handle, cb) { - var ads = this - getHandle.call(ads, handle, function (err, handle) { +const write = (ads, handle, cb) => { + getHandle(ads, handle, (err, handle) => { if (!err) { getBytesFromHandle(handle) - var commandOptions = { + const commandOptions = { indexGroup: handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE, - indexOffset: handle.symhandle, + indexOffset: handle.indexOffset || handle.symhandle, bytelength: handle.totalByteLength, bytes: handle.bytes, symname: handle.symname } - - writeCommand.call(ads, commandOptions, function (err, result) { - cb.call(ads.adsClient, err) + writeCommand(ads, commandOptions, (err, result) => { + if (result) integrateResultInHandle(handle, result) + if (typeof cb !== 'undefined') cb(err, handle) }) - } else { - cb.call(ads.adsClient, err) - } + } else if (typeof cb !== 'undefined') cb(err) }) } -var notify = function (handle, cb) { - var ads = this - getHandle.call(ads, handle, function (err, handle) { +const notify = (ads, handle, cb) => { + getHandle(ads, handle, (err, handle) => { if (!err) { - var commandOptions = { + const commandOptions = { indexGroup: handle.indexGroup || ADSIGRP.RW_SYMVAL_BYHANDLE, - indexOffset: handle.symhandle, + indexOffset: handle.indexOffset || handle.symhandle, bytelength: handle.totalByteLength, transmissionMode: handle.transmissionMode, maxDelay: handle.maxDelay, cycleTime: handle.cycleTime, symname: handle.symname } - - addNotificationCommand.call(ads, commandOptions, function (err, notiHandle) { + addNotificationCommand(ads, commandOptions, (res, err, notiHandle) => { if (!err) { - if (ads.options.verbose > 0) { - debug('Add notiHandle ' + notiHandle) - } - + if (ads.options.verbose > 0) debug('Add notiHandle ' + notiHandle) handle.notifyHandle = notiHandle - this.notifications[notiHandle] = handle - } - if (typeof cb !== 'undefined') { - cb.call(ads.adsClient, err) + res.notifications[notiHandle] = handle } + if (typeof cb !== 'undefined') cb(err) }) - } else if (typeof cb !== 'undefined') cb.call(ads.adsClient, err) + } else if (typeof cb !== 'undefined') cb(err) }) } -var getSymbols = function (cb, raw) { - var ads = this - var cmdLength = { +const getSymbols = (ads, cb, raw) => { + const cmdLength = { indexGroup: ADSIGRP.SYM_UPLOADINFO2, indexOffset: 0x00000000, bytelength: 0x30 } - - var cmdSymbols = { + let cmdSymbols = { indexGroup: ADSIGRP.SYM_UPLOAD, indexOffset: 0x00000000 } - - readCommand.call(ads, cmdLength, function (err, result) { + readCommand(ads, cmdLength, (err, result) => { if (!err) { - var data = result.readInt32LE(4) + const data = result.readInt32LE(4) cmdSymbols.bytelength = data - - readCommand.call(ads, cmdSymbols, function (err, result) { - var symbols = [] - var initialPos = 0 + readCommand(ads, cmdSymbols, (err, result) => { + let symbols = [] + let initialPos = 0 if (!err) { while (initialPos < result.length) { - var symbol = {} - var pos = initialPos - var readLength = result.readUInt32LE(pos) + let symbol = {} + let pos = initialPos + const readLength = result.readUInt32LE(pos) initialPos = initialPos + readLength symbol.indexGroup = result.readUInt32LE(pos + 4) symbol.indexOffset = result.readUInt32LE(pos + 8) symbol.size = result.readUInt32LE(pos + 12) // symbol.type = result.readUInt32LE(pos + 16) //ADST_ ... // symbol.something = result.readUInt32LE(pos + 20) - var nameLength = result.readUInt16LE(pos + 24) + 1 - var typeLength = result.readUInt16LE(pos + 26) + 1 - var commentLength = result.readUInt16LE(pos + 28) + 1 + const nameLength = result.readUInt16LE(pos + 24) + 1 + const typeLength = result.readUInt16LE(pos + 26) + 1 + const commentLength = result.readUInt16LE(pos + 28) + 1 - pos = pos + 30 + pos += 30 - var nameBuf = Buffer.alloc(nameLength) + let nameBuf = Buffer.alloc(nameLength) result.copy(nameBuf, 0, pos, pos + nameLength) symbol.name = nameBuf.toString('binary', 0, findStringEnd(nameBuf, 0)) - pos = pos + nameLength + pos += nameLength - var typeBuf = Buffer.alloc(typeLength) + let typeBuf = Buffer.alloc(typeLength) result.copy(typeBuf, 0, pos, pos + typeLength) symbol.type = typeBuf.toString('binary', 0, findStringEnd(typeBuf, 0)) - pos = pos + typeLength + pos += typeLength - var commentBuf = Buffer.alloc(commentLength) + let commentBuf = Buffer.alloc(commentLength) result.copy(commentBuf, 0, pos, pos + commentLength) symbol.comment = commentBuf.toString('binary', 0, findStringEnd(commentBuf, 0)) - pos = pos + commentLength + pos += commentLength if (!raw && symbol.type.indexOf('ARRAY') > -1) { - var re = /ARRAY[\s]+\[([\-\d]+)\.\.([\-\d]+)\][\s]+of[\s]+(.*)/i - var m - + let re = /ARRAY[\s]+\[([\-\d]+)\.\.([\-\d]+)\][\s]+of[\s]+(.*)/i + let m if ((m = re.exec(symbol.type)) !== null) { - if (m.index === re.lastIndex) { - re.lastIndex++ - } - - m[1] = parseInt(m[1]) - m[2] = parseInt(m[2]) - - for (var i = m[1]; i <= m[2]; i++) { - var newSymbol = JSON.parse(JSON.stringify(symbol)) + if (m.index === re.lastIndex) re.lastIndex++ + let v1 = parseInt(m[1]) + let v2 = parseInt(m[2]) + for (let i = v1; i <= v2; i++) { + let newSymbol = JSON.parse(JSON.stringify(symbol)) newSymbol.arrayid = i + 0 - newSymbol.type = m[3] + '' - newSymbol.name += '[' + i + ']' + newSymbol.type = `${m[3]}` + newSymbol.name += `[${i}]` symbols.push(newSymbol) } } - } else { - symbols.push(symbol) - } + } else symbols.push(symbol) } } - - cb.call(ads.adsClient, err, symbols) + if (typeof cb !== 'undefined') cb(err, symbols) }) - } else { - cb.call(ads.adsClient, err) - } + } else if (typeof cb !== 'undefined') cb(err) }) } -var getDatatyps = function (cb) { - var ads = this - var cmdLength = { +const getDatatypes = (ads, cb) => { + const cmdLength = { indexGroup: ADSIGRP.SYM_UPLOADINFO2, indexOffset: 0x00000000, bytelength: 0x30 } - - var cmdDatatye = { + const cmdDatatye = { indexGroup: ADSIGRP.SYM_DT_UPLOAD, indexOffset: 0x00000000 } - - readCommand.call(ads, cmdLength, function (err, result) { + readCommand(ads, cmdLength, (err, result) => { if (!err) { - var data = result.readInt32LE(12) + const data = result.readInt32LE(12) cmdDatatye.bytelength = data - - - readCommand.call(ads, cmdDatatye, function (err, result) { - var datatyps = [] - var pos = 0 + readCommand(ads, cmdDatatye, (err, result) => { + let datatyps = [] + let pos = 0 if (!err) { - - function getDatatypEntry(datatyps,result,p,index) { - var pos = p - var datatyp = {} + const getDatatypEntry = (datatyps, result, p, index) => { + let pos = p + let datatyp = {} datatyps.push(datatyp) - if (index) { - datatyp.index = index - } - var readLength = result.readUInt32LE(pos) - datatyp.version = result.readUInt32LE(pos+4) + if (index) datatyp.index = index + const readLength = result.readUInt32LE(pos) + datatyp.version = result.readUInt32LE(pos + 4) //datatyp.hashValue = result.readUInt32LE(pos+8) //offsGetCode //datatyp.typeHashValue = result.readUInt32LE(pos+12) //offsSetCode - datatyp.size = result.readUInt32LE(pos+16) - datatyp.dataType = result.readUInt32LE(pos+24) - var flags = result.readUInt32LE(pos+28) - if (flags==2) { - datatyp.offs = result.readUInt32LE(pos+20) - } - var nameLength = result.readUInt16LE(pos + 32) + 1 - var typeLength = result.readUInt16LE(pos + 34) + 1 - var commentLength = result.readUInt16LE(pos + 36) + 1 - var arrayDim = result.readUInt16LE(pos + 38) + datatyp.size = result.readUInt32LE(pos + 16) + datatyp.dataType = result.readUInt32LE(pos + 24) + const flags = result.readUInt32LE(pos + 28) + if (flags == 2) datatyp.offs = result.readUInt32LE(pos + 20) + const nameLength = result.readUInt16LE(pos + 32) + 1 + const typeLength = result.readUInt16LE(pos + 34) + 1 + const commentLength = result.readUInt16LE(pos + 36) + 1 + const arrayDim = result.readUInt16LE(pos + 38) datatyp.arrayDim = arrayDim - var subItems = result.readUInt16LE(pos + 40) + const subItems = result.readUInt16LE(pos + 40) datatyp.subItems = subItems - pos = pos + 42 + pos += 42 - var nameBuf = Buffer.alloc(nameLength) + let nameBuf = Buffer.alloc(nameLength) result.copy(nameBuf, 0, pos, pos + nameLength) datatyp.name = nameBuf.toString('binary', 0, findStringEnd(nameBuf, 0)) - pos = pos + nameLength + pos += nameLength - var typeBuf = Buffer.alloc(typeLength) + let typeBuf = Buffer.alloc(typeLength) result.copy(typeBuf, 0, pos, pos + typeLength) datatyp.type = typeBuf.toString('binary', 0, findStringEnd(typeBuf, 0)) - pos = pos + typeLength + pos += typeLength - var commentBuf = Buffer.alloc(commentLength) + let commentBuf = Buffer.alloc(commentLength) result.copy(commentBuf, 0, pos, pos + commentLength) datatyp.comment = commentBuf.toString('binary', 0, findStringEnd(commentBuf, 0)) - pos = pos + commentLength + pos += commentLength - if (arrayDim>0) { + if (arrayDim > 0) { datatyp.array = [] - for (var i=0; i0) { + if (subItems > 0) { datatyp.datatyps = [] - for (var i=0; i { handle = parseHandle(handle) if (typeof handle.symname === 'undefined') { handle.symname = handle.indexOffset - cb.call(ads, null, handle) + if (typeof cb !== 'undefined') cb(null, handle) } else { - var buf = stringToBuffer(handle.symname) - + const buf = stringToBuffer(handle.symname) if (typeof handle.symhandle === 'undefined') { - var commandOptions = { + const commandOptions = { indexGroup: ADSIGRP.GET_SYMHANDLE_BYNAME, indexOffset: 0x00000000, writeBuffer: buf, readLength: 4, symname: handle.symname } - - writeReadCommand.call(ads, commandOptions, function (err, result) { + writeReadCommand(ads, commandOptions, (err, result) => { if (err) { - cb.call(ads, err) + if (typeof cb !== 'undefined') cb(err) } else { if (result.length > 0) { ads.symHandlesToRelease.push(result) handle.symhandle = result.readUInt32LE(0) - cb.call(ads, null, handle) + if (typeof cb !== 'undefined') cb(null, handle) } } }) - } else cb.call(ads, null, handle) + } else if (typeof cb !== 'undefined') cb(null, handle) } } -var releaseSymHandles = function (cb) { - var ads = this +const releaseSymHandles = (ads, cb) => { if (ads.symHandlesToRelease.length > 0) { - var symHandle = ads.symHandlesToRelease.shift() - releaseSymHandle.call(ads, symHandle, function () { - releaseSymHandles.call(ads, cb) - }) - } else if (typeof cb !== 'undefined') cb.call(ads) + const symHandle = ads.symHandlesToRelease.shift() + releaseSymHandle(ads, symHandle, () => releaseSymHandles(ads, cb)) + } else if (typeof cb !== 'undefined') cb() } -var releaseSymHandle = function (symhandle, cb) { - var ads = this +const releaseSymHandle = (ads, symhandle, cb) => { if (ads.connected) { - var commandOptions = { + const commandOptions = { indexGroup: ADSIGRP.RELEASE_SYMHANDLE, indexOffset: 0x00000000, bytelength: symhandle.length, bytes: symhandle } - writeCommand.call(ads, commandOptions, function (err) { - if (typeof cb !== 'undefined') cb.call(ads, err) + writeCommand(ads, commandOptions, err => { + if (typeof cb !== 'undefined') cb(err) }) - } else if (typeof cb !== 'undefined') cb.call(ads) + } else if (typeof cb !== 'undefined') cb() } -var releaseNotificationHandles = function (cb) { - var ads = this +const releaseNotificationHandles = (ads, cb) => { if (ads.notificationsToRelease.length > 0) { - var notificationHandle = ads.notificationsToRelease.shift() - deleteDeviceNotificationCommand.call(ads, notificationHandle, function () { - releaseNotificationHandles.call(ads, cb) - }) - } else if (typeof cb !== 'undefined') cb.call(ads) + const notificationHandle = ads.notificationsToRelease.shift() + deleteDeviceNotificationCommand(ads, notificationHandle, () => releaseNotificationHandles(ads, cb)) + } else if (typeof cb !== 'undefined') cb() } -var releaseNotificationHandle = function (handle,cb) { - var ads = this - if (handle.notifyHandle === 'undefined'){ - throw new Error("The handle doesn't have a notifyHandle!") - } - var index = ads.notificationsToRelease.indexOf(handle.notifyHandle) - if (index>-1) { +const releaseNotificationHandle = (ads, handle, cb) => { + if (handle.notifyHandle === 'undefined') throw new Error("The handle doesn't have a notifyHandle!") + const index = ads.notificationsToRelease.indexOf(handle.notifyHandle) + if (index > -1) { delete ads.notifications[handle.notifyHandle] - ads.notificationsToRelease.splice(index,1) - deleteDeviceNotificationCommand.call(ads, handle.notifyHandle, function () { + ads.notificationsToRelease.splice(index, 1) + deleteDeviceNotificationCommand(ads, handle.notifyHandle, () => { delete handle.notifyHandle - if (typeof cb !== 'undefined') cb.call(ads) + if (typeof cb !== 'undefined') cb() }) } } /// ///////////////////// COMMANDS /////////////////////// -var readCommand = function (commandOptions, cb) { - var buf = Buffer.alloc(12) +const readCommand = (ads, commandOptions, cb) => { + let buf = Buffer.alloc(12) buf.writeUInt32LE(commandOptions.indexGroup, 0) buf.writeUInt32LE(commandOptions.indexOffset, 4) buf.writeUInt32LE(commandOptions.bytelength, 8) - - var options = { + const options = { commandId: ID_READ, data: buf, cb: cb, symname: commandOptions.symname } - runCommand.call(this, options) + runCommand(ads, options) } -var writeCommand = function (commandOptions, cb) { - var buf = Buffer.alloc(12 + commandOptions.bytelength) +const writeCommand = (ads, commandOptions, cb) => { + let buf = Buffer.alloc(12 + commandOptions.bytelength) buf.writeUInt32LE(commandOptions.indexGroup, 0) buf.writeUInt32LE(commandOptions.indexOffset, 4) buf.writeUInt32LE(commandOptions.bytelength, 8) commandOptions.bytes.copy(buf, 12) - - var options = { + const options = { commandId: ID_WRITE, data: buf, cb: cb, symname: commandOptions.symname } - runCommand.call(this, options) + runCommand(ads, options) } -var addNotificationCommand = function (commandOptions, cb) { - var buf = Buffer.alloc(40); +const addNotificationCommand = (ads, commandOptions, cb) => { + let buf = Buffer.alloc(40); buf.writeUInt32LE(commandOptions.indexGroup, 0) buf.writeUInt32LE(commandOptions.indexOffset, 4) buf.writeUInt32LE(commandOptions.bytelength, 8) @@ -882,138 +719,119 @@ var addNotificationCommand = function (commandOptions, cb) { buf.writeUInt32LE(0, 28) buf.writeUInt32LE(0, 32) buf.writeUInt32LE(0, 36) - - var options = { + const options = { commandId: ID_ADD_NOTIFICATION, data: buf, cb: cb, symname: commandOptions.symname } - runCommand.call(this, options) + runCommand(ads, options) } -var writeReadCommand = function (commandOptions, cb) { - var buf = Buffer.alloc(16 + commandOptions.writeBuffer.length) +const writeReadCommand = (ads, commandOptions, cb) => { + let buf = Buffer.alloc(16 + commandOptions.writeBuffer.length) buf.writeUInt32LE(commandOptions.indexGroup, 0) buf.writeUInt32LE(commandOptions.indexOffset, 4) buf.writeUInt32LE(commandOptions.readLength, 8) buf.writeUInt32LE(commandOptions.writeBuffer.length, 12) commandOptions.writeBuffer.copy(buf, 16) - - var options = { + const options = { commandId: ID_READ_WRITE, data: buf, cb: cb, symname: commandOptions.symname } - runCommand.call(this, options) + runCommand(ads, options) } -var deleteDeviceNotificationCommand = function (notificationHandle, cb) { - var ads = this +const deleteDeviceNotificationCommand = (ads, notificationHandle, cb) => { if (ads.connected) { - var buf = Buffer.alloc(4) + let buf = Buffer.alloc(4) buf.writeUInt32LE(notificationHandle, 0) - - var options = { + const options = { commandId: ID_DEL_NOTIFICATION, data: buf, cb: cb } - runCommand.call(ads, options) - } else if (typeof cb !== 'undefined') cb.call(ads) + runCommand(ads, options) + } else if (typeof cb !== 'undefined') cb() } -var runCommand = function (options) { - var ads = this - var tcpHeaderSize = 6 - var headerSize = 32 - var offset = 0 +const runCommand = (ads, options) => { + const tcpHeaderSize = 6 + const headerSize = 32 + let offset = 0 - if (!options.cb) { - throw new Error('A command needs a callback function!') - } + if (!options.cb) throw new Error('A command needs a callback function!') - var header = Buffer.alloc(headerSize + tcpHeaderSize) + let header = Buffer.alloc(headerSize + tcpHeaderSize) - // 2 bytes resserver (=0) header.writeUInt16LE(0, offset) - offset += 2 + offset += 2 // 2 bytes resserver (=0) - // 4 bytes length header.writeUInt32LE(headerSize + options.data.length, offset) - offset += 4 + offset += 4 // 4 bytes length - // 6 bytes: amsNetIdTarget - var amsNetIdTarget = ads.options.amsNetIdTarget.split('.') - for (var i = 0; i < amsNetIdTarget.length; i++) { + let amsNetIdTarget = ads.options.amsNetIdTarget.split('.') + for (let i = 0; i < amsNetIdTarget.length; i++) { if (i >= 6) { throw new Error('Incorrect amsNetIdTarget length!') } amsNetIdTarget[i] = parseInt(amsNetIdTarget[i], 10) header.writeUInt8(amsNetIdTarget[i], offset) - offset++ + offset++ // 6 bytes: amsNetIdTarget } - // 2 bytes: amsPortTarget header.writeUInt16LE(ads.options.amsPortTarget, offset) - offset += 2 + offset += 2 // 2 bytes: amsPortTarget - // 6 bytes amsNetIdSource - var amsNetIdSource = ads.options.amsNetIdSource.split('.') - for (i = 0; i < amsNetIdSource.length; i++) { + let amsNetIdSource = ads.options.amsNetIdSource.split('.') + for (let i = 0; i < amsNetIdSource.length; i++) { if (i >= 6) { throw new Error('Incorrect amsNetIdSource length!') } amsNetIdSource[i] = parseInt(amsNetIdSource[i], 10) header.writeUInt8(amsNetIdSource[i], offset) - offset++ + offset++ // 6 bytes amsNetIdSource } - // 2 bytes: amsPortTarget header.writeUInt16LE(ads.options.amsPortSource, offset) - offset += 2 + offset += 2 // 2 bytes: amsPortTarget - // 2 bytes: Command ID header.writeUInt16LE(options.commandId, offset) - offset += 2 + offset += 2 // 2 bytes: Command ID - // 2 bytes: state flags (ads request tcp) header.writeUInt16LE(4, offset) - offset += 2 + offset += 2 // 2 bytes: state flags (ads request tcp) - // 4 bytes: length of the data header.writeUInt32LE(options.data.length, offset) - offset += 4 + offset += 4 // 4 bytes: length of the data - // 4 bytes: error code header.writeUInt32LE(0, offset) - offset += 4 + offset += 4 // 4 bytes: error code - // 4 bytes: invoke id header.writeUInt32LE(++ads.invokeId, offset) - offset += 4 + offset += 4 // 4 bytes: invoke id - var buf = Buffer.alloc(tcpHeaderSize + headerSize + options.data.length) + let buf = Buffer.alloc(tcpHeaderSize + headerSize + options.data.length) header.copy(buf, 0, 0) options.data.copy(buf, tcpHeaderSize + headerSize, 0) - ads.pending[ads.invokeId] = {cb: options.cb, - timeout: setTimeout(function () { + ads.pending[ads.invokeId] = { + cb: options.cb, + timeout: setTimeout(() => { delete ads.pending[ads.invokeId] + if (typeof options.cb !== 'undefined') options.cb('timeout') + }, ads.options.timeout || 500) + } - options.cb('timeout') - }.bind(ads), ads.options.timeout)} - - logPackage.call(ads, 'sending', buf, options.commandId, ads.invokeId, options.symname) + logPackage(ads, 'sending', buf, options.commandId, ads.invokeId, options.symname) ads.tcpClient.write(buf) } /// ////////////////// COMMAND RESULT PARSING //////////////////////////// -var getDeviceInfoResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - // emitAdsError.call(ads, adsError) - var err = getError(adsError) - var result = {} - +const getDeviceInfoResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + // emitAdsError(ads, adsError) + const err = getError(adsError) + let result = {} if (!err) { try { result = { @@ -1022,116 +840,100 @@ var getDeviceInfoResult = function (data, cb) { versionBuild: data.readUInt16LE(6), deviceName: data.toString('binary', 8, findStringEnd(data, 8)) } - } catch(error) { - cb.call(ads.adsClient, err, result) + } catch (error) { + if (typeof cb !== 'undefined') cb(err, result) } } - - cb.call(ads.adsClient, err, result) + if (typeof cb !== 'undefined') cb(err, result) } -var getReadResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - var result - // emitAdsError.call(ads, adsError) - var err = getError(adsError) +const getReadResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + // emitAdsError(ads, adsError) + const err = getError(adsError) + let result if (!err) { - var bytelength = data.readUInt32LE(4) + const bytelength = data.readUInt32LE(4) result = Buffer.alloc(bytelength) data.copy(result, 0, 8, 8 + bytelength) } - cb.call(ads, err, result) + console.log(result) + if (typeof cb !== 'undefined') cb(err, result) } -var getWriteReadResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - var result - // emitAdsError.call(ads, adsError) - var err = getError(adsError) +const getWriteReadResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + // emitAdsError(ads, adsError) + const err = getError(adsError) + let result if (!err) { - var bytelength = data.readUInt32LE(4) + const bytelength = data.readUInt32LE(4) result = Buffer.alloc(bytelength) data.copy(result, 0, 8, 8 + bytelength) } - cb.call(ads, err, result) + if (typeof cb !== 'undefined') cb(err, result) } -var getWriteResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - var err = getError(adsError) - // emitAdsError.call(ads, adsError) - cb.call(ads, err) +const getWriteResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + const err = getError(adsError) + // emitAdsError(ads, adsError) + if (typeof cb !== 'undefined') cb(err) } -var getReadStateResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - // emitAdsError.call(ads, adsError) - var err = getError(adsError) - var result +const getReadStateResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + // emitAdsError(ads, adsError) + const err = getError(adsError) + let result if (!err) { result = { adsState: data.readUInt16LE(4), deviceState: data.readUInt16LE(6) } } - - cb.call(ads.adsClient, err, result) + if (typeof cb !== 'undefined') cb(err, result) } -var getAddDeviceNotificationResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - var notificationHandle - // emitAdsError.call(ads, adsError) - var err = getError(adsError) +const getAddDeviceNotificationResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + // emitAdsError(ads, adsError) + const err = getError(adsError) + let notificationHandle if (!err) { notificationHandle = data.readUInt32LE(4) ads.notificationsToRelease.push(notificationHandle) } - cb.call(ads, err, notificationHandle) + if (typeof cb !== 'undefined') cb(err, notificationHandle) } -var getDeleteDeviceNotificationResult = function (data, cb) { - var ads = this - var adsError = data.readUInt32LE(0) - // emitAdsError.call(ads, adsError) - var err = getError(adsError) - cb.call(ads, err) +const getDeleteDeviceNotificationResult = (ads, data, cb) => { + const adsError = data.readUInt32LE(0) + // emitAdsError(ads, adsError) + const err = getError(adsError) + if (typeof cb !== 'undefined') cb(err) } -var getNotificationResult = function (data) { - var ads = this - var length = data.readUInt32LE(0) - var stamps = data.readUInt32LE(4) - var offset = 8 - var timestamp = 0 - var samples = 0 - var notiHandle = 0 - var size = 0 - - for (var i = 0; i < stamps; i++) { - timestamp = data.readUInt32LE(offset) // TODO 8 bytes and convert +const getNotificationResult = (ads, data) => { + //let timestamp + const stamps = data.readUInt32LE(4) + let offset = 8 + + for (let i = 0; i < stamps; i++) { + //timestamp = data.readUInt32LE(offset) // TODO 8 bytes and convert offset += 8 - samples = data.readUInt32LE(offset) + const samples = data.readUInt32LE(offset) offset += 4 - for (var j = 0; j < samples; j++) { - notiHandle = data.readUInt32LE(offset) + for (let j = 0; j < samples; j++) { + const notiHandle = data.readUInt32LE(offset) offset += 4 - size = data.readUInt32LE(offset) + const size = data.readUInt32LE(offset) offset += 4 - var buf = Buffer.alloc(size) + let buf = Buffer.alloc(size) data.copy(buf, 0, offset) offset += size - - if (ads.options.verbose > 0) { - debug('Get notiHandle ' + notiHandle) - } - - var handle = ads.notifications[notiHandle] + if (ads.options.verbose > 0) debug('Get notiHandle ' + notiHandle) + const handle = ads.notifications[notiHandle] // It can happen that there is a notification before I // even have the notification handle. @@ -1139,189 +941,115 @@ var getNotificationResult = function (data) { if (handle !== undefined) { integrateResultInHandle(handle, buf) ads.adsClient.emit('notification', handle) - } else { - if (ads.options.verbose > 0) { - debug('skipping notification ' + notiHandle) - } - } + } else if (ads.options.verbose > 0) debug('skipping notification ' + notiHandle) } } } /// ///////////////// HELPERS ///////////////////////////////////////// -var stringToBuffer = function (someString) { - var buf = Buffer.alloc(someString.length + 1) +const stringToBuffer = someString => { + someString = someString.toString() + let buf = Buffer.alloc(someString.length + 1) buf.write(someString) buf[someString.length] = 0 return buf } -var parseOptions = function (options) { +const parseOptions = options => { // Defaults - if (typeof options.port === 'undefined') { - options.port = 48898 - } - - if (typeof options.amsPortSource === 'undefined') { - options.amsPortSource = 32905 - } - - if (typeof options.amsPortTarget === 'undefined') { - options.amsPortTarget = 801 - } - - if (typeof options.timeout === 'undefined') { - options.timeout = 500 - } - - if (typeof options.host === 'undefined') { - throw new Error('host not defined!') - } - - if (typeof options.amsNetIdTarget === 'undefined') { - throw new Error('amsNetIdTarget not defined!') - } - - if (typeof options.amsNetIdSource === 'undefined') { - throw new Error('amsNetIdTarget not defined!') - } - - if (options.verbose === undefined) { - options.verbose = 0 - } - + if (typeof options.port === 'undefined') options.port = 48898 + if (typeof options.amsPortSource === 'undefined') options.amsPortSource = 32905 + if (typeof options.amsPortTarget === 'undefined') options.amsPortTarget = 801 + if (typeof options.timeout === 'undefined') options.timeout = 500 + if (typeof options.host === 'undefined') throw new Error('host not defined!') + if (typeof options.amsNetIdTarget === 'undefined') throw new Error('amsNetIdTarget not defined!') + if (typeof options.amsNetIdSource === 'undefined') throw new Error('amsNetIdTarget not defined!') + if (options.verbose === undefined) options.verbose = 0 return options } -var getCommandDescription = function (commandId) { - var desc = 'Unknown command' +const getCommandDescription = commandId => { switch (commandId) { - case ID_READ_DEVICE_INFO: - desc = 'Read device info' - break - case ID_READ: - desc = 'Read' - break - case ID_WRITE: - desc = 'Write' - break - case ID_READ_STATE: - desc = 'Read state' - break - case ID_WRITE_CONTROL: - desc = 'Write control' - break - case ID_ADD_NOTIFICATION: - desc = 'Add notification' - break - case ID_DEL_NOTIFICATION: - desc = 'Delete notification' - break - case ID_NOTIFICATION: - desc = 'Notification' - break - case ID_READ_WRITE: - desc = 'ReadWrite' - break + case ID_READ_DEVICE_INFO: return 'Read device info' + case ID_READ: return 'Read' + case ID_WRITE: return 'Write' + case ID_READ_STATE: return 'Read state' + case ID_WRITE_CONTROL: return 'Write control' + case ID_ADD_NOTIFICATION: return 'Add notification' + case ID_DEL_NOTIFICATION: return 'Delete notification' + case ID_NOTIFICATION: return 'Notification' + case ID_READ_WRITE: return 'ReadWrite' + default: return 'Unknown command' } - return desc } -var getValue = function (dataName, result, offset, useLocalTimezone) { - var value - var timeoffset + +const getValue = (dataName, result, offset, useLocalTimezone) => { switch (dataName) { - case 'BOOL': - value = result.readUInt8(offset) != 0 - break + case 'BOOL': return result.readUInt8(offset) != 0 case 'BYTE': - case 'USINT': - value = result.readUInt8(offset) - break - case 'SINT': - value = result.readInt8(offset) - break + case 'USINT': return result.readUInt8(offset) + case 'SINT': return result.readInt8(offset) case 'UINT': - case 'WORD': - value = result.readUInt16LE(offset) - break - case 'INT': - value = result.readInt16LE(offset) - break + case 'WORD': return result.readUInt16LE(offset) + case 'INT': return result.readInt16LE(offset) case 'DWORD': - case 'UDINT': - value = result.readUInt32LE(offset) - break - case 'DINT': - value = result.readInt32LE(offset) - break - case 'REAL': - value = result.readFloatLE(offset) - break - case 'LREAL': - value = result.readDoubleLE(offset) - break - case 'STRING': - value = result.toString('binary', offset, findStringEnd(result, offset)) - break + case 'UDINT': return result.readUInt32LE(offset) + case 'DINT': return result.readInt32LE(offset) + case 'REAL': return result.readFloatLE(offset) + case 'LREAL': return result.readDoubleLE(offset) + case 'STRING': return result.toString('binary', offset, findStringEnd(result, offset)) case 'TIME': case 'TIME_OF_DAY': - case 'TOD': - var milliseconds = result.readUInt32LE(offset) - value = new Date(milliseconds) + case 'TOD': { + const milliseconds = result.readUInt32LE(offset) + let value = new Date(milliseconds) if (useLocalTimezone) { - timeoffset = value.getTimezoneOffset() + let timeoffset = value.getTimezoneOffset() value = new Date(value.setMinutes(value.getMinutes() + timeoffset)) } - break + return value + } case 'DATE': case 'DATE_AND_TIME': - case 'DT': - var seconds = result.readUInt32LE(offset) - value = new Date(seconds * 1000) + case 'DT': { + const seconds = result.readUInt32LE(offset) + let value = new Date(seconds * 1000) if (useLocalTimezone) { - timeoffset = value.getTimezoneOffset() + let timeoffset = value.getTimezoneOffset() value = new Date(value.setMinutes(value.getMinutes() + timeoffset)) } - break + return value + } + default: return undefined } - return value } -var integrateResultInHandle = function (handle, result) { - var offset = 0 - var convert = {} - for (var i = 0; i < handle.propname.length; i++) { - getItemByteLength(handle.bytelength[i], convert) - - for (var idx = convert.lowIndex; idx <= convert.hiIndex; idx++){ - - var value = null - if (result.length >= (offset+convert.length)) { - if (convert.isAdsType) { - value = getValue(handle.bytelength[i].name, result, offset, checkUseLocalTimezone(handle,i)) - } else { - value = result.slice(offset, offset + (convert.length)) - } - } - if (convert.isAdsArray) { - setObjectProperty(handle,handle.propname[i]+"["+idx+"]",value,true) - } else { - setObjectProperty(handle,handle.propname[i],value,true) +const integrateResultInHandle = (handle, result) => { + let offset = 0 + let convert = {} + for (let i = 0; i < handle.propname.length; i++) { + getItemByteLength(handle.bytelength[i], convert) + for (let idx = convert.lowIndex; idx <= convert.hiIndex; idx++) { + let value = null + if (result.length >= (offset + convert.length)) { + if (convert.isAdsType) value = getValue(handle.bytelength[i].name, result, offset, checkUseLocalTimezone(handle, i)) + else value = result.slice(offset, offset + (convert.length)) } - + if (convert.isAdsArray) setObjectProperty(handle, handle.propname[i] + "[" + idx + "]", value, true) + else setObjectProperty(handle, handle.propname[i], value, true) offset += convert.length } } } -function getObjectProperty(handle,propname) { - var result = null - //var propParts = normalisePropertyExpression(propname) - normalisePropertyExpression(propname,function(err,propParts) { +const getObjectProperty = (handle, propname) => { + let result = null + //let propParts = normalisePropertyExpression(propname) + normalisePropertyExpression(propname, (err, propParts) => { if (!err) { - var m - propParts.reduce(function(obj, key) { + let m + propParts.reduce((obj, key) => { result = (typeof obj[key] !== "undefined" ? obj[key] : undefined) return result }, handle) @@ -1330,266 +1058,203 @@ function getObjectProperty(handle,propname) { return result } -function setObjectProperty(handle,propname,value,createMissing) { - if (typeof createMissing === 'undefined') { - createMissing = (typeof value !== 'undefined') - } - //var propParts = normalisePropertyExpression(propname) - normalisePropertyExpression(propname,function(err,propParts) { +const setObjectProperty = (handle, propname, value, createMissing) => { + if (typeof createMissing === 'undefined') createMissing = (typeof value !== 'undefined') + //let propParts = normalisePropertyExpression(propname) + normalisePropertyExpression(propname, (err, propParts) => { if (!err) { - var depth = 0 - var length = propParts.length - var obj = handle - var key - for (var i=0;i { + const length = propname.length if (length === 0) { - cb("Invalid property expression: zero-length",null) + if (typeof cb !== 'undefined') cb("Invalid property expression: zero-length", null) return false } - var parts = [] - var start = 0 - var inString = false - var inBox = false - var quoteChar - var v - for (var i=0;i + (typeof handle.bytelength[i].useLocalTimezone !== 'undefined' ? handle.bytelength[i].useLocalTimezone : + (typeof handle.useLocalTimezone === 'undefined' || handle.useLocalTimezone)) -var parseHandle = function (handle) { + +const parseHandle = handle => { if (typeof handle.symname === 'undefined' && - (typeof handle.indexGroup === 'undefined' || typeof handle.indexOffset === 'undefined') ) { + (typeof handle.indexGroup === 'undefined' || typeof handle.indexOffset === 'undefined')) { throw new Error("The handle doesn't have a symname or an indexGroup and indexOffset property!") } - - if (typeof handle.bytelength === 'undefined') { - handle.bytelength = [exports.BOOL] - } - + if (typeof handle.bytelength === 'undefined') handle.bytelength = [makeType('BOOL').length] if (typeof handle.propname !== 'undefined') { - if (!Array.isArray(handle.propname)) { - handle.propname = [handle.propname] - } + if (!Array.isArray(handle.propname)) handle.propname = [handle.propname] } else { - if (!Array.isArray(handle.bytelength)) { - handle.propname = ['value'] - } else { + if (!Array.isArray(handle.bytelength)) handle.propname = ['value'] + else { handle.propname = [] - for (var i = 0; i < handle.bytelength.length; i++) { - handle.propname[i] = 'value['+i+']' - } + for (let i = 0; i < handle.bytelength.length; i++) handle.propname[i] = `value[${i}]` } } - - if (!Array.isArray(handle.bytelength)) { - handle.bytelength = [handle.bytelength] - } - - if (handle.bytelength.length !== handle.propname.length) { - throw new Error('The array bytelength and propname should have the same length!') - } - + if (!Array.isArray(handle.bytelength)) handle.bytelength = [handle.bytelength] + if (handle.bytelength.length !== handle.propname.length) throw new Error('The array bytelength and propname should have the same length!') handle.totalByteLength = 0 - for (var i = 0; i < handle.bytelength.length; i++) { - handle.totalByteLength += getItemByteLength(handle.bytelength[i],{}) - normalisePropertyExpression(handle.propname[i], function(err) { - if (err) { - throw new Error(err) - } - }) + for (let i = 0; i < handle.bytelength.length; i++) { + handle.totalByteLength += getItemByteLength(handle.bytelength[i], {}) + normalisePropertyExpression(handle.propname[i], err => { if (err) throw new Error(err) }) } - - if (typeof handle.transmissionMode === 'undefined') { - handle.transmissionMode = exports.NOTIFY.ONCHANGE - } - - if (typeof handle.maxDelay === 'undefined') { - handle.maxDelay = 0 - } - - if (typeof handle.cycleTime === 'undefined') { - handle.cycleTime = 10 - } - + if (typeof handle.transmissionMode === 'undefined') handle.transmissionMode = NOTIFY.ONCHANGE + if (typeof handle.maxDelay === 'undefined') handle.maxDelay = 0 + if (typeof handle.cycleTime === 'undefined') handle.cycleTime = 10 return handle } -var getBytesFromHandle = function (handle) { - var p = '' - var buf = Buffer.alloc(handle.totalByteLength) - var offset = 0 - var convert = {} - for (var i = 0; i < handle.propname.length; i++) { +const getBytesFromHandle = handle => { + let p = '' + let buf = Buffer.alloc(handle.totalByteLength) + let offset = 0 + let convert = {} + for (let i = 0; i < handle.propname.length; i++) { p = handle.propname[i] getItemByteLength(handle.bytelength[i], convert) - - for (var idx = convert.lowIndex; idx <= convert.hiIndex; idx++) { - - var val = getObjectProperty(handle,p) - if (convert.isAdsArray) { - val = val[idx] - } - - if (!convert.isAdsType) { - val.copy(buf, offset,0,convert.length) - } - - if ((typeof val !== 'undefined') && convert.isAdsType && (buf.length >= offset+convert.length)) { - var datetime - var timeoffset + for (let idx = convert.lowIndex; idx <= convert.hiIndex; idx++) { + let val = getObjectProperty(handle, p) + if (convert.isAdsArray) val = val[idx] + if (!convert.isAdsType) val.copy(buf, offset, 0, convert.length) + if ((typeof val !== 'undefined') && convert.isAdsType && (buf.length >= offset + convert.length)) { + let datetime + let timeoffset switch (handle.bytelength[i].name) { case 'BOOL': case 'BYTE': @@ -1620,24 +1285,24 @@ var getBytesFromHandle = function (handle) { buf.writeDoubleLE(val, offset) break case 'STRING': - var stringbuf = Buffer.from(val.toString().slice(0,convert.length-1) + '\0', 'binary') + let stringbuf = Buffer.from(val.toString().slice(0, convert.length - 1) + '\0', 'binary') stringbuf.copy(buf, offset) break case 'TIME': case 'TIME_OF_DAY': case 'TOD': datetime = new Date(val) - if (checkUseLocalTimezone(handle,i)) { + if (checkUseLocalTimezone(handle, i)) { timeoffset = datetime.getTimezoneOffset() datetime = new Date(datetime.setMinutes(datetime.getMinutes() - timeoffset)) } - buf.writeUInt32LE( datetime.getTime(), offset) + buf.writeUInt32LE(datetime.getTime(), offset) break case 'DATE': case 'DATE_AND_TIME': case 'DT': datetime = new Date(val) - if (checkUseLocalTimezone(handle,i)) { + if (checkUseLocalTimezone(handle, i)) { timeoffset = datetime.getTimezoneOffset() datetime = new Date(datetime.setMinutes(datetime.getMinutes() - timeoffset)) } @@ -1645,108 +1310,87 @@ var getBytesFromHandle = function (handle) { break } } else if (typeof val === 'undefined') throw new Error('Property ' + p + ' not available on handle!') - offset+=convert.length + offset += convert.length } } - handle.bytes = buf } -var getItemByteLength = function (bytelength, convert) { +const getItemByteLength = (bytelength, convert) => { convert.isAdsType = false convert.isAdsArray = false convert.lowIndex = 0 convert.hiIndex = 0 convert.arrayElements = 1 convert.length = 0 - if (typeof bytelength === 'number') { + if (typeof bytelength === 'number') convert.length = bytelength - } else { + else { convert.length = bytelength.length - if (typeof bytelength.lowIndex !== 'undefined' && - typeof bytelength.hiIndex !== 'undefined') { - convert.arrayElements = (bytelength.hiIndex-bytelength.lowIndex+1) + if (typeof bytelength.lowIndex !== 'undefined' && typeof bytelength.hiIndex !== 'undefined') { + convert.arrayElements = (bytelength.hiIndex - bytelength.lowIndex + 1) convert.lowIndex = 0 - convert.hiIndex = convert.arrayElements -1 + convert.hiIndex = convert.arrayElements - 1 convert.isAdsArray = true } convert.isAdsType = true } - return (convert.length * convert.arrayElements) + return convert.length * convert.arrayElements } -var findStringEnd = function (data, offset) { +const findStringEnd = (data, offset) => { if (!offset) { offset = 0 } - var endpos = offset - for (var i = offset; i < data.length; i++) { + let endpos = offset + for (let i = offset; i < data.length; i++) if (data[i] === 0x00) { endpos = i break } - } return endpos } -var logPackage = function (info, buf, commandId, invokeId, symname) { - while (info.length < 10) info = info + ' ' - - var msg = info + ' -> commandId: ' + commandId - msg += ' (' + getCommandDescription(commandId) + ') ' - - msg += ', invokeId: ' + invokeId - - if (symname !== undefined) { - msg += ' symname: ' + symname - } - - if (this.options.verbose > 0) { - debug(msg) - } - - if (this.options.verbose > 1) { - debug(buf.inspect()) - // debug(buf) - } +const logPackage = (ads, info, buf, commandId, invokeId, symname) => { + let msg = `${(info + '').padEnd(10, ' ')} -> commandId: '${commandId}' (${getCommandDescription(commandId)}), invokeId: '${invokeId}'${symname !== undefined ? `, symname: ${symname}` : ''}` + if (ads.options.verbose > 0) debug(msg) + if (ads.options.verbose > 1) debug(buf.inspect()) } -var emitAdsError = function (errorId) { - var err = getError(errorId) - if (err) { - this.adsClient.emit('error', err) - } +const emitAdsError = (ads, errorId) => { + const err = getError(errorId) + if (err) ads.adsClient.emit('error_', err) } -var getError = function (errorId) { - var error = null - if (errorId > 0) { - error = new Error(ERRORS[errorId]) - } +const getError = (ads, errorId) => { + let error = null + if (errorId > 0) error = new Error(ERRORS[errorId]) return error } /// /////////////////////////// ADS TYPES ///////////////////////////////// -var adsType = { +const adsType = { length: 1, name: '' } -exports.makeType = function (name) { - var t = Object.create(adsType) +const makeType = name => { + let t = Object.create(adsType) t.length = typeLength[name] t.name = name return t } -function exportType (name) { - var t = exports.makeType(name) +exports.makeType = makeType + +const exportType = name => { + let t = makeType(name) Object.defineProperty(exports, name, { value: t, writable: false }) } -var typeLength = { +const typeLength = { 'BOOL': 1, 'BYTE': 1, 'WORD': 2, @@ -1795,31 +1439,28 @@ exportType('DATE_AND_TIME') exportType('DT') // DATE_AND_TIME alias exportType('STRING') -exports.string = function (length) { - var t = { +exports.string = length => { + let t = { length: 81, name: 'STRING' } - - if (typeof length !== 'undefined') { - t.length = arguments[0]+1 - } + if (typeof length !== 'undefined') t.length = length + 1 return t } -exports.array = function (typ,lowIndex,hiIndex) { - var t = Object.assign({},typ) +exports.array = (typ, lowIndex, hiIndex) => { + let t = Object.assign({}, typ) if (typeof lowIndex !== 'undefined' && - typeof hiIndex !== 'undefined' && - lowIndex <= hiIndex) { + typeof hiIndex !== 'undefined' && + lowIndex <= hiIndex) { t.lowIndex = lowIndex t.hiIndex = hiIndex } return t } -exports.useLocalTimezone = function (typ,use) { - var t = Object.assign({},typ) +exports.useLocalTimezone = (typ, use) => { + let t = Object.assign({}, typ) t.useLocalTimezone = typeof use === 'undefined' || use return t } @@ -1907,92 +1548,93 @@ const ERRORS = { } exports.ERRORS = ERRORS -exports.NOTIFY = { +const NOTIFY = { CYCLIC: 3, ONCHANGE: 4 } +exports.NOTIFY = NOTIFY const ADSSTATE = { - INVALID: 0, - IDLE: 1, - RESET: 2, - INIT: 3, - START: 4, - RUN: 5, - STOP: 6, - SAVECFG: 7, - LOADCFG: 8, + INVALID: 0, + IDLE: 1, + RESET: 2, + INIT: 3, + START: 4, + RUN: 5, + STOP: 6, + SAVECFG: 7, + LOADCFG: 8, POWERFAILURE: 9, - POWERGOOD: 10, - ERROR: 11, - SHUTDOWN: 12, - SUSPEND: 13, - RESUME: 14, - CONFIG: 15, - RECONFIG: 16, - STOPPING: 17, - fromId: function(id) { - var adsstates = this - var adsstate - Object.keys(adsstates).map(function(key){if (adsstates[key]==id) adsstate=key}) + POWERGOOD: 10, + ERROR: 11, + SHUTDOWN: 12, + SUSPEND: 13, + RESUME: 14, + CONFIG: 15, + RECONFIG: 16, + STOPPING: 17, + fromId: (adsstates, id) => { + let adsstate + Object.keys(adsstates).map(key => { if (adsstates[key] == id) adsstate = key }) return adsstate } } exports.ADSSTATE = ADSSTATE // ADS reserved index groups -var ADSIGRP = { - SYMTAB: 0xF000, - SYMNAME: 0xF001, - SYMVAL: 0xF002, +const ADSIGRP = { + SYMTAB: 0xF000, + SYMNAME: 0xF001, + SYMVAL: 0xF002, GET_SYMHANDLE_BYNAME: 0xF003, // {TcAdsDef.h: ADSIGRP_SYM_HNDBYNAME} - READ_SYMVAL_BYNAME: 0xF004, // {TcAdsDef.h: ADSIGRP_SYM_VALBYNAME} - RW_SYMVAL_BYHANDLE: 0xF005, // {TcAdsDef.h: ADSIGRP_SYM_VALBYHND} - RELEASE_SYMHANDLE: 0xF006, // {TcAdsDef.h: ADSIGRP_SYM_RELEASEHND} - SYM_INFOBYNAME: 0xF007, - SYM_VERSION: 0xF008, - SYM_INFOBYNAMEEX: 0xF009, - SYM_DOWNLOAD: 0xF00A, - SYM_UPLOAD: 0xF00B, - SYM_UPLOADINFO: 0xF00C, - SYM_DOWNLOAD2: 0xF00D, - SYM_DT_UPLOAD: 0xF00E, - SYM_UPLOADINFO2: 0xF00F, - SYMNOTE: 0xF010, // notification of named handle - SUMUP_READ: 0xF080, // AdsRW IOffs list size or 0 (=0 -> list size == WLength/3*sizeof(ULONG)) - // W: {list of IGrp, IOffs, Length} - // if IOffs != 0 then R: {list of results} and {list of data} - // if IOffs == 0 then R: only data (sum result) - SUMUP_WRITE: 0xF081, // AdsRW IOffs list size - // W: {list of IGrp, IOffs, Length} followed by {list of data} - // R: list of results - SUMUP_READWRITE: 0xF082, // AdsRW IOffs list size - // W: {list of IGrp, IOffs, RLength, WLength} followed by {list of data} - // R: {list of results, RLength} followed by {list of data} - SUMUP_READEX: 0xF083, // AdsRW IOffs list size - // W: {list of IGrp, IOffs, Length} - SUMUP_READEX2: 0xF084, // AdsRW IOffs list size - // W: {list of IGrp, IOffs, Length} - // R: {list of results, Length} followed by {list of data (returned lengths)} - SUMUP_ADDDEVNOTE: 0xF085, // AdsRW IOffs list size - // W: {list of IGrp, IOffs, Attrib} - // R: {list of results, handles} - SUMUP_DELDEVNOTE: 0xF086, // AdsRW IOffs list size - // W: {list of handles} - // R: {list of results, Length} followed by {list of data} - IOIMAGE_RWIB: 0xF020, // read/write input byte(s) - IOIMAGE_RWIX: 0xF021, // read/write input bit - IOIMAGE_RISIZE: 0xF025, // read input size (in byte) - IOIMAGE_RWOB: 0xF030, // read/write output byte(s) - IOIMAGE_RWOX: 0xF031, // read/write output bit - IOIMAGE_CLEARI: 0xF040, // write inputs to null - IOIMAGE_CLEARO: 0xF050, // write outputs to null - IOIMAGE_RWIOB: 0xF060, // read input and write output byte(s) - DEVICE_DATA: 0xF100, // state, name, etc... + READ_SYMVAL_BYNAME: 0xF004, // {TcAdsDef.h: ADSIGRP_SYM_VALBYNAME} + RW_SYMVAL_BYHANDLE: 0xF005, // {TcAdsDef.h: ADSIGRP_SYM_VALBYHND} + RELEASE_SYMHANDLE: 0xF006, // {TcAdsDef.h: ADSIGRP_SYM_RELEASEHND} + SYM_INFOBYNAME: 0xF007, + SYM_VERSION: 0xF008, + SYM_INFOBYNAMEEX: 0xF009, + SYM_DOWNLOAD: 0xF00A, + SYM_UPLOAD: 0xF00B, + SYM_UPLOADINFO: 0xF00C, + SYM_DOWNLOAD2: 0xF00D, + SYM_DT_UPLOAD: 0xF00E, + SYM_UPLOADINFO2: 0xF00F, + SYMNOTE: 0xF010, // notification of named handle + SUMUP_READ: 0xF080, // AdsRW IOffs list size or 0 (=0 -> list size == WLength/3*sizeof(ULONG)) + // W: {list of IGrp, IOffs, Length} + // if IOffs != 0 then R: {list of results} and {list of data} + // if IOffs == 0 then R: only data (sum result) + SUMUP_WRITE: 0xF081, // AdsRW IOffs list size + // W: {list of IGrp, IOffs, Length} followed by {list of data} + // R: list of results + SUMUP_READWRITE: 0xF082, // AdsRW IOffs list size + // W: {list of IGrp, IOffs, RLength, WLength} followed by {list of data} + // R: {list of results, RLength} followed by {list of data} + SUMUP_READEX: 0xF083, // AdsRW IOffs list size + // W: {list of IGrp, IOffs, Length} + SUMUP_READEX2: 0xF084, // AdsRW IOffs list size + // W: {list of IGrp, IOffs, Length} + // R: {list of results, Length} followed by {list of data (returned lengths)} + SUMUP_ADDDEVNOTE: 0xF085, // AdsRW IOffs list size + // W: {list of IGrp, IOffs, Attrib} + // R: {list of results, handles} + SUMUP_DELDEVNOTE: 0xF086, // AdsRW IOffs list size + // W: {list of handles} + // R: {list of results, Length} followed by {list of data} + IOIMAGE_RWIB: 0xF020, // read/write input byte(s) + IOIMAGE_RWIX: 0xF021, // read/write input bit + IOIMAGE_RISIZE: 0xF025, // read input size (in byte) + IOIMAGE_RWOB: 0xF030, // read/write output byte(s) + IOIMAGE_RWOX: 0xF031, // read/write output bit + IOIMAGE_CLEARI: 0xF040, // write inputs to null + IOIMAGE_CLEARO: 0xF050, // write outputs to null + IOIMAGE_RWIOB: 0xF060, // read input and write output byte(s) + DEVICE_DATA: 0xF100, // state, name, etc... } exports.ADSIGRP = ADSIGRP -exports.ADSIOFFS_DEVDATA = { - ADSSTATE: 0x0000, // ads state of device - DEVSTATE: 0x0002 // device state +const ADSIOFFS_DEVDATA = { + ADSSTATE: 0x0000, // ads state of device + DEVSTATE: 0x0002 // device state } +exports.ADSIOFFS_DEVDATA = ADSIOFFS_DEVDATA