node-red-contrib-hasskit
Version:
hasskit for node-red
54 lines (44 loc) • 1.24 kB
JavaScript
const handleTcpObj = {
'clowire': require('../clowire/handleTcp'),
'alive': require('../alive/handleTcp'),
'hasskit': require('../hasskit/handleTcp'),
'lf': require('../lf/handleTcp')
}
class handleTcp {
constructor(tcpClient, devtype) {
this.tcpClient = tcpClient
this._onData = null
this._devtype = devtype
this.tcpClient.onData = [this._receiveData, this]
this.handleTcp = handleTcpObj[devtype]
}
set onData(cb) {
this._onData = cb
}
/**
* 收到tcp数据预处理
* @param {*} data
*/
_receiveData(data) {
const buf = this.handleTcp.handleRece(data)
if (!buf && !this._onData) return
this._onData(buf)
}
/**
*
* @param {同步带锁发送数据}} buf
* @returns
*/
write_lock(buf) {
const buffer = this.handleTcp.handle_write(buf)
if (!buffer) return
this.tcpClient.write_lock(buffer)
}
getBufToObj(domain) {
return this.handleTcp.getBufToObj(domain)
}
getObjToBuf(domain) {
return this.handleTcp.getObjToBuf(domain)
}
}
module.exports = handleTcp