def stop(self): """ stop process """ logger.logInfo("Stoping...") try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if not pid: logger.logInfo("Pidfile %s does not exist. Daemon not running." % self.pidfile) # no pidfile - no error return CONST.RET_OK #Try killing the daemon process try: logger.logDebug("Trying to kill pid %s" % pid) while True: os.kill(pid, SIGTERM) time.sleep(0.1) except OSError, err: err = str(err) if err.find("No such process") > 0: if os.path.exists(self.pidfile): self.delPID() else: logger.logError(str(err)) return CONST.RET_ERROR
def start(self): """ start main process if not debug - daemonize """ logger.logInfo("Starting...") pid = self.status() if pid: logger.logError("Daemon already running with pid %s" % self.pidfile) return CONST.RET_ERROR # Start the daemon if not self.debug: logger.logDebug("Demonizing...") if self.daemonize() > CONST.RET_OK: return CONST.RET_ERROR logger.logInfo("Running") try: self.run() logger.logDebug("Main loop finished") except Exception as err: logger.logError("Main loop error: %s" % str(err)) return CONST.RET_ERROR return CONST.RET_OK
def start(self): """ start main process if not debug - daemonize """ logger.logInfo("Starting...") pid = self.status() if pid: logger.logError("Daemon already running with pid %s" % self.pidfile) return CONST.RET_ERROR # Start the daemon if not self.debug: logger.logDebug("Demonizing...") if self.daemonize() > CONST.RET_OK: return CONST.RET_ERROR logger.logInfo("Running") try: self.run() logger.logDebug("Main loop finished") except Exception as err: logger.logError("Main loop error: %s" % str(err)) return CONST.RET_ERROR return CONST.RET_OK
def cecLogCallback(self, level, timestamp, message): """ cec-callback for log """ libCECloglevel_dict = {cec.CEC_LOG_ERROR: "ERROR", cec.CEC_LOG_WARNING: "WARNING", cec.CEC_LOG_NOTICE: "NOTICE", cec.CEC_LOG_TRAFFIC: "TRAFFIC", cec.CEC_LOG_DEBUG: "DEBUG", } if hasattr(CFG, 'CEC_LOG_TO_DEFAULT_LOG') and CFG.CEC_LOG_TO_DEFAULT_LOG: logger.logDebug("[CEC_LOG_%s] %s %s" % (libCECloglevel_dict[level], str(timestamp), message)) if level == cec.CEC_LOG_DEBUG: pattern_active_source = "^making .* \((.+?)\) the active source$" m_active_source = re.search(pattern_active_source, message) if m_active_source: iAddress = int(m_active_source.group(1), 16) osdName = self.getDeviceOSDName(int(iAddress)) logger.logInfo("Source %s is active" % osdName) try: self.sendEvents(EVENT.CECLOG_ACTIVESOURCE_TEMPLATE % osdName) except Exception as err: logger.logError("error %s" % str(err))
def stop(self): """ stop process """ logger.logInfo("Stoping...") try: pf = file(self.pidfile,'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if not pid: logger.logInfo("Pidfile %s does not exist. Daemon not running." % self.pidfile) # no pidfile - no error return CONST.RET_OK #Try killing the daemon process try: logger.logDebug("Trying to kill pid %s" % pid) while True: os.kill(pid, SIGTERM) time.sleep(0.1) except OSError, err: err = str(err) if err.find("No such process") > 0: if os.path.exists(self.pidfile): self.delPID() else: logger.logError(str(err)) return CONST.RET_ERROR
def cecLogCallback(self, level, timestamp, message): """ cec-callback for log """ libCECloglevel_dict = { cec.CEC_LOG_ERROR: "ERROR", cec.CEC_LOG_WARNING: "WARNING", cec.CEC_LOG_NOTICE: "NOTICE", cec.CEC_LOG_TRAFFIC: "TRAFFIC", cec.CEC_LOG_DEBUG: "DEBUG", } if hasattr(CFG, 'CEC_LOG_TO_DEFAULT_LOG') and CFG.CEC_LOG_TO_DEFAULT_LOG: logger.logDebug( "[CEC_LOG_%s] %s %s" % (libCECloglevel_dict[level], str(timestamp), message)) if level == cec.CEC_LOG_DEBUG: pattern_active_source = "^making .* \((.+?)\) the active source$" m_active_source = re.search(pattern_active_source, message) if m_active_source: iAddress = int(m_active_source.group(1), 16) osdName = self.getDeviceOSDName(int(iAddress)) logger.logInfo("Source %s is active" % osdName) try: self.sendEvents(EVENT.CECLOG_ACTIVESOURCE_TEMPLATE % osdName) except Exception as err: logger.logError("error %s" % str(err))
def restart(self): """ restart process - stop and start again """ logger.logInfo("Restarting...") if self.stop() == CONST.RET_OK: return self.start() return CONST.RET_ERROR
def restart(self): """ restart process - stop and start again """ logger.logInfo("Restarting...") if self.stop() == CONST.RET_OK: return self.start() return CONST.RET_ERROR
def listenUDPThread(self): """ UDP listener thread """ while True: if not self.sock: break logger.logInfo("UDP listener started on '%s'" % str(self.addr)) self.startComunication(self.addr, self.sock) logger.logInfo("UDP listener on '%s' ended" % str(self.addr))
def listenUDPThread(self): """ UDP listener thread """ while True: if not self.sock: break logger.logInfo("UDP listener started on '%s'" % str(self.addr)) self.startComunication(self.addr, self.sock) logger.logInfo("UDP listener on '%s' ended" % str(self.addr))
def listenTCPThread(self): """ TCP listener thread """ logger.logInfo("TCP listener started on '%s'" % str(self.addr)) while True: try: (client_sock, client_addr) = self.sock.accept() except socket.error as err: logger.logError("Failed to accept TCP connection. Error Code: " + str(err[0]) + ' Message: ' + err[1]) break logger.logDebug("Connection with '%s' accepted." % str(client_addr)) threading.Thread(target=self.startComunication, args=(client_addr, client_sock)).start() logger.logInfo("TCP listener on '%s' ended" % str(self.addr))
def detectDevices(self): """ detect hdmi-cec devices """ logger.logDebug("Detecting CEC devices") try: self.libCEC.RescanActiveDevices() addresses = self.libCEC.GetActiveDevices() activeSource = self.libCEC.GetActiveSource() logger.logInfo("CEC active device: %s" % self.getDeviceOSDName(activeSource) ) for iAddress in range(0, 15): if addresses.IsSet(iAddress): self.devices[self.libCEC.GetDeviceOSDName(iAddress)] = self.Device(iAddress, self.libCEC) logger.logInfo("CEC device %s detected on address: %i" % (self.libCEC.GetDeviceOSDName(iAddress), iAddress) ) except Exception as err: logger.logError("Detecting CEC devices error: %s" % str(err) )
def detectAdapter(self): """ detect an adapter and return the com port path """ logger.logDebug("Detecting CEC adapters") try: adapter_found = None adapters = self.libCEC.DetectAdapters() for adapter in adapters: logger.logInfo("CEC adapter found on port: %s vendor: %s product: %s" % (adapter.strComName, hex(adapter.iVendorId), hex(adapter.iProductId))) adapter_found = adapter.strComName return adapter_found except Exception as err: logger.logError("Detecting CEC adapter error: %s" % str(err) ) return None
def listenTCPThread(self): """ TCP listener thread """ logger.logInfo("TCP listener started on '%s'" % str(self.addr)) while True: try: (client_sock, client_addr) = self.sock.accept() except socket.error as err: logger.logError( "Failed to accept TCP connection. Error Code: " + str(err[0]) + ' Message: ' + err[1]) break logger.logDebug("Connection with '%s' accepted." % str(client_addr)) threading.Thread(target=self.startComunication, args=(client_addr, client_sock)).start() logger.logInfo("TCP listener on '%s' ended" % str(self.addr))
def detectAdapter(self): """ detect an adapter and return the com port path """ logger.logDebug("Detecting CEC adapters") try: adapter_found = None adapters = self.libCEC.DetectAdapters() for adapter in adapters: logger.logInfo( "CEC adapter found on port: %s vendor: %s product: %s" % (adapter.strComName, hex( adapter.iVendorId), hex(adapter.iProductId))) adapter_found = adapter.strComName return adapter_found except Exception as err: logger.logError("Detecting CEC adapter error: %s" % str(err)) return None
def detectDevices(self): """ detect hdmi-cec devices """ logger.logDebug("Detecting CEC devices") try: self.libCEC.RescanActiveDevices() addresses = self.libCEC.GetActiveDevices() activeSource = self.libCEC.GetActiveSource() logger.logInfo("CEC active device: %s" % self.getDeviceOSDName(activeSource)) for iAddress in range(0, 15): if addresses.IsSet(iAddress): self.devices[self.libCEC.GetDeviceOSDName( iAddress)] = self.Device(iAddress, self.libCEC) logger.logInfo( "CEC device %s detected on address: %i" % (self.libCEC.GetDeviceOSDName(iAddress), iAddress)) except Exception as err: logger.logError("Detecting CEC devices error: %s" % str(err))
def initlibCEC(self): """ initialize hdmi-cec """ try: self.libCEC = cec.ICECAdapter.Create(self.cecconfig) except Exception as err: logger.logError("Creating CEC adapter error: %s" % str(err) ) adapter = self.detectAdapter() if adapter == None: logger.logInfo("No CEC adapters found") else: if self.libCEC.Open(adapter): logger.logInfo("CEC connection opened") else: logger.logInfo("Failed to open a connection to the CEC adapter")
def initlibCEC(self): """ initialize hdmi-cec """ try: self.libCEC = cec.ICECAdapter.Create(self.cecconfig) except Exception as err: logger.logError("Creating CEC adapter error: %s" % str(err)) adapter = self.detectAdapter() if adapter == None: logger.logInfo("No CEC adapters found") else: if self.libCEC.Open(adapter): logger.logInfo("CEC connection opened") else: logger.logInfo( "Failed to open a connection to the CEC adapter")
def init(self): """ plugin init function - can be overwritten """ logger.logInfo("Define Init() in your plugin %s." % self.plugin_name)
def systemCmd(self, cmd): """ run the command """ logger.logInfo("Running command '%s'" % cmd) os.system(cmd)
def init(self): """ plugin init function - can be overwritten """ logger.logInfo("Define Init() in your plugin %s." % self.plugin_name)
logger.logError("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) return CONST.RET_ERROR logger.logDebug("fork #2 finished") try: # redirect standard file descriptors if not yet logger.closestdIO() except: pass # write pidfile atexit.register(self.delPID) pid = str(os.getpid()) file(self.pidfile,'w+').write("%s\n" % pid) logger.logInfo("Sucessfully daemonized with pid: %s" % pid) return CONST.RET_OK def delPID(self): """ delete pidfile """ try: os.remove(self.pidfile) except: logger.logError("Remove pidfile %s failed" % self.pidfile) def status(self): """
def systemCmd(self, cmd): """ run the command """ logger.logInfo("Running command '%s'" % cmd) os.system(cmd)
(e.errno, e.strerror)) return CONST.RET_ERROR logger.logDebug("fork #2 finished") try: # redirect standard file descriptors if not yet logger.closestdIO() except: pass # write pidfile atexit.register(self.delPID) pid = str(os.getpid()) file(self.pidfile, 'w+').write("%s\n" % pid) logger.logInfo("Sucessfully daemonized with pid: %s" % pid) return CONST.RET_OK def delPID(self): """ delete pidfile """ try: os.remove(self.pidfile) except: logger.logError("Remove pidfile %s failed" % self.pidfile) def status(self): """