def main(): """Main to start the telegraf service """ ctx = cfg.ConfigMgr() app_cfg = ctx.get_app_config() app_name = ctx.get_app_name() dev_mode = ctx.is_dev_mode() cfg_inst = os.getenv('ConfigInstance', app_name) log = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) log.info("=============== STARTING telegraf ===============") try: command = None if len(sys.argv) > 1: command = str(sys.argv[1]) read_config(app_cfg, dev_mode, log) if command is None: if dev_mode: telegraf_conf = "/etc/Telegraf/" \ + cfg_inst \ + "/" \ + cfg_inst \ + "_devmode.conf" else: telegraf_conf = "/etc/Telegraf/"+cfg_inst+"/"+cfg_inst+".conf" subprocess.call(["telegraf", "-config=" + telegraf_conf]) else: subprocess.call(shlex.split(command)) except subprocess.CalledProcessError as err: log.error(err, exc_info=True) sys.exit(1)
def get_image_data(topic_name): """Get the Images from Zmq """ dev_mode = ctx.is_dev_mode() logger = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) try: final_image = get_blank_image(TEXT) msg_bus_subscriber(topic_name, logger, app_cfg) while True: if topic_name in queue_dict.keys(): if not queue_dict[topic_name].empty(): frame = queue_dict[topic_name].get() ret, jpeg = cv2.imencode('.jpg', frame) del frame final_image = jpeg.tobytes() del jpeg else: raise Exception(f"Topic: {topic_name} doesn't exist") yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + final_image + b'\r\n\r\n') except KeyboardInterrupt: log.exception('Quitting due to keyboard interrupt...') except Exception: log.exception('Error during execution:')
def get_image_data(topic_name): """Get the Images from Zmq """ dev_mode = ctx.is_dev_mode() # Initializing Etcd to set env variables logger = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) json_config = ctx.get_app_config() try: final_image = get_blank_image(TEXT) msg_bus_subscriber(topic_name, logger, json_config) while True: if topic_name in queue_dict.keys(): if not queue_dict[topic_name].empty(): frame = queue_dict[topic_name].get() ret, jpeg = cv2.imencode('.jpg', frame) del frame final_image = jpeg.tobytes() del jpeg else: msg_txt = "Topic Not Found: " + topic_name final_image = get_blank_image(msg_txt) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + final_image + b'\r\n\r\n') except KeyboardInterrupt: logger.info('Quitting...') except Exception: logger.exception('Error during execution:')
def main(): """Main to start kapacitor service """ try: ctx = cfg.ConfigMgr() app_cfg = ctx.get_app_config() config = app_cfg.get_dict() app_name = ctx.get_app_name() dev_mode = ctx.is_dev_mode() except Exception as e: logger = configure_logging( os.getenv('PY_LOG_LEVEL', 'info').upper(), __name__, dev_mode) logger.exception( "Fetching app configuration failed, Error: {}".format(e)) sys.exit(1) logger = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) kapacitor_classifier = KapacitorClassifier(logger) logger.info("=============== STARTING kapacitor ==============") host_name = shlex.quote(os.environ["KAPACITOR_SERVER"]) if not host_name: error_log = ('Kapacitor hostname is not Set in the container. ' 'So exiting...') kapacitor_classifier.exit_with_failure_message(error_log) msg, status = kapacitor_classifier.start_udfs(config) if status is FAILURE: kapacitor_classifier.exit_with_failure_message(msg) kapacitor_started = False if (kapacitor_classifier.start_kapacitor(config, host_name, dev_mode, app_name) is True): kapacitor_started = True else: error_log = "Kapacitor is not starting. So Exiting..." kapacitor_classifier.exit_with_failure_message(error_log) msg, status = kapacitor_classifier.enable_tasks(config, kapacitor_started, host_name, dev_mode) if status is FAILURE: kapacitor_classifier.exit_with_failure_message(msg)
def main(): """Main method to FactoryControl App """ config_client = cfg.ConfigMgr() dev_mode = config_client.is_dev_mode() log = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) log.info("=============== STARTING factoryctrl_app ===============") try: factory_control_app = FactoryControlApp(dev_mode, config_client, log) factory_control_app.main() except Exception as ex: log.exception(ex)
python start.py run --name sample python start.py run --name monitor python start.py recall --start '2018-07-10 00:01:00' --end '2018-07-17 18:01:00' """ import logging from concurrent.futures import ThreadPoolExecutor import click from influxdb import InfluxDBClient from config.DB import INFLUXDB_CONFIG from config.APP import ( LOG_PATH, INFLUXDB_DATABASE_NAME ) from util.log import configure_logging;configure_logging(LOG_PATH) from util.tools import now_dt from util.transfer import t2s, s2t from util.misc import TimeSeries from task.workers import async_sampler, async_monitor logger = logging.getLogger('start') def run_sampler(): now = now_dt() logger.info(f'Start sampler time: [{t2s(now)}]') async_sampler(t2s(now.replace(second=0))) def run_monitor():
def __init__(self): """Constructor. .. warning:: This should *NEVER* be called directly by the application. """ # Verify initial state if not os.path.exists('config_schema.json'): raise AssertionError('Missing config schema') if BridgeState._instance is not None: raise AssertionError('BridgeState already initialized') BridgeState._instance = self # Configure initial logging self.log = configure_logging('INFO', __name__, False) self.log.info('Reading configuration JSON schema') with open('config_schema.json', 'r') as f: self.schema = json.load(f) # Assign initial state values self.loop = asyncio.get_event_loop() self.ipc_msgbus_ctxs = {} self.tcp_msgbus_ctxs = {} self.config_listener = None self.subscriber_listeners = None self.subscribers = [] self.config = None # Saved digital twin # Setup Azure Blob connection conn_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING') if conn_str is not None: self.log.info('Azure blob storage ENABLED in Azure bridge') self.bsc = BlobServiceClient.from_connection_string(conn_str) else: self.log.warn('Azure blob storage DISABLED') self.bsc = None self.log.info('Initializing Azure module client') self.module_client = IoTHubModuleClient.create_from_edge_environment() self.loop.run_until_complete(self.module_client.connect()) self.log.info('Getting initial digital twin') twin = self.loop.run_until_complete(self.module_client.get_twin()) self.log.debug('Received initial digital twin') self.log.info('Initializing EII config manager') try: self.config_mgr = cfg.ConfigMgr() self.dev_mode = self.config_mgr.is_dev_mode() self.app_name = self.config_mgr.get_app_name() except Exception as ex: self.log.exception(f'Exception: {ex}') raise ex self.log.debug('Finished initializing config manager') # Configure the Azure bridge state with its initial state self.configure(twin['desired']) # Setup twin listener self.config_listener = asyncio.gather(config_listener(self))
def configure(self, config): """Configure the Azure Bridge using the given Azure digital twin for the module. .. warning:: This will clear out any previous state that existed (i.e. all subscribers will be stopped) :param dict config: Azure IoT Hub digital twin for the Azure Bridge """ self.log.info('Configuring the Azure Bridge') # Verify the configuration self.log.debug('Validating JSON schema of new configuration') validate(instance=config, schema=self.schema) # Reset message bus state if needed if self.ipc_msgbus_ctxs or self.tcp_msgbus_ctxs: # Stop all subscribers self.log.debug('Stopping previous subscribers') # Clean up the message bus contexts and subscribers self._cleanup_msgbus_ctxs() # Configure logging if 'log_level' in config: log_level = config['log_level'].upper() else: log_level = 'INFO' self.log = configure_logging(log_level, __name__, False) self.log.info('Getting EII Message Bus configuration') ipc_msgbus_config, tcp_msgbus_config = get_msgbus_config( self.app_name, self.config_mgr, self.dev_mode) self.log.debug(f'Topic msgbus config dict: \nIPC: ' f'{ipc_msgbus_config}, \nTCP:{tcp_msgbus_config}') # Initialize message bus context self.log.info('Initializing EII Message Bus') if ipc_msgbus_config: for topic, msgbus_config in ipc_msgbus_config.items(): ipc_msgbus_ctx = emb.MsgbusContext(msgbus_config) self.ipc_msgbus_ctxs[topic] = ipc_msgbus_ctx if tcp_msgbus_config: for topic, msgbus_config in tcp_msgbus_config.items(): tcp_msgbus_ctx = emb.MsgbusContext(msgbus_config) self.tcp_msgbus_ctxs[topic] = tcp_msgbus_ctx # Initialize subscribers listener_coroutines = [] try: for (in_topic, topic_conf) in config['topics'].items(): self.log.info(f'Creating subscriber {in_topic}') self.log.debug(f'{in_topic} config: {topic_conf}') if 'az_output_topic' not in topic_conf: raise AssertionError('Missing az_output_topic') if self.bsc is not None and \ 'az_blob_container_name' in topic_conf: cn = topic_conf['az_blob_container_name'] else: cn = None msgbus_ctx = None if in_topic in self.ipc_msgbus_ctxs: msgbus_ctx = self.ipc_msgbus_ctxs[in_topic] elif in_topic in self.tcp_msgbus_ctxs: msgbus_ctx = self.tcp_msgbus_ctxs[in_topic] else: raise RuntimeError( f'Cannot find {in_topic} msgbus context') # Initialize the subcsriber subscriber = msgbus_ctx.new_subscriber(in_topic) self.subscribers.append(subscriber) listener_coroutines.append( emb_subscriber_listener(self, subscriber, topic_conf['az_output_topic'], cn)) except Exception as ex: # Clean up the message bus contexts self._cleanup_msgbus_ctxs() self.log.exception(f'Exception: {ex}') # Re-raise whatever exception just occurred raise # Schedule task for C2D Listener self.subscriber_listeners = asyncio.gather(*listener_coroutines) # Configure EII self.log.info('Getting ETCD configuration') # NOTE: THIS IS A HACK, AND NEEDS TO BE FIXED IN THE FUTURE hostname = 'localhost' # This change will be moved to an argument to the function in 2.3 # This is done now for backward compatibility etcd_host = os.getenv('ETCD_HOST') if etcd_host is not None and etcd_host != '': hostname = etcd_host port = os.getenv('ETCD_CLIENT_PORT', '2379') if not Util.check_port_availability(hostname, port): raise RuntimeError(f'etcd service port: {port} is not up!') try: if self.dev_mode: etcd = etcd3.client(host=hostname, port=port) else: etcd = etcd3.client( host=hostname, port=port, ca_cert='/run/secrets/rootca/cacert.pem', cert_key='/run/secrets/root/root_client_key.pem', cert_cert='/run/secrets/root/root_client_certificate.pem') except Exception as e: self.log.exception(f'Exception raised when creating etcd' f'client instance with error: {e}') raise e resp = etcd.get_all() eii_config = {} for value, meta in resp: try: value = json.loads(value.decode('utf-8')) key = meta.key.decode('utf-8') eii_config[key] = value except Exception as e: # NOTE: Errors may happen if security is enabled, because the # first part is the request's key self.log.error(f'{e}') self.log.debug('Finding changes in EII configuration') new_eii_config = json.loads(config['eii_config']) changed_keys, removed_keys = find_root_changes(eii_config, new_eii_config) self.log.debug(f'Changed service configs: {changed_keys}') self.log.debug(f'Removed service configs: {removed_keys}') self.log.info('Applying EII configuration') self.log.debug('Applying changes to the changed configs') for key in changed_keys: self.log.debug(f'Pushing config for {key}') etcd.put(key, json.dumps(new_eii_config[key], indent=4)) self.log.debug(f'Successfully pushed config for {key}') self.log.debug('Deleting removed service configs') for key in removed_keys: # NOTE: THIS IS A HACK, AND NEEDS TO BE FIXED IN THE FUTURE self.log.debug(f'Removing config for {key}') etcd.delete(key) self.log.debug(f'Successfully removed config for {key}') self.log.info('EII configuration update applied') # Save configuration for future comparisons self.config = config
@contact: [email protected] @module: workers @date: 2018/7/9 """ import logging from datetime import timedelta from queue import Queue from concurrent.futures import ThreadPoolExecutor from influxdb import InfluxDBClient from config.DB import INFLUXDB_CONFIG from config.APP import (LOG_PATH, DB_URL, DB_NAME, ACTIVE_EVENT_KEYS, INFLUXDB_DATABASE_NAME, MIN_ALERT_VALUE) from util.log import configure_logging configure_logging(LOG_PATH) from util.error_track import track_error, track_warn, track_info from util.transfer import s2t from util.misc import get_lib_cls from models import get_session from models.event_config import EventConfig as Config from task.app import app, Task logger = logging.getLogger('workers') executor = ThreadPoolExecutor() map_ = executor.map class Sampler:
def main(args): """Main method. """ # Initializing Etcd to set env variables ctx = cfg.ConfigMgr() num_of_subscribers = ctx.get_num_subscribers() dev_mode = ctx.is_dev_mode() logger = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) window_name = 'EII Visualizer App' visualizer_config = ctx.get_app_config() # Validating config against schema with open('./schema.json', "rb") as infile: schema = infile.read() if not (Util.validate_json(schema, json.dumps(visualizer_config.get_dict()))): sys.exit(1) image_dir = os.environ["IMAGE_DIR"] # If user provides image_dir, create the directory if don't exists if image_dir: if not os.path.exists(image_dir): os.mkdir(image_dir) # Initializing required variables queue_dict = {} topic_config_list = [] topics_list = [] for index in range(num_of_subscribers): # Fetching subscriber element based on index sub_ctx = ctx.get_subscriber_by_index(index) # Fetching msgbus config of subscriber msgbus_cfg = sub_ctx.get_msgbus_config() # Fetching topics of subscriber topic = sub_ctx.get_topics()[0] # Adding topic & msgbus_config to # topic_config tuple topic_config = (topic, msgbus_cfg) topic_config_list.append(topic_config) topics_list.append(topic) queue_dict[topic] = queue.Queue(maxsize=10) try: root_win = tkinter.Tk() button_dict = {} image_dict = {} window_width = 600 window_height = 600 window_geometry = str(window_width) + 'x' + str(window_height) root_win.geometry(window_geometry) root_win.title(window_name) column_value = len(topics_list) // 2 row_value = len(topics_list) % 2 height_value = int(window_height / (row_value + 1)) width_value = int(window_width / (column_value + 1)) blank_image_shape = (300, 300, 3) blank_image = np.zeros(blank_image_shape, dtype=np.uint8) text = 'Disconnected' text_position = (20, 250) text_font = cv2.FONT_HERSHEY_PLAIN text_color = (255, 255, 255) cv2.putText(blank_image, text, text_position, text_font, 2, text_color, 2, cv2.LINE_AA) blankimg = Image.fromarray(blank_image) for button_count in range(len(topics_list)): button_str = "button{}".format(button_count) image_dict[button_str] = ImageTk.PhotoImage(image=blankimg) button_count, row_count, column_count = 0, 0, 0 if len(topics_list) == 1: height_value = window_height width_value = window_width button_dict[str(button_count)] = \ tkinter.Button(root_win, text=topics_list[0]) button_dict[str(button_count)].grid(sticky='NSEW') tkinter.Grid.rowconfigure(root_win, 0, weight=1) tkinter.Grid.columnconfigure(root_win, 0, weight=1) else: for key in queue_dict: button_dict[str(button_count)] = tkinter.Button(root_win, text=key) if column_count > column_value: row_count = row_count + 1 column_count = 0 if row_count > 0: height_value = int(window_height / (row_count + 1)) for key2 in button_dict: button_dict[key2].config(height=height_value, width=width_value) else: for key2 in button_dict: button_dict[key2].config(height=height_value, width=width_value) button_dict[str(button_count)].grid(row=row_count, column=column_count, sticky='NSEW') tkinter.Grid.rowconfigure(root_win, row_count, weight=1) tkinter.Grid.columnconfigure(root_win, column_count, weight=1) button_count = button_count + 1 column_count = column_count + 1 root_win.update() msg_bus_subscriber(topic_config_list, queue_dict, logger, visualizer_config) while True: button_count = 0 for key in queue_dict: if not queue_dict[key].empty(): logger.info('Preparing frame for visualization') frame = queue_dict[key].get_nowait() img = Image.fromarray(frame) del frame if len(img.split()) > 3: blue, green, red, _ = img.split() else: blue, green, red = img.split() img = Image.merge("RGB", (red, green, blue)) imgwidth, imgheight = img.size aspect_ratio = (imgwidth / imgheight) + 0.1 resized_width = button_dict[str( button_count)].winfo_width() resized_height = round( button_dict[str(button_count)].winfo_width() / aspect_ratio) resized_img = img.resize((resized_width, resized_height)) del img image_dict["button" + str(button_count)] = ImageTk.PhotoImage( image=resized_img) button_dict[str(button_count)].config( image=image_dict["button" + str(button_count)], compound=tkinter.BOTTOM) del resized_img else: try: button_dict[str(button_count)].config( image=image_dict["button" + str(button_count)], compound=tkinter.BOTTOM) except Exception: logger.exception("Tkinter exception") button_count = button_count + 1 root_win.update() except KeyboardInterrupt: logger.info('Quitting...') except Exception: logger.exception('Error during execution:') finally: logger.exception('Destroying EII databus context') sys.exit(1)
TEMP_DS = "{}/conf/provisioning/datasources/datasource.yml".format(GRAFANA_DIR) TEXT = 'Disconnected' TEXTPOSITION = (10, 110) TEXTFONT = cv2.FONT_HERSHEY_PLAIN TEXTCOLOR = (255, 255, 255) # Config manager initialization ctx = cfg.ConfigMgr() app_cfg = ctx.get_app_config() dev_mode = ctx.is_dev_mode() topics_list = [] topic_config_list = [] queue_dict = {} # Initializing logger log = configure_logging( os.getenv('PY_LOG_LEVEL', 'DEBUG').upper(), __name__, dev_mode) # Visualization related variables FRAME_QUEUE_SIZE = 10 # Initializing flask related variables NONCE = secrets.token_urlsafe(8) APP = Flask(__name__) LOADER = FileSystemLoader(searchpath="Grafana/templates/") # Setting default auto-escape for all templates ENV = Environment(loader=LOADER, autoescape=select_autoescape( enabled_extensions=('html'), default_for_string=True, ))
if isinstance(val, str): val = val.encode('utf-8') else: val = bytes(val) return val def ensure_unicode(val): return b2s(val) if __name__ == '__main__': from util.log import configure_logging configure_logging() now = datetime.now() print(t2s(now)) print(s2num('是')) print(s2num('否')) print(s2num('')) print(s2num('1')) print(s2num('1.0')) print(s2num('-1')) print(s2num('-1.0')) print(s2f('')) print(s2f(1)) print(s2f(1.0)) print(s2f('1')) print(s2f('-1')) print(s2f('1.2'))
""" @User : frank @Time : 2019/7/18 20:10 @File : __init__.py @Email : [email protected] references: http://docs.jinkan.org/docs/flask/patterns/fileuploads.html """ import logging from flask import Flask from app.main import readImageFromDisk from util.log import configure_logging configure_logging("../logs/") logger = logging.getLogger(__name__) def register_blueprint(app): from app.web.upload_service import web app.register_blueprint(web) pass def create_app(): app = Flask(__name__) # 加载app配置文件 app.config.from_object('config.DB')