Skip to content

Commit

Permalink
Merge pull request #53 from boneIO-eu/socomec
Browse files Browse the repository at this point in the history
v0.8.0
  • Loading branch information
pszafer authored Nov 14, 2024
2 parents a4c8512 + 3d78578 commit c86c09a
Show file tree
Hide file tree
Showing 53 changed files with 3,834 additions and 959 deletions.
252 changes: 235 additions & 17 deletions boneio/bonecli.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
"""Bonecli script."""

from __future__ import annotations
import argparse
import asyncio
import logging
import sys

import os
from typing import Any

os.environ["W1THERMSENSOR_NO_KERNEL_MODULE"] = "1"

import argparse
import asyncio
import logging
import sys
from colorlog import ColoredFormatter
from yaml import MarkedYAMLError

from boneio.modbus.modbuscli import (
async_run_modbus_set,
async_run_modbus_get,
async_run_modbus_search,
)
from boneio.modbus.client import VALUE_TYPES


from boneio.const import ACTION
from boneio.helper import load_config_from_file
from boneio.helper.exceptions import ConfigurationException, RestartRequestException
from boneio.helper.exceptions import (
ConfigurationException,
RestartRequestException,
)
from boneio.helper.events import GracefulExit
from boneio.helper.logger import configure_logger
from boneio.runner import async_run
Expand Down Expand Up @@ -49,30 +61,167 @@ def get_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="boneIO app for BeagleBone Black.",
)
parser.add_argument(ACTION, type=str, default="run")
parser.add_argument("--version", action="version", version=__version__)
parser.add_argument(
subparsers = parser.add_subparsers(dest=ACTION, required=True)
run_parser = subparsers.add_parser("run")
run_parser.add_argument(
"--debug",
"-d",
action="count",
help="Start boneIO in debug mode",
default=0,
)
run_parser.add_argument(
"-c",
"--config",
metavar="path_to_config_dir",
default="./config.yaml",
help="File which contains boneIO configuration",
)
parser.add_argument(
"--debug", "-d", action="count", help="Start boneIO in debug mode", default=0
run_parser.add_argument(
"--mqttusername",
help="Mqtt username to use if you don't want provide in file.",
)
run_parser.add_argument(
"--mqttpassword",
help="Mqtt password to use if you don't want provide in file.",
)
modbus_parser = subparsers.add_parser("modbus")
modbus_parser.add_argument(
"--debug",
"-d",
action="count",
help="Start boneIO in debug mode",
default=0,
)
modbus_parser.add_argument(
"--uart",
type=str,
choices=["uart1", "uart4"],
help="Choose UART",
required=True,
)
modbus_parser.add_argument(
"--address",
type=lambda x: int(x, 0),
required=False,
default=1,
help="Current device address (hex or integer)",
)
modbus_parser.add_argument(
"--baudrate",
type=int,
choices=[2400, 4800, 9600, 14400, 19200],
required=True,
help="Current baudrate",
)
parser.add_argument(
"--mqttusername", help="Mqtt username to use if you don't want provide in file."

modbus_parser.add_argument(
"--bytesize",
type=int,
required=False,
default=8,
help="Bytesize",
)

modbus_parser.add_argument(
"--stopbits",
type=int,
required=False,
default=1,
help="stopbits",
)

modbus_parser.add_argument(
"--parity",
type=str,
choices=["P", "E", "N"],
default="N",
required=False,
help="Parity",
)

modbus_sub_parser = modbus_parser.add_subparsers(
dest="modbus_action", required=True
)
parser.add_argument(
"--mqttpassword", help="Mqtt password to use if you don't want provide in file."
set_modbus_parser = modbus_sub_parser.add_parser("set")
set_modbus_parser.add_argument(
"--custom-value",
type=int,
help="Set Custom value",
)
set_modbus_parser.add_argument(
"--custom-register-address",
type=int,
help="Register address for custom value",
)
set_modbus_parser.add_argument(
"--device",
type=str,
choices=["cwt", "r4dcb08", "liquid-sensor", "sht20", "custom"],
help="Choose device to set modbus address/baudrate. For custom you must provide --custom-value and --custom-register-address",
required=True,
)
set_modbus_parser_group = set_modbus_parser.add_mutually_exclusive_group()
set_modbus_parser_group.add_argument(
"--new-address",
type=lambda x: int(x, 0),
help="Set new address (hex or integer / 1 - 253/)",
)

set_modbus_parser_group.add_argument(
"--new-baudrate",
type=int,
choices=[1200, 2400, 4800, 9600, 19200],
help="Choose new baudrate to set. CWT doesn't work on 1200.",
)

get_modbus_parser = modbus_sub_parser.add_parser("get")
get_modbus_parser.add_argument(
"--register-address",
type=int,
help="Register address",
required=True,
)
get_modbus_parser.add_argument(
"--register-type",
type=str,
choices=["input", "holding"],
help="Register type",
required=True,
)
get_modbus_parser.add_argument(
"--value-type",
type=str,
choices=VALUE_TYPES.keys(),
help="Value types",
required=True,
)
parser.add_argument("--version", action="version", version=__version__)
search_modbus_parser = modbus_sub_parser.add_parser(
name="search",
help="Search for device. Iterate over every address 1-253 with provided register address",
)
search_modbus_parser.add_argument(
"--register-address",
type=int,
help="Register address",
required=True,
)
search_modbus_parser.add_argument(
"--register-type",
type=str,
choices=["input", "holding"],
help="Register type",
required=True,
)
arguments = parser.parse_args()

return arguments


def run(config: str, debug: int, mqttusername: str = "", mqttpassword: str = "") -> int:
def run(
config: str, debug: int, mqttusername: str = "", mqttpassword: str = ""
) -> int:
"""Run BoneIO."""
_LOGGER.info("BoneIO %s starting.", __version__)
try:
Expand All @@ -99,6 +248,66 @@ def run(config: str, debug: int, mqttusername: str = "", mqttpassword: str = "")
return 1


def run_modbus_command(
args: argparse.Namespace,
) -> int:
"""Run BoneIO."""
_LOGGER.info("BoneIO %s starting.", __version__)
try:
configure_logger(log_config={}, debug=args.debug)
ret = 0
if args.modbus_action == "set":
ret = asyncio.run(
async_run_modbus_set(
device=args.device,
uart=args.uart,
address=args.address,
baudrate=args.baudrate,
parity=args.parity,
bytesize=args.bytesize,
stopbits=args.stopbits,
new_baudrate=args.new_baudrate,
new_address=args.new_address,
custom_address=args.custom_register_address,
custom_value=args.custom_value,
),
)
elif args.modbus_action == "get":
ret = asyncio.run(
async_run_modbus_get(
uart=args.uart,
device_address=args.address,
baudrate=args.baudrate,
register_address=args.register_address,
register_type=args.register_type,
parity=args.parity,
bytesize=args.bytesize,
stopbits=args.stopbits,
value_type=args.value_type,
),
)
else:
ret = asyncio.run(
async_run_modbus_search(
uart=args.uart,
baudrate=args.baudrate,
register_address=args.register_address,
register_type=args.register_type,
parity=args.parity,
bytesize=args.bytesize,
stopbits=args.stopbits,
),
)
return ret
except (RestartRequestException, GracefulExit) as err:
if err is not None:
_LOGGER.info(err)
return 0
except (ConfigurationException, MarkedYAMLError) as err:
_LOGGER.error("Failed to load config. %s Exiting.", err)
return 1


def main() -> int:
"""Start boneIO."""

Expand All @@ -113,7 +322,16 @@ def main() -> int:
mqttpassword=args.mqttpassword,
debug=debug,
)
_LOGGER.info("Exiting with exit code %s", exit_code)
elif args.action == "modbus":
_LOGGER.info("BoneIO Modbus helper %s .", __version__)
exit_code = run_modbus_command(
args=args,
)

if exit_code == 0:
_LOGGER.info("Exiting with exit code %s", exit_code)
else:
_LOGGER.error("Exiting with exit code %s", exit_code)
return exit_code


Expand Down
56 changes: 55 additions & 1 deletion boneio/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
"uart5": {ID: "/dev/ttyS5", TX: "P8.37", RX: "P8.38"},
}

relay_actions = {ON: "async_turn_on", OFF: "async_turn_off", TOGGLE: "async_toggle"}
relay_actions = {
ON: "async_turn_on",
OFF: "async_turn_off",
TOGGLE: "async_toggle",
}

# HA CONSTS
HOMEASSISTANT = "homeassistant"
Expand Down Expand Up @@ -160,3 +164,53 @@
}

INA219 = "ina219"
PINS = {
"P9_22": {"chip": 0, "line": 2},
"P9_21": {"chip": 0, "line": 3},
"P9_18": {"chip": 0, "line": 4},
"P9_17": {"chip": 0, "line": 5},
"P8_35": {"chip": 0, "line": 8},
"P8_33": {"chip": 0, "line": 9},
"P8_31": {"chip": 0, "line": 10},
"P8_32": {"chip": 0, "line": 11},
"P8_19": {"chip": 0, "line": 22},
"P8_13": {"chip": 0, "line": 23},
"P8_14": {"chip": 0, "line": 26},
"P8_17": {"chip": 0, "line": 27},
"P8_12": {"chip": 1, "line": 12},
"P8_11": {"chip": 1, "line": 13},
"P8_16": {"chip": 1, "line": 14},
"P8_15": {"chip": 1, "line": 15},
"P9_23": {"chip": 1, "line": 17},
"P9_14": {"chip": 1, "line": 18},
"P9_16": {"chip": 1, "line": 19},
"P8_26": {"chip": 1, "line": 29},
"P8_18": {"chip": 2, "line": 1},
"P8_7": {"chip": 2, "line": 2},
"P8_8": {"chip": 2, "line": 3},
"P8_10": {"chip": 2, "line": 4},
"P8_9": {"chip": 2, "line": 5},
"P8_45": {"chip": 2, "line": 6},
"P8_46": {"chip": 2, "line": 7},
"P8_43": {"chip": 2, "line": 8},
"P8_44": {"chip": 2, "line": 9},
"P8_41": {"chip": 2, "line": 10},
"P8_42": {"chip": 2, "line": 11},
"P8_39": {"chip": 2, "line": 12},
"P8_40": {"chip": 2, "line": 13},
"P8_37": {"chip": 2, "line": 14},
"P8_38": {"chip": 2, "line": 15},
"P8_36": {"chip": 2, "line": 16},
"P8_34": {"chip": 2, "line": 17},
"P8_27": {"chip": 2, "line": 22},
"P8_29": {"chip": 2, "line": 23},
"P8_28": {"chip": 2, "line": 24},
"P8_30": {"chip": 2, "line": 25},
"P9_31": {"chip": 3, "line": 14},
"P9_29": {"chip": 3, "line": 15},
"P9_30": {"chip": 3, "line": 16},
"P9_28": {"chip": 3, "line": 17},
"P9_27": {"chip": 3, "line": 19},
"P9_25": {"chip": 3, "line": 21},
}
NAME = "name"
Loading

0 comments on commit c86c09a

Please sign in to comment.