Skip to content

Commit

Permalink
Generalize API exposed enum generation
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed May 23, 2024
1 parent e6ac0a2 commit 8168581
Show file tree
Hide file tree
Showing 48 changed files with 240 additions and 300 deletions.
3 changes: 3 additions & 0 deletions software/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ src/**/*.embedded.h
src/**/*.embedded.cpp
src/modules/*/module_available.h
src/modules/*/module_dependencies.h
src/modules/*/*.enum.h
src/modules/*/*.enum.cpp
src/modules.h
src/modules.cpp
src/build.h
Expand All @@ -32,6 +34,7 @@ web/src/ts/api_defs.ts
web/src/ts/branding.ts
web/src/ts/translation.tsx
web/src/ts/translation.json
web/src/modules/*/*.enum.ts
web/**/*.embedded.ts
coredump_py_gdb_cmds
api_info.json
80 changes: 79 additions & 1 deletion software/pio_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ def include_bindings(path):
mod_path = os.path.join('src', 'modules', backend_module.under)

if not os.path.exists(mod_path) or not os.path.isdir(mod_path):
print("Backend module {} not found.".format(backend_module.space, mod_path))
print("Backend module {} not found.".format(backend_module.space))

for root, dirs, files in os.walk(mod_path):
for name in files:
Expand Down Expand Up @@ -1076,6 +1076,84 @@ def format_translation(translation, type_only, indent):
with ChangedDirectory('web'):
subprocess.check_call([env.subst('$PYTHONEXE'), "-u", "check_override_completeness.py"])

# Generate enums
for backend_module in backend_modules:
mod_path = os.path.join('src', 'modules', backend_module.under)

if not os.path.exists(mod_path) or not os.path.isdir(mod_path):
print("Backend module {} not found.".format(backend_module.space))
else:
for name in os.listdir(mod_path):
if not name.endswith(".enum"):
continue

name_parts = name.split('.')

if len(name_parts) != 3:
print('Error: Invalid enum file "{}" in backend {}'.format(name, mod_path))
sys.exit(1)

enum_name = util.FlavoredName(name_parts[0]).get()
enum_values = []
enum_cases = []
value_number = -1
value_count = 0

with open(os.path.join(mod_path, name), 'r', encoding='utf-8') as f:
for line in f.readlines():
line = line.strip()

if len(line) == 0 or line.startswith('#'):
continue

line_parts = line.split('=', 1)
value_name = util.FlavoredName(line_parts[0].strip()).get()

if len(line_parts) > 1:
value_number = int(line_parts[1].strip())
value_count = None
else:
value_number += 1

if value_count != None:
value_count += 1

enum_values.append(' {0} = {1},\n'.format(value_name.camel, value_number))
enum_cases.append(' case {0}::{1}: return "{2}";\n'.format(enum_name.camel, value_name.camel, value_name.space))

with open(os.path.join(mod_path, enum_name.under + '.enum.h'), 'w', encoding='utf-8') as f:
f.write(f'// WARNING: This file is generated from "{name}"\n\n')
f.write('#include <stdint.h>\n\n')
f.write('#pragma once\n\n')
f.write(f'enum class {enum_name.camel} : {name_parts[1]}_t {{\n')
f.write(''.join(enum_values))
f.write('};\n\n')

if value_count != None:
f.write(f'#define {enum_name.upper}_COUNT {value_count}\n\n')

f.write(f'const char *get_{enum_name.under}_name({enum_name.camel} value);\n')

with open(os.path.join(mod_path, enum_name.under + '.enum.cpp'), 'w', encoding='utf-8') as f:
f.write(f'// WARNING: This file is generated from {name}.\n\n')
f.write(f'#include "{enum_name.under}.enum.h"\n\n')
f.write(f'const char *get_{enum_name.under}_name({enum_name.camel} value)\n')
f.write('{\n')
f.write(' switch (value) {\n')
f.write(''.join(enum_cases))
f.write(' default: return "Unknown";\n')
f.write(' }\n')
f.write('}\n')

frontend_mod_path = os.path.join('web', 'src', 'modules', backend_module.under)

if os.path.exists(frontend_mod_path) and os.path.isdir(frontend_mod_path):
with open(os.path.join(frontend_mod_path, enum_name.under + '.enum.ts'), 'w', encoding='utf-8') as f:
f.write(f'// WARNING: This file is generated from "{name}"\n\n')
f.write(f'export const enum {enum_name.camel} {{\n')
f.write(''.join(enum_values))
f.write('}\n')

# Generate web interface
util.log('Checking web interface dependencies')

Expand Down
1 change: 0 additions & 1 deletion software/src/modules/automation/.gitignore

This file was deleted.

16 changes: 16 additions & 0 deletions software/src/modules/automation/Automation Action ID.uint8.enum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
None
Print
MQTT
Set Current
LED
Meter Reset
Set Manager Current
NFC Inject Tag
Charge Limits
EVSE GP Output
Charge Tracker Reset
PM Phase Switch
PM Charge Mode Switch
EM Relay Switch
PM Limit Max Current
PM Block Charge
19 changes: 19 additions & 0 deletions software/src/modules/automation/Automation Trigger ID.uint8.enum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
None
Cron
Charger State
MQTT
EVSE Button
NFC
Charge Limits
EVSE Shutdown Input
EVSE GP Input
EVSE External Current Wd
Require Meter
Charge Manager Wd
EM Input Three
EM Input Four
EM Phase Switch
EM Contactor Monitoring
PM Power Available
PM Grid Power Draw
HTTP
8 changes: 4 additions & 4 deletions software/src/modules/automation/automation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ void Automation::pre_setup()
};

state = Config::Object({
{"registered_triggers", Config::Array({}, new Config{Config::Uint8(0)}, 0, static_cast<uint16_t>(AutomationTriggerID::Count), Config::type_id<Config::ConfInt>())},
{"registered_actions", Config::Array({}, new Config{Config::Uint8(0)}, 0, static_cast<uint16_t>(AutomationActionID::Count), Config::type_id<Config::ConfInt>())},
{"enabled_triggers", Config::Array({}, new Config{Config::Uint8(0)}, 0, static_cast<uint16_t>(AutomationTriggerID::Count), Config::type_id<Config::ConfInt>())},
{"enabled_actions", Config::Array({}, new Config{Config::Uint8(0)}, 0, static_cast<uint16_t>(AutomationActionID::Count), Config::type_id<Config::ConfInt>())},
{"registered_triggers", Config::Array({}, new Config{Config::Uint8(0)}, 0, AUTOMATION_TRIGGER_ID_COUNT, Config::type_id<Config::ConfInt>())},
{"registered_actions", Config::Array({}, new Config{Config::Uint8(0)}, 0, AUTOMATION_ACTION_ID_COUNT, Config::type_id<Config::ConfInt>())},
{"enabled_triggers", Config::Array({}, new Config{Config::Uint8(0)}, 0, AUTOMATION_TRIGGER_ID_COUNT, Config::type_id<Config::ConfInt>())},
{"enabled_actions", Config::Array({}, new Config{Config::Uint8(0)}, 0, AUTOMATION_ACTION_ID_COUNT, Config::type_id<Config::ConfInt>())},
});

for (auto const &trigger : trigger_map) {
Expand Down
5 changes: 3 additions & 2 deletions software/src/modules/automation/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
#include "config.h"
#include <map>
#include <vector>
#include "automation_defs.h"
#include "automation_trigger_id.enum.h"
#include "automation_action_id.enum.h"
#include "automation_backend.h"

class Automation : public IModule, public IAutomationBackend
{
public:
typedef std::function<void(const Config *)> ActionCb;
typedef std::function<String (const Config *)> ValidatorCb;
typedef std::function<String(const Config *)> ValidatorCb;

struct ActionValue {
ActionCb callback;
Expand Down
94 changes: 0 additions & 94 deletions software/src/modules/automation/prepare.py

This file was deleted.

2 changes: 1 addition & 1 deletion software/src/modules/meters_modbus_tcp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
meters_modbus_tcp_defs.h
Modbus Value Type.uint8.enum
meters_modbus_tcp_defs.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
None
Inverter
Grid
Battery
Load
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
None
Custom
Sungrow Hybrid Inverter
Sungrow String Inverter
Solarmax Max Storage
Victron Energy GX
Deye Hybrid Inverter
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Address
Number
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Holding Register
Input Register
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
None
Inverter
Grid
Battery
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
None
Inverter
Grid
Battery
Load
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
None
Inverter
Grid
Load
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
None
Inverter
Grid
Battery
Load
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "ModbusTCP.h"

#include "tools.h"
#include "meters_modbus_tcp_defs.h"
#include "modbus_register_type.enum.h"

#if defined(__GNUC__)
#pragma GCC diagnostic push
Expand Down
Loading

0 comments on commit 8168581

Please sign in to comment.