Hier mal ein paar Methoden um einen einfachen Timer in einem Modul zu nutzen.
Verwendung :
public function Create()
{
//Never delete this line!
parent::Create();
$this->RegisterTimer('RequestPinState', $this->ReadPropertyInteger('Interval'), 'XBee_RequestState($_IPS[\'TARGET\']);');
// IDENT INTERVAL FUNKTION
}
public function ApplyChanges()
{
//Never delete this line!
parent::ApplyChanges();
$this->SetTimerInterval('RequestPinState', $this->ReadPropertyInteger('Interval'));
}
Methoden:
protected function RegisterTimer($Name, $Interval, $Script)
{
$id = @IPS_GetObjectIDByIdent($Name, $this->InstanceID);
if ($id === false)
$id = 0;
if ($id > 0)
{
if (!IPS_EventExists($id))
throw new Exception("Ident with name " . $Name . " is used for wrong object type");
if (IPS_GetEvent($id)['EventType'] <> 1)
{
IPS_DeleteEvent($id);
$id = 0;
}
}
if ($id == 0)
{
$id = IPS_CreateEvent(1);
IPS_SetParent($id, $this->InstanceID);
IPS_SetIdent($id, $Name);
}
IPS_SetName($id, $Name);
IPS_SetHidden($id, true);
IPS_SetEventScript($id, $Script);
if ($Interval > 0)
{
IPS_SetEventCyclic($id, 0, 0, 0, 0, 1, $Interval);
IPS_SetEventActive($id, true);
}
else
{
IPS_SetEventCyclic($id, 0, 0, 0, 0, 1, 1);
IPS_SetEventActive($id, false);
}
}
protected function UnregisterTimer($Name)
{
$id = @IPS_GetObjectIDByIdent($Name, $this->InstanceID);
if ($id > 0)
{
if (!IPS_EventExists($id))
throw new Exception('Timer not present');
IPS_DeleteEvent($id);
}
}
protected function SetTimerInterval($Name, $Interval)
{
$id = @IPS_GetObjectIDByIdent($Name, $this->InstanceID);
if ($id === false)
throw new Exception('Timer not present');
if (!IPS_EventExists($id))
throw new Exception('Timer not present');
$Event = IPS_GetEvent($id);
if ($Interval < 1)
{
if ($Event['EventActive'])
IPS_SetEventActive($id, false);
}
else
{
if ($Event['CyclicTimeValue'] <> $Interval)
IPS_SetEventCyclic($id, 0, 0, 0, 0, 1, $Interval);
if (!$Event['EventActive'])
IPS_SetEventActive($id, true);
}
}