コード例 #1
0
ファイル: MY_Controller.php プロジェクト: Jaesin/zim-web
 public function __construct()
 {
     global $CFG;
     parent::__construct();
     // 		$this->load->helper(array('corestatus', 'url'));
     $this->load->helper(array('corestatus', 'printerlog'));
     // set proper error handler
     set_error_handler(array($this, 'errorToSSO'));
     // add header to disable cache (IE need these headers)
     $this->output->set_header('Pragma: no-cache');
     $this->output->set_header('Cache-Control: no-cache');
     // initialisation status files
     if (!CoreStatus_initialFile()) {
         $this->load->helper('printerlog');
         PrinterLog_logError('status files initialisation error when MY_Controller started', __FILE__, __LINE__);
         // let request failed
         $this->_exitWithError500('file initialisation error');
     }
     // check tromboning autorisation
     if (CoreStatus_checkTromboning(FALSE)) {
         $this->load->helper(array('printerlog', 'errorcode'));
         PrinterLog_logMessage('detected and refused tromboning connection', __FILE__, __LINE__);
         // let request failed
         $this->_exitWithError500(ERROR_REMOTE_REFUSE . ' ' . MyERRMSG(ERROR_REMOTE_REFUSE), ERROR_REMOTE_REFUSE);
     }
     // Workflow management
     if (CoreStatus_checkCallREST()) {
         // we place the control for REST web service in his own class
         // because there are the special error codes for REST web service
         // and we do not need them in normal condition
         return;
     } else {
         $status_current = '';
         $url_redirect = '';
         $array_status = array();
         // stats info (do not stats rest, app can initialize cookies in each request)
         $this->load->library('session');
         if (FALSE === $this->input->cookie('stats_browserLog')) {
             $this->input->set_cookie('stats_browserLog', 'ok', 2592000);
             // 30 days for browser stats
             PrinterLog_statsWebAgent();
         }
         // check initialization issue
         if (CoreStatus_checkInInitialization()) {
             if (CoreStatus_checkCallInitialization($url_redirect)) {
                 return;
                 // we are calling the right page
             }
         } else {
             if (CoreStatus_checkCallInitialization()) {
                 $url_redirect = '/';
             } else {
                 if (CoreStatus_checkInUSB()) {
                     if (CoreStatus_checkCallUSB($url_redirect)) {
                         return;
                         // we are calling the right page
                     }
                 } else {
                     if (CoreStatus_checkCallUSB()) {
                         $url_redirect = '/';
                     } else {
                         if (CoreStatus_checkCallDebug()) {
                             // we always let these interfaces go for debug
                             return;
                         } else {
                             if (CoreStatus_checkInConnection()) {
                                 if (CoreStatus_checkCallNoBlockPageInConnection()) {
                                     return;
                                     // we are calling set hostname, activation or account page
                                 }
                                 if (CoreStatus_checkCallConnection($url_redirect)) {
                                     return;
                                     // we are calling the right page
                                 }
                             } else {
                                 if (CoreStatus_checkCallConnection()) {
                                     $url_redirect = '/';
                                 } else {
                                     if (!CoreStatus_checkInIdle($status_current, $array_status)) {
                                         switch ($status_current) {
                                             case CORESTATUS_VALUE_RECOVERY:
                                                 //TODO finish and test me
                                                 if (CoreStatus_checkCallRecovery($url_redirect)) {
                                                     return;
                                                     // we are calling the right page
                                                 }
                                                 break;
                                             case CORESTATUS_VALUE_PRINT:
                                                 if (CoreStatus_checkCallPrinting($array_status, $url_redirect)) {
                                                     return;
                                                     // we are calling the right page
                                                 }
                                                 break;
                                             case CORESTATUS_VALUE_CANCEL:
                                                 if (CoreStatus_checkCallCanceling($url_redirect)) {
                                                     return;
                                                     // we are calling the right page
                                                 }
                                                 break;
                                             case CORESTATUS_VALUE_LOAD_FILA_L:
                                             case CORESTATUS_VALUE_LOAD_FILA_R:
                                                 if (CoreStatus_checkCallloading($url_redirect)) {
                                                     return;
                                                     // we are calling the right page
                                                 }
                                                 // 						return; // we do not block users when charging filament
                                                 break;
                                             case CORESTATUS_VALUE_UNLOAD_FILA_L:
                                             case CORESTATUS_VALUE_UNLOAD_FILA_R:
                                                 //FIXME finish here to block users
                                                 if (CoreStatus_checkCallUnloading($url_redirect)) {
                                                     return;
                                                     // we are calling the right page
                                                 }
                                                 break;
                                             case CORESTATUS_VALUE_SLICE:
                                                 if (CoreStatus_checkCallSlicing($url_redirect)) {
                                                     return;
                                                 }
                                                 break;
                                             default:
                                                 $url_redirect = '/';
                                                 // internal error, never reach here normally
                                                 break;
                                         }
                                     } else {
                                         if (CoreStatus_checkCallPrintingAjax() || CoreStatus_checkCallCancelingAjax()) {
                                             // let ajax request failed when we finishing printing / canceling
                                             $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
                                             header($protocol . ' 403');
                                             header('Content-type: text/plain; charset=UTF-8');
                                             echo 'Not in printing / canceling';
                                             exit;
                                         } else {
                                             if (CoreStatus_checkCallPrinting() || CoreStatus_checkCallCanceling()) {
                                                 $url_redirect = '/';
                                             } else {
                                                 if (CoreStatus_checkInPrinted()) {
                                                     if (CoreStatus_checkCallEndPrinting($url_redirect) || CoreStatus_checkCallEndPrintingPlus()) {
                                                         return;
                                                     }
                                                 } else {
                                                     if (CoreStatus_checkCallEndPrinting()) {
                                                         $url_redirect = '/';
                                                         // redirect to homepage when we have no timelapse
                                                     }
                                                 }
                                             }
                                         }
                                         if ($url_redirect) {
                                             header('Location: ' . $url_redirect);
                                             exit;
                                         }
                                         return;
                                         // continue to generate the current page
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // log error if we have no redirect url when reaching here
         if (is_null($url_redirect)) {
             $this->load->helper('printerlog');
             PrinterLog_logError('no redirect place when MY_Controller finished', __FILE__, __LINE__);
         }
         header('Location: ' . $url_redirect);
         exit;
     }
 }
コード例 #2
0
ファイル: rest.php プロジェクト: Jaesin/zim-web
 function __construct()
 {
     parent::__construct();
     $this->load->helper(array('form', 'url', 'json', 'errorcode', 'corestatus'));
     $status_current = '';
     if (CoreStatus_checkInInitialization() || CoreStatus_checkInConnection() || CoreStatus_checkInUSB()) {
         // let no block REST web service go for setting network
         if (CoreStatus_checkCallNoBlockRESTInConnection()) {
             return;
         }
         // we haven't finished initialization or connection yet
         $this->_exitWithError500(ERROR_BUSY_PRINTER . ' ' . t(MyERRMSG(ERROR_BUSY_PRINTER)), ERROR_BUSY_PRINTER);
     } else {
         if (!CoreStatus_checkInIdle($status_current)) {
             // check if the status is changed
             $ret_val = 0;
             $this->load->helper('printerstate');
             switch ($status_current) {
                 case CORESTATUS_VALUE_SLICE:
                     if (CoreStatus_checkCallNoBlockRESTInSlice()) {
                         // do not block some special REST for action in slicing
                         return;
                     }
                     $ret_val = PrinterState_checkBusyStatus($status_current);
                     if ($ret_val == FALSE) {
                         // still in slicing
                         break;
                     } else {
                         if ($status_current == CORESTATUS_VALUE_IDLE) {
                             // encounted some errors
                             break;
                         } else {
                             // CORESTATUS_VALUE_PRINT
                             $this->load->helper('printerlog');
                             PrinterLog_logMessage('call rest when we are in slicing, but finished really', __FILE__, __LINE__);
                         }
                     }
                     // we treat canceling as printing
                 // we treat canceling as printing
                 case CORESTATUS_VALUE_PRINT:
                     // do not block some special REST for action in printing
                     if (CoreStatus_checkCallNoBlockRESTInPrint()) {
                         return;
                     }
                 case CORESTATUS_VALUE_CANCEL:
                     //TODO test here for canceling
                     $ret_val = PrinterState_checkInPrint();
                     if ($ret_val == FALSE) {
                         $ret_val = CoreStatus_setInIdle();
                         if ($ret_val == TRUE) {
                             $this->load->helper('printerlog');
                             PrinterLog_logDebug('set idle when calling REST', __FILE__, __LINE__);
                             return;
                             // continue to generate if we are now in idle
                         }
                         $this->load->helper('printerlog');
                         PrinterLog_logError('can not set status in idle', __FILE__, __LINE__);
                     }
                     break;
                 default:
                     $ret_val = PrinterState_checkBusyStatus($status_current);
                     if ($ret_val == TRUE) {
                         $this->load->helper('printerlog');
                         PrinterLog_logDebug('set idle when calling REST', __FILE__, __LINE__);
                         return;
                         // status has changed to idle
                     }
                     break;
             }
             // do not block some special REST
             if (CoreStatus_checkCallNoBlockREST()) {
                 return;
             }
             // return that printer is busy
             $this->_exitWithError500(ERROR_BUSY_PRINTER . ' ' . t(MyERRMSG(ERROR_BUSY_PRINTER)), ERROR_BUSY_PRINTER);
         }
     }
 }
コード例 #3
0
function PrinterState_checkStatusAsArray($extra_info = TRUE)
{
    $command = '';
    $output = array();
    $ret_val = 0;
    $data_json = array();
    $temp_data[PRINTERSTATE_TITLE_LASTERROR] = array();
    $time_start = NULL;
    $status_current = '';
    // if we need duration, the function that get duration by id is necessary
    // and we must stock print list id somewhere in json file
    $CI =& get_instance();
    $CI->load->helper('corestatus');
    if (CoreStatus_checkInUSB()) {
        $data_json[PRINTERSTATE_TITLE_STATUS] = CORESTATUS_VALUE_USB;
        return $data_json;
    } else {
        if (CoreStatus_checkInConnection()) {
            $data_json[PRINTERSTATE_TITLE_STATUS] = CORESTATUS_VALUE_WAIT_CONNECT;
            return $data_json;
        }
    }
    $ret_val = CoreStatus_checkInIdle($status_current, $status_json);
    if ($ret_val == TRUE) {
        $data_json[PRINTERSTATE_TITLE_STATUS] = CORESTATUS_VALUE_IDLE;
        //TODO think about if we need to display last error as 200 (error_ok) or not
        if (array_key_exists(CORESTATUS_TITLE_LASTERROR, $status_json) && !is_null($status_json[CORESTATUS_TITLE_LASTERROR]) && $status_json[CORESTATUS_TITLE_LASTERROR] != ERROR_OK) {
            // 			$data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_LASTERROR] = $status_json[CORESTATUS_TITLE_LASTERROR];
            // 			$data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_DETAILMSG] = $status_json[CORESTATUS_TITLE_MESSAGE];
            $data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_SLICE_ERR] = $status_json[CORESTATUS_TITLE_LASTERROR] . ' ' . $status_json[CORESTATUS_TITLE_MESSAGE];
        }
    } else {
        if ($ret_val == FALSE) {
            $temp_data = array();
            $status_old = $status_current;
            PrinterState_checkBusyStatus($status_current, $temp_data);
            if (in_array($status_current, array(CORESTATUS_VALUE_SLICE, CORESTATUS_VALUE_PRINT))) {
                $data_json[PRINTERSTATE_TITLE_PERCENT] = $temp_data[PRINTERSTATE_TITLE_PERCENT];
            }
            $data_json[PRINTERSTATE_TITLE_STATUS] = $status_current;
            // return error code in the first time when we have error from slicing=>idle
            if ($status_old == CORESTATUS_VALUE_SLICE && $status_current == CORESTATUS_VALUE_IDLE && array_key_exists(PRINTERSTATE_TITLE_LASTERROR, $temp_data)) {
                // 			$data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_LASTERROR] = $temp_data[PRINTERSTATE_TITLE_LASTERROR];
                // 			$data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_DETAILMSG] = $temp_data[PRINTERSTATE_TITLE_DETAILMSG];
                $data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_SLICE_ERR] = $temp_data[PRINTERSTATE_TITLE_LASTERROR] . ' ' . $temp_data[PRINTERSTATE_TITLE_DETAILMSG];
            } else {
                if ($status_current == CORESTATUS_VALUE_PRINT) {
                    $print_operation = PRINTERSTATE_VALUE_PRINT_OPERATION_PRINT;
                    $array_status = array();
                    // add temperature
                    if ($extra_info == TRUE && $data_json[PRINTERSTATE_TITLE_PERCENT] != 100) {
                        $data_temperature = PrinterState_getExtruderTemperaturesAsArray();
                        if (!is_array($data_temperature)) {
                            // log internal error
                            $CI->load->helper('printerlog');
                            PrinterLog_logError('API error when getting temperatures in printing', __FILE__, __LINE__);
                        } else {
                            $data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_EXT_TEMP_L] = array_key_exists(PRINTERSTATE_LEFT_EXTRUD, $data_temperature) ? $data_temperature[PRINTERSTATE_LEFT_EXTRUD] : 0;
                            $data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_EXT_TEMP_R] = array_key_exists(PRINTERSTATE_RIGHT_EXTRUD, $data_temperature) ? $data_temperature[PRINTERSTATE_RIGHT_EXTRUD] : 0;
                            $data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_EXT_TEMP_B] = array_key_exists(PRINTERSTATE_HEAT_BED, $data_temperature) ? $data_temperature[PRINTERSTATE_HEAT_BED] : 0;
                        }
                    }
                    // try to calculate time remained when percentage is passed offset
                    $ret_val = CoreStatus_getStatusArray($array_status);
                    if ($ret_val == TRUE && isset($array_status[CORESTATUS_TITLE_STARTTIME]) && isset($array_status[CORESTATUS_TITLE_ESTIMATE_T])) {
                        $time_pass = time() - $array_status[CORESTATUS_TITLE_STARTTIME];
                        $data_json[PRINTERSTATE_TITLE_PASSTIME] = $time_pass;
                        if (isset($data_json[PRINTERSTATE_TITLE_PERCENT]) && ($time_pass >= PRINTERSTATE_VALUE_OFST_TO_CAL_TIME || $data_json[PRINTERSTATE_TITLE_PERCENT] >= PRINTERSTATE_VALUE_OFST_TO_CAL_PCT)) {
                            // rest time = total time - passed time
                            // total time = estimate time (by filament) * (1 - percentage ^ 0.5) + estimate time (by time) * percentage ^ 0.5
                            // estimate time (by time) = passed time / percentage
                            // 					$percentage_finish = $data_json[PRINTERSTATE_TITLE_PERCENT] / 100;
                            $calculate_factor = sqrt($data_json[PRINTERSTATE_TITLE_PERCENT] / 100);
                            $time_estimation = $array_status[CORESTATUS_TITLE_ESTIMATE_T];
                            $data_json[PRINTERSTATE_TITLE_DURATION] = (int) ($time_estimation * (1 - $calculate_factor) + $time_pass * (1 / $calculate_factor - 1));
                        }
                    }
                    // check operation
                    if (file_exists(PRINTERSTATE_FILE_PRINT_HEAT)) {
                        $print_operation = PRINTERSTATE_VALUE_PRINT_OPERATION_HEAT;
                    } else {
                        if ($data_json[PRINTERSTATE_TITLE_PERCENT] == 100) {
                            $print_operation = PRINTERSTATE_VALUE_PRINT_OPERATION_END;
                        }
                    }
                    $data_json[PRINTERSTATE_TITLE_EXTEND_PRM][PRINTERSTATE_TITLE_EXT_OPER] = $print_operation;
                }
            }
        }
    }
    if ($extra_info == TRUE && $status_current == CORESTATUS_VALUE_IDLE) {
        // check if we need to change idle into sliced or not
        PrinterState_checkSlicedCondition($data_json);
        //TODO add timelapse checking
        PrinterState_checkTimelapseCondition($data_json);
    }
    return $data_json;
}