Exemplo n.º 1
0
def add_transaction(sensor_UID, tag_UID, trans_type, value):

    # Get sensor and asset id from sensor_UID
    sensor = (db.session.query(tbl_sensors).filter(
        tbl_sensors.sensor_UID == sensor_UID).add_columns(
            tbl_sensors.id.label('sensor_id'),
            tbl_sensors.parent_asset.label('parent_asset_id')).first())

    sensor_id = sensor.sensor_id
    asset_id = sensor.parent_asset_id

    # Get tag_id based on tag_UID
    tag = (db.session.query(tbl_tags).filter(
        tbl_tags.tag_UID == tag_UID).add_columns(
            tbl_tags.id.label('tag_id')).first())

    tag_id = tag.tag_id

    # Add new transaction to the transactions table
    trans = tbl_transactions()
    trans.tag_id = tag_id
    trans.sensor_id = sensor_id
    trans.asset_id = asset_id
    trans.transaction_type_id = trans_type
    trans.transaction_value = value

    # Perform value transaction

    db.session.add(trans)

    # Commit new transaction to DB
    db.session.commit()
Exemplo n.º 2
0
def add_transaction(asset, status, message_id, payment_address,
                    recieve_address, value):
    transaction = tbl_transactions()
    transaction.asset = asset
    transaction.status = status
    transaction.message_id = message_id
    transaction.settlement = add_settlement(payment_address, recieve_address,
                                            value)

    db.session.add(transaction)
    db.session.commit()
Exemplo n.º 3
0
def add_transaction(asset, price, status, message_id, payment_address,
                    recieve_address, value):
    transaction = tbl_transactions()
    transaction.asset = asset
    transaction.transaction_price = price
    transaction.recieved_amount = value / 1000000
    transaction.status = status
    transaction.message_id = message_id
    transaction.settlement = add_settlement(payment_address, recieve_address,
                                            value)

    db.session.add(transaction)
    db.session.commit()
Exemplo n.º 4
0
def new_trans(mqtt_msg):

    # Check for valid mqtt message syntax
    mqtt_split = mqtt_msg.split(",")
    if len(mqtt_split) == 3:
        mqtt_tag_UID = mqtt_split[0]
        mqtt_tag_KEY = mqtt_split[1]
        mqtt_sensor_UID = mqtt_split[2]
    else:
        new_trans_error(mqtt_msg, 'Invalid mqtt syntax')
        return

    # Get tag data from DB
    tag = (db.session.query(tbl_tags, tbl_accounts)
        .join(tbl_accounts)
        .filter(tbl_tags.UID == mqtt_tag_UID)
        .add_columns(tbl_tags.id.label('tag_id'), tbl_tags.KEY.label('tag_KEY'), tbl_accounts.id.label('tag_account_id'), tbl_accounts.balance.label('tag_account_balance'))
        .first())

    if tag is None:

        new_trans_error(mqtt_msg, 'Tag UID not found')
        return

    # Get sensor data from DB
    sensor = (db.session.query(tbl_sensors, tbl_assets, tbl_accounts)
        .join(tbl_assets, tbl_sensors.parent_asset == tbl_assets.id)
        .join(tbl_accounts)
        .filter(tbl_sensors.UID == mqtt_sensor_UID)
        .add_columns(tbl_sensors.id.label('sensor_id'), tbl_assets.id.label('asset_id'), tbl_accounts.id.label('asset_account_id'), tbl_accounts.balance.label('asset_account_balance'), tbl_assets.price.label('asset_price'))
        .first())

    if sensor is None:

        new_trans_error(mqtt_msg, 'Sensor UID not found')
        return

    # Check for valid KEY
    if mqtt_tag_KEY != tag.tag_KEY:
        new_trans_error(mqtt_msg, 'Invalid KEY')
        return

    # Get accounts
    sender_account = tbl_accounts.query.filter_by(id=tag.tag_account_id).first_or_404()
    reciever_account = tbl_accounts.query.filter_by(id=sensor.asset_account_id).first_or_404()

    # If sufficient funds, perform token transfer
    if  sender_account.balance < float(sensor.asset_price):
        new_trans_error(mqtt_msg, 'Insufficient funding')
        return
    else:
        sender_account.balance = sender_account.balance - float(sensor.asset_price)
        reciever_account.balance = reciever_account.balance + float(sensor.asset_price)

    # Add new transaction to the transactions table
    trans = tbl_transactions()
    trans.tag_id = tag.tag_id
    trans.tag_account_id = tag.tag_account_id
    trans.sensor_id = sensor.sensor_id
    trans.asset_id = sensor.asset_id
    trans.asset_account_id = sensor.asset_account_id
    trans.timestamp = func.now()
    trans.transaction_value = sensor.asset_price
    db.session.add(trans)

    # Commit new transaction to DB
    db.session.commit()