def __init__(self, myHost, myUser, myPasswd, myDb): # Connect to MySQL batabase. self.data = MySQLdb.connect(host=myHost, user=myUser, passwd=myPasswd, db=myDb) #self.conn = self.data self.myHost = myHost self.muUser = myUser self.myPasswd = myPasswd self.myDb = myDb self.c = self.data.cursor() self.mydb = myDb self.db = DataEntry(self.data, self.c) self.deviceTable = "Devices" # Table of all DAQ modules self.addressTable = "Addresses"
def getData(self): t = Twitter(auth=OAuth('Key goes here', 'Key goes here', 'Key goes here', 'Key goes here')) if t.statuses.home_timeline()[0]['text'] != None: twts = [] for i in range(5): twts.append(DataEntry(t.statuses.home_timeline()[i]['text'])) self.entries = twts else: print("Error in pulling twitter data!")
def getData(self): # The xml retrieved from the Hacker News RSS feed is parsed by feedparser into a dictionary HNFeed = feedparser.parse(self._feedURL) # Checks the format of the XML to make sure that it is valid if not HNFeed["bozo"]: # Pull out the important info to display # Selects top(latest) 5 posts that fit query criteria for index in range(5): post1 = HNFeed['entries'][index]['title'] # Add post to list of posts that will be displayed self.entries.append(DataEntry(post1)) else: print("RSS app has recieved invalid XML")
def getData(self): self.updateService() events_results = self.service.events().list( calendarId='primary', timeMin=self.getTime(), maxResults=8, singleEvents=True, orderBy='startTime').execute() self.events = events_results.get('items', []) if not self.events: print('No upcoming events found.') else: for event in self.events: start = event['start'].get('dateTime', event['start'].get('date')) print(start, event['summary']) self.entries.append(DataEntry(event['summary'], start))
def readFromTimeStampSKR(firstTimeStampIn): # read the first line of QKDSequece.log line = datafile.readline() print("Reading SKR data from QKDSequence.log file...") # list to hold QBER data entries dataEntriesSKR = [] # pass each line in file until program reaches first timestamp of interest while line != "": entryComps = line.split("INFO") # process timestamp of each entry timeStampComps = entryComps[0].split() calendarComps = timeStampComps[0].split("-") year = int(calendarComps[0]) month = int(calendarComps[1]) day = int(calendarComps[2]) timeComps = timeStampComps[1].split(":") hour = int(timeComps[0]) minute = int(timeComps[1]) second = int(timeComps[2]) entryTimestamp = TimeStamp(year, month, day, hour, minute, second) if entryTimestamp.__str__() == enteredTimestamp.__str__(): break line = datafile.readline() # read each line until the end of the file while line != "": # split the entry into a timestamp and a message entryComps = line.split("INFO") # process timestamp of each entry timeStampComps = entryComps[0].split() calendarComps = timeStampComps[0].split("-") year = int(calendarComps[0]) month = int(calendarComps[1]) day = int(calendarComps[2]) timeComps = timeStampComps[1].split(":") hour = int(timeComps[0]) minute = int(timeComps[1]) second = int(timeComps[2]) entryTimestamp = TimeStamp(year, month, day, hour, minute, second) # Not all message types follow the same structure. If the entry # components list is only of length 1, it is not SKR data and can be ignored if len(entryComps) > 1: # split the message into message type and message title/value messageComps = entryComps[1].split(":") # make sure that there is more than one component in the message if len(messageComps) > 1: # if the entry contains secret key rate data, record this entry if "Secret Key Rate" in messageComps[1]: # splits privacy amplification data into title of value and value type = "Secret Key Rate" # extract the SKR value from the entry titleAndValue = messageComps[1].split("=") valueAndUnit = titleAndValue[1].split() value = float(valueAndUnit[0]) entrySKR = DataEntry(entryTimestamp, type, value) dataEntriesSKR.append(entrySKR) # read next line of data line = datafile.readline() datafile.close() # Create a file to store SKR Data SKRfile = open("SKR.txt", "w+") for entry in dataEntriesSKR: SKRfile.write(str(entry.timestamp.getRelativeSeconds(enteredTimestamp)) \ + "\t" + str(entry.value) + "\n") print(str(entry)) SKRfile.close()
def readFromStartSKR(): # read the first line line = datafile.readline() dataEntriesSKR = [] # variables used to save first timestamp of file in order to calculate the # relative time between a given entry and the first entry in the file first = True firstTimeStamp = None # read each line until the end of the file while line != "": # split the entry into a timestamp and a message entryComps = line.split("INFO") # process timestamp of each entry timeStampComps = entryComps[0].split() calendarComps = timeStampComps[0].split("-") year = int(calendarComps[0]) month = int(calendarComps[1]) day = int(calendarComps[2]) timeComps = timeStampComps[1].split(":") hour = int(timeComps[0]) minute = int(timeComps[1]) second = int(timeComps[2]) entryTimestamp = TimeStamp(year, month, day, hour, minute, second) # save the timestamp of the first entry if first: firstTimestamp = entryTimestamp first = False # Not all message types follow the same structure. If the entry # components list is only of length 1, it is not SKR data and can be ignored if len(entryComps) > 1: # split the message into message type and message title/value messageComps = entryComps[1].split(":") # if the entry contains secret key rate data, record this entry if "Secret Key Rate" in messageComps[1]: # splits privacy amplification data into title of value and value type = "Secret Key Rate" # extract the SKR value from the entry titleAndValue = messageComps[1].split("=") valueAndUnit = titleAndValue[1].split() value = float(valueAndUnit[0]) entrySKR = DataEntry(entryTimestamp, type, value) dataEntriesSKR.append(entrySKR) # read next line of data line = datafile.readline() datafile.close() # Create a file to store SKR Data SKRfile = open("SKR.txt", "w+") for entry in dataEntriesSKR: SKRfile.write(str(entry.timestamp.getRelativeSeconds(firstTimestamp)) \ + "\t" + str(entry.value) + "\n") print(str(entry)) SKRfile.close()
class DbManager: #conn = None #self.myHost = None #self.myUser = None #self.myPasswd = None #self.myDb = None def __init__(self, myHost, myUser, myPasswd, myDb): # Connect to MySQL batabase. self.data = MySQLdb.connect(host=myHost, user=myUser, passwd=myPasswd, db=myDb) #self.conn = self.data self.myHost = myHost self.muUser = myUser self.myPasswd = myPasswd self.myDb = myDb self.c = self.data.cursor() self.mydb = myDb self.db = DataEntry(self.data, self.c) self.deviceTable = "Devices" # Table of all DAQ modules self.addressTable = "Addresses" # Send a query to the database and reconnect if the user has # timed out. # Param sql the SQL query to send to the database. # Returns A cursor object containing the query results def __query(self, sql): try: #cursor = self.conn.cursor() print(sql) self.c.execute(sql) except (AttributeError, MySQLdb.OperationalError): #self.connect() self.data = MySQLdB.connect(host=self.myHost, user=self.myUser, passwd=self.myPasswd, db=self.myDb) self.c = self.data.cursor() self.c.execute(sql) return self.c.fetchall() # Find all tables for DAQ modules. # Returns list of tables. def __getTables(self): try: self.c.execute( "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_TYPE = 'BASE TABLE' " + "AND TABLE_SCHEMA='" + self.mydb + "'") return self.c.fetchall() except: pass # Find device name from device ID. # Param deciceID The ID given to the DAQ module as defined by # the `Device ID` column in the Devices table. # Returns the name of the device. def __decodeID(self, deviceID): try: self.c.execute("SELECT `Table Name` FROM `" + self.deviceTable + "` WHERE " + "`Device ID` = " + ` deviceID `) return self.c.fetchall()[0][0] except: pass # Find all registered devices. # Returns a list of all registered devices. def __getDevices(self): try: self.c.execute("SELECT `Table Name` FROM `" + self.deviceTable + "`") return self.c.fetchall() except: pass # Find the number of sensors that is supposed to be attached to a device. # Param DeviceID The ID given to the DAQ module. # Returns The number of sensors registered to a device. def getSensorCount(self, DeviceID): try: self.c.execute("SELECT `Sensors` FROM `" + self.deviceTable + "` WHERE " + "`Device ID` = " + ` DeviceID `) return int(self.c.fetchall()[0][0]) except: pass # Find if a table exists for the geven DAQ module. # Param table A lists of all tables for DAQ modules. # Param device The mane of the device to see whether a table # already exists for it. # Returns True if the table already exists, False otherwise. def __tableExists(self, tables, device): if (any(device in x for x in tables)): return True else: return False # Create a new table for the given DAQ module # Param tablename The device's name to be used as a table name. # Param numSensors The number of sensors registered to the DAQ module # Sensors will be named in the format: Sensor1, Sensor2, ... , Sensor N # Returns True if the table was created successfully, False otherwise. def __createTable(self, tableName, numSensors): query = "CREATE TABLE `" + tableName + "` (`TimeStamp` timestamp, " for i in range(0, numSensors): query += "`Sensor" + ` (i + 1) ` + "` double," query = query[:-1] query += ');' print(query) try: self.c.execute(query) return True except: return False # Insert a new entry into the database. # Param DeviceID The ID given to the DAQ module. # Param numSensors The number of sensors registered to the device. # Param data the data to be inserted into the database. Inserted data # should be of the form ["NULL" (or timestamp), Sensor1 data, # sensor2 data, ..., SensorN data] # Return True if the data was successfully inserted, false otherwise. def insertData(self, deviceID, numSensors, data): table = self.__decodeID(deviceID) if (not self.__tableExists(self.__getTables(), table)): print("Creating Table") if (not self.__createTable(table, numSensors)): print("Table Creation Failed") if (self.db.insertData(table, data)): return True else: return False # Find the sample rate for a given DAQ module. # Param deviceID The ID given to the DAQ module. # Return The sample rate of the device as defined in # `Time Interval (Min)` column of the Devices table. def getSampleRate(self, deviceID): try: self.c.execute("SELECT `Time Interval (Min)` FROM `" + self.deviceTable + "` WHERE " + "`Device ID` = " + ` deviceID `) return self.c.fetchall()[0][0] except Exception as e: print(e) pass # Find the number of Devices registered in the system. This # function ignores the Eagle Eye as it behaves differently # from all other DAQ modules. # Return The number of devices registered in the system. def getDeviceCount(self): try: self.c.execute("SELECT COUNT(*) FROM `" + self.deviceTable + "` WHERE `Device ID` > 0") return int(self.c.fetchall()[0][0]) except: pass # Find all IDs for registered devices in the database # Return A double indexed list containing all registered device IDs def getAllIds(self): try: self.c.execute("SELECT `Device ID` FROM `" + self.deviceTable + "`") return self.c.fetchall() except: pass # Find the highest device id registered in the database. # Return the Highest registered ID as an integer def getMaxID(self): try: self.c.execute("SELECT MAX(`Device ID`) FROM `" + self.deviceTable + "`") return int(self.c.fetchall()[0][0]) except: pass # Insert a new device into the device table # Param id The ID of the device # Param sensors The number of sensors attached to the DAQ module # Return True if successfully inserted, False otherwise def createDevice(self, id, sensors): data = [id, "\"device " + str(id) + "\"", "10", str(sensors)] if (self.db.insertData(self.deviceTable, data)): return True else: return False # Get the contents of the selected table. # Param table The table whose contents will be found. # Param lowerBound The oldest data to be retrieved. # Param upperBound The newest data to be retrieved. # Return the contents of the table as a double indexed list. def getIntervalData(self, table, lowerBound, upperBound): try: self.c.execute("SELECT * FROM `" + table + "` WHERE `TimeStamp` BETWEEN \"" + lowerBound + "\" AND \"" + upperBound + "\"") except Exception as e: print(e) return self.c.fetchall() # Translate a MAC address to a sensor number for the think tank data logger. # table - The table the MAC-to-Sensor translation is defined. # mac - The mac address to be translated to a sensor number. # return - The MAC-to-Sensor translation. def getTTSensor(self, table, mac): try: #self.c.execute("SELECT `Sensor Number` FROM `" + table + "` Where `MAC Addr` = \"" + # mac + "\"") return self.__query("SELECT `Sensor Number` FROM `" + table + "` Where `MAC Addr` = \"" + mac + "\"")[0][0] except Exception as e: print(e) #return int(self.c.fetchall()[0][0]) # Get a list of MOC addresses that are currently in the database. # return - A list of MAC addresses that are currently in the database. def getMac(self, table): try: __oldMac = [] ##__table = self.__decodeID(deviceID) #__table = "Think Tank 2" print(table) __dbMac = self.__query("SELECT `MAC Addr` FROM `" + table + "` WHERE 1") for mac in __dbMac: __oldMac.append(mac[0]) return __oldMac except Exception as e: print(e) # Update the MAC addresses in database and assign an abitrary sensor number. # oldData - The data that is currently in the database. # newData - The data to be put into that database. # table - The meta data table. # return - True if updated successfully, false otherwise. def updateMac(self, table, oldData, newData): try: __newAddrCount = 0 if (len(oldData) < len(newData)): __newAddrCount = len(oldData) else: __newAddrCount = len(newData) for i in range(0, __newAddrCount): __query = "UPDATE `" + table + "` SET `MAC Addr`=\'" + newData[ i] + "\' WHERE `Sensor Number` = " + ` i + 1 ` print(__query) self.c.execute(__query) self.c.fetchall() self.data.commit() #time.sleep(1) return True except Exception as e: print(e) self.data.rollback() return False
calendarComps = dataTimestampString.split("-") year = int(calendarComps[0]) month = int(calendarComps[1]) day = int(calendarComps[2]) timeComps = calendarComps[3].split(":") hour = int(timeComps[0]) minute = int(timeComps[1]) second = int(timeComps[2]) entryTimestamp = TimeStamp(year, month, day, hour, minute, second) # extract type and value of data entry type = dataEntryComps[5] value = dataEntryComps[6] # add data entry to an array of all data entries following initial timestamp dataEntry = DataEntry(entryTimestamp, type, value) dataEntries.append(dataEntry) # read next line of rawlogs line = datafile.readline() datafile.close() # create a file with the QBER readings and the time of their measurement QBERFile = open("QBER.txt", "w+") # If the data entry is a QBER entry, record its time and value in # corresponding files. Also, print the string rep of QBER data entry # for debugging for entry in dataEntries: if entry.type == "QBER":
def __getitem__(self, item): if isinstance(item, tuple): return self.dataDict[self.name][item] self.points[self.name].getRow(item, self.tempPoint) self.values[self.name].getRow(item, self.tempValue) return DataEntry(self.tempPoint, self.tempValue)
def next(self): for row in xrange(0, self.size): self.points[self.name].getRow(row, self.tempPoint) self.tempValue = self.values[self.name][row] yield DataEntry(self.tempPoint, self.tempValue) return