<?php
///////////////////////////////////////////////////////////////////////////
class ZTVApi
{
///////////////////////////////////////////////////////////////////////////
const DATETIME_FORMAT = 'Y:m:d H:i:sO';
///////////////////////////////////////////////////////////////////////////
public static function set_terminal_params(stdClass $profile)
{
return HD::make_json_rpc_request('set_terminal_params',
'macaddr' => ZTVApi::get_mac_addr(),
'profile_id' => $profile->id
)
);
}
///////////////////////////////////////////////////////////////////////////
public static function get_terminal_params()
{
return HD::make_json_rpc_request('get_terminal_params',
'macaddr' => ZTVApi::get_mac_addr()
)
);
}
///////////////////////////////////////////////////////////////////////////
public static function get_profiles()
{
return HD::make_json_rpc_request('get_profiles',
array( 'macaddr' => ZTVApi
::get_mac_addr() ) );
}
///////////////////////////////////////////////////////////////////////////
public static function get_playlists()
{
return HD::make_json_rpc_request('get_playlists',
'macaddr' => ZTVApi::get_mac_addr(),
'profile' => true
)
);
}
///////////////////////////////////////////////////////////////////////////
public static function get_epg($media_id, $limit, $start, $stop)
{
return HD::make_json_rpc_request('get_epg',
'media_id' => $media_id,
'limit' => $limit,
'start' => ZTVApi::format_timestamp($start, 'Y-m-d H:i:sO'),
'stop' => ZTVApi::format_timestamp($stop, 'Y-m-d H:i:sO')
)
);
}
///////////////////////////////////////////////////////////////////////////
public static function set_profile(stdClass $profile, $profile_password = null)
{
ZTVApi::set_terminal_params($profile),
ZTVApi::get_terminal_params()
);
if( $profile->require_password && !is_null($profile_password) ) { $query[] = ZTVApi::update_profile($profile, $profile_password);
}
return $query;
}
///////////////////////////////////////////////////////////////////////////
public static function update_profile(stdClass $profile, $profile_password)
{
return HD::make_json_rpc_request('update_profile',
'macaddr' => ZTVApi::get_mac_addr(),
'profile_id' => $profile->id,
'profile_password' => $profile_password
)
);
}
///////////////////////////////////////////////////////////////////////////
public static function call($json_request)
{
$json_reply = null;
for($i = 0; $i < 3; ++$i) {
try {
$doc = HD
::http_post_document( ZTVConfig
::API_URL, json_encode($json_request) ); } catch(Exception $e) {
hd_print("Error: failed to do HTTP-request.");
if($i == 2) {
throw new DuneException('API is unavailable', 0,
ActionFactory::show_error(true, 'Системная ошибка',
'Сервер недоступен(' . $e->getMessage() . ').',
'Пожалуйста обратитесь в тех. поддержку.'
)
)
);
}
continue;
}
break;
}
hd_print("Error: failed to decode API reply: '$doc'");
throw new DuneException('API returned empty result', 0,
ActionFactory::show_error(true, 'Системная ошибка',
'Сервер вернул пустой ответ.',
'Пожалуйста обратитесь в тех. поддержку.'
)
)
);
}
if( isset($json_reply->error) && $json_reply->error ) { hd_print("Error: API returned error status($doc)");
throw new DuneException('API returned error', $json_reply->code,
ActionFactory::show_error(true, 'Системная ошибка',
'Сервер вернул ошибку(' . $json_reply->message . ').',
'Пожалуйста обратитесь в тех. поддержку.'
)
)
);
}
// TODO: Think of a better check.
if( !isset($json_request['method']) ) { $request_count = count($json_request); $reply_count = count($json_reply); if($request_count != $reply_count) {
return false;
}
for($i = 0, $n = 0; $i < $reply_count; $n = $n + ($json_reply[$i]->result ? 1 : 0), $i++);
if($i != $n) {
return false;
}
}
return $json_reply;
}
public static function format_timestamp($ts, $fmt = ZTVApi::DATETIME_FORMAT)
{
return HD::format_timestamp($ts, $fmt);
}
public static function parse_timestamp($ts_str, $fmt = ZTVApi::DATETIME_FORMAT)
{
return null;
}
$ts = DateTime::createFromFormat($fmt, $ts_str);
if ($ts === false) {
hd_print ("Warning: invalid timestamp string '$ts_str'");
$ts = null;
}
return $ts;
}
public static function get_mac_addr()
{
static $mac_addr = null;
$mac_addr = str_replace( ':', '-', HD
::get_mac_addr() ); }
return $mac_addr;
}
}
///////////////////////////////////////////////////////////////////////////
?>