(Syntax probably definitely not exactly Python.)
Code: Select all
# Control logic for some Pi
# This stays the same for all the usage examples below
somepi_control_logic():
something
something
something
if logictools.get_reading(site_ID,sensor_ID) is big:
do something
if logictools.get_reading(site_ID,sensor_ID) is alarming:
do something else
something something
blah blah etc
Code: Select all
# Use it like this, maybe:
logictools.get_reading = DatabaseConnection.get_reading()
somepi_control_logic()
# logic uses DatabaseConnection.get_reading() to get readings
Code: Select all
# Or like this:
# A vastly oversimplified example function
get_reading_from_monitor(site_ID, sensor_ID):
# The point of this example function is to show how we would adapt
# from a source of readings that doesn't let you ask for a
# specific reading into one that does.
# Using this function means that neither the source of readings
# nor the control logic has to bend to the other's will. This
# means they can go on a date together without committing to
# getting married and staying together for the rest of their
# lives. They are loosely coupled. <3
these words represent code that does whatever is necessary to
update the logictools.readings dictionary so that it contains the
latest readings from our source of readings
# Return only the reading we got asked for
# The rest of the readings will stick around for the next call of
# the function
return logictools.readings[site_ID, sensor_ID]
logictools.get_reading = get_reading_from_monitor()
somepi_control_logic()
# logic uses get_reading_from_monitor() to get readings
Code: Select all
# Or, how about:
# Maybe for some reason we need to get all the database readings at
# the start of the logic rather than throughout it.
somepi_gather_readings():
ids = some list of IDs
for each site, sensor in ids:
logictools.readings[site, sensor] = \
DatabaseConnection.get_reading(site, sensor)
get_gathered_reading(site_ID, sensor_ID):
return logictools.readings[site_ID, sensor_ID]
logictools.get_reading = get_gathered_reading()
# gather readings before running logic
somepi_gather_readings()
somepi_control_logic()
# logic uses get_gathered_reading() to get gathered readings