def callback_handler(device_id): """ Receives the json formatted callback message from a given sigfox device. It stores the information into the database. Args: device_id (str): the SigFox device id that has triggered the callback Returns: json: The device_id as a response with HTTP 200 OK. """ json_msg = request.json db_session = Session() # Append message to 'sigfox_messages' table db_msg = SigFoxMessages() db_msg.device_id = json_msg["device_id"] db_msg.seq_num = json_msg["seq_num"] db_msg.weight = json_msg["weight"] db_session.add(db_msg) # Create 'latest_sigfox_messages' table entry or update it if existing for the current device_id if db_session.query(LatestSigFoxMessages).filter( LatestSigFoxMessages.device_id == device_id).count() == 0: latest_db_msg = LatestSigFoxMessages() latest_db_msg.device_id = json_msg["device_id"] latest_db_msg.seq_num = json_msg["seq_num"] latest_db_msg.weight = json_msg["weight"] db_session.add(latest_db_msg) else: entry_to_update = db_session.query(LatestSigFoxMessages).filter( LatestSigFoxMessages.device_id == device_id).one() entry_to_update.seq_num = json_msg["seq_num"] entry_to_update.weight = json_msg["weight"] db_session.commit() db_session.close() # Notify the Angular2 client that the database entry has been updated socket_io.emit('weight_update', { 'device_id': json_msg["device_id"], 'seq_num': json_msg["seq_num"], 'weight': json_msg["weight"] }, namespace='/') return jsonify({"device_id": device_id})
def create_order(user_id, text): session = Session() user = session.query(tables.User).filter( tables.User.user_id == user_id).first() date = create_date() order = tables.Orders(user_id=user_id, user_name=user.user_name, text=text, created=date) try: session.add(order) session.commit() except Exception as e: session.rollback() raise e finally: session.close()
def create_user(user_id, user_name): session = Session() user = tables.User(user_id=user_id, user_name=user_name) access = tables.Access(user_id=user_id, access=user_id) status = tables.UserStatus(user_id=user_id) settings = tables.Settings(user_id=user_id) try: session.add(user) session.add(access) session.add(status) session.add(settings) session.commit() except Exception as e: session.rollback() raise e finally: session.close()
def add_word(word): session = Session() session.add(word) session.commit() session.close()