const Device = require("./Device");
const IoTControler = require("./IoTControler");
module.exports = class Account {
static CheckValidAccount(AccountID){
// Stub: Add Account Check (MySQL (AccountDatabase))
return 1;
// If not In Database return -1;
return -1
};
constructor (AccountID){
if (Account.CheckValidAccount(AccountID) == -1) return null;
this.AccountID = new String(AccountID);
this.Devices = [];
}; // constructor
static CheckArray(AccountsArray,AccountID){
var Index = 0;
var nAccounts = AccountsArray.length;
while (Index < nAccounts) {
if (AccountsArray[Index].AccountID.toString() === AccountID.toString()) {
// Wenn Account in Array, dann ArrayIndex zurück geben.
return Index;
}
Index++;
};// while index < nAccounts
// Falls KEIN Account in Array, dann -1 zurück geben.
return -1;
};
getDeviceIndex(DeviceID){
// Returns -1 if not Registred. If its registred, it returns the Index
return Device.CheckArray(this.Devices,DeviceID);
};
isConnected(DeviceID){
var IndexDevice = Device.CheckArray(this.Devices,DeviceID);
if (IndexDevice >= 0) {
if (Devices[IndexDevice].ClientConnection == null) return 0;
return 1;
}; // if indexDevice >0
return 0;
}
ProcessDevice(Client, DeviceTyp, DeviceID, RAW){
var IndexDevice = Device.CheckArray(this.Devices,DeviceID);
if (IndexDevice < 0) {
this.Devices.push(new Device(DeviceTyp, DeviceID, null, null));
IndexDevice = Device.CheckArray(this.Devices,DeviceID);
}; // if indexDevice <0
this.Devices[IndexDevice].ClientConnection = Client;
this.Devices[IndexDevice].TimeStampLastRx = Date.now();
this.Devices[IndexDevice].DataLastRx = RAW;
if (DeviceTyp.toString() == "IoTControler") IoTControler.Parse(RAW.toString().split(",")[3],this, DeviceID, Client);
// Forward Data
var IndexForward = 0;
var nForwards = this.Devices[IndexDevice].DataForwarderDeviceIDs.length;
while (IndexForward < nForwards){
var VorwarderDeviceID = this.Devices[IndexDevice].DataForwarderDeviceIDs[IndexForward];
var VorwarderDeviceIndex = Device.CheckArray(this.Devices,VorwarderDeviceID);
if (this.Devices[VorwarderDeviceIndex].ClientConnection != null) this.Devices[VorwarderDeviceIndex].ClientConnection.write(RAW);
IndexForward++;
};
}; // ProcessDevice
}; // class