def handle_measurement(self, worker_measurement_data): #log.debug('Receiving worker measurement...') try: with self.measurements_lock: worker_measurement = WorkerMeasurement.deserialize_message( worker_measurement_data) measurement = Measurement() measurement.worker = worker_measurement.worker_name session_detail_id = worker_measurement.session_detail_id session_detail = SessionDetail.objects.get( pk=session_detail_id) measurement.session_detail = session_detail measurement.device = worker_measurement.device_name measurement.value = worker_measurement.value measurement.set_point = worker_measurement.set_point measurement.work = worker_measurement.work measurement.remaining = worker_measurement.remaining if not worker_measurement.debug_timer is None: # In simulation mode, use fake timestamps #measurement.timestamp = datetime.strptime(worker_measurement.debug_timer, "%Y-%m-%d %H:%M:%S.%f") #measurement.timestamp = dt.get_current_timezone.localize(measurement.timestamp) measurement.timestamp = worker_measurement.debug_timer else: measurement.timestamp = dt.now() measurement.save() except Exception as e: log.error( 'Master could not save measurement to database ({0})'.format( e.args[0]))
def process_command(self, body): data = body.split(MessageSplit) command = data[0] if len(data) > 1: worker_id = data[1] else: worker_id = None if len(data) > 2: instruction = data[2] else: instruction = None if not hasattr(self, command): log.error('No such command in Master ({0})'.format(command)) return method = getattr(self, command) if command in self.broadcasts.keys(): method() elif command in self.messages.keys(): method(worker_id) elif command in self.instructions.keys(): method(worker_id, instruction) else: log.debug( 'Master requested to send an unauthorized command: {0}'.format( command))
def parse_config(configfile=defaults.DEFAULT_CONFIG): try: config = cfg.Config(configfile) return config except Exception as e: log.error('Unable to parse config: {0}'.format(e.args[0])) return None
def send(self, data, worker=""): try: if worker == "": self.master_socket.send_string(data) else: data = "{0}#{1}".format(worker, data) self.master_socket.send_string(data) except Exception as e: log.error('Error sending message {0} ({1})'.format(data, e))
def run_device(self): if self.enabled: return if hasattr(self.owner, self.callback_name): self.callback = getattr(self.owner, self.callback_name) else: log.error('Callback function {0} not found for owner {1}'.format( self.callback_name, self.owner)) self.auto_setup() self.start()
def finish(self): try: self.pause_all_devices() self.working = False self.done() self.session_detail_id = 0 return True except Exception as e: log.error('Error in cleaning up after work: {0}'.format(e.args[0]), True) return False
def check(self): try: if self.queue == CONNECTION_MASTER_QUEUE: return self.worker_socket.recv_string() else: data = self.worker_socket.recv_string() pos = data.find("#") data = data[pos + 1:] return data except Exception as e: log.error('Error receiving message ({0})'.format(e))
def load_device(config, owner, simulation): try: device_instance = construct_class_instance(config.device_class, config) if not issubclass(device_instance.__class__, devices.device.Device): log.error('Unable to load device from config: {0} is not a subclass of core.devices.device.Device'. format(config.class_name)) return None device_instance.owner = owner device_instance.simulation = simulation return device_instance except Exception as e: log.error('Unable to load device from config: {0}'.format(e.args[0])) return None
def brews_import(request): uri = None try: if request.POST: index = int(request.POST['index']) file = request.FILES['file'] filename = request.FILES['file'].name uri = os.path.dirname(os.path.realpath(__file__)) uri += '/importing/uploaded_files/' uri += filename fd = open(uri, 'wb') for chunk in file.chunks(): fd.write(chunk) fd.close() brew_importer.BrewImporter.import_brews(index, uri) except Exception as e: try: if uri is not None: log.error('Failed to upload file {0} : {1}'.format( uri, e.args[0])) else: log.error('Failed to upload file : {0}'.format(e.args[0])) except Exception: if uri is not None: log.error('Failed to upload file {0}'.format(uri)) else: log.error('Failed to upload file') return HttpResponseRedirect('/brew/brews/')
def load_worker(communication_config, config): try: worker_instance = construct_class_instance(config.class_name, config.name) if not issubclass(worker_instance.__class__, workers.baseworker.BaseWorker): log.error('Unable to load worker from config: {0} is not a subclass of core.workers.BaseWorker'. format(config.class_name)) return None ip = communication_config.ip master_port = int(communication_config.master_port) worker_port = int(communication_config.worker_port) worker_instance.init_communication(ip, master_port, worker_port) worker_instance.simulation = config.simulation worker_instance.input_config = config.inputs worker_instance.output_config = config.outputs return worker_instance except Exception as e: log.error('Unable to load worker from config: {0}'.format(e.args[0])) return None
def do_import(self, uri): try: if uri is None: return 0 self.recipe_file = uri log.debug('Loading recipe file {0}...'.format(self.recipe_file)) beer = BeerParser() recipe_data = beer.get_recipes(self.recipe_file) self.counter = 0 for item in recipe_data: name = lookup_brew_name(item).strip() if name is not None: # Save to database brew, sections = create_brew_model(item) brew.save() self.counter += 1 s_count = m_count = b_count = f_count = 0 for section in sections: s_count += 1 s = create_section(section, brew, s_count) s.save() for step in section.steps: brew_step = None if s.worker_type == 'core.workers.mash.MashWorker': m_count += 1 brew_step = create_mash_step(step, s, m_count) elif s.worker_type == 'core.workers.boil.BoilWorker': b_count += 1 brew_step = create_boil_step(step, s, b_count) elif s.worker_type == 'core.workers.fermentation.FermentationWorker': f_count += 1 brew_step = create_fermentation_step( step, s, f_count) if brew_step is not None: brew_step.save() log.debug('...done loading recipe file {0}'.format( self.recipe_file)) except Exception as e: log.error('Failed to load recipes {0} ({1})'.format( self.recipe_file, e.args[0])) return self.counter
def serialize_message(worker_measurement): if worker_measurement is None: log.error( 'Unable to deserialize worker measurement, object is None') if not worker_measurement.__class__ is WorkerMeasurement: log.error( 'Unable to serialize worker measurement, object is of type {0}' .format(worker_measurement.__class__.__name)) else: try: if not worker_measurement.debug_timer is None: worker_measurement.debug_timer = str( worker_measurement.debug_timer) return MessageMeasurement + MessageSplit + json.dumps( worker_measurement.__dict__) except Exception: log.error( 'Unable to serialize worker measurement, object is {0}'. format(worker_measurement)) return None
def deserialize_message(serialized_worker_measurement): if serialized_worker_measurement is None: log.error( 'Unable to deserialize worker measurement, serialized object is None' ) if not serialized_worker_measurement.startswith(MessageMeasurement + MessageSplit): log.error('Serialized worker measurement should begin with {0}{1}'. format(MessageMeasurement, MessageSplit)) else: try: serialized_worker_measurement = serialized_worker_measurement[ len(MessageMeasurement + MessageSplit):] worker_measurement_data = json.loads( serialized_worker_measurement) worker_measurement = WorkerMeasurement() worker_measurement.session_detail_id = worker_measurement_data[ 'session_detail_id'] worker_measurement.worker_name = worker_measurement_data[ 'worker_name'] worker_measurement.device_name = worker_measurement_data[ 'device_name'] worker_measurement.value = worker_measurement_data['value'] worker_measurement.set_point = worker_measurement_data[ 'set_point'] worker_measurement.work = worker_measurement_data['work'] worker_measurement.remaining = worker_measurement_data[ 'remaining'] if 'debug_timer' in worker_measurement_data.keys(): worker_measurement.debug_timer = worker_measurement_data[ 'debug_timer'] else: worker_measurement.debug_timer = None return worker_measurement except Exception: log.error( 'Unable to deserialize worker measurement, serialized object is {0}' .format(serialized_worker_measurement)) return None
def report_error(self, err): log.error('{0}: {1}'.format(self.name, err), True)
def run(self): if not self.create_device_threads(): log.error('Unable to load all devices, shutting down', True) self.info() self.listen()
def register(self): if self.simulation: return True log.error("Can not register probe at \"{0}\", try to run \"sudo modprobe w1-gpio && sudo modprobe w1_therm\" in commandline or check your probe connections".format(self.io))