Exemple #1
0
function Slicer_checkSlice(&$progress, &$message = NULL, &$array_extruder = array())
{
    $cr = 0;
    $ret_val = 0;
    if (file_exists(SLICER_FILE_REMOTE_STATUS)) {
        // remote slicing case
        $tmp_array = array();
        $CI =& get_instance();
        // read remote slicing status
        $CI->load->helper('json');
        $tmp_array = json_read(SLICER_FILE_REMOTE_STATUS, TRUE);
        if (!file_exists(SLICER_FILE_REMOTE_STATUS)) {
            $progress = 99;
            $message = 'state_finalize_remote';
            $cr = ERROR_OK;
        } else {
            if (isset($tmp_array['error'])) {
                $CI->load->helper('printerlog');
                PrinterLog_logError('read json error: ' . json_encode($tmp_array), __FILE__, __LINE__);
                Slicer_sliceHalt(TRUE);
                // force to cancel remote slicing
                $cr = ERROR_INTERNAL;
            } else {
                $status_array = $tmp_array['json'];
                // check state
                if (!isset($status_array[SLICER_TITLE_REMOTE_STATE])) {
                    $CI->load->helper('printerlog');
                    PrinterLog_logError('unknown remote slicing status json format', __FILE__, __LINE__);
                    return ERROR_INTERNAL;
                }
                $cr = ERROR_OK;
                switch ($status_array[SLICER_TITLE_REMOTE_STATE]) {
                    case SLICER_VALUE_REMOTE_STATE_INITIAL:
                        $progress = 0;
                        $message = 'state_initialize_remote';
                        break;
                    case SLICER_VALUE_REMOTE_STATE_REQUEST:
                        $progress = 0;
                        $message = 'state_request_remote';
                        break;
                    case SLICER_VALUE_REMOTE_STATE_UPLOAD:
                        $progress = 0;
                        $message = 'state_upload_remote';
                        break;
                    case SLICER_VALUE_REMOTE_STATE_WORKING:
                        // use default value for error cases (phrasing extended failed, etc.)
                        $progress = 0;
                        $message = 'state_working_remote';
                        $CI->load->helper('printerlog');
                        if (!file_exists(PRINTERLOG_STATS_TMPFILE_SLICE_UPLOAD_END)) {
                            $stats_info = array();
                            $fp = fopen(PRINTERLOG_STATS_TMPFILE_SLICE_UPLOAD_END, 'w');
                            if ($fp) {
                                fwrite($fp, 'upload_end');
                                fclose($fp);
                            }
                            // stats info
                            $stats_info[PRINTERLOG_STATS_SLICE_SERVER] = isset($status_array[SLICER_TITLE_REMOTE_URL]) ? $status_array[SLICER_TITLE_REMOTE_URL] : PRINTERLOG_STATS_VALUE_REMOTE;
                            PrinterLog_statsSlice(PRINTERLOG_STATS_ACTION_UPLOADED, $stats_info);
                        }
                        if (isset($status_array[SLICER_TITLE_REMOTE_EXTENDED])) {
                            if ($status_array[SLICER_TITLE_REMOTE_EXTENDED] == SLICER_MSG_REMOTE_INITIAL) {
                                // remote slicing initialization case
                                $message = 'state_loading_remote';
                                break;
                            }
                            $explode_array = explode("=", $status_array[SLICER_TITLE_REMOTE_EXTENDED]);
                            if (count($explode_array) >= 2) {
                                $progress = (int) trim($explode_array[0]);
                                $message = trim($explode_array[1]);
                                // blind model path
                                if (strpos($message, SLICER_MSG_EXPORTING_MODEL) !== FALSE) {
                                    $message = SLICER_MSG_EXPORTING_MODEL;
                                }
                            }
                        }
                        break;
                    case SLICER_VALUE_REMOTE_STATE_DOWNLOAD:
                        $progress = 99;
                        $message = 'state_download_remote';
                        $CI->load->helper('printerlog');
                        if (!file_exists(PRINTERLOG_STATS_TMPFILE_SLICE_DOWNLOAD_START)) {
                            $stats_info = array();
                            $fp = fopen(PRINTERLOG_STATS_TMPFILE_SLICE_DOWNLOAD_START, 'w');
                            if ($fp) {
                                fwrite($fp, 'download_start');
                                fclose($fp);
                            }
                            // stats info
                            $stats_info[PRINTERLOG_STATS_SLICE_SERVER] = isset($status_array[SLICER_TITLE_REMOTE_URL]) ? $status_array[SLICER_TITLE_REMOTE_URL] : PRINTERLOG_STATS_VALUE_REMOTE;
                            PrinterLog_statsSlice(PRINTERLOG_STATS_ACTION_DOWNLOAD, $stats_info);
                        }
                        break;
                    case SLICER_VALUE_REMOTE_STATE_LOCAL:
                        $progress = 0;
                        $message = 'state_local_remote';
                        break;
                    case SLICER_VALUE_REMOTE_STATE_ERROR:
                    default:
                        $progress = -2;
                        // 					$message = 'state_error_remote';
                        if ($status_array[SLICER_TITLE_REMOTE_STATE] != SLICER_VALUE_REMOTE_STATE_ERROR) {
                            $CI->load->helper('printerlog');
                            PrinterLog_logError('unknown remote slicing status', __FILE__, __LINE__);
                        }
                        // use message as remote server url
                        $message = isset($status_array[SLICER_TITLE_REMOTE_URL]) ? $status_array[SLICER_TITLE_REMOTE_URL] : PRINTERLOG_STATS_VALUE_REMOTE;
                        $cr = ERROR_REMOTE_SLICE;
                        break;
                }
            }
        }
        return $cr;
    }
    // local slicing case
    $ret_val = Slicer__requestSlicer(SLICER_URL_SLICE_STATUS, TRUE, $response);
    if ($ret_val == SLICER_RESPONSE_OK) {
        if ((int) $response < 0) {
            $cr = ERROR_NO_SLICING;
            $progress = -1;
        } else {
            $cr = ERROR_OK;
            $progress = (int) $response;
            if ($progress == 100) {
                $CI =& get_instance();
                $CI->load->helper('printerstate');
                $explode_array = explode("\n", $response);
                if (isset($explode_array[1])) {
                    $explode_array = explode(';', $explode_array[1]);
                    foreach ($explode_array as $key_value) {
                        $tmp_array = explode(':', $key_value);
                        $abb_filament = PrinterState_cartridgeNumber2Abbreviate((int) $tmp_array[0]);
                        $array_extruder[$abb_filament] = ceil($tmp_array[1]);
                    }
                } else {
                    $cr = ERROR_INTERNAL;
                }
            } else {
                $explode_array = explode("\n", $response);
                if (isset($explode_array[1])) {
                    $message = $explode_array[1];
                    // blind model path
                    if (strpos($message, SLICER_MSG_EXPORTING_MODEL) !== FALSE) {
                        $message = SLICER_MSG_EXPORTING_MODEL;
                    }
                }
            }
        }
    } else {
        if ($ret_val == SLICER_RESPONSE_ERROR) {
            if (strpos($response, "InitalError") !== FALSE) {
                $cr = ERROR_WRONG_PRM;
            } else {
                if (strpos($response, "ExportError") !== FALSE) {
                    $cr = ERROR_UNKNOWN_MODEL;
                } else {
                    $cr = ERROR_INTERNAL;
                }
            }
            $progress = -1;
            $message = $response;
            $CI =& get_instance();
            $CI->load->helper('printerlog');
            PrinterLog_logError('slicer error: ' . $response);
        } else {
            $cr = ERROR_INTERNAL;
        }
    }
    return $cr;
}
function PrinterState_checkBusyStatus(&$status_current, &$array_data = array())
{
    $ret_val = 0;
    $time_wait = NULL;
    $time_max = NULL;
    // 	$temp_status = NULL;
    $temp_array = array();
    $CI =& get_instance();
    $CI->load->helper('corestatus');
    switch ($status_current) {
        case CORESTATUS_VALUE_WAIT_CONNECT:
            $ret_val = CoreStatus_checkInConnection();
            if ($ret_val == FALSE) {
                CoreStatus_setInIdle();
                $status_current = CORESTATUS_VALUE_IDLE;
                return TRUE;
            }
            break;
        case CORESTATUS_VALUE_CANCEL:
            // jump out if it's a simulator or when cancelling is finished
            if ($CI->config->item('simulator') || !file_exists(PRINTERSTATE_FILE_STOPFILE)) {
                $stats_info = PrinterState_prepareStatsPrintLabel();
                PrinterLog_statsPrint(PRINTERLOG_STATS_ACTION_CANCEL, $stats_info);
                CoreStatus_setInIdle();
                $status_current = CORESTATUS_VALUE_IDLE;
                return TRUE;
            }
            break;
        case CORESTATUS_VALUE_SLICE:
            // get percentage and check finished or not
            $progress = 0;
            $message = NULL;
            $array_slicer = array();
            $CI->load->helper('slicer');
            $ret_val = Slicer_checkSlice($progress, $message, $array_slicer);
            if ($ret_val != ERROR_OK) {
                $error_message = NULL;
                $url_remote = NULL;
                $stats_info = array();
                // handle error for slicing
                $CI->load->helper('printerlog');
                $url_remote = PRINTERLOG_STATS_VALUE_LOCAL;
                $array_data[PRINTERSTATE_TITLE_LASTERROR] = $ret_val;
                $status_current = CORESTATUS_VALUE_IDLE;
                switch ($ret_val) {
                    //TODO treat the error with api and ui
                    case ERROR_NO_SLICING:
                        $error_message = 'not in slicing';
                        break;
                    case ERROR_WRONG_PRM:
                        $error_message = 'slicer error';
                        // perhaps because of parameter
                        break;
                    case ERROR_UNKNOWN_MODEL:
                        $error_message = 'slicer export error';
                        // perhaps because of model
                        break;
                    case ERROR_REMOTE_SLICE:
                        $error_message = 'remote slicer error';
                        $url_remote = $message;
                        break;
                    default:
                        $error_message = 'slicer internal error';
                        // internal system error
                        PrinterLog_logDebug('return: ' . $ret_val . ', progress: ' . $progress);
                        break;
                }
                PrinterLog_logMessage($error_message, __FILE__, __LINE__);
                CoreStatus_setInIdle($ret_val, $error_message);
                $array_data[PRINTERSTATE_TITLE_DETAILMSG] = $error_message;
                // stats info
                $stats_info = PrinterState_prepareStatsSliceLabel();
                $stats_info[PRINTERLOG_STATS_SLICE_ERROR] = $error_message;
                $stats_info[PRINTERLOG_STATS_SLICE_SERVER] = $url_remote;
                PrinterLog_statsSlice(PRINTERLOG_STATS_ACTION_ERROR, $stats_info);
                return TRUE;
            } elseif ($progress == 100) {
                $url_remote = NULL;
                $stats_info = array();
                // set temp json file for every service
                $ret_val = PrinterState__setSlicedJson($array_slicer);
                $status_current = CORESTATUS_VALUE_IDLE;
                if ($ret_val != ERROR_OK) {
                    $array_data[PRINTERSTATE_TITLE_LASTERROR] = $ret_val;
                    CoreStatus_setInIdle($ret_val);
                } else {
                    CoreStatus_setInIdle();
                }
                // stats info
                $CI->load->helper('printerlog');
                $stats_info = PrinterState_prepareStatsSliceLabel(TRUE);
                //detect remote slicing
                if (file_exists(SLICER_FILE_REMOTE_REQUEST_URL)) {
                    $url_remote = trim(@file_get_contents(SLICER_FILE_REMOTE_REQUEST_URL));
                    if (strlen($url_remote) == 0) {
                        $url_remote = PRINTERLOG_STATS_VALUE_REMOTE;
                    }
                } else {
                    $url_remote = PRINTERLOG_STATS_VALUE_LOCAL;
                }
                $stats_info[PRINTERLOG_STATS_SLICE_SERVER] = $url_remote;
                PrinterLog_statsSlice(PRINTERLOG_STATS_ACTION_END, $stats_info);
                return TRUE;
            } else {
                // still in slicing, so get percentage (estimated time is useless for now, slicer exports percentage badly)
                $array_data[PRINTERSTATE_TITLE_PERCENT] = $progress;
                $array_data[PRINTERSTATE_TITLE_DETAILMSG] = $message;
            }
            break;
        case CORESTATUS_VALUE_LOAD_FILA_L:
        case CORESTATUS_VALUE_LOAD_FILA_R:
            // 			CoreStatus_checkInIdle($temp_status, $temp_array);
            CoreStatus_getStatusArray($temp_array);
            if (array_key_exists(CORESTATUS_TITLE_FILA_MAT, $temp_array) && $temp_array[CORESTATUS_TITLE_FILA_MAT] == PRINTERSTATE_DESP_MATERIAL_PVA) {
                $time_wait = PRINTERSTATE_VALUE_OFFSET_TO_CHECK_LOAD_PVA;
                $time_max = PRINTERSTATE_VALUE_TIMEOUT_TO_CHECK_LOAD_PVA;
            } else {
                $time_wait = PRINTERSTATE_VALUE_OFFSET_TO_CHECK_LOAD;
                $time_max = PRINTERSTATE_VALUE_TIMEOUT_TO_CHECK_LOAD;
            }
        case CORESTATUS_VALUE_UNLOAD_FILA_L:
        case CORESTATUS_VALUE_UNLOAD_FILA_R:
            $abb_filament = 'r';
            $status_fin_filament = FALSE;
            if (in_array($status_current, array(CORESTATUS_VALUE_LOAD_FILA_L, CORESTATUS_VALUE_UNLOAD_FILA_L))) {
                $abb_filament = 'l';
                $status_fin_filament = TRUE;
            }
            if (is_null($time_wait) || is_null($time_max)) {
                if (file_exists(PRINTERSTATE_FILE_UNLOAD_HEAT)) {
                    $time_start = @file_get_contents(PRINTERSTATE_FILE_UNLOAD_HEAT);
                    if (is_null($time_start)) {
                        PrinterLog_logError('check unload heat status file error', __FILE__, __LINE__);
                        break;
                    } else {
                        if (time() - $time_start <= PRINTERSTATE_VALUE_TIMEOUT_UNLOAD_HEAT) {
                            // block the status if in timeout, and refresh the start time for the following state
                            CoreStatus_setInUnloading($abb_filament);
                            break;
                        } else {
                            // always in heating when we passed timeout, we unlock the mobile site
                            PrinterLog_logError('always in heating process when we unload filament', __FILE__, __LINE__);
                            @unlink(PRINTERSTATE_FILE_UNLOAD_HEAT);
                            $ret_val = CoreStatus_setInIdle();
                            if ($ret_val == TRUE) {
                                $status_current = CORESTATUS_VALUE_IDLE;
                                return TRUE;
                            }
                            $CI->load->helper('printerlog');
                            PrinterLog_logError('can not set status into idle', __FILE__, __LINE__);
                            break;
                        }
                    }
                }
                // 				CoreStatus_checkInIdle($temp_status, $temp_array);
                CoreStatus_getStatusArray($temp_array);
                if (array_key_exists(CORESTATUS_TITLE_FILA_MAT, $temp_array) && $temp_array[CORESTATUS_TITLE_FILA_MAT] == PRINTERSTATE_DESP_MATERIAL_PVA) {
                    $time_wait = PRINTERSTATE_VALUE_OFFSET_TO_CHECK_UNLOAD_PVA;
                    $time_max = PRINTERSTATE_VALUE_TIMEOUT_TO_CHECK_UNLOAD_PVA;
                } else {
                    $time_wait = PRINTERSTATE_VALUE_OFFSET_TO_CHECK_UNLOAD;
                    $time_max = PRINTERSTATE_VALUE_TIMEOUT_TO_CHECK_UNLOAD;
                }
            }
            // wait the time for arduino before checking filament when loading / unloading filament
            if (CoreStatus_checkInWaitTime($time_wait)) {
                break;
            }
            // generate parameters by different status
            $ret_val = PrinterState_getFilamentStatus($abb_filament);
            if ($ret_val == $status_fin_filament || !CoreStatus_checkInWaitTime($time_max)) {
                if ($ret_val != $status_fin_filament) {
                    $CI->load->helper('printerlog');
                    PrinterLog_logError('we pass timeout when we are in changing catridge, status: ' . $status_current, __FILE__, __LINE__);
                }
                $ret_val = CoreStatus_setInIdle();
                if ($ret_val == TRUE) {
                    $status_current = CORESTATUS_VALUE_IDLE;
                    return TRUE;
                    // continue to generate if we are now in idle
                }
                $CI->load->helper('printerlog');
                PrinterLog_logError('can not set status into idle', __FILE__, __LINE__);
            }
            break;
        case CORESTATUS_VALUE_PRINT:
            $output = array();
            $command = $CI->config->item('arcontrol_c') . PRINTERSTATE_CHECK_STATE;
            if (file_exists($CI->config->item('printstatus'))) {
                $output = @file($CI->config->item('printstatus'));
                if (count($output) == 0) {
                    // case: read the percentage status file when arcontrol_cli is writing in it
                    // so we let the percentage as 1 to continue printing
                    $output = array('1');
                    $CI->load->helper('printerlog');
                    PrinterLog_logDebug('read percentage file when arcontrol_cli wrinting in it', __FILE__, __LINE__);
                }
            } else {
                $output = array('0');
            }
            PrinterLog_logArduino($command, $output);
            // 			if (count($output)) {
            // 				// we have right return
            if ((int) $output[0] == 0) {
                $stats_info = array();
                // not in printing(?), now we consider it is just idle (no slicing)
                $CI->load->helper('printerlog');
                PrinterLog_logDebug('check in idle - checkstatusasarray', __FILE__, __LINE__);
                $status_current = CORESTATUS_VALUE_IDLE;
                if (!CoreStatus_setInIdle()) {
                    PrinterLog_logError('cannot set in idle - checkstatusasarray', __FILE__, __LINE__);
                }
                //stats info
                $stats_info = PrinterState_prepareStatsPrintLabel();
                PrinterLog_statsPrint(PRINTERLOG_STATS_ACTION_END, $stats_info);
                return TRUE;
            } else {
                $array_data[PRINTERSTATE_TITLE_PERCENT] = $output[0];
            }
            // 			}
            // 			else {
            // 				$CI->load->helper('printerlog');
            // 				PrinterLog_logError('print check status command error', __FILE__, __LINE__);
            // 			}
            break;
        default:
            // log internal API error
            $CI->load->helper('printerlog');
            PrinterLog_logError('unknown status in work json', __FILE__, __LINE__);
            break;
    }
    return FALSE;
    // status has not changed
}