Skip to content

Commit

Permalink
Snapshot #2 website done?
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfin committed Dec 5, 2023
1 parent e6a205e commit 82ea4c4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 77 deletions.
85 changes: 41 additions & 44 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,86 +1,83 @@
import { createSignal, createResource, onCleanup } from "solid-js";
import { createSignal, onCleanup } from "solid-js";
import ledIcon from "./assets/led_icon.svg";

function App() {
const [count, setCount] = createSignal(0);
const [ledStatus, setLedStatus] = createSignal("");
const [color, setColor] = createSignal(0);

const fetchLedStatus = async () => {
const fetchColor = async () => {
const response = await fetch("/api/v1/led");

const json = await response.json();
setLedStatus(json["led on"] ? "on" : "off");
setColor(json["color"]);
};

const [led] = createResource(fetchLedStatus);

const interval = setInterval(fetchLedStatus, 500);

const interval = setInterval(fetchColor, 500);
onCleanup(() => clearInterval(interval));

const storedCount = localStorage.getItem("count");

if (storedCount != null) {
setCount(parseInt(storedCount));
}

function updateCount(num: number) {
setCount(count() + num);
localStorage.setItem("count", count().toString());
}

const turnOnLed = async (on: boolean) => {
const changeColor = async (c: number) => {
const response = await fetch("/api/v1/led", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ "led on": on }),
body: JSON.stringify({ "color": c }),
});

const json = await response.json();
setLedStatus(json["led on"] ? "on" : "off");
setColor(json["color"]);
};

function translateColor(c: number) {
switch (c) {
case 0:
return "red";
case 1:
return "green";
case 2:
return "blue";
default:
return "violet";
}
}

return (
<div class="container mx-0 flex min-w-full flex-col items-center px-10 py-10">
<div class="outlined-4 rounded-full outline outline-offset-2">
<div
class={`outlined-4 rounded-full bg-${translateColor(
color(),
)}-500 outline outline-offset-2 transition duration-300 ease-in-out`}
>
<img class="h-48 w-48 p-10" src={ledIcon} alt="Solid logo" />
</div>
<h1 class="my-3 text-5xl">
LED Status: {led.loading ? "" : ledStatus()}
</h1>

<h1 class="my-10 text-5xl">Lumenaries</h1>

<div>
<button
onClick={() => updateCount(1)}
class="m-2 rounded bg-violet-500 px-4 py-2 font-bold text-white hover:bg-violet-700"
>
stored count is {count()}
</button>
<button
class="m-2 rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700"
onClick={() => updateCount(count() * -1)}
onClick={() => changeColor(0)}
class="m-2 my-2 w-40 rounded bg-red-500 py-6 text-2xl font-bold text-white"
>
reset count
Red
</button>
</div>

<div>
<button
class="m-2 rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700"
onClick={() => turnOnLed(true)}
onClick={() => changeColor(1)}
class="m-2 my-2 w-40 rounded bg-green-500 py-6 text-2xl font-bold text-white"
>
Turn LED on
Green
</button>
</div>
<div>
<button
class="m-2 rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700"
onClick={() => turnOnLed(false)}
onClick={() => changeColor(2)}
class="m-2 my-2 w-40 rounded bg-blue-500 py-6 text-2xl font-bold text-white"
>
Turn LED off
Blue
</button>
</div>

<div id="list"></div>
</div>
);
}
Expand Down
48 changes: 15 additions & 33 deletions main/lumen/web/rest_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ constexpr auto tag = "rest_server";

} // namespace

const gpio_num_t led_pin = GPIO_NUM_13;

#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
#define SCRATCH_BUFSIZE (10240)

Expand Down Expand Up @@ -131,23 +129,18 @@ esp_err_t led_post_handler(httpd_req_t* req)

cJSON* root = cJSON_Parse(buf);

bool led_on;
if (cJSON_GetObjectItem(root, "led on")->type == cJSON_True) {
led_on = true;
} else {
led_on = false;
}
ESP_LOGI(tag, "LED POST: %d", led_on);
const int color = cJSON_GetObjectItem(root, "color")->valueint;
ESP_LOGI(tag, "LED POST: %d", color);

gpio_set_level(led_pin, led_on);
// set the color

cJSON* response = cJSON_CreateObject();
cJSON_AddBoolToObject(response, "led on", led_on);
const char* button_status = cJSON_Print(response);
cJSON_AddNumberToObject(response, "color", color);
const char* color_status = cJSON_Print(response);

httpd_resp_sendstr(req, button_status);
httpd_resp_sendstr(req, color_status);

free((void*)button_status);
free((void*)color_status);
cJSON_Delete(response);

cJSON_Delete(root);
Expand All @@ -156,20 +149,21 @@ esp_err_t led_post_handler(httpd_req_t* req)

esp_err_t led_get_handler(httpd_req_t* req)
{
const bool led_on = gpio_get_level(led_pin);
// get the color
const int color = 0;

httpd_resp_set_type(req, "application/json");

cJSON* response = cJSON_CreateObject();
cJSON_AddBoolToObject(response, "led on", led_on);
cJSON_AddNumberToObject(response, "color", color);

const char* button_status = cJSON_Print(response);
const char* color_status = cJSON_Print(response);

ESP_LOGI(tag, "LED GET: %d", led_on);
ESP_LOGI(tag, "LED GET: %d", color);

httpd_resp_sendstr(req, button_status);
httpd_resp_sendstr(req, color_status);

free((void*)button_status);
free((void*)color_status);
cJSON_Delete(response);

return ESP_OK;
Expand All @@ -195,7 +189,7 @@ esp_err_t sse_handler(httpd_req_t* req)

ESP_LOGI(tag, "SSE Initial Handler");

//xTaskCreate("
// xTaskCreate("

// Keep the connection open for further messages
while (true) {
Expand Down Expand Up @@ -243,11 +237,6 @@ esp_err_t start_rest_server(const char* base_path)
free(rest_context);
}

ESP_LOGI(tag, "Initializing led_pin");
gpio_reset_pin(led_pin);
// GPIO_MODE_INPUT_OUTPUT allows us to get the current level
gpio_set_direction(led_pin, GPIO_MODE_INPUT_OUTPUT);

httpd_uri_t led_post_uri = {.uri = "/api/v1/led",
.method = HTTP_POST,
.handler = led_post_handler,
Expand All @@ -260,13 +249,6 @@ esp_err_t start_rest_server(const char* base_path)
.user_ctx = rest_context};
httpd_register_uri_handler(server, &led_get_uri);

httpd_uri_t sse_uri = {.uri = "/api/v1/sse",
.method = HTTP_GET,
.handler = sse_handler,
.user_ctx = rest_context};

//httpd_register_uri_handler(server, &sse_uri);

/* URI handler for getting web server files */
httpd_uri_t common_get_uri = {.uri = "/*",
.method = HTTP_GET,
Expand Down

0 comments on commit 82ea4c4

Please sign in to comment.