node-red-contrib-clowire
Version:
clowire for node-red
54 lines (43 loc) • 1.25 kB
JavaScript
const handleTcpObj = {
'clowire': require('../clowire/handleTcp'),
'alive': require('node-red-contrib-clowire/alive/handleTcp'),
'hasskit': require('node-red-contrib-clowire/hasskit/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