Skip to content

Instantly share code, notes, and snippets.

@FlavioMattosDev
Created February 8, 2025 21:32
Show Gist options
  • Save FlavioMattosDev/57fd65585aa9f846c86d9c6e1f9fb429 to your computer and use it in GitHub Desktop.
Save FlavioMattosDev/57fd65585aa9f846c86d9c6e1f9fb429 to your computer and use it in GitHub Desktop.
Projeto Final EmbarcaTech
{
"version": 1,
"author": "Seu Nome",
"editor": "wokwi",
"parts": [
{
"type": "board-pi-pico-w",
"id": "pico",
"top": 15.95,
"left": -25.25,
"attrs": { "cyw43": "1", "builder": "pico-sdk" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 61.55,
"left": -105.6,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": 25.2,
"left": -140.2,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 159.8,
"left": -134.4,
"attrs": { "color": "green", "bounce": "1" }
},
{ "type": "wokwi-gnd", "id": "gnd1", "top": 211.2, "left": -154.2, "attrs": {} }
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP1", "$serialMonitor:TX", "", [] ],
[ "led1:C", "pico:GND.2", "green", [ "v57.6", "h77.2", "v-9.6" ] ],
[ "r1:2", "pico:GP3", "green", [ "v0" ] ],
[ "r1:1", "led1:A", "green", [ "v0" ] ],
[ "pico:GP12", "btn1:1.r", "green", [ "h0" ] ],
[ "gnd1:GND", "btn1:2.l", "black", [ "v0" ] ]
],
"dependencies": {}
}
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "stdio.h"
#include "hardware/gpio.h"
#include <string.h>
#define LED_PIN 3 // Pino do LED
#define BUTTON_PIN 12 // Pino do botão
// Estado do LED (ligado/desligado)
bool led_status = false;
// Estado de pausa do sistema
bool paused = false;
// Função para ligar o LED
void led_on() {
gpio_put(LED_PIN, 1);
led_status = true;
printf("Houve falha na execução de um Workflow.\n");
}
// Função para desligar o LED
void led_off() {
gpio_put(LED_PIN, 0);
led_status = false;
printf("LED desligado.\n");
}
// Função de debounce para o botão
bool debounce() {
static uint64_t last_time = 0;
uint64_t now = to_us_since_boot(get_absolute_time());
if ((now - last_time) >= 200000) { // 200ms de debounce
last_time = now;
return true;
}
return false;
}
// Função para processar o payload recebido
void process_notification(const char* payload) {
if (strstr(payload, "\"status\": \"failed\"") != NULL) {
led_on(); // Acende o LED se a automação falhou
paused = true; // Pausa o recebimento de novos payloads
printf("Pressione o botão para reiniciar a verificação dos status dos Workflows.\n");
}
}
// Função simulada de fetch de payloads recebidos do Ansible Automation Platform
void fetch_notifications() {
const char* payloads[] = {
"{\"status\": \"success\", \"task\": \"Criação de VM\"}",
"{\"status\": \"success\", \"task\": \"Backup do Sistema\"}",
"{\"status\": \"failed\", \"task\": \"Criação de conta\"}",
"{\"status\": \"success\", \"task\": \"Deploy de aplicação\"}",
"{\"status\": \"success\", \"task\": \"Restauração de backup\"}",
"{\"status\": \"failed\", \"task\": \"Configuração de DNS\"}",
"{\"status\": \"success\", \"task\": \"Provisionamento de infraestrutura\"}",
"{\"status\": \"success\", \"task\": \"Configuração de cluster\"}",
"{\"status\": \"success\", \"task\": \"Configuração de VPN\"}",
"{\"status\": \"success\", \"task\": \"Configuração de firewall\"}"
};
static int current_payload = 0;
// Simulação de um envio de uma requisição HTTP
printf("Fazendo requisição HTTP para o Ansible Automation Platform.\n");
// Simulação de recebimento de uma resposta HTTP
printf("Resposta HTTP recebida: %s\n", payloads[current_payload]);
process_notification(payloads[current_payload]);
// Incremento sobre o payload atual
current_payload = (current_payload + 1) % 10;
}
// Função principal
int main() {
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
// Inicialização simulada
printf("Inicializando sistema.\n");
sleep_ms(1000);
printf("Sistema de verificação de falhas de Workflows inicializado.\n");
while (1) {
if (!paused) {
fetch_notifications(); // Faz requisições HTTP para o Ansible Automation Platform
sleep_ms(4000); // Intervalo de 4 segundos entre requisições
}
// Resetar o LED caso o botão seja pressionado
if (!gpio_get(BUTTON_PIN) && debounce()) {
if (led_status) {
led_off(); // Apaga o LED se estiver aceso
paused = false; // Retoma o recebimento de payloads
printf("Sistema reiniciado, recebendo notificações.\n");
}
}
sleep_ms(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment