def _log(type, text, callframe = None): LogConfig.init() if callframe is None: callframe = _getframe(1) func = callframe.f_code.co_name cls = callframe.f_locals.get('self', None) msg = '' if not text: text = '<no detail>' if cls != None: cls = cls.__class__.__name__ msg = '%s [%s.%s] :: %s' % (type, cls, func, text) else: msg = '%s [%s] :: %s' % (type, func, text) if LogConfig.verbose(): line = callframe.f_lineno filename = callframe.f_code.co_filename msg = '%s {%s:%s}' % (msg, filename, line) if LogConfig.colored(): if type == LOG_TYPE_ERROR: msg = '\x1b[0;31m%s\x1b[1;m' % msg elif type == LOG_TYPE_WARNING: msg = '\x1b[1;33m%s\x1b[1;m' % msg elif type == LOG_TYPE_DEBUG: msg = '\x1b[0;37m%s\x1b[1;m' % msg print msg return
def _log(type, text, callframe=None): LogConfig.init() if callframe is None: callframe = _getframe(1) func = callframe.f_code.co_name cls = callframe.f_locals.get('self', None) msg = "" if not text: text = "<no detail>" if cls != None: cls = cls.__class__.__name__ msg = "%s [%s.%s] :: %s" % (type, cls, func, text) else: msg = "%s [%s] :: %s" % (type, func, text) if LogConfig.verbose(): line = callframe.f_lineno filename = callframe.f_code.co_filename msg = "%s {%s:%s}" % (msg, filename, line) if LogConfig.colored(): if type == LOG_TYPE_ERROR: msg = "\033[0;31m%s\033[1;m" % msg elif type == LOG_TYPE_WARNING: msg = "\033[1;33m%s\033[1;m" % msg elif type == LOG_TYPE_DEBUG: msg = "\033[0;37m%s\033[1;m" % msg print(msg)
def e(text = ''): LogConfig.init() if LogConfig.level() >= LOG_LEVEL_ERROR: callframe = _getframe(1) Log._log(LOG_TYPE_ERROR, text, callframe)
def d(text = ''): LogConfig.init() if LogConfig.level() >= LOG_LEVEL_DEBUG: callframe = _getframe(1) Log._log(LOG_TYPE_DEBUG, text, callframe)
def i(text = ''): LogConfig.init() if LogConfig.level() >= LOG_LEVEL_INFO: callframe = _getframe(1) Log._log(LOG_TYPE_INFO, text, callframe)
def w(text = ''): LogConfig.init() if LogConfig.level() >= LOG_LEVEL_WARNING: callframe = _getframe(1) Log._log(LOG_TYPE_WARNING, text, callframe)
def e(text=""): #ERROR LogConfig.init() if LogConfig.level() >= LOG_LEVEL_ERROR: callframe = _getframe(1) Log._log(LOG_TYPE_ERROR, text, callframe)
def d(text=""): #DEBUG LogConfig.init() if LogConfig.level() >= LOG_LEVEL_DEBUG: callframe = _getframe(1) Log._log(LOG_TYPE_DEBUG, text, callframe)
def i(text=""): #INFO LogConfig.init() if LogConfig.level() >= LOG_LEVEL_INFO: callframe = _getframe(1) Log._log(LOG_TYPE_INFO, text, callframe)
def w(text=""): #WARNING LogConfig.init() if LogConfig.level() >= LOG_LEVEL_WARNING: callframe = _getframe(1) Log._log(LOG_TYPE_WARNING, text, callframe)
class HttpSender: logging.config.dictConfig(LogConfig().getConfig()) LOG = logging.getLogger(__name__) ## # def sendPayload(self, payload): result = False try: mc = MainConfig().getConfig() url = mc.get(Constants.SERVER_HTTP_URL) headers = { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' } data = { 'body': payload, 'deviceId': Status.deviceId, 'fwVersion': Status.fwVersion } print('data: ', data) response = requests.post(url, data=data, headers=headers, timeout=15) resStatus = response.status_code if resStatus == 200: Status.messageSendingFailedCount = 0 resbody = response.content.decode('utf-8') print(resbody) self.processResponse(resbody) result = True else: Status.messageSendingFailedCount += 1 except Exception as e: self.LOG.error('Error sending payload: %s', e) Status.messageSendingFailedCount += 1 finally: pass return result ## # def processResponse(self, response): respJson = {} try: respJson = json.loads(response) except Exception as e: self.LOG.error("Error parsing response: %s", e) return if 'command_id' in respJson and 'args' in respJson: commandId = respJson.get('command_id') args = respJson.get('args') ce.executeCommand(commandId, args) def processCommands(self, commandList): for command in commandList: try: print('Executing command: ', command) subprocess.call(command, shell=True) except Exception as e: self.LOG.error('Error executing command: %s', command) self.LOG.error('Error in executinf command: %s', e) ## # def doGetRequest(self, url, requestParams): resp = None if requestParams is None: resp = requests.get(url=url) else: reap = requests.get(url=url, params=requestParams) return resp
ledt = LedStatus.LedStatus() ledt.start() # Wait for the threads to complete while True: if Status.stop: MessageSender.stop = True MessageComposer.stop = True ModbusDataService.stop = True PeriodicTimer.stop = True ProcessMonitor.stop = True time.sleep( int(MainConfig().getConfig().get(Constants.PERIODIC_INTERVAL))) break else: time.sleep( int(MainConfig().getConfig().get(Constants.PERIODIC_INTERVAL))) if __name__ == "__main__": print(LogConfig().getConfig()) logging.config.dictConfig(LogConfig().getConfig()) LOG = logging.getLogger(__name__) LOG.debug('Starting the Data Logger') main() #pt.join() #modbusDataService.join() #messageComposer.join() #messageSender.join()
#!/bin/env python3 # -*- coding=utf-8 -*- from LogConfig import LogConfig import os import sys import ftplib import time from ftplib import FTP_TLS import ssl import socket log = LogConfig(log_level='INFO').get_console_and_file_logger() # 输出到控制台与文件 # log = LogConfig(log_level='INFO').get_console_logger() # 输出到控制台 # log = LogConfig(log_level='INFO').get_file_logger() # 输出到日志文件 class FtpTLS(FTP_TLS): """Ftp 使用 TLS""" def connect(self, host='', port=0, timeout=-999): if host != '': self.host = host if port > 0: self.port = port if timeout != -999: self.timeout = timeout self.sock = socket.create_connection((self.host, self.port), self.timeout) self.af = self.sock.family try: # TSL 证书错误