PHP Sample Code

The following is an complete example of how to use all of the functionality of Zeus API in PHP.


require './ZeusAPI.php';

// Notes: You can obtain your authaccount and authpass,
// when you have made login on ZeusWeb, going to URL: api_credentials.html
// - IP should have this format "192.168.1.1", or the domain name
// - If you want the client connect to a secure server (SSL), the SSL parameter must be True 

try
{
    // Get your yourAuthAccount and yourAuthpass by logging into ZeusWeb and going to api_credentials.html
    $client = new Client("IP of the Server", Port, False, "yourAuthAccount", "yourPassword", "yourAuthpass");
} catch(Exception $e) {
    echo $e;
}

// We check that the object creation was correct.
if($client->IsCreated() == false)
{
    echo $client->GetZeusApiLastError();
	// You should not continue.
	return;
}

// Gets historian of selected channels (0, 1) of station "MyStation" since yesterday until now
$myHistorical = $client->GetHistorical("MyStation", (new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')))->sub(new DateInterval('P1D')), new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')), array(0,1));
if(is_null($myHistorical) == true)
    echo $client->GetZeusApiLastError();
else
{
    foreach($myHistorical as $element)
        echo "DATE: " . ConvertFromJson($element->DateOfRecord)->format('Y-m-d H:i:s.u') . " REASON: " . $element->Reason . " CHANNEL_ID: " . $element->ChannelID . " VALUE: " . $element->Value . "
"; } // Gets the real time values / last known values of the station "MyStation" $mylastknownvalues = $client->GetLastKnownValues("MyStation"); if(is_null($mylastknownvalues) == true) echo $client->GetZeusApiLastError(); else { $mylastknownvalues->values["0"] = 52.5; $mylastknownvalues->values["1"] = 40.3; // Uploads the changed values to the Server if($client->SetLastKnownValues("MyStation", new LastKnownValues(ConvertFromJson($mylastknownvalues->lastConnectionTime), $mylastknownvalues->RSSI, $mylastknownvalues->PowerSupply, $mylastknownvalues->values)) == false) echo $client->GetZeusApiLastError(); } // Gets alarms of all stations that user can see, since yesterday until now $myAlarm = $client->GetAlarm((new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')))->sub(new DateInterval('P1D')), new DateTimeAPI("now", new DateTimeZone('Europe/Madrid'))); if(is_null($myAlarm) == true) echo $client->GetZeusApiLastError(); else { foreach($myAlarm as $element) echo "DATE: " . ConvertFromJson($element->DateOfRecord)->format('Y-m-d H:i:s.u') . " STATION: " . $element->StationID . " REASON: " . $element->Reason . " CHANNEL_ID: " . $element->ChannelID . " TEXT: " . $element->AlarmText . "
"; } // Upload historians for station "MyStation" if($client->SetHistorical("MyStation", array(new Historical(new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')), 0, "0", 10.2), new Historical(new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')), 1, "0", 15.5))) == false) echo $client->GetZeusApiLastError(); // Upload alarms for station "MyStation" if($client->SetAlarms(array(new Alarms("MyStation", new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')), 1, 21, "This is my high level alarm", ""), new Alarms("MyStation", new DateTimeAPI("now", new DateTimeZone('Europe/Madrid')), 3, 22, "This is my restored alarm", ""))) == false) echo $client->GetZeusApiLastError(); // Send a message to the station "Microcom" if($client->SendMessage("Microcom", "my message") == false) echo $client->GetZeusApiLastError(); // Gets enqueued messages pending of execution on server $myMessages = $client->PendingMessages(); if(is_null($myMessages) == true) echo $client->GetZeusApiLastError(); else { foreach($myMessages as $element) echo "KEY: " . $element->Key . " VALUE: " . $element->Value . "
"; } // Gets ProgrammableRelayPeriod of selected output (0) of station "MyStation" $myConfigurationRelay = $client->GetConfigurationRelay("MyStation", 0); if(is_null($myConfigurationRelay) == true) echo $client->GetZeusApiLastError(); else { foreach($myConfigurationRelay as $element) echo "WEEKDAYS: " + element->Weekdays . " ACTIVATIONTIME: " . element->ActivationTime . " DURATION: " . element->Duration . "
"; } // Send a Relay Configuration to the station "Microcom" to output 0 ' $ProgrammableRelayPeriod = array(); $ProgrammableRelayPeriod[0] = new ProgrammableRelayPeriod(array("L", "M", "X"), new DateTimeAPI(), 5); $ProgrammableRelayPeriod[1] = new ProgrammableRelayPeriod(array("L", "M", "X", "J", "V", "S", "D"), new DateTimeAPI(), 20); if($client->SetConfigurationRelay("Microcom", 0, $ProgrammableRelayPeriod) == false) echo $client->GetZeusApiLastError();