summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-05-17 04:06:18 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-05-17 04:06:18 +0300
commitc0111bf4d3dd91f54d27346970e4c6e0a1ce357e (patch)
treebeb15167412bc3ed60e3e11e9076d27ea6f437e5 /src
parent893e21cc83ee1ecf236a005f1bf4893448e9b3ea (diff)
pio: products refactoring
Diffstat (limited to 'src')
-rw-r--r--src/home/mqtt/temphum.py6
-rw-r--r--src/home/pio/__init__.py1
-rw-r--r--src/home/pio/exceptions.py2
-rw-r--r--src/home/pio/products.py108
-rw-r--r--src/pio_build.py4
-rwxr-xr-xsrc/pio_ini.py58
6 files changed, 179 insertions, 0 deletions
diff --git a/src/home/mqtt/temphum.py b/src/home/mqtt/temphum.py
index 83886ac..44810ef 100644
--- a/src/home/mqtt/temphum.py
+++ b/src/home/mqtt/temphum.py
@@ -19,6 +19,12 @@ class MqttTempHumNodes(HashableEnum):
KBN_BH_1FL_BEDROOM = auto()
KBN_BH_1FL_BATHROOM = auto()
+ KBN_NH_1FL_INV = auto()
+ KBN_NH_1FL_CENTER = auto()
+ KBN_NH_1LF_KT = auto()
+ KBN_NH_1FL_DS = auto()
+ KBN_NH_1FS_EZ = auto()
+
SPB_FLAT120_CABINET = auto()
diff --git a/src/home/pio/__init__.py b/src/home/pio/__init__.py
new file mode 100644
index 0000000..7216bc4
--- /dev/null
+++ b/src/home/pio/__init__.py
@@ -0,0 +1 @@
+from .products import get_products, platformio_ini \ No newline at end of file
diff --git a/src/home/pio/exceptions.py b/src/home/pio/exceptions.py
new file mode 100644
index 0000000..a6afd20
--- /dev/null
+++ b/src/home/pio/exceptions.py
@@ -0,0 +1,2 @@
+class ProductConfigNotFoundError(Exception):
+ pass
diff --git a/src/home/pio/products.py b/src/home/pio/products.py
new file mode 100644
index 0000000..4cb3cf2
--- /dev/null
+++ b/src/home/pio/products.py
@@ -0,0 +1,108 @@
+import os
+import logging
+
+from io import StringIO
+from collections import OrderedDict
+
+
+_logger = logging.getLogger(__name__)
+_products_dir = os.path.join(
+ os.path.dirname(__file__),
+ '..', '..', '..',
+ 'platformio'
+)
+
+
+def get_products():
+ products = []
+ for f in os.listdir(_products_dir):
+ # temp hack
+ if f.endswith('-esp01'):
+ continue
+ # skip the common dir
+ if f in ('common',):
+ continue
+
+ if os.path.isdir(os.path.join(_products_dir, f)):
+ products.append(f)
+
+ return products
+
+
+def platformio_ini(product_config: dict,
+ target: str,
+ node_id: str,
+ platform: str,
+ framework: str = 'arduino',
+ upload_port: str = '/dev/ttyUSB0',
+ monitor_speed: int = 115200,
+ debug=False,
+ debug_network=False) -> str:
+ # defines
+ defines = {
+ **product_config['common_defines'],
+ 'CONFIG_NODE_ID': node_id,
+ 'CONFIG_WIFI_AP_SSID': ('HK_'+node_id)[:31]
+ }
+ try:
+ defines.update(product_config['target_defines'][target])
+ except KeyError:
+ pass
+ defines['CONFIG_NODE_SECRET_SIZE'] = len(defines['CONFIG_NODE_SECRET'])
+ defines['CONFIG_MQTT_CLIENT_ID'] = node_id
+
+ build_type = 'release'
+ if debug:
+ defines['DEBUG'] = True
+ build_type = 'debug'
+ if debug_network:
+ defines['DEBUG'] = True
+ defines['DEBUG_ESP_SSL'] = True
+ defines['DEBUG_ESP_PORT'] = 'Serial'
+ build_type = 'debug'
+ defines = OrderedDict(sorted(defines.items(), key=lambda t: t[0]))
+
+ # libs
+ libs = []
+ if 'common_libs' in product_config:
+ libs.extend(product_config['common_libs'])
+ if 'target_libs' in product_config and target in product_config['target_libs']:
+ libs.extend(product_config['target_libs'][target])
+ libs = list(set(libs))
+ libs.sort()
+
+ try:
+ target_real_name = product_config['target_board_names'][target]
+ except KeyError:
+ target_real_name = target
+
+ buf = StringIO()
+
+ buf.write('; Generated by pio_ini.py\n\n')
+ buf.write(f'[env:{target_real_name}]\n')
+ buf.write(f'platform = {platform}\n')
+ buf.write(f'board = {target_real_name}\n')
+ buf.write(f'framework = {framework}\n')
+ buf.write(f'upload_port = {upload_port}\n')
+ buf.write(f'monitor_speed = {monitor_speed}\n')
+ if libs:
+ buf.write(f'lib_deps =')
+ for lib in libs:
+ buf.write(f' {lib}\n')
+ buf.write(f'build_flags =\n')
+ if defines:
+ for name, value in defines.items():
+ buf.write(f' -D{name}')
+ if type(value) is not bool:
+ buf.write('=')
+ if type(value) is str:
+ buf.write('"\\"')
+ value = value.replace('"', '\\"')
+ buf.write(f'{value}')
+ if type(value) is str:
+ buf.write('"\\"')
+ buf.write('\n')
+ buf.write(f' -I../common/include')
+ buf.write(f'\nbuild_type = {build_type}')
+
+ return buf.getvalue()
diff --git a/src/pio_build.py b/src/pio_build.py
new file mode 100644
index 0000000..1916e5e
--- /dev/null
+++ b/src/pio_build.py
@@ -0,0 +1,4 @@
+#!/usr/bin/env python3
+
+if __name__ == '__main__':
+ print('TODO') \ No newline at end of file
diff --git a/src/pio_ini.py b/src/pio_ini.py
new file mode 100755
index 0000000..2d9c7c6
--- /dev/null
+++ b/src/pio_ini.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+import os
+import yaml
+
+from argparse import ArgumentParser, ArgumentError
+from home.pio import get_products, platformio_ini
+from home.pio.exceptions import ProductConfigNotFoundError
+
+
+def get_config(product: str) -> dict:
+ config_path = os.path.join(
+ os.getenv('HOME'), '.config',
+ 'homekit_pio', f'{product}.yaml'
+ )
+ if not os.path.exists(config_path):
+ raise ProductConfigNotFoundError(f'{config_path}: product config not found')
+
+ with open(config_path, 'r') as f:
+ return yaml.safe_load(f)
+
+
+if __name__ == '__main__':
+ products = get_products()
+ parser = ArgumentParser()
+ parser.add_argument('--product', type=str, choices=products,
+ help='PIO product name')
+ parser.add_argument('--target', type=str, required=True,
+ help='PIO build target')
+ parser.add_argument('--node-id', type=str)
+ parser.add_argument('--platform', default='espressif8266', type=str)
+ parser.add_argument('--framework', default='arduino', type=str)
+ parser.add_argument('--upload-port', default='/dev/ttyUSB0', type=str)
+ parser.add_argument('--monitor-speed', default=115200)
+ parser.add_argument('--debug', action='store_true')
+ parser.add_argument('--debug-network', action='store_true')
+ arg = parser.parse_args()
+
+ if not arg.product:
+ product = os.path.basename(os.path.realpath(os.getcwd()))
+ if product not in products:
+ raise ArgumentError(None, 'invalid product')
+ else:
+ product = arg.product
+
+ product_config = get_config(product)
+ if arg.target not in product_config['targets']:
+ raise ArgumentError(None, f'target {arg.target} not found for product {product}')
+
+ ini = platformio_ini(product_config=product_config,
+ target=arg.target,
+ node_id=arg.node_id,
+ platform=arg.platform,
+ framework=arg.framework,
+ upload_port=arg.upload_port,
+ monitor_speed=arg.monitor_speed,
+ debug=arg.debug,
+ debug_network=arg.debug_network)
+ print(ini)