module.exports = class Device {
    constructor (DeviceTyp,DeviceID,Comment,Handler){
        this.DeviceTyp = new String(DeviceTyp);
        this.DeviceID  = new String(DeviceID);
        this.Comment   = new String(Comment);
        this.Handler   = Handler;
        this.DataForwarderDeviceIDs = [];
        this.TimeStampLastRx        = 0;
        this.DataLastRx             = null;
        this.ClientConnection       = null;
    };

    static CheckArray(DevicesArray,DeviceID){
        var Index = 0;
        var nAccounts = DevicesArray.length;
        //console.log("DEBUG getDeviceIndex/CheckArray:")
        while (Index < nAccounts) {
            //console.log("-> "+DevicesArray[Index].DeviceID.toString() +" / " + DeviceID.toString())
            if (DevicesArray[Index].DeviceID.toString() === DeviceID.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;
    };

    DisConnect(ClientConnection){
        Index = 0;
        nDataForwarders = length(this.DataForwarderDeviceIDs);

        while (Index<nDataForwarders){
            if (this.ClientConnection === ClientConnection) {
                Index                 = nDataForwarders; // Wenn Passende Verbindung gefunden, abbruch bedingung herstellen.
                this.ClientConnection = null;
                //Stub: Try to Add Check if ClientConnection (function Parameter) is alive
                ClientConnection.close();
            }; // if connection === connection
            Index++;
        }; // while Index < nDataForwarders
    }; // DisConnect


    addForwarder(TargetID){
        var Index = 0;
        var nDataForwarders = this.DataForwarderDeviceIDs.length;

        // Prüfen ob entsprechende TargetID bereits registriert, wenn registriert dann abbruch
        while (Index<nDataForwarders){
            if (this.DataForwarderDeviceIDs[Index].toString() == TargetID.toString()) return;
            Index++;
        }; // while Index < nDataForwarders
        this.DataForwarderDeviceIDs.push(TargetID.toString());
    };// addForwarder

}