def parseProcess(self, process): """ Converts a psutil process into a BrewPiProcess object by parsing the config file it has been called with. Params: a psutil.Process object Returns: BrewPiProcess object """ bp = BrewPiProcess() db_config = None cfg = None try: bp.pid = process._pid try: # If this is a database configured installation, try loading via the process ID db_config = models.BrewPiDevice.objects.get(process_id=bp.pid) except: cfg = [s for s in process.cmdline() if '.cfg' in s] # get config file argument except psutil.NoSuchProcess: # process no longer exists return None if db_config is not None: # If this is a database-configured installation, use the database configuration bp.cfg = util.read_config_from_database_without_defaults(db_config) else: if cfg: cfg = cfg[0] # add full path to config file else: # use default config file location cfg = util.scriptPath() + "/settings/config.cfg" bp.cfg = util.read_config_file_with_defaults(cfg) bp.port = bp.cfg['port'] bp.sock = BrewPiSocket.BrewPiSocket(bp.cfg) return bp
return self.__asciiToUnicode(lines[0]) # remove extended ascii characters from string, because they can raise UnicodeDecodeError later def __asciiToUnicode(self, s): s = s.replace(chr(0xB0), '°') return unicode(s, 'ascii', 'ignore') if __name__ == '__main__': # some test code that requests data from serial and processes the response json import simplejson import BrewPiUtil as util # TODO - Rewrite the test code below to work with the database config_file = util.addSlash(sys.path[0]) + 'settings/config.cfg' config = util.read_config_file_with_defaults(config_file) ser = util.setupSerial(config, time_out=0) if not ser: printStdErr("Could not open Serial Port") exit() bg_ser = BackGroundSerial(ser) bg_ser.start() success = 0 fail = 0 for i in range(1, 5): # request control variables 4 times. This would overrun buffer if it was not read in a background thread # the json decode will then fail, because the message is clipped bg_ser.writeln('v') bg_ser.writeln('v')