DuoFernConfigurator/module.php
/**
     * Sets dynamic configuration form for device list
     *
     * @return string configuration json string
     */
Sets dynamic configuration form for device list
@return string configuration json string
Sets dynamic configuration form for device list @return string configuration json string
public function GetConfigurationForm()
    {
        $deviceInstanceIds = IPS_GetInstanceListByModuleID("{BE62B172-BABA-4EB1-8C4C-507526645ED5}");
        $parentId = IPS_GetInstance($this->InstanceID)['ConnectionID'];
        $deviceList = array();
        $seenDevicesBuffer = (array)$this->SeenDevicesBuffer; 
        foreach ($deviceInstanceIds as $instanceId) {
            if (IPS_GetInstance($instanceId)['ConnectionID'] != $parentId) {
                continue;
            }
            $duoFernCode = IPS_GetProperty($instanceId, "duoFernCode");
            $deviceType = DuoFernDeviceType::getDeviceType($duoFernCode);
            $timeStamp = isset ($seenDevicesBuffer[$duoFernCode]) ? $seenDevicesBuffer[$duoFernCode] : null;
            $timeString = date('Ymd') == date('Ymd', $timeStamp) && $timeStamp != null ?
                date('H:i:s', $timeStamp) : date('d.m.Y H:i:s', $timeStamp);
            $device = [
                "duoFernCode" => trim(chunk_split($duoFernCode, 2, " ")),
                "type" => $deviceType != null ? $deviceType : $this->Translate("Unknown"),
                "name" => IPS_GetLocation($instanceId),
                "lastMsg" => $timeStamp != null ? $timeString : "N/A",
                "instanceId" => $instanceId,
                "rowColor" => "#C0FFC0"
            ];
            $deviceList[$duoFernCode] = $device;
        }
        foreach ($seenDevicesBuffer as $duoFernCode => $lastMessageTimestamp) {
            if (array_key_exists($duoFernCode, $deviceList)) {
                continue;
            }
            $deviceType = DuoFernDeviceType::getDeviceType($duoFernCode);
            $timeStamp = isset ($seenDevicesBuffer[$duoFernCode]) ? $seenDevicesBuffer[$duoFernCode] : null;
            $timeString = date('Ymd') == date('Ymd', $timeStamp) && $timeStamp != null ?
                date('H:i:s', $timeStamp) : date('d.m.Y H:i:s', $timeStamp);
            $device = [
                "duoFernCode" => trim(chunk_split($duoFernCode, 2, " ")),
                "type" => $deviceType != null ? $deviceType : $this->Translate("Unknown"),
                "name" => "N/A",
                "lastMsg" => $timeStamp != null ? $timeString : "N/A",
                "instanceId" => "N/A"
            ];
            $deviceList[$duoFernCode] = $device;
        }
        ksort($deviceList);
        $data = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . "form.json"), true);
        $data['actions'][1]['label'] = sprintf($this->Translate("Devices: %d"), count($deviceList));
        $data['actions'][2]['values'] = array_values($deviceList);
        $data['actions'][3]['onClick'] = <<< 'EOT'
                // define helper function to translate
                function Translate($string)
                {
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir()
                        . DIRECTORY_SEPARATOR . "modules"
                        . DIRECTORY_SEPARATOR . "IPSDuoFern"
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator"
                        . DIRECTORY_SEPARATOR . "locale.json"), true);
                    $translations = $translations["translations"]["de"];
                    // found translation
                    if (array_key_exists($string, $translations)) {
                        return $translations[$string];
                    }
                    // do not translate
                    return $string;
                }
                // check device has already an instance
                if ($deviceList == null) {
                    echo Translate("No DuoFern device selected");
                    return;
                }
                // check device has already an instance
                if ($deviceList['instanceId'] > 0) {
                    echo Translate("Instance already exists");
                    return;
                }
                // create instance
                $instanceId = IPS_CreateInstance('{BE62B172-BABA-4EB1-8C4C-507526645ED5}');
                if ($instanceId == false) {
                    trigger_error(Translate("Instance could not be created") . PHP_EOL, E_USER_ERROR);
                    return;
                }
                // connect instance to actual gateway if not connected
                if (IPS_GetInstance($instanceId)['ConnectionID'] != IPS_GetInstance($id)['ConnectionID']) {
                    if (IPS_GetInstance($instanceId)['ConnectionID'] > 0) {
                        IPS_DisconnectInstance($instanceId);
                    }
                    IPS_ConnectInstance($instanceId, IPS_GetInstance($id)['ConnectionID']);
                }
                // set duo fern code property
                @IPS_SetProperty($instanceId, 'duoFernCode', str_replace(' ', '', $deviceList['duoFernCode']));
                @IPS_ApplyChanges($instanceId);
                // set instance name
                $instanceName = 'DuoFern ' . $deviceList['type'] . ' (' . $deviceList['duoFernCode'] . ')' ;
                IPS_SetName($instanceId, $instanceName);
                echo Translate("Instance created") . ": " .  $instanceName;
EOT;
        $data['actions'][4]['onClick'] = <<< 'EOT'
                // include library
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules"
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern"
                                                . DIRECTORY_SEPARATOR . "libs"
                                                . DIRECTORY_SEPARATOR . "library.php");
                // define helper function to translate
                function Translate($string)
                {
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir()
                        . DIRECTORY_SEPARATOR . "modules"
                        . DIRECTORY_SEPARATOR . "IPSDuoFern"
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator"
                        . DIRECTORY_SEPARATOR . "locale.json"), true);
                    $translations = $translations["translations"]["de"];
                    // found translation
                    if (array_key_exists($string, $translations)) {
                        return $translations[$string];
                    }
                    // do not translate
                    return $string;
                }
                DUOFERN_GetAllDevicesStatus($id);
                echo Translate("Search for devices...");
EOT;
        $data['actions'][9]['onClick'] = <<< 'EOT'
                // include library
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules"
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern"
                                                . DIRECTORY_SEPARATOR . "libs"
                                                . DIRECTORY_SEPARATOR . "library.php");
                // define helper function to translate
                function Translate($string)
                {
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir()
                        . DIRECTORY_SEPARATOR . "modules"
                        . DIRECTORY_SEPARATOR . "IPSDuoFern"
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator"
                        . DIRECTORY_SEPARATOR . "locale.json"), true);
                    $translations = $translations["translations"]["de"];
                    // found translation
                    if (array_key_exists($string, $translations)) {
                        return $translations[$string];
                    }
                    // do not translate
                    return $string;
                }
                // enter duo fern code or select device from list
                if ($deviceList == null && $remotePairDuoFernCode == null) {
                    echo Translate("No DuoFern Code entered and no device selected");
                    return;
                }
                if ($remotePairDuoFernCode != null) { // duo fern code entered
                    // check valid duo fern code
                    if (!preg_match(DuoFernRegex::DUOFERN_REGEX_DUOFERN_CODE, $remotePairDuoFernCode)) {
                        echo Translate("Invalid DuoFern code (Format: XXXXXX with X = 0-9, A-F)");
                        return;
                    }
                    $duoFernCode = str_replace(' ', '', $remotePairDuoFernCode);
                } else if ($deviceList != null) { // device selected from list
                    $duoFernCode = str_replace(' ', '', $deviceList['duoFernCode']);
                }
                //  get device type name
                $duoFernDeviceType = DuoFernDeviceType::getDeviceType(substr($duoFernCode, 0, 2));
                // start remote pair
                $result = DUOFERN_DeviceRemotePair($id, $duoFernCode);
                if ($result !== false) {
                    echo sprintf(Translate("Device %s paired successful"), ($duoFernDeviceType != false ? 
                            $duoFernDeviceType . " (" . trim(chunk_split($duoFernCode, 2, " ")) . ")" :
                            trim(chunk_split($duoFernCode, 2, " "))));
                } else {
                    echo sprintf(Translate("Device %s not paired"), ($duoFernDeviceType != false ? 
                            $duoFernDeviceType . " (" . trim(chunk_split($duoFernCode, 2, " ")) . ")" :
                            trim(chunk_split($duoFernCode, 2, " "))));
                }    
EOT;
        $data['actions'][13]['onClick'] = <<< 'EOT'
                // include library
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules"
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern"
                                                . DIRECTORY_SEPARATOR . "libs"
                                                . DIRECTORY_SEPARATOR . "library.php");
                // define helper function to translate
                function Translate($string)
                {
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir()
                        . DIRECTORY_SEPARATOR . "modules"
                        . DIRECTORY_SEPARATOR . "IPSDuoFern"
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator"
                        . DIRECTORY_SEPARATOR . "locale.json"), true);
                    $translations = $translations["translations"]["de"];
                    // found translation
                    if (array_key_exists($string, $translations)) {
                        return $translations[$string];
                    }
                    // do not translate
                    return $string;
                }
                echo Translate("Start pairing mode...") . "\n" . Translate("See messages for more details");
                DUOFERN_StartPairingMode($id, 60);
EOT;
        $data['actions'][14]['onClick'] = <<< 'EOT'
                // include library
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules"
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern"
                                                . DIRECTORY_SEPARATOR . "libs"
                                                . DIRECTORY_SEPARATOR . "library.php");
                // define helper function to translate
                function Translate($string)
                {
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir()
                        . DIRECTORY_SEPARATOR . "modules"
                        . DIRECTORY_SEPARATOR . "IPSDuoFern"
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator"
                        . DIRECTORY_SEPARATOR . "locale.json"), true);
                    $translations = $translations["translations"]["de"];
                    // found translation
                    if (array_key_exists($string, $translations)) {
                        return $translations[$string];
                    }
                    // do not translate
                    return $string;
                }
                echo Translate("Start unpairing mode...") . "\n" . Translate("See messages for more details");
                DUOFERN_StartUnpairingMode($id, 60);
EOT;
        return json_encode($data);
    }
public function GetConfigurationForm ( ) { $ deviceInstanceIds = IPS_GetInstanceListByModuleID ( " {BE62B172-BABA-4EB1-8C4C-507526645ED5} " ) ; $ parentId = IPS_GetInstance ( $ this -> InstanceID ) [ 'ConnectionID' ] ; $ deviceList = array ( ) ; $ seenDevicesBuffer = ( array ) $ this -> SeenDevicesBuffer ; foreach ( $ deviceInstanceIds as $ instanceId ) { if ( IPS_GetInstance ( $ instanceId ) [ 'ConnectionID' ] != $ parentId ) { continue ; } $ duoFernCode = IPS_GetProperty ( $ instanceId , " duoFernCode " ) ; $ deviceType = DuoFernDeviceType :: getDeviceType ( $ duoFernCode ) ; $ timeStamp = isset ( $ seenDevicesBuffer [ $ duoFernCode ] ) ? $ seenDevicesBuffer [ $ duoFernCode ] : null ; $ timeString = date ( 'Ymd' ) == date ( 'Ymd' , $ timeStamp ) && $ timeStamp != null ? date ( 'H:i:s' , $ timeStamp ) : date ( 'd.m.Y H:i:s' , $ timeStamp ) ; $ device = [ " duoFernCode " => trim ( chunk_split ( $ duoFernCode , 2 , " " ) ) , " type " => $ deviceType != null ? $ deviceType : $ this -> Translate ( " Unknown " ) , " name " => IPS_GetLocation ( $ instanceId ) , " lastMsg " => $ timeStamp != null ? $ timeString : " N/A " , " instanceId " => $ instanceId , " rowColor " => " #C0FFC0 " ] ; $ deviceList [ $ duoFernCode ] = $ device ; } foreach ( $ seenDevicesBuffer as $ duoFernCode => $ lastMessageTimestamp ) { if ( array_key_exists ( $ duoFernCode , $ deviceList ) ) { continue ; } $ deviceType = DuoFernDeviceType :: getDeviceType ( $ duoFernCode ) ; $ timeStamp = isset ( $ seenDevicesBuffer [ $ duoFernCode ] ) ? $ seenDevicesBuffer [ $ duoFernCode ] : null ; $ timeString = date ( 'Ymd' ) == date ( 'Ymd' , $ timeStamp ) && $ timeStamp != null ? date ( 'H:i:s' , $ timeStamp ) : date ( 'd.m.Y H:i:s' , $ timeStamp ) ; $ device = [ " duoFernCode " => trim ( chunk_split ( $ duoFernCode , 2 , " " ) ) , " type " => $ deviceType != null ? $ deviceType : $ this -> Translate ( " Unknown " ) , " name " => " N/A " , " lastMsg " => $ timeStamp != null ? $ timeString : " N/A " , " instanceId " => " N/A " ] ; $ deviceList [ $ duoFernCode ] = $ device ; } ksort ( $ deviceList ) ; $ data = json_decode ( file_get_contents ( __DIR__ . DIRECTORY_SEPARATOR . " form.json " ) , true ) ; $ data [ 'actions' ] [ 1 ] [ 'label' ] = sprintf ( $ this -> Translate ( " Devices: %d " ) , count ( $ deviceList ) ) ; $ data [ 'actions' ] [ 2 ] [ 'values' ] = array_values ( $ deviceList ) ; $ data [ 'actions' ] [ 3 ] [ 'onClick' ] = <<< ' EOT '                 // define helper function to translate 
                function Translate($string) 
                { 
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir() 
                        . DIRECTORY_SEPARATOR . "modules" 
                        . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator" 
                        . DIRECTORY_SEPARATOR . "locale.json"), true); 
                    $translations = $translations["translations"]["de"]; 

                    // found translation 
                    if (array_key_exists($string, $translations)) { 
                        return $translations[$string]; 
                    } 

                    // do not translate 
                    return $string; 
                } 

                // check device has already an instance 
                if ($deviceList == null) { 
                    echo Translate("No DuoFern device selected"); 
                    return; 
                } 

                // check device has already an instance 
                if ($deviceList['instanceId'] > 0) { 
                    echo Translate("Instance already exists"); 
                    return; 
                } 

                // create instance 
                $instanceId = IPS_CreateInstance('{BE62B172-BABA-4EB1-8C4C-507526645ED5}'); 
                if ($instanceId == false) { 
                    trigger_error(Translate("Instance could not be created") . PHP_EOL, E_USER_ERROR); 
                    return; 
                } 

                // connect instance to actual gateway if not connected 
                if (IPS_GetInstance($instanceId)['ConnectionID'] != IPS_GetInstance($id)['ConnectionID']) { 
                    if (IPS_GetInstance($instanceId)['ConnectionID'] > 0) { 
                        IPS_DisconnectInstance($instanceId); 
                    } 
                    IPS_ConnectInstance($instanceId, IPS_GetInstance($id)['ConnectionID']); 
                } 

                // set duo fern code property 
                @IPS_SetProperty($instanceId, 'duoFernCode', str_replace(' ', '', $deviceList['duoFernCode'])); 
                @IPS_ApplyChanges($instanceId); 

                // set instance name 
                $instanceName = 'DuoFern ' . $deviceList['type'] . ' (' . $deviceList['duoFernCode'] . ')' ; 
                IPS_SetName($instanceId, $instanceName); 

                echo Translate("Instance created") . ": " .  $instanceName; EOT ; $ data [ 'actions' ] [ 4 ] [ 'onClick' ] = <<< ' EOT '                 // include library 
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules" 
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                                                . DIRECTORY_SEPARATOR . "libs" 
                                                . DIRECTORY_SEPARATOR . "library.php"); 

                // define helper function to translate 
                function Translate($string) 
                { 
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir() 
                        . DIRECTORY_SEPARATOR . "modules" 
                        . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator" 
                        . DIRECTORY_SEPARATOR . "locale.json"), true); 
                    $translations = $translations["translations"]["de"]; 

                    // found translation 
                    if (array_key_exists($string, $translations)) { 
                        return $translations[$string]; 
                    } 

                    // do not translate 
                    return $string; 
                } 
                
                DUOFERN_GetAllDevicesStatus($id); 
                echo Translate("Search for devices..."); EOT ; $ data [ 'actions' ] [ 9 ] [ 'onClick' ] = <<< ' EOT '                 // include library 
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules" 
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                                                . DIRECTORY_SEPARATOR . "libs" 
                                                . DIRECTORY_SEPARATOR . "library.php"); 

                // define helper function to translate 
                function Translate($string) 
                { 
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir() 
                        . DIRECTORY_SEPARATOR . "modules" 
                        . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator" 
                        . DIRECTORY_SEPARATOR . "locale.json"), true); 
                    $translations = $translations["translations"]["de"]; 

                    // found translation 
                    if (array_key_exists($string, $translations)) { 
                        return $translations[$string]; 
                    } 

                    // do not translate 
                    return $string; 
                } 
                
                // enter duo fern code or select device from list 
                if ($deviceList == null && $remotePairDuoFernCode == null) { 
                    echo Translate("No DuoFern Code entered and no device selected"); 
                    return; 
                } 
                
                if ($remotePairDuoFernCode != null) { // duo fern code entered 
                    // check valid duo fern code 
                    if (!preg_match(DuoFernRegex::DUOFERN_REGEX_DUOFERN_CODE, $remotePairDuoFernCode)) { 
                        echo Translate("Invalid DuoFern code (Format: XXXXXX with X = 0-9, A-F)"); 
                        return; 
                    } 
                    $duoFernCode = str_replace(' ', '', $remotePairDuoFernCode); 
                   
                } else if ($deviceList != null) { // device selected from list 
                    $duoFernCode = str_replace(' ', '', $deviceList['duoFernCode']); 
                } 
                
                //  get device type name 
                $duoFernDeviceType = DuoFernDeviceType::getDeviceType(substr($duoFernCode, 0, 2)); 
                        
                // start remote pair 
                $result = DUOFERN_DeviceRemotePair($id, $duoFernCode); 
                
                if ($result !== false) { 
                    echo sprintf(Translate("Device %s paired successful"), ($duoFernDeviceType != false ?  
                            $duoFernDeviceType . " (" . trim(chunk_split($duoFernCode, 2, " ")) . ")" : 
                            trim(chunk_split($duoFernCode, 2, " ")))); 
                } else { 
                    echo sprintf(Translate("Device %s not paired"), ($duoFernDeviceType != false ?  
                            $duoFernDeviceType . " (" . trim(chunk_split($duoFernCode, 2, " ")) . ")" : 
                            trim(chunk_split($duoFernCode, 2, " ")))); 
                }     EOT ; $ data [ 'actions' ] [ 13 ] [ 'onClick' ] = <<< ' EOT '                 // include library 
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules" 
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                                                . DIRECTORY_SEPARATOR . "libs" 
                                                . DIRECTORY_SEPARATOR . "library.php"); 

                // define helper function to translate 
                function Translate($string) 
                { 
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir() 
                        . DIRECTORY_SEPARATOR . "modules" 
                        . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator" 
                        . DIRECTORY_SEPARATOR . "locale.json"), true); 
                    $translations = $translations["translations"]["de"]; 

                    // found translation 
                    if (array_key_exists($string, $translations)) { 
                        return $translations[$string]; 
                    } 

                    // do not translate 
                    return $string; 
                } 
    
                echo Translate("Start pairing mode...") . "\n" . Translate("See messages for more details"); 
                DUOFERN_StartPairingMode($id, 60); EOT ; $ data [ 'actions' ] [ 14 ] [ 'onClick' ] = <<< ' EOT '                 // include library 
                require_once(IPS_GetKernelDir() . DIRECTORY_SEPARATOR . "modules" 
                                                . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                                                . DIRECTORY_SEPARATOR . "libs" 
                                                . DIRECTORY_SEPARATOR . "library.php"); 

                // define helper function to translate 
                function Translate($string) 
                { 
                    $translations = json_decode(file_get_contents(IPS_GetKernelDir() 
                        . DIRECTORY_SEPARATOR . "modules" 
                        . DIRECTORY_SEPARATOR . "IPSDuoFern" 
                        . DIRECTORY_SEPARATOR . "DuoFernConfigurator" 
                        . DIRECTORY_SEPARATOR . "locale.json"), true); 
                    $translations = $translations["translations"]["de"]; 

                    // found translation 
                    if (array_key_exists($string, $translations)) { 
                        return $translations[$string]; 
                    } 

                    // do not translate 
                    return $string; 
                } 
    
                echo Translate("Start unpairing mode...") . "\n" . Translate("See messages for more details"); 
                DUOFERN_StartUnpairingMode($id, 60); EOT ; return json_encode ( $ data ) ; }
Sets dynamic configuration form for device list
 @return string configuration json string
Sets dynamic configuration form for device list @return string configuration json string
// get seen devices buffer // add device instances // discard if not connected // add seen devices // skip devices with instance // sort devices bei duo fern code // get form json as array // set devices count // add items to list // define onClick method for create instance button // define onClick method for search devices button // define onClick method for remote Pairing // define onClick method for pairing mode // define onClick method for unpairing mode