node.js - 数据未传递到 SQL Server
问题描述
我正在做一个项目,我必须将传感器数据保存到 SQL Server 中。我为传感器和 SQL Server 创建了编码。我可以INSERT
使用 POST(邮递员)进行编码,但无法将传感器数据传递到 SQL Server。
如何将传感器参数传递到 T-SQL 查询中?如果有人可以指导我解决这个问题,那就太好了。
exports.add = function (req, resp, reqBody) {
try {
if (!reqBody) throw new Error("Input not valid");
var data = JSON.parse(reqBody);
var outputJSON = JSON.stringify(reqBody);
if (data) {//add more validations if necessary
var sql = "INSERT INTO arduinoData (Machine, StartTime, EndTime, LengthTime) VALUES ";
sql += util.format("(%s, '%s', '%s','%s') ", reqBody.data.Machine, reqBody.data.StartTime, reqBody.data.EndTime, reqBody.data.LengthTime);
db.executeSql(sql, function (data, err) {
if (err) {
httpMsgs.show500(req, resp, err);
}
else {
httpMsgs.send200(req, resp);
}
});
}
else {
throw new Error("Input not valid");
}
}
catch (ex) {
httpMsgs.show500(req, resp, ex);
}
};
function vibrationStart()
{
data = [{
Machine : "Machine",
StartTime : "StartTime",
EndTime : "EndTime",
LengthTime : "LengthTime"
}];
var jsonTable = { "table": [] };
var startTime = getTime();
console.log(startTime[0] + " " + startTime[1]);
var startData = {
Machine: Machine,
start_time: startTime[0] + " " + startTime[1],
day_night: startTime[3],
active: "true"
};
const options = {
url: serverURL,
method: "POST",
form: startData
};
request.post(options, function (error, response, body) {
if (!error) {
console.log("Sent starting message!");
sendBackupData();
} else {
console.log("CANT'T SEND");
// Write to JSON file for backup if can't send to server
fs.readFile("backup.json", "utf8", function readFileCallback(err, data) {
if (err) throw err;
jsonTable = JSON.parse(data);
jsonTable.table.push(startData);
var outputJSON = JSON.stringify(jsonTable);
fs.writeFile("backup.json", outputJSON, "utf8", function (err) {
if (err) throw err;
});
});
}
});
return startTime[2];
}
function vibrationStop(startTimeUnix)
{
// Should get:
// - End time and date the vibration ended
// - Total length of time
// Will send the message, if there is network connection, once complete.
// Will store message into a JSON file if there is no network connection.
data = [{
Machine : "Machine",
StartTime : "StartTime",
EndTime : "EndTime",
LengthTime : "LengthTime"
}];
var jsonTable = { "table": [] };
var endTime = getTime();
console.log(endTime[0] + " " + endTime[1]);
var endTimeUnix = endTime[2];
var LengthTime = endTimeUnix - startTimeUnix;
console.log("Length time: " + LengthTime);
var endData = {
Machine: Machine,
end_time: endTime[0] + " " + endTime[1],
LengthTime: LengthTime,
active: "false"
};
const options = {
url: serverURL,
method: "POST",
form: endData
};
request.post(options, function (error, response, body) {
if (!error) {
console.log("Sent end message!");
sendBackupData();
} else {
console.log("CANT'T SEND");
// Write to JSON file for backup if can't send to server
fs.readFile("backup.json", "utf8", function readFileCallback(err, data) {
if (err) throw err;
jsonTable = JSON.parse(data);
jsonTable.table.push(endData);
var data = JSON.stringify(executeSql);
var outputJSON = JSON.stringify(jsonTable);
fs.writeFile("backup.json", outputJSON, "utf8", function (err) {
if (err) throw err;
});
});
}
});
}
arduinoBoard.on("ready", function () {
// Main function that runs when Arduino is 'ready'
console.log("Board ready!");
var tilt = new johnnyFive.Sensor.Digital(8);
var sensorCount = 0;
var sensorFlag = false, prevSensorFlag = false;
var startTime = 0;
var sendEmailTimeout;
// When sensor changes value
tilt.on("change", function () {
sensorCount = 0;
sensorFlag = true;
console.log("TILTING!");
});
// Continuously loops
var timeoutValue = 250; // Change timeout value later on.
tilt.on("data", function () {
// Sensor just started turning on
if (sensorFlag & !prevSensorFlag) {
prevSensorFlag = true;
startTime = vibrationStart();
console.log("Vibration started.");
clearTimeout(sendEmailTimeout); // Don't send email if switch activated before 5 minutes of inactivity
}
// Sensor just turned off
if (!sensorFlag && prevSensorFlag) {
prevSensorFlag = false;
EndTime = vibrationStop(startTime);
console.log("Vibration stopped.");
sendEmailTimeout = setTimeout(sendEmail, userOptions.email_time * 1000); // Send email after 5 minutes of inactivity
}
// Sensor reaches timeout value
if (sensorCount == timeoutValue) {
sensorCount = 0;
sensorFlag = false;
}
sensorCount++;
});
});
解决方案
推荐阅读
- python - 使精灵在点击时消失
- sql - 通过在 Hive/Athena 的 CSV 文件中添加两列来创建外部表
- android - 深度链接在 chrome expo react native 项目中不起作用
- python - 在 MacOS Big Sur 上安装 Python3.4 时出错
- powershell - 创建 NotifyIcon 并使用 powershell 处理单击事件
- typescript - 数组 concat 的返回类型
- android - 如何在 ViewModel 中使用 Strings.xml 而无需在 Android 中使用上下文?
- java - Spring WebClient 下载 PDF 会出现 HTTP 错误
- sql - SQL - 记录案例记录中的状态变化
- javascript - if(id != null) 与 if(!id) 之间的 Javascript 区别