Skip to content

Commit

Permalink
Generate MeterClassID and EthernetState enum
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed May 23, 2024
1 parent 8168581 commit 8690f5b
Show file tree
Hide file tree
Showing 27 changed files with 57 additions and 89 deletions.
11 changes: 3 additions & 8 deletions software/pio_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,12 +1111,10 @@ def format_translation(translation, type_only, indent):

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
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))
Expand All @@ -1128,14 +1126,11 @@ def format_translation(translation, type_only, indent):
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'#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'// 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')
Expand Down
2 changes: 1 addition & 1 deletion software/src/modules/energy_manager/energy_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void EnergyManager::start_network_check_task()
bool disconnected;
do {
#if MODULE_ETHERNET_AVAILABLE()
if (ethernet.get_connection_state() == EthernetState::CONNECTED) {
if (ethernet.get_connection_state() == EthernetState::Connected) {
disconnected = false;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions software/src/modules/ethernet/Ethernet State.uint8.enum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Not Configured
Not Connected
Connecting
Connected
22 changes: 11 additions & 11 deletions software/src/modules/ethernet/ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void Ethernet::pre_setup()
}};

state = Config::Object({
{"connection_state", Config::Uint((uint)EthernetState::NOT_CONFIGURED)},
{"connection_state", Config::Uint((uint)EthernetState::NotConfigured)},
{"connection_start", Config::Uint(0)},
{"connection_end", Config::Uint(0)},
{"ip", Config::Str("0.0.0.0", 7, 15)},
Expand Down Expand Up @@ -107,7 +107,7 @@ void Ethernet::setup()

config_in_use = config.get_owned_copy();

connection_state = EthernetState::NOT_CONFIGURED;
connection_state = EthernetState::NotConfigured;
state.get("connection_state")->updateUint((uint)connection_state);

initialized = true;
Expand All @@ -121,14 +121,14 @@ void Ethernet::setup()
hostname = String(BUILD_HOST_PREFIX) + "-" + local_uid_str;
#endif

connection_state = EthernetState::NOT_CONNECTED;
state.get("connection_state")->updateUint((uint)EthernetState::NOT_CONNECTED);
connection_state = EthernetState::NotConnected;
state.get("connection_state")->updateUint((uint)connection_state);

WiFi.onEvent([this](arduino_event_id_t event, arduino_event_info_t info) {
logger.printfln("Started");
ETH.setHostname(hostname.c_str());

connection_state = EthernetState::NOT_CONNECTED;
connection_state = EthernetState::NotConnected;

task_scheduler.scheduleOnce([this](){
state.get("connection_state")->updateUint((uint)connection_state);
Expand Down Expand Up @@ -157,7 +157,7 @@ void Ethernet::setup()
ETH.config((uint32_t)0, (uint32_t)0, (uint32_t)0);
}

connection_state = EthernetState::CONNECTING;
connection_state = EthernetState::Connecting;

task_scheduler.scheduleOnce([this, link_speed, full_duplex](){
state.get("connection_state")->updateUint((uint)connection_state);
Expand All @@ -183,7 +183,7 @@ void Ethernet::setup()
was_connected = true;
last_connected = now;

connection_state = EthernetState::CONNECTED;
connection_state = EthernetState::Connected;

task_scheduler.scheduleOnce([this, now, ip, subnet](){
state.get("connection_state")->updateUint((uint)connection_state);
Expand All @@ -205,7 +205,7 @@ void Ethernet::setup()

uint32_t now = millis();

connection_state = EthernetState::CONNECTING;
connection_state = EthernetState::Connecting;

task_scheduler.scheduleOnce([this, now](){
state.get("connection_state")->updateUint((uint)connection_state);
Expand All @@ -222,7 +222,7 @@ void Ethernet::setup()

uint32_t now = millis();

connection_state = EthernetState::NOT_CONNECTED;
connection_state = EthernetState::NotConnected;

task_scheduler.scheduleOnce([this, now](){
state.get("connection_state")->updateUint((uint)connection_state);
Expand All @@ -239,7 +239,7 @@ void Ethernet::setup()

uint32_t now = millis();

connection_state = EthernetState::NOT_CONNECTED;
connection_state = EthernetState::NotConnected;

task_scheduler.scheduleOnce([this, now](){
state.get("connection_state")->updateUint((uint)connection_state);
Expand Down Expand Up @@ -268,7 +268,7 @@ void Ethernet::register_urls()
EthernetState Ethernet::get_connection_state() const
{
if (!initialized)
return EthernetState::NOT_CONFIGURED;
return EthernetState::NotConfigured;
return this->connection_state;
}

Expand Down
10 changes: 2 additions & 8 deletions software/src/modules/ethernet/ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
#include "config.h"

#include "module.h"

enum class EthernetState {
NOT_CONFIGURED = 0,
NOT_CONNECTED = 1,
CONNECTING = 2,
CONNECTED = 3
};
#include "ethernet_state.enum.h"

class Ethernet final : public IModule
{
Expand All @@ -52,5 +46,5 @@ class Ethernet final : public IModule

OwnedConfig config_in_use;
String hostname;
EthernetState connection_state = EthernetState::NOT_CONFIGURED;
EthernetState connection_state = EthernetState::NotConfigured;
};
1 change: 0 additions & 1 deletion software/src/modules/meters/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
meter_value_id.h
meter_value_id.cpp
meters_defs.h
13 changes: 13 additions & 0 deletions software/src/modules/meters/Meter Class ID.uint8.enum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
None
RS485 Bricklet
EVSE V2
Energy Manager
API
Sun Spec
Modbus TCP
SMA Speedwire

# Unofficial meter classes
Mqtt Mirror = 200
Pv Faker = 201
Meta = 202
2 changes: 1 addition & 1 deletion software/src/modules/meters/imeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <stdint.h>
#include "WString.h"
#include "meters_defs.h"
#include "meter_class_id.enum.h"
#include "config.h"

class IMeter
Expand Down
2 changes: 1 addition & 1 deletion software/src/modules/meters/meter_class_none.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Boston, MA 02111-1307, USA.
*/

#include "meters_defs.h"
#include "meter_class_id.enum.h"
#include "meter_class_none.h"

#include "gcc_warnings.h"
Expand Down
2 changes: 1 addition & 1 deletion software/src/modules/meters/meters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void Meters::pre_setup()
init_uint32_array(meter_slot.index_cache_currents, INDEX_CACHE_CURRENT_COUNT, UINT32_MAX);
}

generators.reserve(METER_CLASSES);
generators.reserve(METER_CLASS_ID_COUNT);
register_meter_generator(MeterClassID::None, &meter_generator_none);

last_reset_prototype = Config::Object({
Expand Down
37 changes: 0 additions & 37 deletions software/src/modules/meters/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,40 +272,3 @@ def format_value_id_tree(sub_tree, indent):
'{{{groups}}}': ',\n '.join(translation_groups[lang]),
'{{{fragments}}}': ',\n '.join(translation_fragments[lang]),
})

# NEVER EVER EDIT OR REMOVE IDS. Only append new ones. Changing or removing IDs is a breaking API and config change!
classes = [
'None',
'RS485 Bricklet',
'EVSE V2',
'Energy Manager',
'API',
'Sun Spec',
'Modbus TCP',
'SMA Speedwire',
]

class_values = []

for i, name in enumerate(classes):
class_values.append(' {0} = {1},\n'.format(util.FlavoredName(name).get().camel, i))

# Unofficial meter classes
class_values.append(' {0} = {1},\n'.format(util.FlavoredName('Mqtt Mirror').get().camel, 200))
class_values.append(' {0} = {1},\n'.format(util.FlavoredName('Pv Faker' ).get().camel, 201))
class_values.append(' {0} = {1},\n'.format(util.FlavoredName('Meta' ).get().camel, 202))

with open('meters_defs.h', 'w', encoding='utf-8') as f:
f.write('// WARNING: This file is generated.\n\n')
f.write('#include <stdint.h>\n\n')
f.write('#pragma once\n\n')
f.write('enum class MeterClassID : uint8_t {\n')
f.write(''.join(class_values))
f.write('};\n\n')
f.write(f'#define METER_CLASSES {len(class_values)}')

with open('../../../web/src/modules/meters/meters_defs.ts', 'w', encoding='utf-8') as f:
f.write('// WARNING: This file is generated.\n\n')
f.write('export const enum MeterClassID {\n')
f.write(''.join(class_values))
f.write('}\n')
6 changes: 3 additions & 3 deletions software/src/modules/meters_modbus_tcp/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def make_modbus_value_type(register_count, is_signed, is_float, register_order_i
return register_count | ((1 if is_signed else 0) << 3) | ((1 if is_float else 0) << 4) | ((1 if register_order_is_le else 0) << 5)


modbus_value_type = [
modbus_value_types = [
('None', 0),
('U16', make_modbus_value_type(1, False, False, False)),
('S16', make_modbus_value_type(1, True, False, False)),
Expand All @@ -47,8 +47,8 @@ def make_modbus_value_type(register_count, is_signed, is_float, register_order_i
with open('Modbus Value Type.uint8.enum', 'w', encoding='utf-8') as f:
f.write('# WARNING: This file is generated\n')

for name_and_value in modbus_value_type:
f.write(f'{name_and_value[0]} = {name_and_value[1]}\n')
for item in modbus_value_types:
f.write(f'{item[0]} = {item[1]}\n')


VALUE_ID_META = 0xFFFFFFFF - 1
Expand Down
2 changes: 1 addition & 1 deletion software/src/modules/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ void Mqtt::register_events()
// Wait 20 secs to not spam the event log with a failed connection attempt.
bool start_immediately = false;
#if MODULE_ETHERNET_AVAILABLE()
start_immediately = ethernet.get_connection_state() == EthernetState::CONNECTED;
start_immediately = ethernet.get_connection_state() == EthernetState::Connected;
#endif
if (start_immediately) {
esp_mqtt_client_start(client);
Expand Down
4 changes: 2 additions & 2 deletions software/src/modules/wifi/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ void Wifi::setup()
bool connected = (WifiState)state.get("connection_state")->asInt() == WifiState::CONNECTED;

#if MODULE_ETHERNET_AVAILABLE()
connected = connected || ethernet.get_connection_state() == EthernetState::CONNECTED;
connected = connected || ethernet.get_connection_state() == EthernetState::Connected;
#endif

static int stop_soft_ap_runs = 0;
Expand All @@ -682,7 +682,7 @@ void Wifi::setup()
},
enable_sta
#if MODULE_ETHERNET_AVAILABLE()
|| (ethernet.is_enabled() && ethernet.get_connection_state() != EthernetState::NOT_CONNECTED)
|| (ethernet.is_enabled() && ethernet.get_connection_state() != EthernetState::NotConnected)
#endif
? 30 * 1000 : 1000, 10 * 1000);
}
Expand Down
2 changes: 1 addition & 1 deletion software/web/src/modules/meters/api.ts.template_header
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MeterClassID } from "./meters_defs";
import { MeterClassID } from "./meter_class_id.enum";
{{{imports}}}

type MeterConfig =
Expand Down
4 changes: 2 additions & 2 deletions software/web/src/modules/meters/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { METERS_SLOTS } from "../../build";
import * as util from "../../ts/util";
import * as API from "../../ts/api";
import { __, translate_unchecked } from "../../ts/translation";
import { h, createRef, Fragment, Component, RefObject, ComponentChild, toChildArray } from "preact";
import { h, createRef, Fragment, Component, RefObject, ComponentChild } from "preact";
import { Button, ButtonGroup } from "react-bootstrap";
import { FormRow } from "../../ts/components/form_row";
import { InputSelect } from "../../ts/components/input_select";
Expand All @@ -33,7 +33,7 @@ import { SubPage } from "../../ts/components/sub_page";
import { UplotLoader } from "../../ts/components/uplot_loader";
import { UplotWrapper, UplotData } from "../../ts/components/uplot_wrapper";
import { MeterValueID, METER_VALUE_IDS, METER_VALUE_INFOS, METER_VALUE_ORDER } from "./meter_value_id";
import { MeterClassID } from "./meters_defs";
import { MeterClassID } from "./meter_class_id.enum";
import { MeterConfig, MeterConfigPlugin } from "./types";
import { Table } from "../../ts/components/table";
import { PageHeader } from "../../ts/components/page_header";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, Fragment, Component, ComponentChildren } from 'preact'
import { __, translate_unchecked } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterValueID, MeterValueTreeType, METER_VALUE_INFOS, METER_VALUE_TREE } from "../meters/meter_value_id";
import { MeterConfig } from "../meters/types";
import { Table, TableRow } from "../../ts/components/table";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, ComponentChildren } from "preact";
import { __, translate_unchecked } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { InputText } from "../../ts/components/input_text";
import { FormRow } from "../../ts/components/form_row";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, ComponentChildren } from "preact";
import { __, translate_unchecked } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { InputText } from "../../ts/components/input_text";
import { FormRow } from "../../ts/components/form_row";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as util from "../../ts/util";
import { __ } from "../../ts/translation";
import { METERS_SLOTS } from "../../build";

import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";

import { h, ComponentChildren } from "preact";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import * as util from "../../ts/util";
import { h, Fragment, Component, ComponentChildren } from "preact";
import { __ } from "../../ts/translation";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { MeterValueIDSelector, get_meter_value_id_name } from "../meters_api/plugin_meters_config";
import { MeterModbusTCPTableID } from "./meter_modbus_tcp_table_id.enum";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, ComponentChildren } from "preact";
import { __, translate_unchecked } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { InputText } from "../../ts/components/input_text";
import { FormRow } from "../../ts/components/form_row";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, ComponentChildren } from "preact";
import { __ } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { FormRow } from "../../ts/components/form_row";
import { InputText } from "../../ts/components/input_text";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, ComponentChildren } from "preact";
import { __, translate_unchecked } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { InputText } from "../../ts/components/input_text";
import { FormRow } from "../../ts/components/form_row";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { h, ComponentChildren } from "preact";
import { __, translate_unchecked } from "../../ts/translation";
import * as util from "../../ts/util";
import { MeterClassID } from "../meters/meters_defs";
import { MeterClassID } from "../meters/meter_class_id.enum";
import { MeterConfig } from "../meters/types";
import { InputText } from "../../ts/components/input_text";
import { FormRow } from "../../ts/components/form_row";
Expand Down
Loading

0 comments on commit 8690f5b

Please sign in to comment.