Barcode Reader Firebase Upload

buddhikakgsl

Well-known member
  • Jul 19, 2012
    1,136
    118
    63
    ඉන්න තැන
    මේ පලවෙනි code එකේ තියෙන්නෙ barcode reader එකකින් barcode read කරන්න. දෙවෙනි එකෙන් read කරපු barcode එක firebase realtime database එකට send වෙන්න.
    පලවෙනි program එකෙන් දෙවෙනි program එකට dataimport කරල firebase එකට send කරනන් හැදුවට කරන්න බැ. හැබැයි data import karala කරල printවෙනවත් එක්ක.
    එකෙ databse settings එහෙමත් හරි. ඇයි මේක barcode readings firebase ekata send වෙන්නෙ නැත්තෙ

    Code Number 01

    Python:
    import sys
    
    def barcode_reader():
        hid = {4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm',
               17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y',
               29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ',
               45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';', 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'}
    
        hid2 = {4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M',
                17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y',
                29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ',
                45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':', 52: '"', 53: '~', 54: '<', 55: '>', 56: '?'}
    
        fp = open('hidraw0', 'w+')
    
        ss = ""
        shift = False
    
        done = False
    
        while not done:
            # Get the character from the HID
            buffer = fp.read(8)
            for c in buffer:
                if ord(c) > 0:
    
                    #  40 is carriage return which signifies
                 
                    if int(ord(c)) == 40:
                        done = True
                        break;
    
                   
                    #  use the hid2 characters.
                    if shift:
    
                        # If it is a '2' then it is the shift key
                        if int(ord(c)) == 2:
                            shift = True
    
                        # if not a 2 then lookup the mapping
                        else:
                            ss += hid2[int(ord(c))]
                            shift = False
    
                    #  If not shifted then use
                    #  the hid characters
    
                    else:
    
                        # If it is a '2' then it is the shift key
                        if int(ord(c)) == 2:
                            shift = True
    
                        # if not a 2 then lookup the mapping
                        else:
                            ss += hid[int(ord(c))]
        return ss
        #print (type(ss))
    
    #a = input("Enter barcode here: ")
    #data={'Barcode Code':a}
    #db.push(data)
    
    
    if __name__ == '__main__':
        try:
            while True:
                barcode_reader()
    
        except KeyboardInterrupt:
            pass

    Code Number 02
    Python:
    import pyrebase
    from test import barcode_reader()
    
    config = {
          "apiKey": "xxxxxx",
              "authDomain": "xxxxxxx",
                "databaseURL": "xxxxxxx",
                "projectId": "xxxxx",
                  "storageBucket": "xxxxx",
                  "messagingSenderId": "xxxxxx",
              "appId": "xxxxxx"
    
    };
    
    firebase = pyrebase.initialize_app(config)
    
    storage = firebase.storage()
    database = firebase.database()
    a = barcode_reader()
    print (a)
    database.child("DB object name")
    data = {"key1": a}
    #print (type (a))
    database.set(data)
     
    • Like
    Reactions: B.Chat

    HAneo

    Well-known member
  • Jan 30, 2007
    12,970
    29,167
    113
    Homagama
    But with the given code, I can send a string or an integer.
    Anyway I will try this as well.
    No you can send any type of data even if it's Key Value pier . it's the Slandered method to set data to Firebase Realtime Database.
    I did this many times with Dart and JS. but for Python it should be the same machan
     
    • Like
    Reactions: saja and B.Chat

    HAneo

    Well-known member
  • Jan 30, 2007
    12,970
    29,167
    113
    Homagama
    I tried this machn. Now it comes with
    AttributeError: 'Database' object has no attribute 'reference'
    Seems like configuration problem
    Database object not being initialize properly
    And if you not familiar pls ready this note . turn on python tab for the codes
    https://firebase.google.com/docs/database/admin/save-data#python


    Try this example


    Code:
    # Import database module.
    from firebase_admin import db
    
    # Get a database reference to our blog.
    ref = db.reference('server/saving-data/fireblog')
    
    users_ref = ref.child('users')
    users_ref.set({
        'alanisawesome': {
            'date_of_birth': 'June 23, 1912',
            'full_name': 'Alan Turing'
        },
        'gracehop': {
            'date_of_birth': 'December 9, 1906',
            'full_name': 'Grace Hopper'
        }
    })
     
    • Like
    Reactions: buddhikakgsl

    buddhikakgsl

    Well-known member
  • Jul 19, 2012
    1,136
    118
    63
    ඉන්න තැන
    Seems like configuration problem
    Database object not being initialize properly
    And if you not familiar pls ready this note . turn on python tab for the codes
    https://firebase.google.com/docs/database/admin/save-data#python


    Try this example


    Code:
    # Import database module.
    from firebase_admin import db
    
    # Get a database reference to our blog.
    ref = db.reference('server/saving-data/fireblog')
    
    users_ref = ref.child('users')
    users_ref.set({
        'alanisawesome': {
            'date_of_birth': 'June 23, 1912',
            'full_name': 'Alan Turing'
        },
        'gracehop': {
            'date_of_birth': 'December 9, 1906',
            'full_name': 'Grace Hopper'
        }
    })
    I tried this method and Firestore as well. No barcode reading values getting uploaded.
    this is for Firestore

    Python:
    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    #from test import addition
    from barcode import barcode_reader
    
    cred = credentials.Certificate("serviceAccountKey.json")
    firebase_admin.initialize_app(cred)
    db=firestore.client()
    a = barcode_reader()
    #print(a)
    #b= addition()
    
    ref= db.collection('barcodes').document()
    ref.set({"barcode": a})

    I cant find any error.

    bump
    ------ Post added on May 17, 2022 at 8:42 PM