Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
csenet committed Nov 25, 2023
1 parent 613ce0c commit 281f2e2
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 56 deletions.
4 changes: 0 additions & 4 deletions backend/state-manager/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ func main() {
cancel()
return
}
eg.Go(func() error {
slog.Default().Info("start mqtt handler")
return mqttHandler.Start(ctx)
})

r := chi.NewRouter()
// r.Use(middleware.Recoverer)
Expand Down
3 changes: 2 additions & 1 deletion hardware/esp32-control/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"initializer_list": "cpp",
"format": "cpp",
"span": "cpp",
"mixinvector": "cpp"
"mixinvector": "cpp",
"stacktrace": "cpp"
}
}
File renamed without changes.
File renamed without changes.
67 changes: 67 additions & 0 deletions hardware/esp32-control/config/hasen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "sakurajosui",
"stops": [
{
"stop_id": "sakurajosui_up_s1",
"pin": 14
},
{
"stop_id": "sakurajosui_up_s2",
"pin": 15
},
{
"stop_id": "sakurajosui_down_s1",
"pin": 16
},
{
"stop_id": "sakurajosui_down_s2",
"pin": 17
}
],
"points": [
{
"point_id": "sakurajosui_down_p1",
"pin": 13
}
],
"detectors": [
{
"block_id": "shinjyuku_down_b1",
"target": "OPEN",
"pin": 12
},
{
"block_id": "sakurajosui_up_b1",
"target": "CLOSE",
"pin": 22
},
{
"block_id": "chofu_up_b1",
"target": "OPEN",
"pin": 23
},
{
"block_id": "chofu_down_b1",
"target": "CLOSE",
"pin": 25
}
],
"nfcs": [
{
"nfc_id": "sakurajosui_up_s1",
"pin": 4
},
{
"nfc_id": "sakurajosui_up_s2",
"pin": 5
},
{
"nfc_id": "sakurajosui_down_s1",
"pin": 32
},
{
"nfc_id": "sakurajosui_down_s2",
"pin": 33
}
]
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"stops": [
{
"stop_id": "shinjyuku_down_s1",
"pin": 13
"pin": 16
},
{
"stop_id": "shinjyuku_up_s1",
"pin": 14
"pin": 17
}
],
"detectors": [
Expand Down Expand Up @@ -42,4 +42,4 @@
"pin": 5
}
]
}
}
41 changes: 29 additions & 12 deletions hardware/esp32-control/data/setting.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
{
"name": "test",
"name": "shinjyuku",
"stops": [
{
"stop_id": "test_s1",
"pin": 13
}
],
"points": [
"stop_id": "shinjyuku_down_s1",
"pin": 16
},
{
"point_id": "test_p1",
"pin": 14
"stop_id": "shinjyuku_up_s1",
"pin": 17
}
],
"detectors": [
{
"block_id": "test_b1",
"block_id": "shinjyuku_down_b1",
"target": "CLOSE",
"pin": 12
},
{
"block_id": "shinjyuku_up_b1",
"target": "CLOSE",
"pin": 22
},
{
"block_id": "shinjyuku_up_b1",
"target": "OPEN",
"pin": 15
"pin": 23
},
{
"block_id": "sakurajosui_up_b1",
"target": "OPEN",
"pin": 25
}
],
"nfcs": [
{
"nfc_id": "test_t1",
"pin": 15
"nfc_id": "shinjyuku_down_s1",
"pin": 4
},
{
"nfc_id": "shinjyuku_up_s1",
"pin": 5
}
]
}
31 changes: 26 additions & 5 deletions hardware/esp32-control/src/IOManager.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "IOManager.h"

IOManager::IOManager(PubSubClient client)
StopRail STOP_LIST[MAX_STOP_NUM];

IOManager::IOManager(PubSubClient *client)
{
client = client;
this->client = client;
POINT_LIST_INDEX = 0;
STOP_LIST_INDEX = 0;
DETECTOR_LIST_INDEX = 0;
Expand All @@ -11,6 +13,7 @@ IOManager::IOManager(PubSubClient client)

void IOManager::addPoint(uint8_t pin, String point_id)
{
POINT_LIST[POINT_LIST_INDEX] = PointRail();
POINT_LIST[POINT_LIST_INDEX].attach(pin, point_id);
POINT_LIST_INDEX++;
}
Expand All @@ -23,13 +26,14 @@ void IOManager::addStop(uint8_t pin, String stop_id)

void IOManager::addDetector(uint8_t pin, String block_id, String target)
{
DETECTOR_LIST[DETECTOR_LIST_INDEX].init(block_id, target, pin, &client);
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[NFC_LIST_INDEX].init(nfc_id, pin, client);
NFC_LIST_INDEX++;
}

Expand All @@ -49,14 +53,31 @@ 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)
Serial.println(STOP_LIST[i].stop_id);
if (STOP_LIST[i].stop_id == stop_id)
{
Serial.println(STOP_LIST[i].getId());
Serial.println(stop_id);
STOP_LIST[i].set_state(state);
return;
}
}
}

void IOManager::getInitialState()
{
for (int i = 0; i < POINT_LIST_INDEX; i++)
{
String topic = "point/" + POINT_LIST[i].getId() + "/get";
this->client->publish(topic.c_str(), "get");
}
for (int i = 0; i < STOP_LIST_INDEX; i++)
{
String topic = "stop/" + STOP_LIST[i].stop_id + "/get";
this->client->publish(topic.c_str(), "get");
}
}

void IOManager::loop()
{
for (int i = 0; i < DETECTOR_LIST_INDEX; i++)
Expand Down
6 changes: 4 additions & 2 deletions hardware/esp32-control/src/IOManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
class IOManager
{
public:
IOManager(PubSubClient client);
PubSubClient client;
IOManager(PubSubClient *client);
PubSubClient *client;
void addStop(uint8_t pin, String stop_id);
void addPoint(uint8_t pin, String point_id);

void getInitialState();

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);
Expand Down
3 changes: 2 additions & 1 deletion hardware/esp32-control/src/NFCReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void NFCReader::init(String nfc_id, uint8_t ss_pin, PubSubClient *client)

void NFCReader::loop()
{
Serial.println(reader.PICC_IsNewCardPresent());
if (!reader.PICC_IsNewCardPresent())
{
return;
Expand All @@ -30,7 +31,7 @@ void NFCReader::loop()
}
Serial.print("NFC ID: ");
Serial.print(nfc_id);
Serial.print( "UID: ");
Serial.print("UID: ");
Serial.println(uid);
reader.PICC_HaltA();
reader.PCD_StopCrypto1();
Expand Down
4 changes: 1 addition & 3 deletions hardware/esp32-control/src/NFCReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <MFRC522.h>
#include <PubSubClient.h>

#define RST_PIN 9
#define SS_1_PIN 10
#define SS_2_PIN 8
#define RST_PIN 3

class NFCReader
{
Expand Down
2 changes: 2 additions & 0 deletions hardware/esp32-control/src/PassDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ void PassDetector::init(String id, String state, int pin, PubSubClient *client)
this->id = id;
this->pin = pin;
this->state = state;
this->client = client;
pinMode(pin, INPUT_PULLUP);
this->last_switch_time = 0;
}

void PassDetector::loop()
Expand Down
8 changes: 5 additions & 3 deletions hardware/esp32-control/src/SettingLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void readFile(fs::FS &fs, const char *path, char *buf)

void loadSetting(char *input, IOManager *manager)
{
StaticJsonDocument<384> doc;
StaticJsonDocument<512> doc;

DeserializationError error = deserializeJson(doc, input);

Expand All @@ -36,7 +36,7 @@ void loadSetting(char *input, IOManager *manager)
}

// 端末名
const char* host_name = doc["name"];
const char *host_name = doc["name"];
strcpy(HOST, host_name);

// STOPS
Expand Down Expand Up @@ -67,6 +67,7 @@ void loadSetting(char *input, IOManager *manager)
const char *target = v["target"];
int pin = v["pin"];
Serial.printf("block_id: %s, target: %s, pin: %d\n", block_id, target, pin);
manager->addDetector(pin, block_id, target);
}

// NFCS
Expand All @@ -76,14 +77,15 @@ void loadSetting(char *input, IOManager *manager)
const char *nfc_id = v["nfc_id"];
int pin = v["pin"];
Serial.printf("nfc_id: %s, pin: %d\n", nfc_id, pin);
// manager->addNfc(pin, nfc_id);
}
Serial.println("Setiing loaded.");
SETTING_LOADED = true;
}

void getSetting(IOManager *manager)
{
char json_buf[4096];
char json_buf[1024];
readFile(LittleFS, "/setting.json", json_buf); // jsonファイル読み込み
loadSetting(json_buf, manager);
}
4 changes: 2 additions & 2 deletions hardware/esp32-control/src/Settings.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Settings.h"

const char *ssid = "SSID";
const char *password = "PASSWORD";
const char *ssid = "plarail-2g";
const char *password = "plarail2023";

char HOST[25];
11 changes: 6 additions & 5 deletions hardware/esp32-control/src/StopRail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

StopRail::StopRail() {}

void StopRail::attach(uint8_t pin, String point_id)
void StopRail::attach(uint8_t pin, String stop_id)
{
point_id = point_id;
pin = pin;
servo.setPeriodHertz(50);
this->stop_id = stop_id;
this->pin = pin;
}

void StopRail::set_state(STOP_STATE state)
{
state = state;
servo.attach(pin, 500, 2400);
servo.setPeriodHertz(50);
this->state = state;
if (state == STOP_STATE_STOP)
{
servo.write(STOP_ON_ANGLE);
Expand All @@ -21,6 +21,7 @@ void StopRail::set_state(STOP_STATE state)
{
servo.write(STOP_OFF_ANGLE);
}
delay(200);
servo.detach();
}

Expand Down
Loading

0 comments on commit 281f2e2

Please sign in to comment.