我已经完成了Node.js代码,它运行正常。但是,由于某些更改,我需要将代码放在server.js
client.js
下的代码将数据附加到mongodb
var monitoredItem = the_subscription.monitor({
nodeId: opcua.resolveNodeId("ns=2000;s=TEST"),
attributeId: opcua.AttributeIds.Value
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 10
},
opcua.read_service.TimestampsToReturn.Both
);
console.log("-------------------------------------");
monitoredItem.on("changed",function(dataValue){
console.log(" New Data Receive = ",dataValue.value.value);
if(dataValue.value.value!='No New Data'){
var row = JSON.parse(dataValue.value.value);
if(row[0]!='Machine Unit'){
var machineUnit = row[0];
var airTemperature = row[1];
var waterTemperature = row[2];
var heatTemperature = row[3];
var roomTemperature = row[4];
var date = row[5];
var time = row[6];
MongoClient.connect('mongodb://127.0.0.1:27017/meiban', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
var document = {
"machineUnit" : machineUnit,
"airTemperature" : airTemperature,
"waterTemperature" : waterTemperature,
"heatTemperature" : heatTemperature,
"roomTemperature" : roomTemperature,
"date" : date,
"time" : time
};
//insert record
db.collection('meibandb').insert(document, function(err, records) {
if (err) throw err;
console.log("A new record added ! ");
});
db.close();
});
}
}
});
}
,

现在,我需要将代码放到服务器端。我想要的代码附在下面。
if(dataValue.value.value!='No New Data'){
var row = JSON.parse(dataValue.value.value);
if(row[0]!='Machine Unit'){
var machineUnit = row[0];
var airTemperature = row[1];
var waterTemperature = row[2];
var heatTemperature = row[3];
var roomTemperature = row[4];
var date = row[5];
var time = row[6];
MongoClient.connect('mongodb://127.0.0.1:27017/meiban', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
var document = {
"machineUnit" : machineUnit,
"airTemperature" : airTemperature,
"waterTemperature" : waterTemperature,
"heatTemperature" : heatTemperature,
"roomTemperature" : roomTemperature,
"date" : date,
"time" : time
};
//insert record
db.collection('meibandb').insert(document, function(err, records) {
if (err) throw err;
console.log("A new record added ! ");
});
db.close();
});