def validate_session(device): query = db_session.query(Session).filter(Session.device_id == device.id, Session.completed.is_(None)) session = query.first() if session is None: session = create_session(device) return session
def temp_timeout(db_session): while True: query = db_session.query(Session).filter(Session.completed.is_(None)) sessions = query.all() for session in sessions: query = db_session.query(Temp).filter( Temp.session_id == session.id).order_by( Temp.sesssion_temp_id.desc()) temp = query.first() delta = datetime.datetime.utcnow() - temp.created seconds = delta.seconds print('Session {}. Seconds since last temp update: {}'.format( session.id, seconds)) if seconds > TEMP_TIMEOUT: session.completed = temp.created db_session.commit() time.sleep(10)
def api_session_last_temp(id): query = db_session.query(Session).filter(Session.id == id) _session = query.first() session = _session.asdict() temps = [_session.last_temp.asdict()] response = { 'status': 'success', 'data': { 'session': session, 'temps': temps } } return jsonify(response)
def api_session_temps(id): query = db_session.query(Session).filter(Session.id == id) _session = query.first() _temps = _session.temps session = _session.asdict() temps = [temp.asdict() for temp in _temps[::-1]] response = { 'status': 'success', 'data': { 'session': session, 'temps': temps } } return jsonify(response)
sys.path.append("..") from bbq.models import Device, Session, Temp from bbq.database import db_session from bbq.database import init_db init_db() d = Device('AABBCCDDEEFF') d2 = Device('FFEEDDCCBBAA') db_session.add(d) db_session.add(d2) db_session.commit() d = db_session.query(Device).all()[0] d2 = db_session.query(Device).all()[1] s = Session(d) s2 = Session(d2) db_session.add(s) db_session.add(s2) db_session.commit() d = db_session.query(Device).all()[0] d2 = db_session.query(Device).all()[1] s = Session(d) s2 = Session(d2) db_session.add(s) db_session.add(s2) db_session.commit()
def api_session(id): query = db_session.query(Session).filter(Session.id == id) _session = query.first() session = _session.asdict() response = {'status': 'success', 'data': {'session': session}} return jsonify(response)
def device(id): query = db_session.query(Device).filter(Device.id == id) device = query.first() sessions = device.sessions return render_template('device.html', device=device, sessions=sessions)
def session(id): query = db_session.query(Session).filter(Session.id == id) session = query.first() return render_template('temps.html', session=session)
def validate_device(mac): query = db_session.query(Device).filter(Device.mac == mac) device = query.first() if device is None: device = create_device(mac) return device
def index(): query = db_session.query(Device) devices = query.all() return render_template('index.html', devices=devices)
def api_device_sessions(id): query = db_session.query(Session).filter(Session.device_id == id) sessions = [session.asdict() for session in query[::-1]] response = {'status': 'success', 'data': {'sessions': sessions}} return jsonify(response)
def api_devices(): query = db_session.query(Device).all() devices = [device.asdict() for device in query[::-1]] response = {'status': 'success', 'data': {'devices': devices}} return jsonify(response)
def last_temp(self): query = db_session.query(Temp).filter( Temp.session_id == self.id).order_by(Temp.created.desc()) temp = query.first() return temp
def last_session(self): query = db_session.query(Session).filter( Session.device_id == self.id).order_by(Session.created.desc()) session = query.first() return session