collection = "ModelArchitecture"
email = sys.argv[4]
token = sys.argv[5]

print("System Key: ", SystemKey)
print("System Secret: ", SystemSecret)
print("URL: ", url)
print("email: ", email)
print("token: ", token)

#user = "******"
#pswd = "password"

mySystem = System(SystemKey, SystemSecret, url, safe=False)

user = mySystem.ServiceUser(email, token)
print("Hello User")

mqtt = mySystem.Messaging(user)


def decode_and_save():
    myCol = mySystem.Collection(user, collectionName=collection)
    rows = myCol.getItems()
    model = rows[-1]

    df = pd.DataFrame(data=model, index=[0])
    df = df.drop(['item_id'], axis=1)

    saved_model_pb = df['saved_model_pb'][0]
    saved_model_pb = saved_model_pb.encode('ascii')
def get_data():
    try:
        with open("train_params.json", 'r') as fp:
            train_params = json.load(fp)

        key = train_params["systemKey"]
        secret = train_params["systemSecret"]
        url = train_params["url"]
        collection = train_params["featureCol"]
        user = train_params["email"]
        #pswd = train_params["password"]
        token = train_params["usertoken"]

        logging.debug("Training Parameters fetched")

        SystemKey = key
        SystemSecret = secret

        mySystem = System(SystemKey, SystemSecret, url=url)
        #user = mySystem.User(user, pswd)
        user = mySystem.ServiceUser(user, token)

        myCol = mySystem.Collection(user, collectionName=collection)
        rows = myCol.getItems(pagesize=1000)

        featureDataset = collection + ".json"

        logging.debug("Feature Dataset fetched for CB Collections")

        with open(featureDataset, 'w') as fp:
            json.dump(rows, fp, indent=2)

        myCol1 = mySystem.Collection(user, collectionName="TrainingFiles")
        rows = myCol1.getItems()

        logging.debug("Model files fetched for CB Collections")

        os.system("mkdir myCode")

        files = rows[-1]

        archfile = train_params["archFile"]
        datafile = train_params["dataFile"]
        trainfile = train_params["taskFile"]

        with open("__init__.py", 'w') as init:
            init.close()

        with open(archfile, 'w') as af:
            decoded = base64.b64decode(files["archfile"])
            af.write(decoded.decode('ascii'))

        with open(datafile, 'w') as df:
            decoded = base64.b64decode(files["datafile"])
            df.write(decoded.decode('ascii'))

        with open(trainfile, 'w') as tf:
            decoded = base64.b64decode(files["trainfile"])
            tf.write(decoded.decode('ascii'))

        os.system("mv __init__.py myCode/")
        os.system("mv " + archfile + " myCode/")
        os.system("mv " + datafile + " myCode/")
        os.system("mv " + trainfile + " myCode/")

    except Exception as e:
        logging.error(e)