Skip to content

Commit

Permalink
Getting the development docker commands to work in the make file. Add…
Browse files Browse the repository at this point in the history
…ing USBPort so I can test with my hardware (jblance#334)
  • Loading branch information
rossandrews authored Apr 11, 2023
1 parent 28cada4 commit 812c647
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 41 deletions.
47 changes: 47 additions & 0 deletions docker-compose.development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
services:
mosquitto:
image: eclipse-mosquitto
network_mode: host
volumes:
- ./docker/mosquitto/config:/mosquitto/config
- ./docker/mosquitto/data:/mosquitto/data
- ./docker/mosquitto/log:/mosquitto/log

mppsolar:
network_mode: host
devices:
- /dev/hidraw0:/dev/hidraw0
volumes:
#Bind over the configuration
- type: bind
source: ./docker/dev/config/
target: /config
#Bind the code
# - type: bind
# source: ../..
# target: /mpp-solar
build:
context: ./
dockerfile: docker/dev/Dockerfile
entrypoint:
- powermon
- -C
- /config/powermon.yaml #Available due to volume binding above
- --debug
# - sleep
# - infinity
depends_on:
- mosquitto

# mppsolar-api:
# network_mode: host
# build:
# context: api
# dockerfile: Dockerfile.development
# entrypoint:
# - node
# - index.js
# depends_on:
# - mppsolar


6 changes: 3 additions & 3 deletions docker/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ FROM python:latest
RUN apt-get update
RUN apt-get install -y pkg-config libsystemd-dev gcc

RUN pip install paho-mqtt systemd-python pymongo
RUN pip install paho-mqtt systemd-python pymongo cysystemd

RUN apt-get -y install libpq-dev
RUN pip install psycopg2

COPY . /mpp-solar/
RUN pip install -e /mpp-solar/
COPY ../.. /mpp-solar/
RUN pip install -e /mpp-solar/
File renamed without changes.
19 changes: 19 additions & 0 deletions docker/dev/config/powermon.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
device:
name: Test_Inverter
id: 123456789
model: 8048MAX
manufacturer: MPP-Solar
port:
baud: 2400
path: /dev/hidraw0
type: USB
protocol: PI30
commands:
- command: QPGS0
outputs:
- name: screen
format: hass
entity_id_prefix: inverter42
discovery_prefix: homeassistant
filter: ^serial|^work|^charger|^fault
loop: 60
32 changes: 0 additions & 32 deletions docker/dev/docker-compose.yaml

This file was deleted.

36 changes: 36 additions & 0 deletions docker/powermon.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# yaml config for powermon
device:
name: Test_Inverter
id: 123456789
model: 1012LV-MK
manufacturer: MPP-Solar
port:
baud: 2400
path: /dev/hidraw0
type: USB
protocol: PI30

#Separate the schedule from the device definition
scheduling:
loop: 10
adhoc_command_topic: mpp-solar/api-client
schedules:
- name: QPIGS_5_minutes
type: loop
loopCount: 6 #will run every 1 minute
commands:
- command: QPIGS
type: basic #default command type is basic
outputs:
- type: mqtt
topic: Test_Inverter
tag: QPIGS
mqttbroker:
name: localhost
port: 1883
user: null
pass: null

daemon:
type: systemd
keepalive: 100
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ pypi-upload:
docker-up:
docker-compose up --build

dockerdev-up:
docker-powermon-dev-up:
docker compose -f docker-compose.development.yaml up --build
10 changes: 6 additions & 4 deletions mppsolar/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import configparser
#import configparser

version = configparser.ConfigParser()
version.read("pyproject.toml")
__version__ = version["tool.poetry"]["version"].replace('"', "")
#version = configparser.ConfigParser()
#version.read("pyproject.toml")
#__version__ = version["tool.poetry"]["version"].replace('"', "")

__version__ = "0.15.30"
2 changes: 1 addition & 1 deletion powermon/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def format_data(*args, **kwargs):

formatter = get_kwargs(kwargs, "formatter")

log.info(f"attempting to create output processor: {formatter}")
log.info(f"attempting to create format processor: {formatter}")
try:
_module = importlib.import_module("powermon.formats." + formatter, ".")
_class = getattr(_module, formatter)
Expand Down
89 changes: 89 additions & 0 deletions powermon/ports/usbport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import logging
import os
import time

from mppsolar.helpers import get_kwargs

from .port import Port

log = logging.getLogger("USBPort")


class usbport(Port):
def __init__(self, *args, **kwargs) -> None:
config = get_kwargs(kwargs, "config")
self.path = config["path"]
self.protocol = config["protocol"]
log.debug(f"Initializing usb port. path:{self.path}, protocol: {self.protocol}")

def protocol(self):
return self.protocol

def connect(self) -> None:
log.debug(f"USBPort connecting. path:{self.path}, protocol: {self.protocol}")
try:
self.port = os.open(self.path, os.O_RDWR | os.O_NONBLOCK)
log.debug(f"USBPort port number ${self.port}")
except Exception as e:
log.warning(f"Error openning usb port: {e}")
self.error = e
return

def disconnect(self) -> None:
log.debug(f"USBPort disconnecting {self.port}")

def send_and_receive(self, *args, **kwargs) -> dict:
full_command = get_kwargs(kwargs, "full_command")
response_line = bytes()
self.connect()

# Send the command to the open usb connection
to_send = full_command
try:
log.debug(f"length of to_send: {len(to_send)}")
except: # noqa: E722
import pdb

pdb.set_trace()
if len(to_send) <= 8:
# Send all at once
log.debug("1 chunk send")
time.sleep(0.35)
try:
os.write(self.port, to_send)
except Exception as e:
log.debug("USB read error: {}".format(e))

elif len(to_send) > 8 and len(to_send) < 11:
log.debug("2 chunk send")
time.sleep(0.35)
os.write(self.port, to_send[:5])
time.sleep(0.35)
os.write(self.port, to_send[5:])
else:
while len(to_send) > 0:
log.debug("multiple chunk send")
# Split the byte command into smaller chucks
send, to_send = to_send[:8], to_send[8:]
log.debug("send: {}, to_send: {}".format(send, to_send))
time.sleep(0.35)
os.write(self.port, send)
time.sleep(0.25)
# Read from the usb connection
# try to a max of 100 times
for x in range(100):
# attempt to deal with resource busy and other failures to read
try:
time.sleep(0.15)
r = os.read(self.port, 256)
response_line += r
except Exception as e:
log.debug("USB read error: {}".format(e))
# Finished is \r is in byte_response
if bytes([13]) in response_line:
# remove anything after the \r
response_line = response_line[: response_line.find(bytes([13])) + 1]
break
log.debug("usb response was: %s", response_line)
os.close(self.port)
return response_line

0 comments on commit 812c647

Please sign in to comment.