Wednesday 25 July 2018

Parsing and inserting records in the correct order with Tedious and Promises

I have a parsing application that connects to machines via TCP and waits for data to be sent from them. Once data is sent, I have to parse the messages to organize things correctly for the SQL table. Currently, machines may send multi-line messages that each have a four-component payload, or they may send a single-line message with the four-component payload.It is currently imperative to insert records in the correct order for later automation tasks that run against the database. Unfortunately, when a multi-line message comes in and is parsed, the records are frequently inserted in a random order. Is there some queuing function I could introduce to make sure things are inserted serially?A pared down example of my code is as follows:Collecting the data:clients[i].on('data', function(data) { console.log("Data: " + data + " " + hosts[j]); parse_message(data, hosts[j][1], hosts[j][2]); }); Parsing the strings and payload components:function parse_message(data, m_id, type) { //console.log('Message received = ' + data); var msg = data.toString('utf8'); // Convert from object class, split CRs var strings = msg.split('\r\n'); // split multi-line data console.log("Strings: " + strings) strings.forEach(function(element) { var payload = element.split("///"); // split by separator if (payload.length == 4) { // If array is properly formed payload[3] = payload[3].trim(); if (payload[3].includes('N')) { payload[3] = payload[3].slice(1, payload[3].length); } payload[4] = m_id; // Send machine ID payload[5] = dt.getDateTime(); if (type == 'Machine') { Insert_Machine(payload); } else if (type == 'Pallet') { Insert_Pallet(payload); } else { console.log("Type didn't come through. Where am I supposed to put this?") } } }); } Inserting with Tedious and Promises:function Insert_Machine(msg, callback) { var result = []; var deferred = q.defer(); // init promise var connection = new Connection(machine_config); connection.on('connect', function(err) { var request = new Request( // set up request structure 'INSERT INTO dbo.Data_new (pp_id, d_No, d_Note, d_Data, d_seq, d_machine, d_DateTime) OUTPUT INSERTED.Id VALUES (@pp_id, @d_No, @d_Note, @d_Data, 0, @d_machine, @d_DateTime);', function(err, rowCount) { if (err) { console.log(err); // print error on console if anything happens } else { console.log(rowCount + ' rows inserted to Machine_Data: ' + msg); // confirm the insert } deferred.resolve(result); // resolve promise }); request.on('row', function(columns){ result.push(columns); }); request.addParameter('pp_id', TYPES.NVarChar, msg[0]); request.addParameter('d_No', TYPES.NVarChar, msg[1]); request.addParameter('d_Note', TYPES.NVarChar, msg[2]); request.addParameter('d_Data', TYPES.NVarChar, msg[3]); request.addParameter('d_machine', TYPES.NVarChar, msg[4]); request.addParameter('d_DateTime', TYPES.NVarChar, msg[5]); // Execute SQL statement connection.execSql(request); } ); return deferred.promise; }

Submitted July 25, 2018 at 07:35PM by ckindley

No comments:

Post a Comment