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

ESP32側のCodeを更新 #233

Merged
merged 9 commits into from
Nov 19, 2023
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
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
backend:
- any:
- "backend/**"

hardware:
- any:
- "hardware/**"

dashboard:
- any:
- "frontend/dashboard/**"
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/esp32-cam-udp-build-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: ESP32 Cam UDP CI

on:
push:
branches:
- "main"
- "deployment"
paths:
- "hardware/esp32-cam-udp/**"
pull_request:
paths:
- "hardware/esp32-cam-udp/**"
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: hardware/esp32-cam-udp

steps:
- uses: actions/checkout@v2
- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Cache PlatformIO
uses: actions/cache@v2
with:
path: ~/.platformio
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
- name: Set up Python
uses: actions/setup-python@v2
- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio
- name: Install library dependencies
run: pio lib -g install 1
- name: Run PlatformIO
run: pio run
46 changes: 46 additions & 0 deletions .github/workflows/esp32-control-build-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: ESP32 Control CI

on:
push:
branches:
- "main"
- "deployment"
paths:
- "hardware/esp32-control/**"
pull_request:
paths:
- "hardware/esp32-control/**"
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: hardware/esp32-control

steps:
- uses: actions/checkout@v2
- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Cache PlatformIO
uses: actions/cache@v2
with:
path: ~/.platformio
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
- name: Set up Python
uses: actions/setup-python@v2
- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio
- name: Install library dependencies
run: pio lib -g install 1
- name: Run PlatformIO
run: pio run
Binary file added backend/state-manager/cmd/main
Binary file not shown.
6 changes: 3 additions & 3 deletions backend/state-manager/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func main() {
r := chi.NewRouter()
// r.Use(middleware.Recoverer)
r.Use(middleware.Heartbeat("/debug/ping"))
r.Mount("/debug", middleware.Profiler())
r.Handle(statev1connect.NewStateManagerServiceHandler(&connectHandler.StateManagerServer{}))
r.Use(httplog.RequestLogger(
httplog.NewLogger(
"http_server",
Expand All @@ -101,6 +99,8 @@ func main() {
),
),
)
r.Mount("/debug", middleware.Profiler())
r.Handle(statev1connect.NewStateManagerServiceHandler(&connectHandler.StateManagerServer{}))

srv := &http.Server{
Addr: net.JoinHostPort("0.0.0.0", "8080"),
Expand All @@ -121,5 +121,5 @@ func main() {
newCtx, srvTimeOutCancel := context.WithTimeout(context.Background(), 3*time.Second)
defer srvTimeOutCancel()
srv.Shutdown(newCtx)
<-newCtx.Done()
<-newCtx.Done()
}
25 changes: 24 additions & 1 deletion backend/state-manager/pkg/mqtt_handler/mqtt_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -56,7 +57,7 @@ func StartHandler(ctx context.Context) error {
msgCh <- msg
}
cc := MakeClient()
Subscribe(cc, []string{"point/#", "stop/#", "block/#", "train/#"}, f)
Subscribe(cc, []string{"point/#", "stop/#", "block/#", "train/#", "setting/#"}, f)

for {
select {
Expand Down Expand Up @@ -137,6 +138,28 @@ func getState(cc mqtt.Client, target string, id string) {
token := cc.Publish("block/"+id+"/get/accepted", 0, false, res)
token.Wait()

case "setting":
// read from /setting/esp/{id}.json
// check file exists
_, err := os.Stat("../settings/esp/" + id + ".json")
if err != nil {
log.Println(err.Error())
// Return error message
token := cc.Publish("setting/"+id+"/get/accepted", 0, false, "error")
token.Wait()
return
}
raw, err := ioutil.ReadFile("../settings/esp/" + id + ".json")
if err != nil {
log.Println(err.Error())
return
}
// remove \n code
raw = []byte(strings.Replace(string(raw), "\n", "", -1))
raw = []byte(strings.Replace(string(raw), " ", "", -1))
token := cc.Publish("setting/"+id+"/get/accepted", 0, false, string(raw))
token.Wait()

case "train":
// TODO: implement
}
Expand Down
28 changes: 28 additions & 0 deletions backend/state-manager/settings/esp/sakurajosui.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "test",
"stops": [
{
"stop_id": "test_s1",
"pin": 13
}
],
"points": [
{
"point_id": "test_p1",
"pin": 14
}
],
"detectors": [
{
"block_id": "test_b1",
"target": "OPEN",
"pin": 15
}
],
"nfcs": [
{
"nfc_id": "test_t1",
"pin": 15
}
]
}
52 changes: 52 additions & 0 deletions hardware/esp32-control/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"files.associations": {
"thread": "cpp",
"*.ipp": "cpp",
"texture": "cpp",
"*.tcc": "cpp",
"boundingbox": "cpp",
"boundingsphere": "cpp",
"timer": "cpp",
"numeric": "cpp",
"config": "cpp",
"cstdint": "cpp",
"types": "cpp",
"cstddef": "cpp",
"string_view": "cpp",
"bitset": "cpp",
"quat": "cpp",
"vec2d": "cpp",
"vec2f": "cpp",
"vec3d": "cpp",
"vec3f": "cpp",
"vec4d": "cpp",
"vec4f": "cpp",
"array": "cpp",
"chrono": "cpp",
"functional": "cpp",
"istream": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"variant": "cpp",
"charconv": "cpp",
"compare": "cpp",
"complex": "cpp",
"graphicscontext": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"initializer_list": "cpp",
"format": "cpp",
"span": "cpp",
"mixinvector": "cpp"
}
}
4 changes: 4 additions & 0 deletions hardware/esp32-control/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
upload:
pio run -t upload
file:
pio run -t uploadfs
1 change: 1 addition & 0 deletions hardware/esp32-control/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ESP32 Control
28 changes: 28 additions & 0 deletions hardware/esp32-control/data/setting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "test",
"stops": [
{
"stop_id": "test_s1",
"pin": 13
}
],
"points": [
{
"point_id": "test_p1",
"pin": 14
}
],
"detectors": [
{
"block_id": "test_b1",
"target": "OPEN",
"pin": 15
}
],
"nfcs": [
{
"nfc_id": "test_t1",
"pin": 15
}
]
}
5 changes: 5 additions & 0 deletions hardware/esp32-control/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ framework = arduino
lib_deps =
knolleary/PubSubClient@^2.8
madhephaestus/ESP32Servo@^1.1.0
bblanchon/ArduinoJson@^6.21.3
jandrassy/ArduinoOTA@^1.0.12
miguelbalboa/MFRC522@^1.4.10
board_build.filesystem = littlefs
; targets = upload
monitor_speed = 115200
70 changes: 70 additions & 0 deletions hardware/esp32-control/src/IOManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "IOManager.h"

IOManager::IOManager(PubSubClient client)
{
client = client;
POINT_LIST_INDEX = 0;
STOP_LIST_INDEX = 0;
DETECTOR_LIST_INDEX = 0;
NFC_LIST_INDEX = 0;
}

void IOManager::addPoint(uint8_t pin, String point_id)
{
POINT_LIST[POINT_LIST_INDEX].attach(pin, point_id);
POINT_LIST_INDEX++;
}

void IOManager::addStop(uint8_t pin, String stop_id)
{
STOP_LIST[STOP_LIST_INDEX].attach(pin, stop_id);
STOP_LIST_INDEX++;
}

void IOManager::addDetector(uint8_t pin, String block_id, String target)
{
DETECTOR_LIST[DETECTOR_LIST_INDEX].init(block_id, target, pin, &client);
DETECTOR_LIST_INDEX++;
}

void IOManager::addNfc(uint8_t pin, String nfc_id)
{
NFC_LIST[NFC_LIST_INDEX].init(nfc_id, pin, &client);
NFC_LIST_INDEX++;
}

void IOManager::setPointState(String point_id, POINT_STATE state)
{
for (int i = 0; i < POINT_LIST_INDEX; i++)
{
if (POINT_LIST[i].getId() == point_id)
{
POINT_LIST[i].set_state(state);
return;
}
}
}

void IOManager::setStopState(String stop_id, STOP_STATE state)
{
for (int i = 0; i < STOP_LIST_INDEX; i++)
{
if (STOP_LIST[i].getId() == stop_id)
{
STOP_LIST[i].set_state(state);
return;
}
}
}

void IOManager::loop()
{
for (int i = 0; i < DETECTOR_LIST_INDEX; i++)
{
DETECTOR_LIST[i].loop();
}
for (int i = 0; i < NFC_LIST_INDEX; i++)
{
NFC_LIST[i].loop();
}
}
Loading
Loading