Skip to content

Commit

Permalink
cleanup of code, TODOs and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
schwabix-1311 committed Feb 6, 2025
1 parent 2b8c250 commit 38660c6
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 81 deletions.
5 changes: 2 additions & 3 deletions aquaPi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@


def create_app() -> Flask:
# TODO wrap in try/catch, but how should exceptions be handled?
app = Flask(__name__, instance_relative_config=True)

config_file = path.join(app.instance_path, "log_config.json")
Expand Down Expand Up @@ -125,7 +124,7 @@ def create_app() -> Flask:
CONFIG=os.path.join(app.instance_path, cfg_file)
)

# FIXME: this would use app.debug before assignment
# FIXME: move this to the alert driver
# if False:
# mail_handler = SMTPHandler(
# mailhost='127.0.0.1',
Expand Down Expand Up @@ -182,7 +181,7 @@ def create_app() -> Flask:
log.fatal("Fatal error in App.__init__. Subsequent errors are a side effect.")
return None

#FIXME bus is used by Python code in jinja template 'settings'
#FIXME bus is used by jinja template 'settings' only
@app.context_processor
def inject_globals():
return dict(bus=app.extensions['machineroom'].bus)
Expand Down
2 changes: 1 addition & 1 deletion aquaPi/driver/DriverOneWire.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DriverDS1820(AInDriver):
def find_ports() -> dict[str, IoPort]:
io_ports = {}
if is_raspi():
# TODO: GPIO 4 is the Raspi default, allow alternatives!
# TODO: GPIO 4 is the Raspi default, auto-detect alternatives!
deps = ['GPIO 4 in', 'GPIO 4 out']

for sensor in glob.glob('/sys/bus/w1/devices/28-*'):
Expand Down
61 changes: 0 additions & 61 deletions aquaPi/driver/DriverPWM.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,64 +110,3 @@ def write(self, value:float) -> None:
with open(path.join(self._pwmchannel, 'enable'), 'wt', encoding='ascii') as p:
p.write('1' if value > 0 else '0')
self._val = float(value)


dummy = '''if False:
Review code to utilize the IoPorts.dependants. This solves implicit use of GPIO port.
class DriverSoftPWM(DriverPWMbase):
""" one PWM channel, software PWM on arbitrary GPIO port, unprecise!
This is problematic, as it uses GPIO ports, thus should be handled by DriverGPIO, but then
the PortFunc.IO is unexpected. Make PortFunc a set of PortFuncs and have a PortFunc.SoftPWM ?
"""
@staticmethod
def find_ports():
if not is_raspi() or True:
# name: IoPort('function', 'driver', 'cfg')
io_ports = { 'softPWM 0': IoPort(PortFunc.PWM, DriverSoftPWM, {'pin': 24, 'channel': 0, 'fake': True})
, 'softPWM 1': IoPort(PortFunc.PWM, DriverSoftPWM, {'pin': 25, 'channel': 1, 'fake': True}) }
else:
io_ports = {}
cnt = 0
for pin in range(28):
try:
func = PinFunc(gpio_function(pin))
if func in [PinFunc.PWM]:
port_name = 'softPWM %d' % cnt
io_ports[port_name] = IoPort(PortFunc.PWM, DriverPWM, {'pin': pin, 'channel': cnt})
cnt += 1
else:
log.debug('pin %d is in use as %s', pin, func.name)
except KeyError:
log.debug('Unknown function on pin %d = %d', pin, gpio_function(pin))
return io_ports
def __init__(self, cfg, func):
super().__init__(cfg, func)
self.name = 'SoftPWM %d @ pin %d' % (self._channel, self._pin)
if not self._fake:
GPIO.setup(self._pin, GPIO.OUT)
self._pwm = GPIO.PWM(self._pin, 300)
self._pwm.start(0)
if self._fake:
self.name = '!' + self.name
self.write(0)
def __del__(self):
self.close()
def close(self):
log.debug('Closing %r', self)
if not self._fake:
self._pwm.stop()
GPIO.cleanup(self._pin)
def write(self, value):
log.info('%s -> %f', self.name, float(value))
if not self._fake:
self._pwm.ChangeDutyCycle(float(value))
else:
self._val = float(value)
'''
16 changes: 8 additions & 8 deletions aquaPi/machineroom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ def create_default_nodes(self) -> None:
# __Temperatures__ #
# single water temp sensor
# 2-point switched relay or triac ...
#wasser_i = AnalogInput('Wasser', 'DS1820 xA2E9C', 25.0, '°C',
#wasser_i1 = AnalogInput('Wasser', 'DS1820 xA2E9C', 25.0, '°C',
# avg=1, interval=60)
#wasser = MinimumCtrl('Temperatur', wasser_i.id, 25.0)
#wasser = MinimumCtrl('Temperatur', wasser_i1.id, 25.0)
#wasser_o = SwitchDevice('Heizstab', wasser.id,
# 'GPIO 12 out', inverted=False)

# ... or PID driven triac (relay has increased wear, not recomm.)
# PID for my 60cm/100W: sensor cycle 300s, PID 1.0/0.05/5, PWM 10s
wasser_i = AnalogInput('Wasser', 'DS1820 xA2E9C', 25.0, '°C',
wasser_i1 = AnalogInput('Wasser', 'DS1820 xA2E9C', 25.0, '°C',
avg=1, interval=300)
wasser = PidCtrl('PID Temperatur', wasser_i.id, 25.0,
p_fact=1.0, i_fact=0.05, d_fact=5.0)
wasser = PidCtrl('PID Temperatur', wasser_i1.id, 25.0,
p_fact=1.0, i_fact=0.05, d_fact=0.0)
wasser_o = SlowPwmDevice('Heizstab', wasser.id,
'GPIO 12 out', inverted=False, cycle=10)
wasser_i.plugin(self.bus)
wasser_i1.plugin(self.bus)
wasser.plugin(self.bus)
wasser_o.plugin(self.bus)

Expand All @@ -159,7 +159,7 @@ def create_default_nodes(self) -> None:
wasser_i2.plugin(self.bus)

# fancy: if water temp >26 a cooling fan spins dynamically up
coolspeed = ScaleAux('Lüftersteuerung', wasser_i.id, '%',
coolspeed = ScaleAux('Lüftersteuerung', wasser_i1.id, '%',
points=[(26.0, 0), (28.0, 100)])
cool = AnalogDevice('Kühlungslüfter', coolspeed.id,
'PWM 1', minimum=10, maximum=80)
Expand All @@ -168,7 +168,7 @@ def create_default_nodes(self) -> None:

# ... and history for a diagram
t_history = History('Temperaturen',
[wasser_i.id, wasser_i2.id,
[wasser_i1.id, wasser_i2.id,
wasser.id, # wasser_o.id,
coolspeed.id]) # , cool.id])
t_history.plugin(self.bus)
Expand Down
1 change: 0 additions & 1 deletion aquaPi/machineroom/ctrl_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ def _fader(self):
now = time()
self.data = 0 # end of descend


log.brief('SunCtrl %s: fader %s', self.id, 'DONE' if not self._fader_stop else 'stopped')
self.post(MsgData(self.id, self.data))
self.alert = None
Expand Down
1 change: 0 additions & 1 deletion aquaPi/machineroom/in_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class InputNode(BusNode, ABC):
""" Base class for IN_ENDP delivering measurments,
e.g. temperature, pH, water level switch
All use a reader thread, most reading from IoRegistry port
Semantically abstract - do not instantiate!
"""
ROLE = BusRole.IN_ENDP

Expand Down
9 changes: 4 additions & 5 deletions aquaPi/machineroom/msg_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __getstate__(self) -> dict[str, Any]:
state = {'name': self.name}
state.update(id=self.id)
state.update(identifier=self.identifier)
state.update(receives=self.receives) #FIXME: "No overload var. of "update" of "MutableMapping" matches list[str]"
state.update(receives=self.receives)
state.update(data=self.data)
state.update(unit=self.unit)
state.update(data_range=self.data_range.name)
Expand Down Expand Up @@ -225,9 +225,7 @@ def __setstate__(self, state: dict[str, Any]) -> None:
n.plugin(self)

def __str__(self) -> str:
if not self._queue:
return '{}({} nodes)'.format(type(self).__name__, len(self.nodes))
return '{}({} nodes, {} queue\'d)'.format(type(self).__name__, len(self.nodes), self._queue.qsize())
return '{}({} nodes)'.format(type(self).__name__, len(self.nodes))

def register(self, node: BusNode) -> None:
""" Add a BusNode to the bus. Do not call directly,
Expand Down Expand Up @@ -295,7 +293,8 @@ def _dispatch_one(self, msg: Msg) -> None:
rcv_nodes = {n for n in self.nodes if n.id != msg.sender}
# ... and apply each node's filter for non-Infra msgs
if not isinstance(msg, MsgInfra):
rcv_nodes = {n for n in rcv_nodes if msg.sender in n.receives or '*' in n.receives}
rcv_nodes = {n for n in rcv_nodes
if msg.sender in n.receives or '*' in n.receives}
for n in rcv_nodes:
log.debug(' -> %s', str(n))
n.listen(msg)
Expand Down
2 changes: 1 addition & 1 deletion run
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ if [[ ${reset_cfg} ]]; then rm "instance/${AQUAPI_CFG}"; fi
export FLASK_APP=aquaPi
export FLASK_ENV=development

nohup flask run --host "$(hostname -i|cut -d ' ' -f 1)" | tee run.log
nohup flask run --host "$(hostname -i|cut -d ' ' -f 1)" | tee logs/run.log

0 comments on commit 38660c6

Please sign in to comment.