Created
October 4, 2018 02:25
Star
You must be signed in to star a gist
colletc POSXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<stringvariable variable="screen_title" value="COLETA NORMAL" /> | |
<stringvariable variable="arquivo_coletas" value="ultima_coleta.txt" /> | |
<stringvariable variable="arquivo_residuos" value="wastes.txt" /> | |
<stringvariable variable="posto_user_info" value="" /> | |
<stringvariable variable="is_valid_uc" value="1" /> | |
<stringvariable variable="__residuos_coletados" value="" /> <!-- lista de coletas --> | |
<stringvariable variable="__input_uc" value="" /> | |
<stringvariable variable="__input_codigo_residuo" value="" /> | |
<stringvariable variable="__input_quantidade_residuo" value="" /> | |
<stringvariable variable="__residuo_valor_unitario" value="" /> | |
<stringvariable variable="__residuo_subtotal" value="" /> | |
<stringvariable variable="__http_endpoint" value="" /> | |
<stringvariable variable="__http_request_body" value="" /> | |
<stringvariable variable="__http_response_data" value="" /> <!-- toda request vai atualizar essa variável --> | |
<function name="print_title"> | |
<display line="0" column="0" message="$(screen_title)"/> | |
<display line="1" column="0" message="---------------------"/> | |
</function> | |
<function name="http_request"> | |
<!-- request_data: É a união de um ou mais informações como: método http, headers, post body/data, URL, etc --> | |
<stringvariable value="" variable="api_key" /> | |
<string.getvaluebykey key="apiKey" string="$(posto_user_info)" variablereturn="$(api_key)" /> | |
<!-- todos os dados que serão enviados como buffer da request --> | |
<stringvariable variable="request_data" value="POST {endpoint}?api_key={api_key} data={body}" /> | |
<string.replace original_string="$(request_data)" old_substring="{endpoint}" new_substring="$(__http_endpoint)" variablereturn="$(request_data)" /> | |
<string.replace original_string="$(request_data)" old_substring="{api_key}" new_substring="$(api_key)" variablereturn="$(request_data)" /> | |
<string.replace original_string="$(request_data)" old_substring="{body}" new_substring="$(__http_request_body)" variablereturn="$(request_data)" /> | |
<integervariable variable="content_length" value="" /> | |
<integervariable value="0" variable="network_send_retval" /> | |
<string.tohex string="$(request_data)" variablereturn="$(request_data)" /> | |
<string.length value="$(request_data)" variablereturn="$(content_length)" /> | |
<integervariable variable="preconnect_retval" value=""/> | |
<preconnect variablestatus="$(preconnect_retval)" /> | |
<network.send buffer="$(request_data)" size="$(content_length)" variablereturn="$(network_send_retval)" /> | |
<integervariable variable="network_receive_retcode" value="" /> | |
<!-- descartamos os primeiros 4 bytes --> | |
<network.receive variablebuffer="$(__http_response_data)" maxsize="4" variablereceivedbytes="0" variablereturn="$(network_receive_retcode)" /> | |
<!-- restante da resposta --> | |
<network.receive variablebuffer="$(__http_response_data)" maxsize="1023" variablereceivedbytes="0" variablereturn="$(network_receive_retcode)" /> | |
<network.hostdisconnect /> | |
<string.fromhex string="$(__http_response_data)" variablereturn="$(__http_response_data)" /> | |
</function> | |
<function name="validar_cliente"> | |
<stringvariable variable="status_code" value="" /> | |
<stringvariable variable="__http_endpoint" value="/v1/consultar-cliente" /> | |
<stringvariable variable="__http_request_body" value="$(__input_uc)" /> | |
<callfunction name="http_request"/> | |
<string.elementat delimiter="|" element_index="0" string="$(__http_response_data)" variablereturn="$(status_code)" /> | |
<if variable="$(status_code)" operator="equalto" value="0"> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<stringvariable variable="mensagem" value="" /> | |
<string.elementat delimiter="|" element_index="1" string="$(__http_response_data)" variablereturn="$(mensagem)" /> | |
<display line="3" column="0" message="$(mensagem)" /> | |
<waitkey/> | |
<exit/> | |
</if> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<stringvariable variable="cliente_nome" value="" /> | |
<string.elementat delimiter="|" element_index="1" string="$(__http_response_data)" variablereturn="$(cliente_nome)" /> | |
<display line="3" column="0" message="$(cliente_nome)" /> | |
<display line="7" column="0" message="Confirma? tecle VERDE" /> | |
<stringvariable variable="pressed_key" value="" /> | |
<readkey miliseconds="60000" variablereturn="$(pressed_key)"/> | |
<if variable="$(pressed_key)" operator="notequalto" value="KEY_ENTER"> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="3" column="0" message=" ENCERRANDO... " /> | |
<wait miliseconds="2000"/> | |
<exit/> | |
</if> | |
</function> | |
<function name="calcular_subtotal"> | |
<!-- cálculo do subtotal ([valor unitário do resíduo] x [quantidade]) --> | |
<string.replace original_string="$(__residuo_valor_unitario)" old_substring="," new_substring="." variablereturn="$(__residuo_valor_unitario)" /> | |
<mathematicaloperation firstvalue="$(__input_quantidade_residuo)" operator="*" secondvalue="$(__residuo_valor_unitario)" variabledestination="$(__residuo_subtotal)"/> | |
<!-- formatamos parte decimal --> | |
<stringvariable value="" variable="parte_inteira" /> | |
<stringvariable value="" variable="parte_decimal" /> | |
<stringvariable variable="formatado" value="{int}.{dec}" /> | |
<string.elementat string="$(__residuo_subtotal)" element_index="0" delimiter="." variablereturn="$(parte_inteira)" /> | |
<string.elementat string="$(__residuo_subtotal)" element_index="1" delimiter="." variablereturn="$(parte_decimal)" /> | |
<string.substring string="$(parte_decimal)" start="0" length="2" variablereturn="$(parte_decimal)" /> | |
<string.replace original_string="$(formatado)" old_substring="{int}" new_substring="$(parte_inteira)" variablereturn="$(formatado)" /> | |
<string.replace original_string="$(formatado)" old_substring="{dec}" new_substring="$(parte_decimal)" variablereturn="$(formatado)" /> | |
<!-- update gloval var --> | |
<stringvariable variable="__residuo_subtotal" value="$(formatado)" /> | |
</function> | |
<function name="nova_entrada"> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<stringvariable variable="__input_codigo_residuo" value="" /> | |
<stringvariable variable="__input_quantidade_residuo" value="" /> | |
<inputformat column="0" line="4" format="999999" variable="$(__input_codigo_residuo)" message="Digite cod. residuo:" /> | |
<inputfloat variable="$(__input_quantidade_residuo)" line="4" column="0" message="Digite a quantidade:" /> | |
<!-- start - busca dados residuo pelo código --> | |
<stringvariable variable="residuo_info" value="" /> | |
<stringvariable variable="residuo_nome" value="" /> | |
<readfile filename="$(arquivo_residuos)" key="$(__input_codigo_residuo)" variabledestination="$(residuo_info)" /> | |
<string.trim string="$(residuo_info)" variablereturn="$(residuo_info)" /> | |
<integervariable variable="charslen" value="0" /> | |
<string.length value="$(residuo_info)" variablereturn="$(charslen)" /> | |
<!-- se o código do produto informado não existe, avisamos e reiniciamos o processo de input --> | |
<if variable="$(charslen)" operator="equalto" value="0"> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="3" column="0" message=" Codigo de produto" /> | |
<display line="4" column="0" message=" invalido!" /> | |
<wait miliseconds="2000"/> | |
<callfunction name="nova_entrada"/> | |
</if> | |
<string.getvaluebykey key="nome" string="$(residuo_info)" variablereturn="$(residuo_nome)" /> | |
<string.getvaluebykey key="valor" string="$(residuo_info)" variablereturn="$(__residuo_valor_unitario)" /> | |
<!-- end - busca dados residuo pelo código --> | |
<!-- formatação linha residuo e quantidade --> | |
<stringvariable variable="fmt_residuo" value="Residuo: {residuo}" /> | |
<stringvariable variable="fmt_quantidade" value="Quantidade: {quantidade}" /> | |
<string.replace original_string="$(fmt_residuo)" old_substring="{residuo}" new_substring="$(residuo_nome)" variablereturn="$(fmt_residuo)" /> | |
<string.replace original_string="$(fmt_quantidade)" old_substring="{quantidade}" new_substring="$(__input_quantidade_residuo)" variablereturn="$(fmt_quantidade)" /> | |
<!-- [start] configuração de linha para caber nome completo do resíduo --> | |
<integervariable variable="display_line" value="4" /> <!-- por padrão, linha 4 --> | |
<integervariable variable="fmt_residuo_strlen" value="0" /> | |
<string.length value="$(fmt_residuo)" variablereturn="$(fmt_residuo_strlen)" /> | |
<if variable="$(fmt_residuo_strlen)" operator="greaterthan" value="21"> | |
<integervariable variable="display_line" value="5" /> | |
</if> | |
<!-- [end] configuração de linha para caber nome completo do resíduo --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="3" column="0" message="$(fmt_residuo)" /> | |
<display line="$(display_line)" column="0" message="$(fmt_quantidade)" /> | |
<display line="7" column="0" message="Confirma? tecle VERDE" /> | |
<!-- Confirma dos dados --> | |
<stringvariable variable="pressed_key" value="" /> | |
<readkey miliseconds="60000" variablereturn="$(pressed_key)"/> | |
<!-- Pedimos ao operador para confirmar os dados que foram passados. --> | |
<!-- Se algum dado está incorreto não gravamos a atual entrada e re-solicitamos a entrada de dados. --> | |
<if variable="$(pressed_key)" operator="notequalto" value="KEY_ENTER"> | |
<!-- não grava os itens atuais e reinicia o processo de input de residuo e quantidade --> | |
<callfunction name="nova_entrada"/> | |
</if> | |
</function> | |
<function name="coletar"> | |
<!-- PROCESSO DE DADOS REFERENTES AOS RESIDUOS DA COLETA --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<callfunction name="nova_entrada"/> | |
<!-- start: atualiza a lista de coletas --> | |
<!-- formato de residuo*quantidade;residuo2*quantidade2;n... --> | |
<stringvariable variable="collect_item" value='{residuo_cod}*{quantidade}*{subtotal};' /> | |
<callfunction name="calcular_subtotal"/> <!-- que atualiza a var __residuo_subtotal --> | |
<string.replace original_string="$(collect_item)" old_substring="{residuo_cod}" new_substring="$(__input_codigo_residuo)" variablereturn="$(collect_item)" /> | |
<string.replace original_string="$(collect_item)" old_substring="{quantidade}" new_substring="$(__input_quantidade_residuo)" variablereturn="$(collect_item)" /> | |
<string.replace original_string="$(collect_item)" old_substring="{subtotal}" new_substring="$(__residuo_subtotal)" variablereturn="$(collect_item)" /> | |
<joinstring firstvalue="$(__residuos_coletados)" secondvalue="$(collect_item)" variabledestination="$(__residuos_coletados)" /> | |
<!-- end: atualiza a lista de coletas --> | |
<!-- Entrar outro resíduo? --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="4" column="0" message=" Continuar coletando" /> | |
<display line="7" column="0" message="Confirma? tecle VERDE" /> | |
<stringvariable variable="pressed_key" value="" /> | |
<readkey miliseconds="60000" variablereturn="$(pressed_key)"/> | |
<if variable="$(pressed_key)" operator="notequalto" value="KEY_CANCEL"> | |
<callfunction name="coletar"/> | |
</if> | |
</function> | |
<function name="confirmar_impressao"> | |
<!-- Processo de impressão --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="3" column="0" message=" Imprimir" /> | |
<display line="4" column="0" message=" comprovante" /> | |
<display line="7" column="0" message="Confirma? tecle VERDE" /> | |
<stringvariable variable="pressed_key" value="" /> | |
<readkey miliseconds="60000" variablereturn="$(pressed_key)"/> | |
<if variable="$(pressed_key)" operator="notequalto" value="KEY_ENTER"> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="3" column="0" message=" COLETA SALVA " /> | |
<display line="4" column="0" message=" SAINDO..." /> | |
<waitkeytimeout seconds="3"/> | |
<exit/> | |
</if> | |
</function> | |
<readfile filename="user.txt" key="user" variabledestination="$(posto_user_info)" /> | |
<stringvariable variable="coleta_tipo" value="n" /> | |
<stringvariable variable="IS_OFFLINE" value="0" /> <!-- por default consideramos que não estamos offline --> | |
<!-- checagem se estamos offline --> | |
<!-- 0 => NAO OFFLINE; 1 => OFFLINE --> | |
<display line="4" column="0" message=" Aguarde..." /> | |
<integervariable variable="preconnect_retval" value=""/> | |
<preconnect variablestatus="$(preconnect_retval)" /> | |
<network.hostdisconnect /> | |
<!-- DEBUG OFFLINE --> | |
<!-- TODO: APAGAR DEPOIS--> | |
<integervariable variable="preconnect_retval" value="1"/> | |
<!-- DEBUG OFFLINE --> | |
<if variable="$(preconnect_retval)" operator="notequalto" value="0"> | |
<stringvariable variable="IS_OFFLINE" value="1" /> | |
<stringvariable variable="screen_title" value="COLETA NORMAL (off)" /> | |
</if> | |
<!-- checagem se estamos offline --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<inputformat column="0" line="4" format="999999" variable="$(__input_uc)" message="Digite a UC:" /> | |
<integervariable variable="int_uc" value="" /> | |
<stringtoint variableinteger="$(int_uc)" variablestring="$(__input_uc)" /> | |
<if variable="$(int_uc)" operator="lessthan" value="0"> | |
<exit /> | |
</if> | |
<!-- =============================================================================================================== --> | |
<!-- start - CONSULTA CLIENTE SE NÃO ESTAMOS OFFLINE --> | |
<!-- =============================================================================================================== --> | |
<if variable="$(IS_OFFLINE)" operator="notequalto" value="1"> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="3" column="0" message="Validando cliente..." /> | |
<callfunction name="validar_cliente"/> | |
</if> | |
<!-- =============================================================================================================== --> | |
<!-- end - CONSULTA CLIENTE SE NÃO ESTAMOS OFFLINE --> | |
<!-- =============================================================================================================== --> | |
<!-- iniciamos aqui o processo de coleta (formulário) --> | |
<callfunction name="coletar"/> | |
<!-- remove o extra ";" (ponto e vírgula) do final da string --> | |
<integervariable variable="string_len" value="" /> | |
<string.length value="$(__residuos_coletados)" variablereturn="$(string_len)" /> | |
<integeroperator operator="--" variablesource="$(string_len)" /> | |
<string.substring start="0" length="$(string_len)" string="$(__residuos_coletados)" variablereturn="$(__residuos_coletados)" /> | |
<!-- remove o extra ";" (ponto e vírgula) do final da string - end --> | |
<!-- gravamos os dados da atual coleta e redirecionamos para a tela de impressao --> | |
<deletefile filename="$(arquivo_coletas)" /> <!-- reset conteúdo --> | |
<editfile filename="$(arquivo_coletas)" key="uc" value="$(__input_uc)" /> | |
<editfile filename="$(arquivo_coletas)" key="coleta" value="$(__residuos_coletados)" /> | |
<!-- Transmissão dos dados da coleta pro server --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<!-- data local da operação --> | |
<stringvariable variable="datetime" value="" /> | |
<getdatetime format="d/M/yy h:m:s" variabledestination="$(datetime)"/> | |
<!-- [start] formatação de dados da coleta para envio (form data) ou armazenagem offline --> | |
<!-- formato: id_projeto|user id|id posto|uc|id_residuo*quantidade;n...|datetime_envio|is_offline|tipo_coleta --> | |
<stringvariable variable="projeto_id" value="" /> | |
<stringvariable variable="user_id" value="" /> | |
<stringvariable variable="posto_id" value="" /> | |
<string.getvaluebykey key="project" string="$(posto_user_info)" variablereturn="$(projeto_id)" /> | |
<string.getvaluebykey key="id" string="$(posto_user_info)" variablereturn="$(user_id)" /> | |
<string.getvaluebykey key="id_posto" string="$(posto_user_info)" variablereturn="$(posto_id)" /> | |
<stringvariable variable="coleta_registro" value="{projeto_id}|{user_id}|{posto_id}|{uc}|{residuos_info}|{datetime}|{is_offline}|{tipo_coleta}" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{projeto_id}" new_substring="$(projeto_id)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{user_id}" new_substring="$(user_id)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{posto_id}" new_substring="$(posto_id)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{uc}" new_substring="$(__input_uc)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{residuos_info}" new_substring="$(__residuos_coletados)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{datetime}" new_substring="$(datetime)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{is_offline}" new_substring="$(IS_OFFLINE)" variablereturn="$(coleta_registro)" /> | |
<string.replace original_string="$(coleta_registro)" old_substring="{tipo_coleta}" new_substring="$(coleta_tipo)" variablereturn="$(coleta_registro)" /> | |
<!-- [end] formatação de dados da coleta para envio (form data) ou armazenagem offline --> | |
<!-- [start] se é coleta offline, armazenamos no arquivo --> | |
<if variable="$(IS_OFFLINE)" operator="equalto" value="1"> | |
<stringvariable variable="timestamp" value="" /> | |
<getdatetime format="yyMdhms" variabledestination="$(timestamp)"/> | |
<!-- No arquivo de coletas, "indexes" é uma chave cujo valor é lista de timestamps onde estes representam chaves (de uma coleta) no mesmo arquivo --> | |
<stringvariable variable="indexes_coletas" value="" /> | |
<readfile filename="coletas_offline.txt" key="indexes" variabledestination="$(indexes_coletas)" /> | |
<string.trim string="$(indexes_coletas)" variablereturn="$(indexes_coletas)" /> | |
<integervariable variable="indexes_count" value="" /> | |
<string.length value="$(indexes_coletas)" variablereturn="$(indexes_count)" /> | |
<!-- se adiciona ou não vírgula (prepend) antes do valor na lista --> | |
<if variable="$(indexes_count)" operator="greaterthan" value="0"> | |
<stringvariable variable="nova_entrada" value="{old},{new}" /> | |
<string.replace original_string="$(nova_entrada)" old_substring="{old}" new_substring="$(indexes_coletas)" variablereturn="$(nova_entrada)" /> | |
<string.replace original_string="$(nova_entrada)" old_substring="{new}" new_substring="$(timestamp)" variablereturn="$(nova_entrada)" /> | |
<editfile filename="coletas_offline.txt" key="indexes" value="$(nova_entrada)" /> | |
<else/> | |
<editfile filename="coletas_offline.txt" key="indexes" value="$(timestamp)" /> | |
</if> | |
<!-- e gravamos o novo registro de coleta --> | |
<editfile filename="coletas_offline.txt" key="$(timestamp)" value="$(coleta_registro)" /> | |
<!-- atualiza index que aponta para última coleta --> | |
<editfile filename="coletas_offline.txt" key="ultima_coleta_index" value="$(timestamp)" /> | |
<execute filename="coletaimpressaooff.posxml" /> | |
</if> | |
<!-- [end] se é coleta offline, armazenamos no arquivo --> | |
<!-- =============================================================================================================== --> | |
<!-- PASSO 2: envio de dados da coleta atual para o server, se não estamos offline --> | |
<!-- =============================================================================================================== --> | |
<!-- REMOTE CALL - start --> | |
<cleandisplay /> | |
<callfunction name="print_title"/> | |
<display line="4" column="0" message=" Transmitindo..." /> | |
<stringvariable variable="__http_endpoint" value="/v1/coleta/processar" /> | |
<stringvariable variable="__http_request_body" value="$(coleta_registro)" /> | |
<callfunction name="http_request"/> | |
<!-- REMOTE CALL - end --> | |
<!-- debug --> | |
<!-- <cleandisplay /> | |
<display line="0" column="0" message="imprimindo online..." /> | |
<display line="1" column="0" message="$(__http_response_data)" /> | |
<waitkey/> | |
<exit/> --> | |
<!-- debug --> | |
<!-- <stringvariable variable="server_retorno_dados" value="1|FULANO DE TAL|XK0527B4-AFC4-4FFD-AB27-27DF3628|5.16|CHUMBO*1.82*0.73KG;PANELA ALUMINIO*2.32*0.93KG;PLASTICO MISTO*1.02*0.41KG|2018-08-08 10:30:44;2018-08-08 13:43:07|1|Peso: 4.4 kg;Unidades: 4;Litros: 1.8L" /> --> | |
<stringvariable variable="server_retorno_dados" value="$(__http_response_data)" /> | |
<stringvariable variable="server_retorno_status" value="" /> | |
<string.elementat delimiter="|" element_index="0" string="$(server_retorno_dados)" variablereturn="$(server_retorno_status)" /> | |
<editfile filename="$(arquivo_coletas)" key="server_retorno_dados" value="$(server_retorno_dados)" /> | |
<!-- salva também o datetime atual da operação --> | |
<editfile filename="$(arquivo_coletas)" key="datetime" value="$(datetime)" /> | |
<callfunction name="confirmar_impressao"/> | |
<execute filename="coletaimpressao.posxml" /> | |
<exit/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment