class CheckEEW(): def __init__(self, argv): if len(argv) < 2: print "Usage: checkbridge <bridge>" exit() else: self.bridges = [argv[1]] for b in self.bridges: b = b.lower() print "Checking ", self.bridges access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') if access_token == "NO_TOKEN": print "No Dropbox access token. You must set CB_DROPBOX_TOKEN environment variable first." exit() try: self.client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() self.manager = DatastoreManager(self.client) self.process() def process(self): for bridge in self.bridges: print bridge fileName = bridge + ".csv" self.f = open(fileName, "w", 0) rows = [] ds = self.manager.open_or_create_datastore(bridge) t = ds.get_table('config') devices = t.query(type='idtoname') values = [] commas = "" heads = "Time," for d in devices: devHandle = d.get('device') devName = d.get('name') self.f.write(devHandle + ',' + devName + '\n') t = ds.get_table(devHandle) for sensor in SENSORS: heads = heads + devName + ' ' + sensor + ',' readings = t.query(Type=sensor) max = 0 for r in readings: timeStamp = float(r.get('Date')) if timeStamp > max: max = timeStamp dat = r.get('Data') line = commas + str("%2.1f" %dat) values.append([timeStamp, line]) commas += "," rows.append([devHandle, devName, sensor, niceTime(max)]) values.sort(key=lambda tup: tup[0]) self.f.write(heads + '\n') for v in values: line = niceTime(v[0]) + "," + v[1] + "\n" self.f.write(line)
class CheckEEW(): def __init__(self, argv): if len(argv) < 2: print "Usage: checkbridge <bridge>" exit() else: self.bridges = [argv[1]] for b in self.bridges: b = b.lower() #print "Checking ", self.bridges access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') if access_token == "NO_TOKEN": print "No Dropbox access token. You must set CB_DROPBOX_TOKEN environment variable first." exit() try: self.client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() self.manager = DatastoreManager(self.client) self.process() def process(self): for bridge in self.bridges: print bridge rows = [] ds = self.manager.open_or_create_datastore(bridge) t = ds.get_table('config') devices = t.query(type='idtoname') print "" for d in devices: devHandle = d.get('device') devName = d.get('name') t = ds.get_table(devHandle) for sensor in SENSORS: readings = t.query(Type=sensor) values = [] max = 0 for r in readings: timeStamp = float(r.get('Date')) if timeStamp > max: max = timeStamp dat = r.get('Data') values.append([timeStamp, dat]) #values.sort(key=lambda tup: tup[0]) #for v in values: #line = self.niceTime(v[0]) + "," + str("%2.1f" %v[1]) + "\n" #self.f.write(line) rows.append([devHandle, devName, sensor, niceTime(max)]) header = ('Handle', 'Friendly Name', 'Sensor', 'Most Recent Sample') txt = matrix_to_string(rows, header) print txt
class CheckEEW(): def __init__(self, argv): if len(argv) < 2: print "Usage: checkbridge <bridge>" exit() else: self.bridges = [argv[1]] for b in self.bridges: b = b.lower() #print "Checking ", self.bridges access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') if access_token == "NO_TOKEN": print "No Dropbox access token. You must set CB_DROPBOX_TOKEN environment variable first." exit() try: self.client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() self.manager = DatastoreManager(self.client) self.process() def process(self): for bridge in self.bridges: print bridge rows = [] ds = self.manager.open_or_create_datastore(bridge) t = ds.get_table('config') devices = t.query(type='idtoname') print "" for d in devices: devHandle = d.get('device') devName = d.get('name') t = ds.get_table(devHandle) for sensor in SENSORS: readings = t.query(Type=sensor) values = [] max = 0 for r in readings: timeStamp = float(r.get('Date')) if timeStamp > max: max = timeStamp dat = r.get('Data') values.append([timeStamp, dat]) #values.sort(key=lambda tup: tup[0]) #for v in values: #line = self.niceTime(v[0]) + "," + str("%2.1f" %v[1]) + "\n" #self.f.write(line) rows.append([devHandle, devName, sensor, niceTime(max)]) header = ('Handle', 'Friendly Name', 'Sensor', 'Most Recent Sample') txt = matrix_to_string(rows, header) print txt
class MakeCVS(): def __init__(self, argv): if len(argv) < 2: print "Usage: makecvs <bridge name> " exit(1) self.bridge = argv[1] self.bridge = self.bridge.lower() print "Writing cvs files for ", self.bridge access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') if access_token == "NO_TOKEN": print "No Dropbox access token. You must set CB_DROPBOX_TOKEN environment variable first." exit() try: self.client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() self.manager = DatastoreManager(self.client) self.ds = self.manager.open_or_create_datastore(self.bridge) self.process() def niceTime(self, timeStamp): localtime = time.localtime(timeStamp) milliseconds = '%03d' % int((timeStamp - int(timeStamp)) * 1000) now = time.strftime('%Y:%m:%d, %H:%M:%S:', localtime) + milliseconds return now def process(self): idToName = {} devTable = {} ir_temps = [] t = self.ds.get_table('config') devices = t.query(type='idtoname') for d in devices: idToName[d.get('device')] = d.get('name') print "idToName: ", idToName for d in idToName: fileName = d + ".csv" self.f = open(fileName, "w", 0) t = self.ds.get_table(d) temps = t.query(Type='ir_temperature') values = [] for t in temps: timeStamp = float(t.get('Date')) temp = t.get('Data') values.append([timeStamp, temp]) values.sort(key=lambda tup: tup[0]) for v in values: line = self.niceTime(v[0]) + "," + str("%2.1f" %v[1]) + "\n" self.f.write(line)
class DropShell: def __init__(self): self.config = configparser.ConfigParser() self.config.read('dropshell.ini') self.settings = self.config["DropBox"] self.wdPath = [] self.datastoreManager = None def getFilePath(self, fileName): if len(self.wdPath) == 0: return '/' + fileName return '/' + '/'.join(self.wdPath) + '/' + fileName def appAuthorization(self): print('Application authorization required') print('---------------------------------------------------------\n') app_key = self.settings['AppKey'] app_secret= self.settings['AppKeySecret'] flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret) authorize_url = flow.start() print(' 1. Go to: ' + authorize_url) # "AppName" moechte auf ihren eigenen Ordner Apps > NotesWiki in Ihrer Dropbox zugreifen. # Allow, Deny print(' 2. Click "Allow" (you might have to log in first)') print(' 3. Copy the authorization code.\n') code = input("Enter the authorization code here: ").strip() access_token, user_id = flow.finish(code) # "The access token is all you'll need to make API requests on behalf # of this user, so you should store it away for safe-keeping. # By storing the access token, you won't need to go through these steps # again unless the user reinstalls your app or revokes access via the # Dropbox website." self.settings['UserAccessToken'] = access_token self.settings['UserAccessId'] = user_id with open('dropshell.ini', 'w') as dropshellIni: self.config.write(dropshellIni, False) def processCommand(self, tokens): cmd = tokens[0].lower() if cmd == "quit": self.isRunning = False elif cmd == "connect": self.connect(tokens[1:]) elif cmd == "ls": self.listFiles(tokens[1:]) elif cmd == "cd": self.changeDirectory(tokens[1:]) elif cmd == "info": self.showAccountInfo() elif cmd == "put": self.putFile(tokens[1:]) elif cmd == "get": self.getFile(tokens[1:]) elif cmd == "rm": self.deleteFile(tokens[1:]) elif cmd == "mkdir": self.mkdir(tokens[1:]) elif cmd == "rmdir": self.rmdir(tokens[1:]) elif cmd == "help": self.showHelp() elif cmd == "dscreate": self.dsCreate(tokens[1:]) elif cmd == "dslist": self.dsList() else: print("Syntax error: {}".format(tokens)) def showHelp(self): print(''' Available commands: ------------------- help - Show this help summary connect - Connect to dropbox info - Show information about the connection ls [-l] - Show files and folders in the current folder cd path - Change the current path mkdir folderName - Creates a new folder in the current folder put fileName - Upload a file to the current folder get fileName - Download a file from the current folder rm name - Deletes the specified file or folder from the current folder ds - execute a Datastore API sample quit - Leave the command line client''') def connect(self, parameters): if self.settings.get('useraccesstoken') is None: self.appAuthorization() self.client = dropbox.client.DropboxClient(self.settings['useraccesstoken']) print("Connected.") self.showAccountInfo() def showAccountInfo(self): info = self.client.account_info() print("Connection information") print("-------------------------------") print(' User : {}'.format(info['display_name'])) print(' eMail: {}'.format(info['email'])) print() def listFiles(self, parameters): isLong = len(parameters) > 0 and parameters[0] == '-l' # Depending on the application type / security settings, the # root directory "/" either points to the applications root directory # (e.g. DropBox > Apps > NotesWiki) or to the User's root directory # ("DropBox") wd = '/' + '/'.join(self.wdPath) contents = self.client.metadata(wd) files = contents['contents'] print() for f in files: filePath = f['path'].split('/') fileName = filePath[-1] isDir = f['is_dir'] if isLong: # Www, DD Mmm YYYY HH:MM:SS +Tz mDate = time.strptime(f['modified'], "%a, %d %b %Y %H:%M:%S %z") printDate = time.strftime("%b %d %Y %H:%M:%S", mDate) size = f['size'] print('{:>10} {} {}{}'.format(size, printDate, fileName, '/' if isDir else '')) else: print('{}{}'.format(fileName, '/' if isDir else '')) def changeDirectory(self, parameters): dirName = parameters[0] if dirName == '..': if len(self.wdPath) > 0: self.wdPath = self.wdPath[:-1] else: self.wdPath.append(dirName) def putFile(self, params): remoteFileName = self.getFilePath(params[0]) print("Uploading: {} => {}".format(params[0], remoteFileName)) f = open(params[0], 'rb') response = self.client.put_file(remoteFileName, f) print("Uploaded {} bytes.".format(response['size'])) #f = open('working-draft.txt', 'rb') #response = client.put_file('/magnum-opus.txt', f) #print "uploaded:", response def getFile(self, params): remoteFileName = self.getFilePath(params[0]) print("Downloading: {} => {}".format(remoteFileName, params[0])) out = open(params[0], 'wb') with self.client.get_file(remoteFileName) as f: out.write(f.read()) print("Done.") def mkdir(self, params): remoteDirName = self.getFilePath(params[0]) result = self.client.file_create_folder(remoteDirName) print("Created directory {}".format(result['path'])) #def rmdir(self, params): # remoteDirName = self.getFilePath(params[0]) # result = self.client.file_delete(remoteDirName) # print("Removed directory {}".format(result['path'])) def deleteFile(self, params): remoteFileName = self.getFilePath(params[0]) response = self.client.file_delete(remoteFileName) print("Removed {} {}".format("directory" if response['is_dir'] else "file", response['path'])) # Datastore API samples def dsCreate(self, params): if self.datastoreManager is None: self.datastoreManager = DatastoreManager(self.client) result = self.datastoreManager.open_or_create_datastore(params[0]) print(str(result)) def dsDelete(self, params): if self.datastoreManager is None: self.datastoreManager = DatastoreManager(self.client) result = self.datastoreManager.delete_datastore(params[0], True) print(str(result)) def dsList(self): if self.datastoreManager is None: self.datastoreManager = DatastoreManager(self.client) result = self.datastoreManager.list_datastores() for ds in result: print(str(ds)) def processInput(self): self.connect(None) self.isRunning = True while self.isRunning: wd = '/' + '/'.join(self.wdPath) s = input('[DS] {} > '.format(wd)) tokens = s.split(maxsplit=1) if len(tokens) > 0: self.processCommand(tokens) print()
class DropShell: def __init__(self): self.config = configparser.ConfigParser() self.config.read('dropshell.ini') self.settings = self.config["DropBox"] self.wdPath = [] self.datastoreManager = None def getFilePath(self, fileName): if len(self.wdPath) == 0: return '/' + fileName return '/' + '/'.join(self.wdPath) + '/' + fileName def appAuthorization(self): print('Application authorization required') print('---------------------------------------------------------\n') app_key = self.settings['AppKey'] app_secret = self.settings['AppKeySecret'] flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret) authorize_url = flow.start() print(' 1. Go to: ' + authorize_url) # "AppName" moechte auf ihren eigenen Ordner Apps > NotesWiki in Ihrer Dropbox zugreifen. # Allow, Deny print(' 2. Click "Allow" (you might have to log in first)') print(' 3. Copy the authorization code.\n') code = input("Enter the authorization code here: ").strip() access_token, user_id = flow.finish(code) # "The access token is all you'll need to make API requests on behalf # of this user, so you should store it away for safe-keeping. # By storing the access token, you won't need to go through these steps # again unless the user reinstalls your app or revokes access via the # Dropbox website." self.settings['UserAccessToken'] = access_token self.settings['UserAccessId'] = user_id with open('dropshell.ini', 'w') as dropshellIni: self.config.write(dropshellIni, False) def processCommand(self, tokens): cmd = tokens[0].lower() if cmd == "quit": self.isRunning = False elif cmd == "connect": self.connect(tokens[1:]) elif cmd == "ls": self.listFiles(tokens[1:]) elif cmd == "cd": self.changeDirectory(tokens[1:]) elif cmd == "info": self.showAccountInfo() elif cmd == "put": self.putFile(tokens[1:]) elif cmd == "get": self.getFile(tokens[1:]) elif cmd == "rm": self.deleteFile(tokens[1:]) elif cmd == "mkdir": self.mkdir(tokens[1:]) elif cmd == "rmdir": self.rmdir(tokens[1:]) elif cmd == "help": self.showHelp() elif cmd == "dscreate": self.dsCreate(tokens[1:]) elif cmd == "dslist": self.dsList() else: print("Syntax error: {}".format(tokens)) def showHelp(self): print(''' Available commands: ------------------- help - Show this help summary connect - Connect to dropbox info - Show information about the connection ls [-l] - Show files and folders in the current folder cd path - Change the current path mkdir folderName - Creates a new folder in the current folder put fileName - Upload a file to the current folder get fileName - Download a file from the current folder rm name - Deletes the specified file or folder from the current folder ds - execute a Datastore API sample quit - Leave the command line client''') def connect(self, parameters): if self.settings.get('useraccesstoken') is None: self.appAuthorization() self.client = dropbox.client.DropboxClient( self.settings['useraccesstoken']) print("Connected.") self.showAccountInfo() def showAccountInfo(self): info = self.client.account_info() print("Connection information") print("-------------------------------") print(' User : {}'.format(info['display_name'])) print(' eMail: {}'.format(info['email'])) print() def listFiles(self, parameters): isLong = len(parameters) > 0 and parameters[0] == '-l' # Depending on the application type / security settings, the # root directory "/" either points to the applications root directory # (e.g. DropBox > Apps > NotesWiki) or to the User's root directory # ("DropBox") wd = '/' + '/'.join(self.wdPath) contents = self.client.metadata(wd) files = contents['contents'] print() for f in files: filePath = f['path'].split('/') fileName = filePath[-1] isDir = f['is_dir'] if isLong: # Www, DD Mmm YYYY HH:MM:SS +Tz mDate = time.strptime(f['modified'], "%a, %d %b %Y %H:%M:%S %z") printDate = time.strftime("%b %d %Y %H:%M:%S", mDate) size = f['size'] print('{:>10} {} {}{}'.format(size, printDate, fileName, '/' if isDir else '')) else: print('{}{}'.format(fileName, '/' if isDir else '')) def changeDirectory(self, parameters): dirName = parameters[0] if dirName == '..': if len(self.wdPath) > 0: self.wdPath = self.wdPath[:-1] else: self.wdPath.append(dirName) def putFile(self, params): remoteFileName = self.getFilePath(params[0]) print("Uploading: {} => {}".format(params[0], remoteFileName)) f = open(params[0], 'rb') response = self.client.put_file(remoteFileName, f) print("Uploaded {} bytes.".format(response['size'])) #f = open('working-draft.txt', 'rb') #response = client.put_file('/magnum-opus.txt', f) #print "uploaded:", response def getFile(self, params): remoteFileName = self.getFilePath(params[0]) print("Downloading: {} => {}".format(remoteFileName, params[0])) out = open(params[0], 'wb') with self.client.get_file(remoteFileName) as f: out.write(f.read()) print("Done.") def mkdir(self, params): remoteDirName = self.getFilePath(params[0]) result = self.client.file_create_folder(remoteDirName) print("Created directory {}".format(result['path'])) #def rmdir(self, params): # remoteDirName = self.getFilePath(params[0]) # result = self.client.file_delete(remoteDirName) # print("Removed directory {}".format(result['path'])) def deleteFile(self, params): remoteFileName = self.getFilePath(params[0]) response = self.client.file_delete(remoteFileName) print("Removed {} {}".format( "directory" if response['is_dir'] else "file", response['path'])) # Datastore API samples def dsCreate(self, params): if self.datastoreManager is None: self.datastoreManager = DatastoreManager(self.client) result = self.datastoreManager.open_or_create_datastore(params[0]) print(str(result)) def dsDelete(self, params): if self.datastoreManager is None: self.datastoreManager = DatastoreManager(self.client) result = self.datastoreManager.delete_datastore(params[0], True) print(str(result)) def dsList(self): if self.datastoreManager is None: self.datastoreManager = DatastoreManager(self.client) result = self.datastoreManager.list_datastores() for ds in result: print(str(ds)) def processInput(self): self.connect(None) self.isRunning = True while self.isRunning: wd = '/' + '/'.join(self.wdPath) s = input('[DS] {} > '.format(wd)) tokens = s.split(maxsplit=1) if len(tokens) > 0: self.processCommand(tokens) print()
from dropbox.client import DropboxClient, DropboxOAuth2Flow, DropboxOAuth2FlowNoRedirect from dropbox.rest import ErrorResponse, RESTSocketError from dropbox.datastore import DatastoreError, DatastoreManager, Date, Bytes from pprint import pprint import time import os access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') print "Dropbox access token = ", access_token try: client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() manager = DatastoreManager(client) ds = manager.list_datastores() #datastore = manager.open_default_datastore() datastore = manager.open_or_create_datastore('cbr-7') devTable = datastore.get_table('dev1') ir_temps = devTable.query(Type='ir_temperature') values = [] for t in ir_temps: #timeStamp = Date.to_datetime_local(t.get('Date')) timeStamp = float(t.get('Date')) temp = t.get('Data') values.append([timeStamp, temp]) values.sort(key=lambda tup: tup[0]) for v in values: print v[0], v[1]
class CheckEEW(): def __init__(self, argv): if len(argv) < 2: print "Usage: checkbridge <bridge>" exit() else: self.bridges = [argv[1]] for b in self.bridges: b = b.lower() print "Checking ", self.bridges access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') if access_token == "NO_TOKEN": print "No Dropbox access token. You must set CB_DROPBOX_TOKEN environment variable first." exit() try: self.client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() self.manager = DatastoreManager(self.client) self.process() def niceTime(self, timeStamp): localtime = time.localtime(timeStamp) milliseconds = '%03d' % int((timeStamp - int(timeStamp)) * 1000) now = time.strftime('%Y:%m:%d, %H:%M:%S:', localtime) + milliseconds return now def matrix_to_string(self,matrix, header=None): """ Return a pretty, aligned string representation of a nxm matrix. This representation can be used to print any tabular data, such as database results. It works by scanning the lengths of each element in each column, and determining the format string dynamically. @param matrix: Matrix representation (list with n rows of m elements). @param header: Optional tuple or list with header elements to be displayed. """ if type(header) is list: header = tuple(header) lengths = [] if header: for column in header: lengths.append(len(column)) for row in matrix: for column in row: i = row.index(column) column = str(column) cl = len(column) try: ml = lengths[i] if cl > ml: lengths[i] = cl except IndexError: lengths.append(cl) lengths = tuple(lengths) format_string = "" for length in lengths: format_string += "%-" + str(length) + "s " format_string += "\n" matrix_str = "" if header: matrix_str += format_string % header for row in matrix: matrix_str += format_string % tuple(row) return matrix_str def process(self): for bridge in self.bridges: print "Reaading and processing data for ", bridge fileName = bridge + ".csv" self.f = open(fileName, "w", 0) rows = [] ds = self.manager.open_or_create_datastore(bridge) t = ds.get_table('config') devices = t.query(type='idtoname') values = [] commas = "" heads = "" devSensors = [] for d in devices: devHandle = d.get('device') devName = d.get('name') self.f.write(devHandle + ',' + devName + '\n') t = ds.get_table(devHandle) for sensor in SENSORS: heads = heads + devName + ' ' + sensor + ',' devSensors.append([devName, sensor]) readings = t.query(Type=sensor) max = 0 for r in readings: timeStamp = float(r.get('Date')) if timeStamp > max: max = timeStamp dat = r.get('Data') line = commas + str("%2.1f" %dat) values.append([timeStamp, line]) commas += "," rows.append([devHandle, devName, sensor, self.niceTime(max)]) values.sort(key=lambda tup: tup[0]) print "Type the numbers of the values you want to plot, separated by spaces:" for d in devSensors: print devSensors.index(d) + 1, ":", d[0], d[1] request = raw_input("Values > ") toProcess = request.split() print "toProcess: ", toProcess
class AnalyseData(): def __init__(self, argv): if len(argv) < 2: print "Usage: checkbridge <bridge>" exit() else: self.bridges = [argv[1]] for b in self.bridges: b = b.lower() print "Checking ", self.bridges access_token = os.getenv('CB_DROPBOX_TOKEN', 'NO_TOKEN') if access_token == "NO_TOKEN": print "No Dropbox access token. You must set CB_DROPBOX_TOKEN environment variable first." exit() try: self.client = DropboxClient(access_token) except: print "Could not access Dropbox. Wrong access token?" exit() self.manager = DatastoreManager(self.client) def niceTime(self, timeStamp): localtime = time.localtime(timeStamp) milliseconds = '%03d' % int((timeStamp - int(timeStamp)) * 1000) now = time.strftime('%Y:%m:%d, %H:%M:%S:', localtime) + milliseconds return now def readData(self): for bridge in self.bridges: print "Reaading and processing data for ", bridge ds = self.manager.open_or_create_datastore(bridge) t = ds.get_table('config') devices = t.query(type='idtoname') self.values = [] devSensors = [] for d in devices: devHandle = d.get('device') devName = d.get('name') t = ds.get_table(devHandle) for sensor in SENSORS: devSensors.append([devName, sensor]) readings = t.query(Type=sensor) for r in readings: timeStamp = float(r.get('Date')) dat = r.get('Data') self.values.append([timeStamp, dat]) def prepareData(self): self.df = {} test = pd.to_datetime([1, 2, 3]) print "test = ", test self.df = pd.DataFrame(self.values) #self.df[t]['epochTime'] = pd.to_datetime(self.df[t]['epochTime'], unit='s') #self.df[t] = self.df[t].set_index('epochTime') #print self.df[t].head() def processTemp(self): self.temp = {} for t in self.tables: print "processTemp, table = ", t self.temp[t] = self.df[t]['ambT'] #print objT.head() print "Mean = ", self.temp[t].mean() self.temp[t] = self.temp[t].resample('10Min') #print objT.head() #objT = objT.cumsum() #objT.plot() #objT = objT.cumsum() #plt.figure(); df.plot(); plt.legend(loc='best') #plt.plot(objT) self.temp[t].plot() plt.ylabel('deg C') plt.show(block=False)