def initialize_database(app, db): """Drop and restore database in a consistent state""" with app.app_context(): db.drop_all() db.create_all() first_pump = Pump('Pump 1', 'OFF', 5.1) second_pump = Pump('Pump 2', 'OFF', 3002.1) third_pump = Pump('Pump 3', 'ON', 5242.1) db.session.add(first_pump) db.session.add(second_pump) db.session.add(third_pump) db.session.commit()
def status(): if flask_request.method == 'GET': pumps = Pump.query.all() app.logger.info(f'Pumps available: {pumps}') pump_obj = {'pump_count': len(pumps), 'status': []} time.sleep(0.75) # sleep to simulate a request taking a little longer for pump in pumps: pump_obj['status'].append(pump.serialize()) return jsonify(pump_obj) elif flask_request.method == 'POST': # create a new device w/ random status pumps_count = len(Pump.query.all()) new_pump = Pump('Pump ' + str(pumps_count + 1), random.choice(['OFF', 'ON']), random.randint(10, 500)) app.logger.info(f'Adding pump {new_pump}') db.session.add(new_pump) db.session.commit() pumps = Pump.query.all() # adding a half sleep to test something time.sleep(0.5) return jsonify([b.serialize() for b in pumps]) app.logger.warn(f'Invalid request method {flask_request.method}') return jsonify({'error': 'Invalid request method'}), 405
def status(): if flask_request.method == 'GET': pumps = Pump.query.all() pump_obj = {'pump_count': len(pumps), 'status': []} app.logger.info(f"Available pumps {pumps}") for pump in pumps: pump_obj['status'].append(pump.serialize()) return jsonify(pump_obj) elif flask_request.method == 'POST': # create a new device w/ random status pumps_count = len(Pump.query.all()) new_pump = Pump('Pump ' + str(pumps_count + 1), random.choice(['OFF', 'ON']), random.randint(10,500)) app.logger.info(f"Adding pump {new_pump}") app.logger.info(f"pump count is {pumps_count}") app.logger.info(f"new_pump is {new_pump}") db.session.add(new_pump) db.session.commit() pumps = Pump.query.all() return jsonify([b.serialize() for b in pumps]) else: err = jsonify({'error': 'Invalid request method'}) err.status_code = 405 return err
def v0_get_cooling_pump_group_configurations(self, fields=None): pump_config_list = [] for pump in Pump.select(): pump_config = { 'id': pump.number, 'outputs': [valve.number for valve in pump.cooling_valves], 'output': pump.number, 'room': 255 } pump_config_list.append(pump_config) return pump_config_list
def v0_get_pump_group_configuration(self, pump_number, fields=None): pump = Pump.get(number=pump_number) pump_config = { 'id': pump.number, 'outputs': ','.join([valve.output.number for valve in pump.heating_valves]), 'output': pump.output.number, 'room': 255 } return pump_config
from config import (PIN, UPLIFT_THRESHOLD, TEMP_CHECK_INTERVAL, PROBE_IN, PROBE_OUT, PROBE_AIR) from models import Pump, DataLog, FlowMeter, Thermometer GPIO.setwarnings(False) # Choose numbering scheme GPIO.setmode(GPIO.BCM) # Setup relay outputs GPIO.setup(PIN.RELAY1, GPIO.OUT) GPIO.setup(PIN.RELAY2, GPIO.OUT) # Setup Input channel, using pulldown load GPIO.setup(PIN.FLOW, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) datalogger = DataLog(10) p = Pump() probe_in = Thermometer(*PROBE_IN) probe_out = Thermometer(*PROBE_OUT) probe_air = Thermometer(*PROBE_AIR) try: while True: # Make a reading and record it temp_in = probe_in.tick() temp_out = probe_out.tick() temp_air = probe_air.tick() # Log data every few loops datalogger.tick(temp_in, temp_out, None, temp_air)