My Project
Loading...
Searching...
No Matches
loadControllerConfiguration.h
Go to the documentation of this file.
1#pragma once
2
4
5#define DEFAULT_CONTROLLER_NAME "MTC4BT"
6#define DEFAULT_PWR_INC_STEP 10
7#define DEFAULT_PWR_DEC_STEP 10
8
10{
11 // New up a configation object, so we can set its properties.
13
14 // Initialize file system.
15 if (!SPIFFS.begin(true)) {
16 Serial.println("Config: An error has occurred while mounting SPIFFS");
17 return config;
18 }
19
20 // Read JSON controller config file.
21 StaticJsonDocument<4096> doc = MCJsonConfig::ReadJsonFile(configFilePath);
22
23 // Read controller name.
24 const char *controllerName = doc["name"] | DEFAULT_CONTROLLER_NAME;
25 config->ControllerName = controllerName;
26 log4MC::vlogf(LOG_INFO, "Config: Read controller name: %s", config->ControllerName);
27
28 int16_t pwrIncStep = doc["pwrIncStep"] | DEFAULT_PWR_INC_STEP;
29 int16_t pwrDecStep = doc["pwrDecStep"] | DEFAULT_PWR_DEC_STEP;
30
31 // Iterate over ESP pins and copy values from the JsonDocument to MCChannelConfig objects.
32 JsonArray espPinConfigs = doc["espPins"].as<JsonArray>();
33 for (JsonObject espPinConfig : espPinConfigs) {
34 // Use pin number as its device address.
35 const std::string address = espPinConfig["pin"];
36 int16_t pinPwrIncStep = espPinConfig["pwrIncStep"] | pwrIncStep;
37 int16_t pinPwrDecStep = espPinConfig["pwrDecStep"] | pwrDecStep;
38 const bool isInverted = espPinConfig["inverted"] | false;
39 const std::string attachedDevice = espPinConfig["attachedDevice"] | "nothing";
40
41 MCChannel *espChannel = new MCChannel(ChannelType::EspPinChannel, address);
42 config->EspPins.push_back(new MCChannelConfig(espChannel, pinPwrIncStep, pinPwrDecStep, isInverted, deviceTypeMap()[attachedDevice]));
43 }
44 log4MC::vlogf(LOG_INFO, "Config: Read ESP pin configuration (%u).", config->EspPins.size());
45
46 // Iterate over loco configs and copy values from the JsonDocument to BLELocomotiveConfiguration objects.
47 JsonArray locoConfigs = doc["locos"].as<JsonArray>();
48 for (JsonObject locoConfig : locoConfigs) {
49 // Read if loco is enabled.
50 const bool enabled = locoConfig["enabled"] | true;
51 if (!enabled) {
52 // Skip if loco is not enabled.
53 continue;
54 }
55
56 config->Locomotives.push_back(BLELocomotiveDeserializer::Deserialize(locoConfig, config->EspPins, pwrIncStep, pwrDecStep));
57 }
58
59 // Read loco config files.
60 JsonArray locoConfigFiles = doc["locoConfigs"].as<JsonArray>();
61 for (int i = 0; i < locoConfigFiles.size(); i++) {
62 const std::string locoConfigFile = locoConfigFiles[i];
63
64 // Read JSON controller config file.
65 DynamicJsonDocument locoConfigDoc = MCJsonConfig::ReadJsonFile(locoConfigFile.c_str());
66 JsonObject locoConfig = locoConfigDoc.as<JsonObject>();
67
68 // Read if loco is enabled.
69 const bool enabled = locoConfig["enabled"] | true;
70 if (!enabled) {
71 // Skip if loco is not enabled.
72 continue;
73 }
74
75 config->Locomotives.push_back(BLELocomotiveDeserializer::Deserialize(locoConfig, config->EspPins, pwrIncStep, pwrDecStep));
76 }
77
78 // Return MTC4BTConfiguration object.
79 return config;
80}
static BLELocomotiveConfiguration * Deserialize(JsonObject locoConfig, std::vector< MCChannelConfig * > espPins, int16_t defaultPwrIncStep, int16_t defaultPwrDecStep)
Definition BLELocomotiveDeserializer.cpp:5
Definition MCChannelConfig.h:8
Definition MCChannel.h:7
static DynamicJsonDocument ReadJsonFile(const char *jsonFilePath)
Definition MCJsonConfig.cpp:3
static void vlogf(uint8_t level, const char *fmt,...)
Definition log4MC.cpp:60
@ EspPinChannel
Definition enums.h:6
#define DEFAULT_CONTROLLER_NAME
Definition loadControllerConfiguration.h:5
MTC4BTConfiguration * loadControllerConfiguration(const char *configFilePath)
Definition loadControllerConfiguration.h:9
#define DEFAULT_PWR_INC_STEP
Definition loadControllerConfiguration.h:6
#define DEFAULT_PWR_DEC_STEP
Definition loadControllerConfiguration.h:7
std::vector< MCChannelConfig * > EspPins
Definition MCConfiguration.h:14
const char * ControllerName
Definition MCConfiguration.h:11
Definition MTC4BTConfiguration.h:6
std::vector< BLELocomotiveConfiguration * > Locomotives
Definition MTC4BTConfiguration.h:9
Definition enums.h:28