def run(self): print 'Begin communication thread' for channel in self.channels.values(): print channel.name self.err = 0 lp = 0 while not self.stop_event.is_set(): try: # t1 = time.clock() tc = current_time(self.config) prnt = True # if tc-lp > 1: # prnt = True for channel in self.channels.values(): channel.measure() if prnt: print "sending " + channel.name print "Measured :" + repr(channel.data) ts = current_time(self.config) data = channel.data #print channel.data data.update({TIMESTAMP: ts}) #print(data) ### Following try/except loop enables upload data to server try: #print channel.data channel.connection.send(**channel.data) except KeyError as e: close_all(self.channels.values()) print "Error sending data for stream : {} \n{}".format( channel.name, e) raise Exception self.queues[channel.name].put(data) # interrupt this with a keystroke and hang connection if self.err == 1: break time.sleep(measurementPeriod) # FOR TIMING: # t2 = time.clock() # deltaT = t2 - t1 # time.sleep(measurementPeriod - deltaT) except KeyboardInterrupt: # Trying to handle turning off the program monitor without leaving devices on and streams open #close_all(channels) # ^ NOW HANDLED BY GUI raise KeyboardInterrupt break print "Stopping communication thread."
def validate_time_range(self, start, stop): '''Make sure time range is valid, if not rearrange start and stop times Returns tuple of validated (start, stop) times in 32b format ''' try: stop = long(stop) * 2**32 except TypeError: self.logger.debug("Using default stop time") stop = current_time(self.config) try: start = long(start) * 2**32 except TypeError: self.logger.debug("Using default start time") start = stop - 5 * 60L * 2**32 # 5 minute range default if start > stop: self.logger.warning( "Requested read range out of order. Swapping range.") self.logger.debug( "Read request time range (start, stop): ({},{})".format( start, stop)) return (stop, start) self.logger.debug( "Read request time range (start, stop): ({},{})".format( start, stop)) return (start, stop)
def measurement(self, stream, measurements): '''Perfoms measurement validation, timestamps data if missing, The saves to destination. Returns a tuple of (error, result_text, measurements) where error=0 for success error: 0 for successful operation result_text: message to return to client measurements: processed data, empty dict if error ''' if stream not in self.known_streams.keys(): msg = "trying to add a measurement to data on an unknown stream: {}" self.logger.warning(msg.format(stream)) return (1, "Unknown stream", {}) if not measurement_validation(measurements, self.known_stream_versions[stream]): self.logger.warning( "Measurement didn't validate against the pre-determined format" ) return (1, "Invalid measurements against schema", {}) try: if measurements[TIMESTAMP] == 0: raise KeyError except KeyError: measurements[TIMESTAMP] = current_time(self.config) self.insert_measurement(stream, measurements) result = 0 result_text = "" return (result, result_text, measurements)
def validate_time_range(self, start, stop): """!@brief Make sure the time range is valid, if not rearrange start and stop times. @param start 32b unix timestamp that defines the start of the data window @param stop 32b unix timestamp that defines the end of the data window @return tuple of validated (start, stop) times in 32b format """ try: stop = long(stop) * 2**32 except TypeError: self.logger.debug("Using default stop time") stop = current_time(self.config) try: start = long(start) * 2**32 except TypeError: self.logger.debug("Using default start time") start = stop - 5 * 60L * 2**32 # 5 minute range default if start > stop: msg = "Requested read range out of order. Swapping range." self.logger.warning(msg) msg = "Read request time range (start, stop): ({},{})" self.logger.debug(msg.format(start, stop)) return (stop, start) msg = "Read request time range (start, stop): ({},{})" self.logger.debug(msg.format(start, stop)) return (start, stop)
def measurement(self, stream, measurements): """!@brief Perfom measurement validation, timestamp data if missing field, then save to destination. @return a tuple of (error, result_text, measurements) error: 0 for successful operation result_text: message to return to client measurements: processed data, empty dict if error """ if stream not in self.known_streams.keys(): msg = ( "Trying to add a measurement to data on an unknown stream: {}" ) self.logger.warning(msg.format(stream)) return (1, "Unknown stream", {}) if not measurement_validation(measurements, self.known_stream_versions[stream]): msg = "Measurement didn't validate against the existing format" self.logger.warning(msg) return (1, "Invalid measurements against schema", {}) try: if measurements[TIMESTAMP] == 0: raise KeyError except KeyError: measurements[TIMESTAMP] = current_time(self.config) self.insert_measurement(stream, measurements) result = 0 result_text = "" return (result, result_text, measurements)
def measurement(self, stream, measurements): """!@brief Perfom measurement validation, timestamp data if missing field, then save to destination. @return a tuple of (error, result_text, measurements) error: 0 for successful operation result_text: message to return to client measurements: processed data, empty dict if error """ if stream not in self.known_streams.keys(): msg = ( "Trying to add a measurement to data on an unknown stream: {}" ) self.logger.warning(msg.format(stream)) return (1, "Unknown stream", {}) if not measurement_validation(measurements, self.known_stream_versions[stream]): msg = "Measurement didn't validate against the existing format" self.logger.warning(msg) return (1, "Invalid measurements against schema", {}) try: if measurements[TIMESTAMP] == 0: raise KeyError except KeyError: measurements[TIMESTAMP] = current_time(self.config) # pointer to either queue_measurement or insert_measurement, resolved in __init__ self.inserter(stream, measurements) result = 0 result_text = "" return (result, result_text, measurements)
def sendOutput(stream_id, data, origin_config, inputFields='A', outputFields='B', matrix="B"): # convert temp from mC to C print data input_vector = [] for input in inputFields: input_vector.append(data[input]) output_vector = np.matmul(np.asarray(matrix), np.asarray(input_vector)) iter = 0 ts = current_time(origin_config) data = {TIMESTAMP: ts} for output in output_vector: data.update({outputFields[iter]: output}) iter += 1 print "\n" print data print "\n" return data
def insert_measurement(stream, template, method): '''Sets up data and saves ''' time64 = current_time(CONFIG) # 64b time data = {TIMESTAMP: time64} for key in template: data[key] = random_data(template[key]) ret = method(stream, data) if ret: return ret, data else: return data
}) print "success" # perhaps print some useful message. Perhaps try to reconnect.... # print "problem establishing connection to server..." # sys.exit(1) # This might need to be more complicated, but you get the gist. Keep sending records forever time.sleep(1) avgs=20 lockState=0 lastLock = -1 while True: ts = current_time(config) trans = 0 lock = 1 for i in range(avgs): tempTrans,tempLock =get_ule_state() trans += tempTrans/avgs if(tempLock != 1): lock = 0 if(lock!=1): if(lockState==1): print "" print "!"*60 print "*"*60 print "!"*60 print "780 Laser Unlock Event Detected"
totangle = [0, 0, 0, 0] error = 0 integral = 0 output = 0 calcount = 0 fullflag = [0, 0, 0, 0] odata = [0, 0, 0, 0] ndata = [0, 0, 0, 0] esign = [1, 1, 1, 1] error_array = [0, 0, 0, 0] ad.SetEnableBuffer(enablebuffer) ad.SelfCalibrate() data_to_save = [] reference_time = current_time(config) while mainloopflag == 1: calcount = 0 for j in range(4): ts = current_time(config) data = [] if trig[j].lent > 0: GPIO.wait_for_edge(trig[j].trigpin,GPIO.RISING)\ # time.sleep(1) else: continue for k in range(trig[j].lent): print k
def sendOutput(self): ts = current_time(self.origin_config) data = {TIMESTAMP: ts, "Output": self.output} self.connection.send(**data)
# channels.append(magChannel("B","float",serv,["X,Y,Z"])) # This might need to be more complicated, but you get the gist. Keep sending records forever time.sleep(10) print 'begin communication' err = 0 #TODO : Make timinig consistent despite wait blocks in monitor classes #TODO : Write data to channels in multiple threads once the number of channels gets large while True: try: #t1 = time.clock() for channel in channels: print "sending " + channel.name print "Measured :" + repr(channel.measure()) ts = current_time(config) data = channel.data data.update({TIMESTAMP: ts}) try: channel.connection.send(**channel.data) except: closeAll(channels) err = 1 break print(data) #interrupt this with a keystroke and hang connection if err == 1: break time.sleep(measurementPeriod) #FOR TIMING: #t2 = time.clock()
serv = server() # alert the server that we are going to be sending this type of data connection = serv.registerStream( stream="Rb_Mogwave", records={ "f_air": "float", "f_vac0RH": "float", "f_vac100RH": "float", "f_vacNTP": "float", "temp_box_c": "float", "temp_sensor_c": "float", "pressure_torr": "float" }) # perhaps print some useful message. Perhaps try to reconnect.... # print "problem establishing connection to server..." # sys.exit(1) # This might need to be more complicated, but you get the gist. Keep sending records forever time.sleep(5) while True: print "sending...." data = retrieve_reading() data[timestamp] = current_time(origin.config) connection.send(**data) #print("toy1: {}\ntoy2: {}".format(t1,t2)) time.sleep(10)