console.log("Start programm");
var net = require('net');
var Account = require('./Account');
const DeviceCheck = require('./DeviceCheck');
const Port = 8888;//55666;
var Accounts =[];
console.log("init done.");
var server = net.createServer(function(client){
client.setTimeout(180000);// 180000 = 3*60*1000
//console.log(client.toString());
//console.log(IoTClient.getClientString(client));
client.on('data', function(data){
//console.log(client.remotePort);
//console.log(data.toString());
var AccountID = DeviceCheck.Account(data);
var DeviceID = DeviceCheck.ID(data);
var DeviceType = DeviceCheck.Type(data);
var IndexAccounts = Account.CheckArray(Accounts,AccountID);
// Falls dies die erste übertragung des Accounts ist:
if (IndexAccounts == -1){
var strLog=Date().toString() + " IoT-Hub.js - New Account. : ";
strLog += client.remoteAddress + " :" + client.remotePort.toString();
// Falls ungültige Account Angabe, verbindung beenden.
if (Account.CheckValidAccount(AccountID) == -1){
client.end();
return;
} ;
Accounts.push(new Account(AccountID));
IndexAccounts = Account.CheckArray(Accounts,AccountID);
} ;// if Erste übertragung des Accounts
Accounts[IndexAccounts].ProcessDevice(client, DeviceType, DeviceID, data.toString());
/*
var sAccount = IoTClient.getAccountFromClient(Accounts,client);
var alegaceAccountClients = IoTClient.getDevices(Accounts,sAccount);
var accountClientIndex = IoTClient.getClientIndex(alegaceAccountClients,client);
var clientType=Accounts[accountClientIndex].clientType;
if ( (clientType.localeCompare("bedSensor") == 0) && (bedSensor.isBedSensorRecord(data.toString().trim())) ) data = alegaceAccountClients[accountClientIndex].bedSensor.processData(data);
if (clientType.localeCompare("") != 0) IoTDevice.defaultHandling(client,data,alegaceAccountClients,alegaceAccountClients) ;// if aDevInfo[1]
if ( (clientType.localeCompare("bedSensor") == 0) && (data.length==2) ) data = clientType + "," + data[0].toString() + ";" + data[1].toString() + "\r\n";
IoTClient.runListener(alegaceAccountClients,client,data);
IoTClient.runForwarder(alegaceAccountClients,client,data);
*/
} ); // on 'data'
client.on('connect', function(had_error){
console.log('Incomming Connection');
});
client.on('close', function(had_error){
/*
var strLog=Date().toString() + " IoT-Hub.js - Client close. : ";
if (typeof(client.remoteAddress) == 'string') strLog += client.remoteAddress + " :" + client.remotePort.toString();
var clientIndex = IoTClient.getClientIndex(Accounts,client);
if (clientIndex >= 0){
strLog+=" index=" + clientIndex.toString();
strLog+=" Type=" + Accounts[clientIndex].clientType;
strLog+=" Account=" + Accounts[clientIndex].account;
strLog+=" ID=" + Accounts[clientIndex].clientID;
Accounts.splice(clientIndex,1);
};
console.log(strLog);
*/
} ); // on 'close
client.on('timeout', function () {
/*
var strLog=Date().toString() + " IoT-Hub.js - Client time out. : ";
if (typeof(client.remoteAddress) == 'string') strLog += client.remoteAddress + " :" + client.remotePort.toString();
var clientIndex = IoTClient.getClientIndex(Accounts,client);
if (clientIndex >= 0){
strLog+=" index=" + clientIndex.toString();
strLog+=" Type=" + Accounts[clientIndex].clientType;
strLog+=" Account=" + Accounts[clientIndex].account;
strLog+=" ID=" + Accounts[clientIndex].clientID;
Accounts.splice(clientIndex,1);
};
console.log(strLog);
*/
client.end();
}); // on 'timeout
client.on('error', function () {
/*
console.log('Client error. ');
var strLog=Date().toString() + " IoT-Hub.js - Client error. : ";
if (typeof(client.remoteAddress) == 'string') strLog += client.remoteAddress + " :" + client.remotePort.toString();
var clientIndex = IoTClient.getClientIndex(Accounts,client);
if (clientIndex >= 0){
strLog+=" index=" + clientIndex.toString();
strLog+=" Type=" + Accounts[clientIndex].clientType;
strLog+=" Account=" + Accounts[clientIndex].account;
strLog+=" ID=" + Accounts[clientIndex].clientID;
Accounts.splice(clientIndex,1);
};
console.log(strLog);
*/
client.end();
}); // on 'error
} ); // createServer
console.log("Start server");
server.listen(Port);