let server = net.createServer(function (connection) { |
console.log('Client connected'); |
connection.on('data', function (data) { |
cdata = data.toString(); |
parts = cdata.split(";"); |
imei_number = parts[1]; |
dbConnecct(); |
if (!empty(imei_number)) { |
if (checkDeviceRegistered(imei_number)) { // Checking the device already registered in DB |
// True |
//Do something if device already registered |
logDeviceConnection(imei_number); // Save the request in the file |
} else { |
// False |
//If device is not registerd this code will execite |
registerDevice(imei_number); // Registering/storing device information in System/DB |
logNewDevice(imei_number); // Save the new device connection is file |
notifySystemAdmin(imei_number); // Notifying sytem admin about device connection |
} |
connection.write("Device connection establised"); |
} else { |
connection.write("Device did not recognised"); |
} |
}); |
connection.on('end', function () { |
console.log('Client disconnected'); |
}); |
}); |
server.timeout =0; |
server.listen(port, function () { |
console.log('Server is now listening on ' + port); |
}); |
server.on('error', function (err) { console.log(err); |
}); |
function dbConnecct() { |
con.connect(function (err) { |
if (err) throw err; |
}); |
} |
function checkDeviceRegistered(imei_number) { |
con.query("SELECTFROM devices WHERE imei=" + imei_number, function (err, result, fields) { |
if (err) throw err; |
if (result) { |
return true; |
} else |
return false; |
}); |
} |
function logDeviceConnection() { |
} |
function registerDevice() { // Registering/storing device information in System/DB |
} |
function logNewDevice() { // Save the new device connection is file |
} |