Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Soccer Activity #96

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/components/ActivityBaseConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function Team(props) {
<Show when={!props.isRunning}>
<div class="pb-4">
<input
class="max-w-32 select-all rounded-lg border border-gray-300 bg-gray-50 p-1 text-center font-bold text-2xl focus:border-accent focus:outline-none focus:ring-0 focus:ring-gray-800"
class="max-w-32 select-all rounded-lg border border-gray-300 bg-gray-50 p-1 text-center font-bold text-2xl uppercase focus:border-accent focus:outline-none focus:ring-0 focus:ring-gray-800"
type="text"
aria-label={`team ${props.teamIndex} name`}
value={teamName()}
Expand Down
51 changes: 50 additions & 1 deletion frontend/src/pages/activityConfig/SoccerConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
import {
Show,
createEffect,
createSignal,
onMount,
} from "solid-js";

import { useEvent } from "../../Event";
import ActivityBaseConfig from "../../components/ActivityBaseConfig";
import ActivityField from "../../components/ActivityField";

function SoccerConfig() {
return <ActivityBaseConfig name="Soccer" />;
const eventData = useEvent();
const endpoint = "/api/v1/soccer";
const [data, setData] = createSignal(null);

const halves = ["1st Half", "2nd Half"];

onMount(async () => {
const resp = await fetch(endpoint);
const json = await resp.json();

if (json != null) {
setData(json);
}
});

createEffect(() => {
if (eventData() != null) {
setData(eventData());
}
});

return (
<Show when={data() != null && data().type == "soccer"}>
<ActivityBaseConfig
name="Soccer"
teamOneName={data().teamOne.name}
teamTwoName={data().teamTwo.name}
teamOneCurrentScore={data().teamOne.score}
teamTwoCurrentScore={data().teamTwo.score}
timerValue={data().timer.value}
timerIsRunning={data().timer.isRunning}
>
<ActivityField
name="half"
endpoint={endpoint}
fields={halves}
index={data().half.value}
/>
</ActivityBaseConfig>
</Show>
);
}

export default SoccerConfig;
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ idf_component_register(SRCS
"lumen/activity/field/text.cpp"
"lumen/activity/field/timer.cpp"
"lumen/activity/football.cpp"
"lumen/activity/soccer.cpp"
"lumen/app_task.cpp"
"lumen/button_callback.cpp"
"lumen/hardware/button/button.cpp"
Expand All @@ -23,6 +24,7 @@ idf_component_register(SRCS
"lumen/web/handler/common.cpp"
"lumen/web/handler/event.cpp"
"lumen/web/handler/football.cpp"
"lumen/web/handler/soccer.cpp"
"lumen/web/server.cpp"
"main.cpp"
INCLUDE_DIRS "."
Expand Down
1 change: 1 addition & 0 deletions src/lumen/activity/activity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum class Type {
none = 0,
connect,
football,
soccer,
};

enum class ButtonEvent {
Expand Down
5 changes: 5 additions & 0 deletions src/lumen/activity/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "lumen/activity/connect.hpp"
#include "lumen/activity/football.hpp"
#include "lumen/activity/soccer.hpp"
#include "lumen/hardware/sd_card.hpp"

#include "esp_log.h"
Expand Down Expand Up @@ -71,6 +72,10 @@ void Context::set_activity(Type type)
activity_ = std::move(std::make_unique<Football>());
break;

case Type::soccer:
activity_ = std::move(std::make_unique<Soccer>());
break;

default:
ESP_LOGW(tag, "Unknown sport");
return;
Expand Down
50 changes: 50 additions & 0 deletions src/lumen/activity/soccer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "lumen/activity/soccer.hpp"

using json = nlohmann::json;

namespace lumen::activity {

field::Team& Soccer::team_one()
{
return team_one_;
}

field::Team& Soccer::team_two()
{
return team_two_;
}

field::Number& Soccer::half()
{
return half_;
}

field::Timer& Soccer::timer()
{
return timer_;
}

void Soccer::update_display() {}

json Soccer::to_json()
{
return {
{"type", "soccer"},
{"teamOne",
{{"name", team_one_.name().get_value()},
{"score", team_one_.score().get_value()}}},
{"teamTwo",
{{"name", team_two_.name().get_value()},
{"score", team_two_.score().get_value()}}},
{"half",
{{"value", half_.get_value()},
{"startValue", half_.get_start_value()}}},
{"timer",
{{"value", timer_.get_value()},
{"startTime", timer_.get_start_time()},
{"countUp", timer_.is_count_up()},
{"isRunning", timer_.is_running()}}}
};
}

} // namespace lumen::activity
46 changes: 46 additions & 0 deletions src/lumen/activity/soccer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "lumen/activity/activity.hpp"
#include "lumen/activity/field/number.hpp"
#include "lumen/activity/field/team.hpp"
#include "lumen/activity/field/timer.hpp"

#include "nlohmann/json.hpp"

namespace lumen::activity {

/// Stores and displays the data for a Soccer game.
class Soccer : public Activity {
public:
/// Return the first team.
field::Team& team_one();

/// Return the second team.
field::Team& team_two();

/// Return the quarter field.
field::Number& half();

/// Return the timer field
field::Timer& timer();

/// Draw the fields to the display.
void update_display() override;

/** Constructs a JSON object from the `Soccer` object.
*
* \returns The JSON object.
*/
nlohmann::json to_json() override;

private:
field::Team team_one_{"Home"};
field::Team team_two_{"Away"};

field::Number half_{1};

// Soccer games typically have 45 minute halves.
field::Timer timer_{45 * 60};
};

} // namespace lumen::activity
151 changes: 151 additions & 0 deletions src/lumen/web/handler/soccer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include "lumen/web/handler/soccer.hpp"

#include "lumen/activity/activity.hpp"
#include "lumen/activity/soccer.hpp"
#include "lumen/web/error.hpp"
#include "lumen/web/server.hpp"

#include "esp_log.h"
#include "nlohmann/json.hpp"

using json = nlohmann::json;

namespace lumen::web::handler {
namespace {

constexpr auto tag = "web/handler/soccer";

constexpr auto buffer_size = 128;

}

esp_err_t soccer_get(httpd_req_t* request)
{
auto* activity_context =
static_cast<Server*>(httpd_get_global_user_ctx(request->handle))
->get_activity_context();

activity_context->set_activity(activity::Type::soccer);

auto soccer_data = activity_context->get_activity_json();

httpd_resp_set_type(request, "application/json");
httpd_resp_sendstr(request, soccer_data.dump().c_str());

return ESP_OK;
}

esp_err_t soccer_put(httpd_req_t* request)
{
ESP_LOGI(tag, "Soccer PUT handler");
// Make sure the correct endpoint was hit
auto* activity_context =
static_cast<Server*>(httpd_get_global_user_ctx(request->handle))
->get_activity_context();

if (activity_context->get_activity_type() != activity::Type::soccer) {
ESP_LOGW(
tag,
"Current activity is not soccer: %d",
static_cast<int>(activity_context->get_activity_type())
);

return send_error(request, 400, "The current activity is not soccer");
}

// Make sure we can store the request JSON in our buffer
auto const content_length = request->content_len;

if (content_length > buffer_size - 1) {
ESP_LOGW(tag, "Received more data than can be processed");
return send_error(
request, 500, "More data was received than can be processed"
);
}

char buffer[buffer_size];

// Read in the request JSON
int received_bytes = httpd_req_recv(request, buffer, buffer_size);
buffer[content_length] = '\0';

if (received_bytes <= 0) {
return send_error(request, 500, "Error reading request content");
}

auto const request_json = json::parse(buffer);

ESP_LOGI(tag, "Request: %s", request_json.dump().c_str());

auto* soccer =
static_cast<activity::Soccer*>(activity_context->get_activity());

// Parse and validate the request JSON

if (request_json.contains("teamOne")) {
if (request_json["teamOne"].contains("name")) {
auto name = request_json["teamOne"]["name"];

// Set a limit of six characters
if (name.is_string() && name.size() <= 6) {
soccer->team_one().name().set_value(name);
}
}
if (request_json["teamOne"].contains("score")) {
auto score = request_json["teamOne"]["score"];

if (score.is_number_unsigned()) {
soccer->team_one().score().set_value(score);
}
}
}

if (request_json.contains("teamTwo")) {
if (request_json["teamTwo"].contains("name")) {
auto name = request_json["teamTwo"]["name"];

// Set a limit of six characters
if (name.is_string() && name.size() <= 6) {
soccer->team_two().name().set_value(name);
}
}
if (request_json["teamTwo"].contains("score")) {
auto score = request_json["teamTwo"]["score"];

if (score.is_number_unsigned()) {
soccer->team_two().score().set_value(score);
}
}
}

if (request_json.contains("half") &&
request_json["half"].is_number_unsigned()) {
soccer->half().set_value(request_json["half"]);
}

if (request_json.contains("timer")) {
if (request_json["timer"].contains("isRunning") &&
request_json["timer"]["isRunning"].is_boolean()) {
if (request_json["timer"]["isRunning"]) {
if (soccer->timer().get_value() > 0) {
soccer->timer().start();
} else {
soccer->timer().reset();
soccer->timer().start();
}
} else {
soccer->timer().stop();
}
}

if (request_json["timer"].contains("value") &&
request_json["timer"]["value"].is_number_unsigned()) {
soccer->timer().set_value(request_json["timer"]["value"]);
}
}

httpd_resp_send(request, nullptr, 0);
return ESP_OK;
}

} // namespace lumen::web::handler
19 changes: 19 additions & 0 deletions src/lumen/web/handler/soccer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "esp_http_server.h"

namespace lumen::web::handler {

/** The GET handler for the soccer endpoint.
*
* \param request Valid pointer to the request being handled.
*/
esp_err_t soccer_get(httpd_req_t* request);

/** The PUT handler for the soccer endpoint.
*
* \param request Valid pointer to the request being handled.
*/
esp_err_t soccer_put(httpd_req_t* request);

} // namespace lumen::web::handler
Loading