altob.rd 8 роки тому
джерело
коміт
ed8705baf8
2 змінених файлів з 152 додано та 0 видалено
  1. +128
    -0
      controllers/Acti_service.php
  2. +24
    -0
      phplibs/Altob.Sync.Integration.php

+ 128
- 0
controllers/Acti_service.php Переглянути файл

@@ -0,0 +1,128 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
file: Acti_service.php 與 acti 介接相關都放這支
*/
// ----- 定義常數(路徑, cache秒數) -----
define('APP_VERSION', '100'); // 版本號
define('MAX_AGE', 604800); // cache秒數, 此定義1個月
define('APP_NAME', 'acti_service'); // 應用系統名稱
define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib
define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name
define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/acti_service.'); // log file name

class Acti_service extends CI_Controller
{
var $vars = array();
function __construct()
{
parent::__construct();
// ----- 程式開發階段log設定 -----
if (@ENVIRONMENT == 'development')
{
ini_set('display_errors', '1');
//error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL);
}
set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
// 阻檔未知的 IP
$from_ip = $this->my_ip();
if(!in_array($from_ip, array('127.0.0.1')))
{
trigger_error('refused://from:'.$from_ip.'..refused..'.print_r($_REQUEST, true));
exit;
}
$method_name = $this->router->fetch_method();
if (in_array($method_name, array('sos')))
{
ob_end_clean();
ignore_user_abort();
ob_start();
echo 'ok';
header('Connection: close');
header('Content-Length: ' . ob_get_length());
ob_end_flush();
flush();
}
}
// 取得 IP
function my_ip()
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_REAL_IP'))
{
$ip = getenv('HTTP_X_REAL_IP');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}

return $ip;
}
// 發生錯誤時集中在此處理
public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
$log_msg = explode('://', $errstr);
if (count($log_msg) > 1)
{
$log_file = $log_msg[0];
$str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
}
else
{
$log_file = APP_NAME;
$str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
}
error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
}
// 顯示logs
public function show_logs()
{
$lines = $this->uri->segment(3); // 顯示行數
if (empty($lines)) $lines = 100; // 無行數參數, 預設為40行
// echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
echo '</pre></body></html>';
}
// [區網] 由設備端呼叫
public function sos()
{
$station_no = $this->uri->segment(3); // 場站編號
$machine_no = $this->uri->segment(4); // 設備編號
trigger_error(__FUNCTION__ . "..{$station_no},{$machine_no}..");
require_once(ALTOB_SYNC_FILE) ;
// 傳送 SOS
$sync_agent = new AltobSyncAgent();
$sync_agent->init($station_no, date('Y-m-d H:i:s'));
$sync_result = $sync_agent->sync_st_sos($machine_no);
trigger_error( "..sync_st_sos.." . $sync_result);
}
}

+ 24
- 0
phplibs/Altob.Sync.Integration.php Переглянути файл

@@ -157,6 +157,30 @@ class AltobSyncAgent
// 傳遞參數至遠端。
return $oService->ServerPost($this->post_parms);
}
// ===============================================
// SOS
// ===============================================
// 傳送 SOS
public function sync_st_sos($machine_no)
{
$error_parms_msg = $this->check_init_parms();
if(!empty($error_parms_msg)) { return $error_parms_msg; }
if(empty($machine_no))
return 'machine_no not found';
$this->post_parms['machine_no'] = $machine_no;
// 初始化網路服務物件。
$oService = new AltobSyncService();
$oService->ServiceURL = AltobSyncAgent::SYNC_CARS_URL;
$oService->ServiceCMD = 'st_sos';
// 傳遞參數至遠端。
return $oService->ServerPost($this->post_parms);
}
// ===============================================
// members


Завантаження…
Відмінити
Зберегти