private function lock($ident)
 {
     for ($i = 0; $i < 100; $i++) {
         if (IPS_SemaphoreEnter('Logamatic_' . (string) $this->InstanceID . (string) $ident, 1)) {
             return true;
         } else {
             IPS_Sleep(mt_rand(1, 5));
         }
     }
     return false;
 }
Example #2
0
 public function Learn()
 {
     $GatewayPassword = $this->GetPassword();
     if ($GatewayPassword !== "") {
         $address = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_USER=user&XC_PASS="******"&XC_FNC=LearnSC&type=FS20");
     } else {
         $address = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_FNC=LearnSC&type=FS20");
     }
     //kurze Pause während das Gateway im Lernmodus ist
     IPS_Sleep(1000);
     //1000 ms
     if ($address == "{XC_ERR}Failed to learn code") {
         $this->response = false;
         $instance = IPS_GetInstance($this->InstanceID)["InstanceID"];
         $address = "Das Gateway konnte keine Adresse empfangen.";
         IPS_LogMessage("FS20 Adresse:", $address);
         echo "Die Adresse vom FS20 Gerät konnte nicht angelernt werden.";
         IPS_SetProperty($instance, "LearnFS20Address", false);
         //Haken entfernen.
     } else {
         //Adresse auswerten {XC_SUC}
         //bei Erfolg {XC_SUC}{"CODE":"123403"}
         (string) ($address = substr($address, 17, 6));
         IPS_LogMessage("FS20 Adresse:", $address);
         //echo "Adresse des FS20 Geräts: ".$address;
         $this->AddAddress($address);
         $this->response = true;
     }
     return $this->response;
 }
Example #3
0
 public function Learn(integer $irid)
 {
     $GatewayPassword = $this->GetPassword();
     if ($GatewayPassword !== "") {
         $ircode = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_USER=user&XC_PASS="******"&XC_FNC=Learn");
     } else {
         $ircode = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_FNC=Learn");
     }
     //kurze Pause während das Gateway im Lernmodus ist
     IPS_Sleep(1000);
     //1000 ms
     if ($ircode == "{XC_ERR}Failed to learn code") {
         $this->response = false;
         //$instance = IPS_GetInstance($this->InstanceID)["InstanceID"];
         $ircode = "Das Gateway konnte keinen IRCode empfangen.";
         IPS_LogMessage("IRCode:", $ircode);
         echo "Der IRCode konnte nicht angelernt werden.";
         //IPS_SetProperty($instance, "LearnIRCode", false); //Haken entfernen.
         IPS_SetProperty($this->InstanceID, "LearnIRCode", false);
         //Haken entfernen.
     } else {
         //Adresse auswerten bei Erfolg {XC_SUC}{"CODE":"1234455667789123456789123456789"}
         (string) ($ircode = substr($ircode, 17));
         $length = strlen($ircode);
         $ircode = substr($ircode, 0, $length - 2);
         IPS_LogMessage("IR Code:", $ircode);
         $this->AddIRCode($ircode, $irid);
         echo "IR Code: " . $ircode;
         $this->response = true;
     }
     return $this->response;
 }
Example #4
0
 public function SendMsg(string $Text, integer $Type, integer $Timeout)
 {
     if ($this->GetPowerState() == 1) {
         if (IPS_SemaphoreEnter("Enigma2BY_SendMsg", 20000)) {
             $IP = $this->ReadPropertyString("Enigma2IP");
             $WebPort = $this->ReadPropertyInteger("Enigma2WebPort");
             $Text = urlencode(trim($Text));
             $Text = str_replace('%A7', '%0A', $Text);
             $url = "http://" . $IP . ":" . $WebPort . "/web/message?text=" . $Text . "&type=" . $Type . "&timeout=" . $Timeout;
             $xml = @simplexml_load_file($url);
             $result = $this->ResultAuswerten($xml->e2state);
             if ($Type == 0) {
                 $this->SendKey("ArrowDown", "short");
                 IPS_Sleep($Timeout * 1000 + 1000);
                 $xml = @simplexml_load_file("http://" . $IP . ":" . $WebPort . "/web/messageanswer?getanswer=now");
                 if (trim($xml->e2statetext) == "Answer is NO!" or trim($xml->e2statetext) == "Antwort lautet NEIN!") {
                     $AntwortINT = 0;
                 } elseif (trim($xml->e2statetext) == "Answer is YES!" or trim($xml->e2statetext) == "Antwort lautet JA!") {
                     $AntwortINT = 1;
                 } elseif (trim($xml->e2statetext) == "No answer in time" or trim($xml->e2statetext) == "Keine rechtzeitige Antwort") {
                     $AntwortINT = 2;
                     if ($this->DistroCheck() == true) {
                         $this->SendKey("Exit", "short");
                     } else {
                         $this->SendKey("OK", "short");
                     }
                 }
                 $this->SetValueInteger("FrageAntwortVAR", $AntwortINT);
                 IPS_SemaphoreLeave("Enigma2BY_SendMsg");
                 return $AntwortINT;
             }
             IPS_SemaphoreLeave("Enigma2BY_SendMsg");
             return $result;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Example #5
0
 public function Learn()
 {
     $ip_aiogateway = $this->GetIPGateway();
     $GatewayPassword = $this->GetPassword();
     if ($GatewayPassword !== "") {
         $address = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_USER=user&XC_PASS="******"&XC_FNC=learnSC&type=L2");
     } else {
         $address = file_get_contents("http://" . $ip_aiogateway . "/command?XC_FNC=learnSC&type=L2");
     }
     //kurze Pause während das Gateway im Lernmodus ist
     IPS_Sleep(1000);
     //1000 ms
     if ($address == "{XC_ERR}Failed to learn code") {
         $this->response = false;
         $instance = IPS_GetInstance($this->InstanceID)["InstanceID"];
         $address = "Das Gateway konnte keine Adresse empfangen.";
         IPS_LogMessage("LED Adresse:", $address);
         IPS_SetProperty($instance, "LearnAddressLED", false);
         //Haken entfernen.
         echo "Die Adresse des LED Controllers konnte nicht angelernt werden.";
     } else {
         //Adresse auswerten {XC_SUC}
         //bei Erfolg {XC_SUC}{"CODE","ABCDEF00"} erste 6 Stellen sind die Adresse
         (string) ($address = substr($address, 17, 6));
         $this->AddAddress($address);
         IPS_LogMessage("LED Adresse:", $address);
         echo "Adresse des LED Controllers: " . $address;
         $this->response = true;
     }
     return $this->response;
 }
 /**
  * Initialization sequence
  */
 private function init()
 {
     $this->debug(__FUNCTION__, 'Init entered');
     $this->SyncParent();
     $this->SetBuffer('');
     if (!$this->HasActiveParent()) {
         $this->debug(__FUNCTION__, 'No active parent');
         return;
     }
     $this->SendDataToParent(chr(2) . chr(2) . chr(0xf1) . chr(1));
     //enable FS20 immediate send
     IPS_Sleep(15);
     $this->SendDataToParent(chr(2) . chr(2) . chr(0xf2) . chr(1));
     //enable weather immediate send
     IPS_Sleep(15);
     $this->SendDataToParent(chr(2) . chr(2) . chr(0xfb) . chr(0));
     //use binary protocol
     IPS_Sleep(100);
 }
Example #7
0
 public function PlayFiles(array $files, $volumeChange)
 {
     $ip = $this->ReadPropertyString("IPAddress");
     $timeout = $this->ReadPropertyString("TimeOut");
     if ($timeout && Sys_Ping($ip, $timeout) != true) {
         throw new Exception("Sonos Box " . $ip . " is not available");
     }
     include_once __DIR__ . "/sonosAccess.php";
     $sonos = new SonosAccess($ip);
     $positionInfo = $sonos->GetPositionInfo();
     $mediaInfo = $sonos->GetMediaInfo();
     $transportInfo = $sonos->GetTransportInfo();
     $isGroupCoordinator = $this->ReadPropertyBoolean("GroupCoordinator");
     if ($isGroupCoordinator) {
         $volume = GetValueInteger($this->GetIDForIdent("GroupVolume"));
     } else {
         $volume = GetValueInteger($this->GetIDForIdent("Volume"));
     }
     //adjust volume if needed
     if ($volumeChange != 0) {
         // pause if playing
         if ($transportInfo == 1) {
             $sonos->Pause();
         }
         // volume request absolte or relative?
         if ($volumeChange[0] == "+" || $volumeChange[0] == "-") {
             if ($isGroupCoordinator) {
                 $this->changeGroupVolume($volumeChange);
             } else {
                 $this->ChangeVolume($volumeChange);
             }
         } else {
             if ($isGroupCoordinator) {
                 $this->SetGroupVolume($volumeChange);
             } else {
                 $this->SetVolume($volumeChange);
             }
         }
     }
     foreach ($files as $key => $file) {
         // only files on SMB share or http server can be used
         if (preg_match('/^\\/\\/[\\w,.,\\d,-]*\\/\\S*/', $file) == 1) {
             $uri = "x-file-cifs:" . $file;
         } elseif (preg_match('/^http:\\/\\/[\\w,.,\\d,-,:]*\\/\\S*/', $file) == 1) {
             $uri = $file;
         } else {
             throw new Exception("File (" . $file . ") has to be located on a Samba share (e.g. //ipsymcon.fritz.box/tts/text.mp3) or a HTTP server (e.g. http://ipsymcon.fritz.box/tts/text.mp3)");
         }
         $sonos->SetAVTransportURI($uri);
         $sonos->Play();
         IPS_Sleep(500);
         $fileTransportInfo = $sonos->GetTransportInfo();
         while ($fileTransportInfo == 1 || $fileTransportInfo == 5) {
             IPS_Sleep(200);
             $fileTransportInfo = $sonos->GetTransportInfo();
         }
     }
     // reset to what was playing before
     $sonos->SetAVTransportURI($mediaInfo["CurrentURI"], $mediaInfo["CurrentURIMetaData"]);
     if ($positionInfo["Track"] > 1) {
         $sonos->Seek("TRACK_NR", $positionInfo["Track"]);
     }
     if ($positionInfo["TrackDuration"] != "0:00:00") {
         $sonos->Seek("REL_TIME", $positionInfo["RelTime"]);
     }
     if ($volumeChange != 0) {
         // set back volume
         if ($isGroupCoordinator) {
             $this->SetGroupVolume($volume);
         } else {
             $this->SetVolume($volume);
         }
     }
     if ($transportInfo == 1) {
         $sonos->Play();
     }
 }
 /**
  * Query Nut daemon
  */
 public function Query()
 {
     $nut_data = array();
     $pid = $this->GetParent();
     //open socket
     IPS_SetProperty($pid, 'Open', true);
     IPS_ApplyChanges($pid);
     //LIST UPS command
     $ups = $this->GetUPSname();
     if (empty($ups)) {
         $this->SetBuffer("");
         $this->SendText("LIST UPS\n");
         IPS_Sleep(1000);
         $in = $this->GetBuffer();
         $lines = explode("\n", $in);
         $this->SetBuffer("");
         //extract UPS Name to query (assume only one!)
         if (isset($lines[1]) && preg_match('/^UPS\\s+(\\w+)\\s+"(.*)"/', $lines[1], $res)) {
             $ups = $res[1];
             $this->SetUPSname($ups);
         }
     }
     //query named UPS
     if (!empty($ups)) {
         $this->SendText("LIST VAR {$ups} \n");
         //wait
         IPS_Sleep(1000);
         //get response
         $in = $this->GetBuffer();
         //cleaning
         $this->SetBuffer("");
         //translate response into array
         $nut = $this->format_data($in);
         if (count($nut) > 1) {
             //more than one valid row found, looks good
             //parse
             $nut_data = $this->Parse($nut);
         } else {
             IPS_LogMessage(__CLASS__, 'no valid VAR data found for UPS $ups');
         }
     } else {
         $this->debug(__FUNCTION__, "UPSname property not set and no UPS found");
     }
     //Socket close=terminate
     IPS_SetProperty($pid, 'Open', false);
     IPS_ApplyChanges($pid);
     if (count($nut_data) > 0) {
         $this->SendENData($nut_data);
     }
 }
 /**
  * Query Device with given command
  * ToDo:Replace polling var with semaphore
  * @param $cmd
  * @return bool|string Record from Device
  */
 private function Poll($cmd)
 {
     $result = "";
     if ($this->isPolling()) {
         $this->debug(__FUNCTION__, 'already running, canceled');
         return $result;
     }
     //send to device
     $this->SetBuffer('');
     $tosend = chr(0xfe) . chr($cmd) . chr(0xfc);
     $this->SendDataToParent($tosend);
     //waiting for response
     $rcount = 0;
     $inbuf = '';
     $ende = false;
     $this->SetPolling(true);
     IPS_Sleep(1000);
     $buffer = $this->GetBuffer();
     $fstart = false;
     $indata = str_split($buffer);
     $il = count($indata);
     $z = 0;
     while ($il > 0) {
         $z++;
         $bt = $indata[0];
         $b = ord($bt);
         if ($b == 0xfe) {
             $fstart = true;
             break;
         } else {
             array_splice($indata, 0, 1);
             $il = count($indata);
             continue;
             //waitfor start
         }
         //if 02
     }
     //while il
     if ($fstart) {
         $il = count($indata);
         //start recording bytes until end byte and do escapes
         $escape = false;
         while ($il > 0) {
             $bt = $indata[0];
             $b = ord($bt);
             array_splice($indata, 0, 1);
             $il = count($indata);
             if ($b == 0xf8) {
                 $escape = true;
                 continue;
             } else {
                 if ($escape) {
                     $b--;
                     $escape = false;
                     $z++;
                     $inbuf = $inbuf . chr($b);
                 } else {
                     $z++;
                     $inbuf = $inbuf . chr($b);
                     if ($b == 0xfc) {
                         $ende = true;
                         break;
                         //for
                     }
                     //fc
                 }
                 //esc
             }
             //f8
             $rcount++;
         }
         //while il
     }
     //if start
     $this->SetPolling(false);
     if (!$ende) {
         if ($rcount = 0) {
             $this->debug(__FUNCTION__, 'no data');
         }
         if ($rcount > 0) {
             $this->debug(__FUNCTION__, ' wrong data: ' . strToHex($inbuf));
         }
         return false;
     }
     //if !ende
     $this->debug(__FUNCTION__, 'RAW: ' . strlen($inbuf) . ' bytes: ' . strToHex($inbuf));
     $this->SetBuffer('');
     $datum = date('Y-m-d H:i:s', time());
     $vid = $this->GetIDForIdent('LastUpdate');
     SetValueString($vid, $datum);
     return $inbuf;
 }
Example #10
0
 public function Learn()
 {
     $GatewayPassword = $this->GetPassword();
     if ($GatewayPassword !== "") {
         $address = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_USER=user&XC_PASS="******"&XC_FNC=LearnSC&type=IT");
     } else {
         $address = file_get_contents("http://" . $this->GetIPGateway() . "/command?XC_FNC=LearnSC&type=IT");
     }
     //kurze Pause während das Gateway im Lernmodus ist
     IPS_Sleep(1000);
     //1000 ms
     if ($address == "{XC_ERR}Failed to learn code") {
         $this->response = false;
         $instance = IPS_GetInstance($this->InstanceID)["InstanceID"];
         $address = "Das Gateway konnte keine Adresse empfangen.";
         IPS_LogMessage("IT Adresse:", $address);
         echo "Die Adresse vom IT Gerät konnte nicht angelernt werden.";
         IPS_SetProperty($instance, "LearnITCode", false);
         //Haken entfernen.
     } else {
         //Adresse auswerten {XC_SUC}
         //bei Erfolg {XC_SUC}{"CODE":"03"}
         //bei machen Rückmeldung {XC_SUC}{"CODE":"010006"}	 //FC 01 = B DC 00 = 1 und an/aus
         $length = strlen($address);
         if ($length == 25) {
             (string) ($address = substr($address, 17, 4));
             IPS_LogMessage("IT Adresse:", $address);
             // Anpassen der Daten
             $address = str_split($address);
             $ITDeviceCode = $address[2] . $address[3];
             //Devicecode
             $ITFamilyCode = $address[0] . $address[1];
             // Familencode
             $hexsfc = array("00", "01", "02", "03", "04", "05", "06", "07", "08", "09");
             $itfc = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
             $ITFamilyCode = str_replace($hexsfc, $itfc, $ITFamilyCode);
             $hexsdc = array("00", "01", "02", "03", "04", "05", "06", "07", "08", "09");
             $itdc = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
             $ITDeviceCode = str_replace($hexsdc, $itdc, $ITDeviceCode);
         } elseif ($length == 21) {
             (string) ($address = substr($address, 17, 2));
             IPS_LogMessage("IT Adresse:", $address);
             // Anpassen der Daten
             $address = str_split($address);
             $ITDeviceCode = $address[1] + 1;
             //Devicecode auf Original umrechen +1
             $ITFamilyCode = $address[0];
             // Zahlencode in Buchstaben Familencode umwandeln
             $hexsend = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
             $itfc = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
             $ITFamilyCode = str_replace($hexsend, $itfc, $ITFamilyCode);
         }
         $this->AddAddress($ITFamilyCode, $ITDeviceCode);
         $this->response = true;
     }
     return $this->response;
 }
 /**
  * Set IO properties
  */
 private function SyncParent()
 {
     $ParentID = $this->GetParent();
     if ($ParentID > 0) {
         $this->debug(__FUNCTION__, 'entered');
         $ParentInstance = IPS_GetInstance($ParentID);
         if ($ParentInstance['ModuleInfo']['ModuleID'] == $this->module_interfaces['SerialPort']) {
             if (IPS_GetProperty($ParentID, 'DataBits') != 8) {
                 IPS_SetProperty($ParentID, 'DataBits', 8);
             }
             if (IPS_GetProperty($ParentID, 'StopBits') != 1) {
                 IPS_SetProperty($ParentID, 'StopBits', 1);
             }
             if (IPS_GetProperty($ParentID, 'BaudRate') != 9600) {
                 IPS_SetProperty($ParentID, 'BaudRate', 9600);
             }
             if (IPS_GetProperty($ParentID, 'Parity') != 'None') {
                 IPS_SetProperty($ParentID, 'Parity', "None");
             }
             if (IPS_HasChanges($ParentID)) {
                 IPS_SetProperty($ParentID, 'Open', false);
                 @IPS_ApplyChanges($ParentID);
                 IPS_Sleep(200);
                 $port = IPS_GetProperty($ParentID, 'Port');
                 if ($port) {
                     IPS_SetProperty($ParentID, 'Open', true);
                     @IPS_ApplyChanges($ParentID);
                 }
             }
         }
         //serialPort
     }
     //parentID
 }
 /**
  * Timer Event to read current data record from AHA Device(e.g.Fritzbox)
  * discard output
  */
 public function UpdateEvent()
 {
     $this->debug(__FUNCTION__, 'UpdateEvent');
     //delay random time to prevent timer clash
     $delay = rand(500, 5000);
     IPS_Sleep($delay);
     $this->Query();
 }
 /**
  * Send commands to enable !Wire HMS Emulation
  */
 private function init_onewire()
 {
     $this->debug(__FUNCTION__, "Entered");
     //get message variable
     $lmid = $this->GetIDForIdent('AuxMessage');
     //$devid=getVid('OneWireDevices',$reg);
     #read OW-IDs
     IPS_Sleep(1000);
     $this->SendText("Of\r\n");
     IPS_Sleep(2000);
     $this->SendText("OHo\r\n");
     IPS_Sleep(2000);
     $res = substr(GetValueString($lmid), 0, 2);
     if ($res != "ON") {
         $this->SendText("OHo\r\n");
         IPS_Sleep(1500);
         $res = substr(GetValueString($lmid), 0, 2);
     }
     if ($res == "ON") {
         #set 180s intervall
         $this->SendText("OHt180\r\n");
         IPS_Sleep(1500);
         $res = GetValueString($lmid);
         $this->debug(__FUNCTION__, "Set OW HMS Timer 180s:{$res}");
     } else {
         IPS_LogMessage(__CLASS__, "Onewire: Failed to set HMS modus({$res})");
     }
     SetValueString($lmid, "");
 }
 /**
  * Query APCUPSD daemon
  */
 public function Query()
 {
     $apc_data = array();
     $pid = $this->GetParent();
     //open socket
     IPS_SetProperty($pid, 'Open', true);
     IPS_ApplyChanges($pid);
     //LIST UPS command
     $this->SetBuffer("");
     $this->SendText(chr(0) . chr(6) . "status");
     IPS_Sleep(1000);
     $in = $this->GetBuffer();
     $apc = $this->format_data($in);
     $this->SetBuffer("");
     if (isset($apc['APC']) && preg_match("/^(\\d+),(\\d+),(\\d+)/", $apc['APC'])) {
         $apc_data = $this->Parse($apc);
     } else {
         IPS_LogMessage(__CLASS__, 'no valid ups data found');
     }
     //Socket close=terminate
     IPS_SetProperty($pid, 'Open', false);
     IPS_ApplyChanges($pid);
     if (count($apc_data) > 0) {
         $this->SendENData($apc_data);
     }
 }
Example #15
0
 /**
  * MBMaster_Update();
  */
 public function Update()
 {
     $this->Log('Update ...');
     $tstart = microtime(true);
     include_once __DIR__ . "/lib/ModbusMaster.php";
     $URL = "http://" . $this->ReadPropertyString("IPAddress");
     if (!Sys_Ping($this->ReadPropertyString("IPAddress"), 1000)) {
         $this->SetStatus(201);
         trigger_error("Invalid IP-Address", E_USER_ERROR);
         exit;
     }
     if ($this->ReadPropertyInteger("GatewayMode") === 0) {
         $modbus = new ModbusMasterTcp($this->ReadPropertyString("IPAddress"));
     } else {
         $modbus = new ModbusMasterUdp($this->ReadPropertyString("IPAddress"));
     }
     $count = 2;
     if ($this->ReadPropertyInteger("Poller") < 1000) {
         $count = 1000 / $this->ReadPropertyInteger("Poller");
     }
     for ($index = 1; $index < $count; $index++) {
         $this->Log('Update (' . $count . ') | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
         $tstartfor = microtime(true);
         $data = array();
         // FC 1 Rücklesen mehrerer digitaler Ausgänge
         if ($this->ReadPropertyInteger("CoilsQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC1) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readCoils($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("CoilsReference"), $this->ReadPropertyInteger("CoilsQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("CoilsReference");
             foreach ($recData as $Value) {
                 $data["FC1"][$Address] = $Value;
                 $Address++;
             }
         }
         // FC 2 Lesen mehrerer digitaler Eingänge
         if ($this->ReadPropertyInteger("DiscretesQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC2) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readInputDiscretes($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("DiscretesReference"), $this->ReadPropertyInteger("DiscretesQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("DiscretesReference");
             foreach ($recData as $Value) {
                 $data["FC2"][$Address] = $Value;
                 $Address++;
             }
         }
         // FC 3 Lesen mehrerer analoger Eingänge(und Ausgänge)
         if ($this->ReadPropertyInteger("RegistersQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC3) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readMultipleRegisters($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("RegistersReference"), $this->ReadPropertyInteger("RegistersQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("RegistersReference");
             $Values = array_chunk($recData, count($recData) / $this->ReadPropertyInteger("RegistersQuantity"));
             foreach ($Values as $Value) {
                 $data["FC3"][$Address] = $Value;
                 $Address++;
             }
         }
         // FC 4 Lesen mehrerer analoger Eingänge(und Ausgänge)
         if ($this->ReadPropertyInteger("InputRegistersQuantity") > 0) {
             try {
                 if (IPS_SemaphoreEnter("ModbusMaster", 1000)) {
                     $this->Log('Update (FC4) | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
                     $recData = $modbus->readMultipleInputRegisters($this->ReadPropertyInteger("DeviceID"), $this->ReadPropertyInteger("InputRegistersReference"), $this->ReadPropertyInteger("InputRegistersQuantity"));
                     IPS_SemaphoreLeave("ModbusMaster");
                 }
             } catch (Exception $e) {
                 $this->SetStatus(200);
                 trigger_error("ModbusMaster: " . $e->getMessage() . "!", E_USER_ERROR);
                 exit;
             }
             $Address = $this->ReadPropertyInteger("InputRegistersReference");
             $Values = array_chunk($recData, count($recData) / $this->ReadPropertyInteger("InputRegistersQuantity"));
             foreach ($Values as $Value) {
                 $data["FC4"][$Address] = $Value;
                 $Address++;
             }
         }
         $this->SendDataToChildren(json_encode(array("DataID" => "{449015FB-6717-4BB6-9F95-F69945CE1272}", "Buffer" => json_encode($data))));
         $this->SetStatus(102);
         //                $this->Log(number_format(((microtime(true)-$tstart)*1000),2) . ' ms');
         if ($this->ReadPropertyInteger("Poller") < 1000) {
             $Sleep = $this->ReadPropertyInteger("Poller") - (microtime(true) - $tstartfor) * 1000;
             if ($Sleep > 0) {
                 IPS_Sleep($Sleep);
             }
         }
         //                $this->Log(number_format(((microtime(true)-$tstart)*1000),2) . ' ms');
     }
     $this->Log('Update! | ' . number_format((microtime(true) - $tstart) * 1000, 2) . ' ms');
 }
Example #16
0
	$eventName = IPS_GetName($_IPS['EVENT']);

	foreach (IPSEDIP_GetConfiguration() as $configId=>$configData) {
		if (substr($eventName,0,strlen($configId))==$configId) {

			if ($configData[EDIP_CONFIG_REFRESHMETHOD]==EDIP_REFRESHMETHOD_EVENT or
			    $configData[EDIP_CONFIG_REFRESHMETHOD]==EDIP_REFRESHMETHOD_BOTH) {

				$result = IPS_SemaphoreEnter($configId, 0);
				if (!$result) {
				   IPSLogger_Trc(__file__, "Refresh is already in Process - ignore ...");
					return;
				}
				IPSLogger_Trc(__file__, "Refresh EDIP by Event=$eventName");
				IPS_Sleep(1000);
				IPS_SemaphoreLeave($configId);


				$instanceClass = $configData[EDIP_CONFIG_CLASSNAME];
				$instanceId    = IPS_GetObjectIDByIdent($configId, EDIP_ID_PROGRAM);

				include_once $instanceClass.'.class.php';
				$edip = new $instanceClass($instanceId);
				$edip->RefreshDisplay();
			}
		}
	}

	/** @}*/
?>
 /**
  * Enter/Lock semaphore
  * @param $resource
  * @return bool
  */
 protected function SemEnter($resource)
 {
     for ($i = 0; $i < 100; $i++) {
         if (IPS_SemaphoreEnter($this->name . "-" . $resource, 1)) {
             return true;
         } else {
             IPS_Sleep(mt_rand(1, 3));
         }
     }
     return false;
 }