Example #1
0
 /**
  * MoneyFormatter constructor.
  *
  * @param null $locale
  */
 public function __construct($locale = null)
 {
     $this->locale = $locale;
     if (!isset(static::$currencies)) {
         static::$currencies = json_read(__DIR__ . '/../../resources/data/currencies.json');
     }
 }
Example #2
0
 private function _get_type_array(&$array_type)
 {
     $cr = FALSE;
     $array_tmp = array();
     $array_data = array();
     $filename = $this->config->item('conf') . SETUPCARTRIDGE_JSON_FILENAME;
     $array_type = array();
     if (file_exists($filename)) {
         $array_tmp = json_read($filename, TRUE);
         if (isset($array_tmp['error'])) {
             $this->load->helper('printerlog');
             PrinterLog_logError('read json error', __FILE__, __LINE__);
         } else {
             $array_data = $array_tmp['json'];
             foreach ($array_data as $ele_type) {
                 if (is_array($ele_type) && array_key_exists('name', $ele_type) && array_key_exists('code', $ele_type)) {
                     $array_type[] = $ele_type;
                 } else {
                     $this->load->helper('printerlog');
                     PrinterLog_logError('json structure error', __FILE__, __LINE__);
                     return $cr;
                 }
             }
             if (count($array_type) == 0) {
                 $this->load->helper('printerlog');
                 PrinterLog_logMessage('json file is empty', __FILE__, __LINE__);
             } else {
                 $cr = TRUE;
             }
         }
     } else {
         $this->load->helper('printerlog');
         PrinterLog_logMessage('json file not found', __FILE__, __LINE__);
     }
     return $cr;
 }
Example #3
0
function ZimAPI_getPresetInfoAsArray($preset_id, &$array_info, &$system_preset = NULL, $set_localization = TRUE)
{
    $presetlist_basepath = NULL;
    $tmp_array = NULL;
    $system_preset = FALSE;
    $CI =& get_instance();
    $CI->load->helper(array('file', 'json'));
    if (!ZimAPI_checkPreset($preset_id, $presetlist_basepath, $system_preset)) {
        return ERROR_WRONG_PRM;
    }
    // 	$presetlist_basepath = $CI->config->item('presetlist');
    try {
        $preset_path = $presetlist_basepath . $preset_id . '/';
        $tmp_array = json_read($preset_path . ZIMAPI_FILE_PRESET_JSON, TRUE);
        if ($tmp_array['error']) {
            throw new Exception('read json error');
        }
    } catch (Exception $e) {
        // log internal error
        $CI->load->helper('printerlog');
        PrinterLog_logError('catch exception when getting preset json ' . $preset_id, __FILE__, __LINE__);
        return ERROR_INTERNAL;
    }
    // localization preset name
    if ($set_localization) {
        ZimAPI__setPresetLocalization($tmp_array['json']);
    }
    $array_info = $tmp_array['json'];
    //asign final data
    return ERROR_OK;
}
Example #4
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;
}
Example #5
0
function CoreStatus_getRandomGUID(&$guid)
{
    global $CFG;
    $state_file = $CFG->config['conf'] . CORESTATUS_FILENAME_WORK;
    $tmp_array = array();
    $data_json = array();
    $guid = NULL;
    $CI =& get_instance();
    $CI->load->helper('json');
    // read json file
    try {
        $tmp_array = json_read($state_file);
        if ($tmp_array['error']) {
            throw new Exception('read json error');
        }
    } catch (Exception $e) {
        return FALSE;
    }
    $data_json = $tmp_array['json'];
    if (!isset($data_json[CORESTATUS_TITLE_GUID])) {
        return FALSE;
    }
    $guid = $data_json[CORESTATUS_TITLE_GUID];
    return TRUE;
}
Example #6
0
function Printer_getFileFromModel($type_model, $id_model, &$gcode_path, $filename = NULL, &$array_info = NULL)
{
    $model_path = NULL;
    $bz2_path = NULL;
    $command = '';
    $output = array();
    $ret_val = 0;
    $filename_json = NULL;
    $filename_bz2 = NULL;
    $filename_gcode = NULL;
    $CI =& get_instance();
    switch ($type_model) {
        case PRINTER_TYPE_MODELLIST:
            $CI->load->helper('printlist');
            $filename_json = PRINTLIST_FILE_JSON;
            $filename_bz2 = PRINTLIST_FILE_GCODE_BZ2;
            $filename_gcode = PRINTLIST_FILE_GCODE;
            $model_cr = ModelList__find($id_model, $model_path);
            // get json info
            if (is_array($array_info)) {
                $json_data = array();
                try {
                    $json_data = json_read($model_path . $filename_json, TRUE);
                    if ($json_data['error']) {
                        throw new Exception('read json error');
                    }
                } catch (Exception $e) {
                    return ERROR_INTERNAL;
                }
                $array_info = $json_data['json'];
            }
            break;
        case PRINTER_TYPE_GCODELIB:
            $CI->load->helper('printerstoring');
            $tmp_array = NULL;
            $filename_json = PRINTERSTORING_FILE_INFO_JSON;
            $filename_bz2 = PRINTERSTORING_FILE_GCODE_BZ2;
            $filename_gcode = PRINTERSTORING_FILE_GCODE_EXT;
            $array_info = PrinterStoring_getInfo('gcode', $id_model, $model_path);
            if (is_null($array_info)) {
                $model_cr = ERROR_WRONG_PRM;
            } else {
                $model_cr = ERROR_OK;
            }
    }
    if ($model_cr == ERROR_OK && $model_path) {
        $ret_val = 0;
        //		//if we don't fix the filename of gcode
        // 		try {
        // 			$json_data = json_read($model_path . PRINTLIST_FILE_JSON);
        // 			if ($json_data['error']) {
        // 				throw new Exception('read json error');
        // 			}
        // 		} catch (Exception $e) {
        // 			return ERROR_INTERNAL;
        // 		}
        // 		$gcode_path = $json_data['json'][PRINTLIST_TITLE_GCODE];
        $bz2_path = $model_path . $filename_bz2;
        $filename = is_null($filename) ? $filename_gcode : $filename;
        $gcode_path = $CI->config->item('temp') . $filename;
        $command = 'bzip2 -dkcf ' . $bz2_path . ' > ' . $gcode_path;
        @unlink($gcode_path);
        // delete old file
        exec($command, $output, $ret_val);
        if ($ret_val != ERROR_NORMAL_RC_OK) {
            return ERROR_INTERNAL;
        }
        return ERROR_OK;
    } else {
        return ERROR_UNKNOWN_MODEL;
    }
    return ERROR_OK;
    // never reach here
}
Example #7
0
function ModelList__getDetailAsArray($id_model, &$array_data, $set_localization = FALSE)
{
    $array_data = NULL;
    $tmp_array = NULL;
    $json_path = NULL;
    $model_path = NULL;
    $model_cr = ModelList__find($id_model, $model_path);
    if ($model_cr == ERROR_OK && $model_path) {
        $json_path = $model_path . PRINTLIST_FILE_JSON;
        $tmp_array = json_read($json_path, TRUE);
        if ($tmp_array['error']) {
            return ERROR_INTERNAL;
        }
        $array_data = $tmp_array['json'];
        ModelList__blindUrl($array_data);
        if ($set_localization) {
            ModelList__setLocalization($array_data);
        }
        return ERROR_OK;
    } else {
        return ERROR_UNKNOWN_MODEL;
    }
    return;
}
Example #8
0
function PrinterState__updateCartridge(&$code_cartridge, $abb_cartridge)
{
    $CI =& get_instance();
    $file_path = $CI->config->item('base_data') . PRINTERSTATE_FILE_UPDATE_RFID;
    if (file_exists($file_path)) {
        $data_json = array();
        $temp_code = NULL;
        try {
            $CI->load->helper('json');
            $tmp_array = @json_read($file_path, TRUE);
            if ($tmp_array['error']) {
                throw new Exception('read json error');
            } else {
                $data_json = $tmp_array['json'];
            }
        } catch (Exception $e) {
            $CI->load->helper('printerlog');
            PrinterLog_logError('read cartridge update data error', __FILE__, __LINE__);
            return;
            // log error and return
        }
        $temp_code = substr($code_cartridge, 0, 26);
        if (array_key_exists($temp_code, $data_json)) {
            $temp_hex = 0;
            $ret_val = 0;
            $CI->load->helper('printerlog');
            PrinterLog_logDebug('detected a cartridge to update', __FILE__, __LINE__);
            // add date in the end
            $temp_code = $data_json[$temp_code] . substr($code_cartridge, 26, 4);
            // calculate checksum
            for ($i = 0; $i <= 14; $i++) {
                $string_tmp = substr($temp_code, $i * 2, 2);
                $hex_tmp = hexdec($string_tmp);
                $temp_hex = $temp_hex ^ $hex_tmp;
            }
            $temp_hex = dechex($temp_hex);
            if (strlen($temp_hex) == 1) {
                $temp_hex = '0' . $temp_hex;
            }
            $temp_code .= strtoupper($temp_hex);
            $code_cartridge = $temp_code;
            $ret_val = PrinterState_setCartridgeCode($temp_code, $abb_cartridge);
            if ($ret_val != ERROR_OK) {
                // log error and return
                $CI->load->helper('printerlog');
                PrinterLog_logError('write cartridge error when in updating cartridge from database', __FILE__, __LINE__);
            }
        }
    }
    return;
}
Example #9
0
<?php

function json_read($task)
{
    global $tasksDir;
    $json = file_get_contents($tasksDir . '/' . $task);
    return json_decode($json, true);
}
function json_write($task, $template)
{
    global $tasksDir;
    $json = json_encode($template, JSON_UNESCAPED_UNICODE);
    file_put_contents($tasksDir . '/' . $task, $json);
}
$template = json_read($tasksList[$_GET['task'] + 1]);
function getTaskConditions($taskNumber, $lang)
{
    global $template;
    return $template[$taskNumber]['conditions'][$lang];
}
function getTaskIO($taskNumber, $which)
{
    global $template;
    return $template[$taskNumber][$which];
}
function getTaskExamples($taskNumber, $which)
{
    global $template;
    return $template[$taskNumber]['examples'][$which];
}