我的脚本在几小时后运行正常,当我运行它时,我从节点脚本中收到以下错误...
Error: Cannot parse topic
at Parser._parsePublish (/home/ubuntu/we2/node_modules/mqtt-packet/parser.js:242:53)
at Parser._parsePayload (/home/ubuntu/we2/node_modules/mqtt-packet/parser.js:104:14)
at Parser.parse (/home/ubuntu/we2/node_modules/mqtt-packet/parser.js:37:48)
at Writable.writable._write (/home/ubuntu/we2/node_modules/mqtt/lib/client.js:258:12)
at doWrite (/home/ubuntu/we2/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:406:64)
at writeOrBuffer (/home/ubuntu/we2/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:395:5)
at Writable.write (/home/ubuntu/we2/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:322:11)
at Socket.ondata (_stream_readable.js:557:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
脚本正在使用mqtt.js监听mosquitto代理。
PS:代码没有更新或更改。 Mosquitto经纪人运行正常并连接到测试客户端。
感谢。
代码:
var moment = require('moment-timezone');
moment().tz("America/Los_Angeles").format();
//mqtt initialize
const mqtt = require('mqtt')
const client = mqtt.connect('----', {
username: '',
password: ''
});
//influx initalisation
var influx = require('influx')
var username = 'root'
var password = 'root'
var database = '---'
//db details
GLOBAL.dat = [];
var client_db = influx({host: 'localhost', username: username, password: password, database: database})
//redis initialisation
var redis = require('redis');
var redisClient = redis.createClient({host: 'localhost', port: 6379});
var sub = redis.createClient(), pub = redis.createClient();
//general declarations
var msg_count = 0;
var gateID = "";
sub.on("subscribe", function (channel, count) {
console.log('info', 'IPC Communication Initiated on channel: ' + channel)
});
sub.on("message", function (channel, message) {
console.log('info', 'IPC Communication Buffer %s', message)
});
//mqtt client
client.on('connect', function () {
client.subscribe('Topic/#')
console.log('info', 'Subscribing to topic %s', "FlowData")
})
client.on('message', function (topic, message) {
console.log("topic is " + topic.toString());
console.log("message is " + message.toString());
//removethis later
var chk = (topic.toString()).substr(0, 2);
var chkFlow = (topic.toString()).substr(12, 2);
if (chk == "Fl") {
console.log('info', "FHandler Inititated");
var messSplit = String(message).split("/");
gateID = String(messSplit[0]);
var compID = String(messSplit[1]);
var temp = String(messSplit[2]).split(":");
var compTime = String(temp[1]);
var compValue = parseInt(temp[0]);
redisPrevData(compID, function (response) {
if (typeof response !== "object") {
console.log(response);
}
else {
prevCompValue == 'NDF';
core(compID, compValue, function (response) {
console.log(response);
});
}
});
client_db.writePoint(gateID.toString(), {value: compValue}, {compID: compID.toString()}, {time: compTime}, function (e, response) {
if (e) throw e
})
console.log('info', "Written data to TimeSeries");
currentDate = moment().format("YYYY-MM-DD hh:mm:ss");
console.log('info', "IPC Send Data to other handlers");
console.log('info', "FHandler Completed");
}
})
function redisPrevData(compID, callback) {
vardummy = apartID + currentDate;
console.log(vardummy);
redisClient.getAsync(vardummy).then(function (res) {
console.log("redis : " + res);
return callback(res);
});
}
function core(compID, value) {
if (prevCompValue == 'NDF') {
redisClient.set(compID, value, redisClient.print);
return callback("New comp init - written to redis");
}
else {
if (compValue < prevCompValue) {
//alarm to trigger - ignore the data log it seprately.
return callback("ALARM TRIGGER ILLEGAL VALUES");
}
else if (compValue >= prevCompValue) {
//update in influx
return callback("Normal - writing to influx");
}
}
}