对于一些js功能,可以通过nodejs快速搭建测试环境
1、这里我们先通过express脚手架快速搭建一个项目,或者init一个空项目
2、mkdir script
3、这里假设我们的场景是MQTT接收消息并存入数据库(6字段字节数分别为212212)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 | var mqtt = require('mqtt');var Sequelize = require('sequelize');var sequelize = new Sequelize('iot', 'root', 'root', { host: '127.0.0.1', dialect: 'mysql', timezone:'+08:00', pool: { max: 5, min: 0, idle: 10000 } })var SmallU = sequelize.define('table1', { 大专栏 nodejs快速测试 id: { type: Sequelize.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true }, deviceName:{ type: Sequelize.STRING, field:'device_name', comment:"设备id" }, temperature: { type: Sequelize.DOUBLE, comment:"温度" }, humidity: { type: Sequelize.DOUBLE, allowNull: false, comment: "湿度" }, pm25: { type: Sequelize.DOUBLE, allowNull: false, comment: "PM2.5" }, formaldehyde: { type: Sequelize.DOUBLE, allowNull: false, comment: "甲醛" }, batteryLevel: { type: Sequelize.STRING, field:"battery_level", comment:"电池电量" }, counter: { type: Sequelize.INTEGER, comment:"计数" }}, { underscored: true,//驼峰式 freezeTableName: true, // paranoid: true // tableName: 'user_less_add'});sequelize.sync({force: false})//MQTTvar client = mqtt.connect({port: 8883, host: '127.0.0.1', keepalive: 60});let topic = 'application/7/node/+/rx';client.subscribe(topic);client.on('message', function (topic, message, puback) { var data = JSON.parse(message.toString()); console.log("========开始====="); console.log(data); console.log("========结束===="); const deviceName = data.deviceName; var rowData = Buffer(data.data, 'base64') var temperature = parseInt(rowData.subarray(0,2).toString('hex'),16)/10; //温度 var humidity = parseInt(rowData.subarray(2,3).toString('hex'),16); //湿度 var pm25 = parseInt(rowData.subarray(3,5).toString('hex'),16); //PM2.5 var formaldehyde = parseInt(rowData.subarray(5,7).toString('hex'),16)/1000; //甲醛 var batteryLevel = parseInt(rowData.subarray(7,8).toString('hex'),16); //电池电量 var counter = parseInt(rowData.subarray(8,10).toString('hex'),16); //计数 SmallU.create({ deviceName:deviceName, temperature:temperature, humidity:humidity, pm25:pm25, formaldehyde:formaldehyde, batteryLevel:batteryLevel, counter:counter })}); |
来源:https://www.cnblogs.com/lijianming180/p/12014337.html