def measure(configFile, logFile, sensorNames, triggers=None, digitempCommand=DIGITEMPCMD):

    archas.logDebug("Measuring temperature, writing results to '%s'..." % logFile)

    if archas.createFileIfNotThere(configFile, _DIGITEMP_CONFIG_TEMPLATE):
        archas.logWarning("Digitemp config file not available. Created template at %s" % configFile)
    
    s, o = commands.getstatusoutput("%s -q -c %s -a" % (digitempCommand, configFile))
    if not os.WIFEXITED(s) or os.WEXITSTATUS(s) != 0:
        archas.logError("Digitemp failed with: %s (%i)" %(o, s))
        archas.logInfo(("Perhaps you are not in the 'dialout' group "
                        "or used the digitemp command for the wrong "
                        "serial controller"))
        return None
    
    archas.logDebug(o)
    
    with open(logFile, "a") as myfile:
        myfile.write(o)
        myfile.write("\n")
    
    sensors = {}
    
    for l in o.splitlines():
        v = parseLogLine(l)
        if v is not None:
            (date, sensor, temp) = v
            sensors[sensor] = (date, temp)
            if triggers is not None:
                # TODO: Process triggers use sensorNames in messages
                pass            
    
    return sensors
Exemple #2
0
def capture(configFile, outputPaths, temperature):
    archas.logDebug("Capturing to '%s'..." % str(outputPaths))

    if archas.createFileIfNotThere(configFile, _FS_WEBCAM_CONFIG_TEMPLATE):
        archas.logWarning(
            "Fswebcam config file not available. Created template at %s" %
            configFile)

    picFile = tempfile.NamedTemporaryFile()

    with open(configFile, 'r') as f:
        content = f.read().decode("utf-8")

    content = content.replace("__TEMP__", temperature)
    content = content.replace("__PATH__", picFile.name)

    confFile = tempfile.NamedTemporaryFile()
    confFile.write(content.encode("utf-8"))
    confFile.flush()

    p = subprocess.Popen([
        "fswebcam",
        "-c",
        confFile.name,
    ],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    out, err = p.communicate()
    if p.returncode < 0:
        archas.logError("Capture has been terminated by signal %i" %
                        -p.returncode)
        return False
    if p.returncode > 0:
        archas.logError("Capture failed with %s %s (%i)" %
                        (out, err, p.returncode))
        return False

    archas.logDebug("Capture: %s\n%s" % (out, err))

    picFile.seek(0, os.SEEK_END)
    picBytes = picFile.tell()
    if picBytes < 100:
        archas.logError(
            "Capture failed without returning error code. But output image has no (%i bytes) of data."
        )
        return False

    for out in outputPaths:
        import shutil
        shutil.copy(picFile.name, out)

    return True
def capture(configFile, outputPaths, temperature):
    archas.logDebug("Capturing to '%s'..." % str(outputPaths))

    if archas.createFileIfNotThere(configFile, _FS_WEBCAM_CONFIG_TEMPLATE):
        archas.logWarning("Fswebcam config file not available. Created template at %s" % configFile)
    
    picFile = tempfile.NamedTemporaryFile()
    
    with open(configFile, 'r') as f:
        content = f.read().decode("utf-8")
    
    content = content.replace("__TEMP__", temperature)
    content = content.replace("__PATH__", picFile.name)

    confFile = tempfile.NamedTemporaryFile()
    confFile.write(content.encode("utf-8"))
    confFile.flush()
        
    p = subprocess.Popen(["fswebcam", "-c", confFile.name,],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    out, err = p.communicate()
    if p.returncode < 0:
        archas.logError("Capture has been terminated by signal %i" % -p.returncode)
        return False
    if p.returncode > 0:
        archas.logError("Capture failed with %s %s (%i)" %(out, err, p.returncode))
        return False
    
    archas.logDebug("Capture: %s\n%s" % (out, err))
    
    picFile.seek(0, os.SEEK_END)
    picBytes = picFile.tell()
    if picBytes < 100:
        archas.logError("Capture failed without returning error code. But output image has no (%i bytes) of data.")
        return False
    
    for out in outputPaths:
        import shutil
        shutil.copy(picFile.name, out)
    
    return True
def measure(configFile,
            logFile,
            sensorNames,
            triggers=None,
            digitempCommand=DIGITEMPCMD):

    archas.logDebug("Measuring temperature, writing results to '%s'..." %
                    logFile)

    if archas.createFileIfNotThere(configFile, _DIGITEMP_CONFIG_TEMPLATE):
        archas.logWarning(
            "Digitemp config file not available. Created template at %s" %
            configFile)

    s, o = commands.getstatusoutput("%s -q -c %s -a" %
                                    (digitempCommand, configFile))
    if not os.WIFEXITED(s) or os.WEXITSTATUS(s) != 0:
        archas.logError("Digitemp failed with: %s (%i)" % (o, s))
        archas.logInfo(("Perhaps you are not in the 'dialout' group "
                        "or used the digitemp command for the wrong "
                        "serial controller"))
        return None

    archas.logDebug(o)

    with open(logFile, "a") as myfile:
        myfile.write(o)
        myfile.write("\n")

    sensors = {}

    for l in o.splitlines():
        v = parseLogLine(l)
        if v is not None:
            (date, sensor, temp) = v
            sensors[sensor] = (date, temp)
            if triggers is not None:
                # TODO: Process triggers use sensorNames in messages
                pass

    return sensors
 def unRegisterRemoteGetTransformator(self, key):
     try:
         del self.remoteGetTransformators[key]
     except:
         archas.logWarning('No remote get transformator for "%s" to remove'%key)
    def sync(self):
        changesLoaded = False
        self.lock.acquire()
        try:
            config = RawConfigParser()
            config.read(self.configFile)
        except Exception as err:
            archas.logError("Config: Can not open %s to sync config: %s" % (self.configFile,str(err)))
            err = WH10_CONFIG_ERR_READ_CONFIG
        else:
            if not config.has_section(self.configSection):
                config.add_section(self.configSection)
            
            err = WH10_CONFIG_ERR_NO_ERR
            writeRequired = False
            for key,c in self.config.iteritems():
                if c.changed:
                    writeRequired = True
                    try:
                        if isinstance(c.value, int):
                            config.set(self.configSection, key, "%i" % c.value)
                        elif isinstance(c.value, float):
                            config.set(self.configSection, key, "%f" % c.value)
                        elif isinstance(c.value, unicode):
                            config.set(self.configSection, key, c.value.encode("utf-8"))
                        else:
                            archas.logError("Config: Invalid value type for %s"%key)
                        c.changed = False
                    except Exception as err:
                        archas.logError("Config: Failed to set config value %s (%s)." % (key, str(err)))
                        err = WH10_CONFIG_ERR_SET_CONFIG
                        
                try:
                    fileValue = None
                    if isinstance(c.default, int):
                        fileValue = config.getint(self.configSection, key)
                    elif isinstance(c.default, float):
                        fileValue = config.getfloat(self.configSection, key)
                    elif isinstance(c.default, unicode):
                        fileValue = config.get(self.configSection, key).decode("utf-8")
                    else:
                        archas.logError("Config: Invalid default value type for %s."%key)
                    if fileValue != None and fileValue != c.value:
                        valid = True
                        validator = self.validators.get(key, None)
                        if validator != None:
                            valid = validator(key, fileValue)
                        if not valid:
                            # We read the value as string for log message again
                            v = config.get(self.configSection, key).decode("utf-8")
                            archas.logWarning('Invalid value "%s" in configuration file for key "%s".'%(v, key))
                        else:
                            c.value = fileValue
                            c.changed = False
                            changesLoaded = True
                except Exception as e:
#                     wh10LogWarning("fail to get value for %s: %s. Setting default."%(key,str(e)))
                    c.value = c.default
                    c.changed = False
            if writeRequired:
                try:
                    with open(self.configFile, 'wb') as cfg_file:
                        config.write(cfg_file)
                except:
                    archas.logError("Config: Failed to write config values to %s." % self.configFile)
                    err = WH10_CONFIG_ERR_WRITE_CONFIG

        self.lock.release()
        
        # Config change handler is called on local and file changes
        if (changesLoaded or writeRequired):
            if callable(self.configChangeHandler):                            
                self.configChangeHandler()
            
        return err
 def unregisterValidator(self, key):
     try:
         del self.validators[key]
     except:
         archas.logWarning('No validator for "%s" to remove'%key)