Skip to content

Commit

Permalink
Update dependencies and remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
csenet committed Nov 18, 2023
1 parent 8c99f27 commit 81a6c07
Show file tree
Hide file tree
Showing 25 changed files with 647 additions and 213 deletions.
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
41 changes: 40 additions & 1 deletion hardware/esp32-control/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@
"boundingsphere": "cpp",
"timer": "cpp",
"numeric": "cpp",
"config": "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
}
]
}
4 changes: 4 additions & 0 deletions hardware/esp32-control/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ framework = arduino
lib_deps =
knolleary/PubSubClient@^2.8
madhephaestus/ESP32Servo@^1.1.0
bblanchon/ArduinoJson@^6.21.3
jandrassy/ArduinoOTA@^1.0.12
board_build.filesystem = littlefs
targets=upload
monitor_speed = 115200
64 changes: 64 additions & 0 deletions hardware/esp32-control/src/IOManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#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::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();
// }
}
39 changes: 39 additions & 0 deletions hardware/esp32-control/src/IOManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef IOMANAGER_H
#define IOMANAGER_H
#include <Arduino.h>
#include <PassDetector.h>
#include <PointRail.h>
#include <StopRail.h>

#define MAX_POINT_NUM 5
#define MAX_STOP_NUM 5
#define MAX_DETECTOR_NUM 5
#define MAX_NFC_NUM 5

class IOManager
{
public:
IOManager(PubSubClient client);
PubSubClient client;
void addStop(uint8_t pin, String stop_id);
void addPoint(uint8_t pin, String point_id);

void setStopState(String stop_id, STOP_STATE state);
void setPointState(String point_id, POINT_STATE state);
void addDetector(uint8_t pin, String block_id, String target);
// addNfc(uint8_t pin, String block_id);

void loop();

private:
uint8_t POINT_LIST_INDEX;
uint8_t STOP_LIST_INDEX;
uint8_t DETECTOR_LIST_INDEX;
// uint8_t NFC_LIST_INDEX;
PointRail POINT_LIST[MAX_POINT_NUM];
StopRail STOP_LIST[MAX_STOP_NUM];
PassDetector DETECTOR_LIST[MAX_DETECTOR_NUM];
// NfcReader NFC_LIST[MAX_NFC_NUM];
};

#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef MQTTSETTINGS_H
#define MQTTSETTINGS_H

const char *mqtt_broker = "p390e24a.ala.us-east-1.emqxsl.com"; // broker address
const char *topic = "stop/#"; // define topic
const char *mqtt_username = "test"; // username for authentication
const char *mqtt_password = "password"; // password for authentication
const int mqtt_port = 8883; // port of MQTT over TLS
Expand Down Expand Up @@ -30,4 +32,6 @@ const char *root_ca =
"4YSB03Yqp2t3TeZHM9ESfkus74nQyW7pRGezj+TC44xCagCQQOzzNmzEAP2SnCrJ\n"
"sNE2DpRVMnL8J6xBRdjmOsC3N6cQuKuRXbzByVBjCqAA8t1L0I+9wXJerLPyErjy\n"
"rMKWaBFLmfK/AHNF4ZihwPGOc7w6UHczBZXH5RFzJNnww+WnKuTPI0HfnVH8lg==\n"
"-----END CERTIFICATE-----\n";
"-----END CERTIFICATE-----\n";

#endif
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "switch.hpp"
#include "PassDetector.h"

BlockSwitch::BlockSwitch() {}
PassDetector::PassDetector() {}

void BlockSwitch::init(String id, String state, int pin, PubSubClient client)
void PassDetector::init(String id, String state, int pin, PubSubClient client)
{
this->id = id;
this->pin = pin;
this->state = state;
pinMode(pin, INPUT_PULLUP);
}

void BlockSwitch::loop()
void PassDetector::loop()
{
if (digitalRead(pin) == LOW)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef PASSDETECTOR_H
#define PASSDETECTOR_H

#include <Arduino.h>
#include <PubSubClient.h>

class BlockSwitch
class PassDetector
{
private:
String id;
Expand All @@ -11,7 +14,9 @@ class BlockSwitch
PubSubClient client;

public:
BlockSwitch();
PassDetector();
void loop();
void init(String id, String state, int pin, PubSubClient client);
};
};

#endif
35 changes: 35 additions & 0 deletions hardware/esp32-control/src/PointRail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "PointRail.h"

PointRail::PointRail() {}

void PointRail::attach(uint8_t pin, String point_id)
{
point_id = point_id;
pin = pin;
servo.setPeriodHertz(50);
}

void PointRail::set_state(POINT_STATE state)
{
state = state;
servo.attach(pin, 500, 2400);
if (state == POINT_STATE_NORMAL)
{
servo.write(POINT_STRAIGHT_ANGLE);
}
else
{
servo.write(POINT_REVERSE_ANGLE);
}
servo.detach();
}

String PointRail::getId()
{
return point_id;
}

POINT_STATE PointRail::getState()
{
return state;
}
Loading

0 comments on commit 81a6c07

Please sign in to comment.