Skip to content

Commit

Permalink
Move common Modbus/TCP client support files to general module
Browse files Browse the repository at this point in the history
To be used from future Modbus/TCP Battery module too.
  • Loading branch information
photron committed Dec 5, 2024
1 parent 098ac3f commit f744381
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 63 deletions.
1 change: 1 addition & 0 deletions software/energy_manager.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ custom_frontend_modules = Device Name
Energy Manager
EM V1
Power Manager
Modbus TCP Client
Meters
Meters EM
Meters API
Expand Down
1 change: 1 addition & 0 deletions software/energy_manager_v2.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ custom_frontend_modules = Device Name
Energy Manager
EM V2
Power Manager
Modbus TCP Client
Meters
Meters EM
Meters API
Expand Down
58 changes: 41 additions & 17 deletions software/pio_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,28 +474,51 @@ def hyphenate_translation(translation, parent_key=None, lang=None):
def repair_rtc_dir():
path = os.path.abspath("src/modules/rtc")

try:
os.remove(os.path.join(path, "real_time_clock_v2_bricklet_firmware_bin.digest"))
os.remove(os.path.join(path, "real_time_clock_v2_bricklet_firmware_bin.embedded.cpp"))
os.remove(os.path.join(path, "real_time_clock_v2_bricklet_firmware_bin.embedded.h"))
except:
pass
for name in ["real_time_clock_v2_bricklet_firmware_bin.digest",
"real_time_clock_v2_bricklet_firmware_bin.embedded.cpp",
"real_time_clock_v2_bricklet_firmware_bin.embedded.h"]:
try:
os.remove(os.path.join(path, name))
except FileNotFoundError:
pass

def repair_firmware_update_dir():
path = os.path.abspath("src/modules/firmware_update")

try:
os.remove(os.path.join(path, "recovery_html.digest"))
os.remove(os.path.join(path, "recovery_html.embedded.cpp"))
os.remove(os.path.join(path, "recovery_html.embedded.h"))
except:
pass
for name in ["recovery_html.digest",
"recovery_html.embedded.cpp",
"recovery_html.embedded.h",
"signature_public_key.embedded.cpp",
"signature_public_key.embedded.h"]:
try:
os.remove(os.path.join(path, name))
except FileNotFoundError:
pass

try:
os.remove(os.path.join(path, "signature_public_key.embedded.cpp"))
os.remove(os.path.join(path, "signature_public_key.embedded.h"))
except:
pass
def repair_meters_modbus_tcp_dir():
path = os.path.abspath("src/modules/meters_modbus_tcp")

for name in ["modbus_register_type.enum.cpp",
"modbus_register_type.enum.h",
"modbus_register_address_mode.enum.cpp",
"modbus_register_address_mode.enum.h",
"Modbus Value Type.uint8.enum",
"modbus_value_type.enum.cpp",
"modbus_value_type.enum.h"]:
try:
os.remove(os.path.join(path, name))
except FileNotFoundError:
pass

path = os.path.abspath("web/src/modules/meters_modbus_tcp")

for name in ["modbus_register_type.enum.ts",
"modbus_register_address_mode.enum.ts",
"modbus_value_type.enum.ts"]:
try:
os.remove(os.path.join(path, name))
except FileNotFoundError:
pass

def find_module_space(modules, name_space):
index = 0
Expand All @@ -518,6 +541,7 @@ def main():

repair_rtc_dir()
repair_firmware_update_dir()
repair_meters_modbus_tcp_dir()

check_call([env.subst('$PYTHONEXE'), "-u", "update_packages.py"])

Expand Down
1 change: 0 additions & 1 deletion software/src/modules/meters_modbus_tcp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Modbus Value Type.uint8.enum
meters_modbus_tcp_defs.inc
4 changes: 2 additions & 2 deletions software/src/modules/meters_modbus_tcp/meter_modbus_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

#include "event_log_prefix.h"
#include "module_dependencies.h"
#include "modbus_tcp_tools.h"
#include "modbus_register_address_mode.enum.h"
#include "modules/modbus_tcp_client/modbus_tcp_tools.h"
#include "modules/modbus_tcp_client/modbus_register_address_mode.enum.h"
#include "meters_modbus_tcp_defs.inc"

#include "gcc_warnings.h"
Expand Down
6 changes: 3 additions & 3 deletions software/src/modules/meters_modbus_tcp/meter_modbus_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

#include <stdint.h>

#include "generic_modbus_tcp_client.h"
#include "modules/modbus_tcp_client/generic_modbus_tcp_client.h"
#include "modules/meters/imeter.h"
#include "modules/meters/meter_value_id.h"
#include "config.h"
#include "meters_modbus_tcp.h"
#include "modbus_register_type.enum.h"
#include "modbus_value_type.enum.h"
#include "modules/modbus_tcp_client/modbus_register_type.enum.h"
#include "modules/modbus_tcp_client/modbus_value_type.enum.h"
#include "meter_modbus_tcp_table_id.enum.h"
#include "sungrow_hybrid_inverter_virtual_meter.enum.h"
#include "sungrow_string_inverter_virtual_meter.enum.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "meter_modbus_tcp.h"
#include "modules/meters/meter_value_id.h"
#include "tools.h"
#include "modbus_register_address_mode.enum.h"
#include "modules/modbus_tcp_client/modbus_register_address_mode.enum.h"

#include "gcc_warnings.h"

Expand Down
31 changes: 0 additions & 31 deletions software/src/modules/meters_modbus_tcp/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,6 @@

from software import util

def make_modbus_value_type(register_count, is_signed, is_float, register_order_is_le):
assert 1 <= register_count <= 4, register_count

# bit [0..2] bit [3] bit [4] bit [5]
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_types = [
('None', 0),
('U16', make_modbus_value_type(1, False, False, False)),
('S16', make_modbus_value_type(1, True, False, False)),
('U32BE', make_modbus_value_type(2, False, False, False)),
('U32LE', make_modbus_value_type(2, False, False, True)),
('S32BE', make_modbus_value_type(2, True, False, False)),
('S32LE', make_modbus_value_type(2, True, False, True)),
('F32BE', make_modbus_value_type(2, True, True, False)),
('F32LE', make_modbus_value_type(2, True, True, True)),
('U64BE', make_modbus_value_type(4, False, False, False)),
('U64LE', make_modbus_value_type(4, False, False, True)),
('S64BE', make_modbus_value_type(4, True, False, False)),
('S64LE', make_modbus_value_type(4, True, False, True)),
('F64BE', make_modbus_value_type(4, True, True, False)),
('F64LE', make_modbus_value_type(4, True, True, True)),
]

with open('Modbus Value Type.uint8.enum', 'w', encoding='utf-8') as f:
f.write('# WARNING: This file is generated\n')

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

specs = sungrow.specs + solarmax.specs + victron_energy.specs + deye.specs + alpha_ess.specs + shelly.specs + goodwe.specs \
+ solax.specs + fronius.specs + hailei.specs + fox_ess.specs + siemens.specs + carlo_gavazzi.specs
spec_values = []
Expand Down
2 changes: 1 addition & 1 deletion software/src/modules/meters_sun_spec/meter_sun_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "event_log_prefix.h"
#include "module_dependencies.h"
#include "modules/meters_modbus_tcp/modbus_tcp_tools.h"
#include "modules/modbus_tcp_client/modbus_tcp_tools.h"
#include "modules/meters_sun_spec/models/model_001.h"

#include "gcc_warnings.h"
Expand Down
4 changes: 2 additions & 2 deletions software/src/modules/meters_sun_spec/meter_sun_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <stdint.h>

#include "modules/meters/imeter.h"
#include "modules/meters_modbus_tcp/generic_modbus_tcp_client.h"
#include "modules/meters_modbus_tcp/modbus_tcp_tools.h"
#include "modules/modbus_tcp_client/generic_modbus_tcp_client.h"
#include "modules/modbus_tcp_client/modbus_tcp_tools.h"
#include "config.h"
#include "model_parser.h"

Expand Down
2 changes: 1 addition & 1 deletion software/src/modules/meters_sun_spec/meters_sun_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "module.h"
#include "modules/meters/meter_generator.h"
#include "modules/meters_modbus_tcp/modbus_tcp_tools.h"
#include "modules/modbus_tcp_client/modbus_tcp_tools.h"
#include "config.h"

#if defined(__GNUC__)
Expand Down
1 change: 1 addition & 0 deletions software/src/modules/modbus_tcp_client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Modbus Value Type.uint8.enum
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <TFModbusTCPClient.h>
#include <TFModbusTCPClientPool.h>

#include "modules/modbus_tcp_client/generic_tcp_client_pool_connector.h"
#include "generic_tcp_client_pool_connector.h"
#include "modbus_register_type.enum.h"
#include "tools.h"

Expand Down
37 changes: 37 additions & 0 deletions software/src/modules/modbus_tcp_client/prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys
import tinkerforge_util as tfutil

tfutil.create_parent_module(__file__, 'software')

from software import util

def make_modbus_value_type(register_count, is_signed, is_float, register_order_is_le):
assert 1 <= register_count <= 4, register_count

# bit [0..2] bit [3] bit [4] bit [5]
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_types = [
('None', 0),
('U16', make_modbus_value_type(1, False, False, False)),
('S16', make_modbus_value_type(1, True, False, False)),
('U32BE', make_modbus_value_type(2, False, False, False)),
('U32LE', make_modbus_value_type(2, False, False, True)),
('S32BE', make_modbus_value_type(2, True, False, False)),
('S32LE', make_modbus_value_type(2, True, False, True)),
('F32BE', make_modbus_value_type(2, True, True, False)),
('F32LE', make_modbus_value_type(2, True, True, True)),
('U64BE', make_modbus_value_type(4, False, False, False)),
('U64LE', make_modbus_value_type(4, False, False, True)),
('S64BE', make_modbus_value_type(4, True, False, False)),
('S64LE', make_modbus_value_type(4, True, False, True)),
('F64BE', make_modbus_value_type(4, True, True, False)),
('F64LE', make_modbus_value_type(4, True, True, True)),
]

with open('Modbus Value Type.uint8.enum', 'w', encoding='utf-8') as f:
f.write('# WARNING: This file is generated\n')

for item in modbus_value_types:
f.write(f'{item[0]} = {item[1]}\n')
1 change: 1 addition & 0 deletions software/warp.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ custom_backend_modules = ESP32 Brick

custom_frontend_modules = Device Name
EVSE Common
Modbus TCP Client
Meters
Meters RS485 Bricklet
Meters API
Expand Down
1 change: 1 addition & 0 deletions software/warp2.ini
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ custom_backend_modules = ESP32 Ethernet Brick
custom_frontend_modules = Device Name
EVSE Common
EVSE V2
Modbus TCP Client
Meters
Meters EVSE V2
Meters API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ 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";
import { ModbusRegisterType } from "./modbus_register_type.enum";
import { ModbusRegisterAddressMode } from "./modbus_register_address_mode.enum";
import { ModbusValueType } from "./modbus_value_type.enum";
import { ModbusRegisterType } from "../modbus_tcp_client/modbus_register_type.enum";
import { ModbusRegisterAddressMode } from "../modbus_tcp_client/modbus_register_address_mode.enum";
import { ModbusValueType } from "../modbus_tcp_client/modbus_value_type.enum";
import { SungrowHybridInverterVirtualMeter } from "./sungrow_hybrid_inverter_virtual_meter.enum";
import { SungrowStringInverterVirtualMeter } from "./sungrow_string_inverter_virtual_meter.enum";
import { SolarmaxMaxStorageVirtualMeter } from "./solarmax_max_storage_virtual_meter.enum";
Expand Down
1 change: 1 addition & 0 deletions software/web/src/modules/modbus_tcp_client/module.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
; Use empty module.ini to make git create this directory for pio_hooks.py to generate files into

0 comments on commit f744381

Please sign in to comment.