Exemplo n.º 1
0
    async def register(
            reg_id=Namespace().get_namespace_sig(),
            namespace=Namespace().to_dict(),
            type=Namespace.DAQSERVER,
            config=dict(),
    ):
        # if not reg_id2:
        #     reg_id2 = Namespace().get_namespace_sig()

        # registration = await DAQRegistry.register_no_wait(
        #     reg_id=reg_id,
        #     reg_id2=reg_id2,
        #     namespace=namespace,
        #     type=type,
        #     config=config,
        #     config2=config2,
        # )

        registration = await DAQRegistry.register_no_wait(
            reg_id=reg_id,
            namespace=namespace,
            type=type,
            config=config,
        )

        # if not registration:
        #     registration = await DAQRegistry.update_registration(namespace, type, config)
        return registration
Exemplo n.º 2
0
    def configure_daq_local(self):

        self.status2.set_config_status(Status.CONFIGURING)
        self.read_current_config()
        if self.config:
            try:
                self.namespace = Namespace().from_dict(
                    self.config["namespace"])
            except KeyError:
                pass

            try:
                self.autoenable_daq = self.config["autoenable_daq"]
            except KeyError:
                pass

            try:
                self.uri = self.config["uri"]
            except KeyError:
                pass

            self.status2.set_config_status(Status.NOT_CONFIGURED)
            try:
                if self.config["ENVDAQ_CONFIG"]:
                    self.status2.set_config_status(Status.CONFIGURED)
            except KeyError:
                pass

        else:
            self.status2.set_config_status(Status.NOT_CONFIGURED)

        print(f"self.config: {self.config}")
        # have tried to configure, now connect to UI
        self.do_ui_connection = True
Exemplo n.º 3
0
    def update_server(config=None, server_id=None, app_list=None, force=False):

        # print(f'update_server: {config}, {server_id}')
        # use config to create PlotApps
        if config:
            server_id = ""
            pass
        elif not server_id:
            # server_id = PlotManager.DEFAULT_ID
            server_id = Namespace()

        plotserver_id = PlotManager.get_server_id(server_id)

        plotserver_port = PlotManager.get_free_port(plotserver_id)
        if not plotserver_port:
            return

        # print(f'server_id = {server_id}, {PlotManager.__server_map}')
        if (server_id in PlotManager.__server_map) or force:
            PlotManager.__server_map[plotserver_id] = PlotServer(
                server_id=plotserver_id,
                host=PlotManager.PLOTSERVER_HOST,
                port=plotserver_port,
                app_list=app_list,
            )
Exemplo n.º 4
0
    def get_server_id(server_id=None):
        if not server_id:
            # server_id = PlotManager.DEFAULT_ID
            server_id = Namespace()
        server_sig = server_id.get_namespace_comp_sig(
            component=Namespace.DAQSERVER)

        return f"{server_sig['host']}:{server_sig['name']}"
Exemplo n.º 5
0
 def get_namespace(self):
     # ns = Namespace(name=self.name, host=self.host, ns_type=Namespace.DAQSERVER)
     parent_ns = self.controller.get_namespace()
     ns = Namespace(name=self.name,
                    ns_type=Namespace.INSTRUMENT,
                    parent_ns=parent_ns)
     # print(f"ns::: {ns.to_dict()}")
     return ns
Exemplo n.º 6
0
 async def get_registration(
         reg_id=Namespace().get_namespace_sig(), type=Namespace.DAQSERVER):
     # registration = await DAQRegistry.get_registration_no_wait(
     #     reg_id=reg_id, reg_id2=reg_id2, type=type
     # )
     registration = await DAQRegistry.get_registration_no_wait(
         reg_id=reg_id, type=type)
     return registration
Exemplo n.º 7
0
    def get_server(server_id=None):
        if not server_id:
            # server_id = PlotManager.DEFAULT_ID
            server_id = Namespace()

        plotserver_id = PlotManager.get_server_id(server_id)

        if plotserver_id not in PlotManager.__server_map:
            PlotManager.add_server(server_id=server_id)

        return PlotManager.__server_map[plotserver_id]
Exemplo n.º 8
0
 async def update_registration(
         reg_id=Namespace().get_namespace_sig(),
         namespace=Namespace().to_dict(),
         type=Namespace.DAQSERVER,
         config=dict(),
         registration=None,
 ):
     # reg = await DAQRegistry.update_registration_no_wait(
     #     reg_id=reg_id,
     #     reg_id2=reg_id2,
     #     namespace=namespace,
     #     type=type,
     #     registration=registration,
     # )
     reg = await DAQRegistry.update_registration_no_wait(
         reg_id=reg_id,
         namespace=namespace,
         type=type,
         registration=registration,
     )
     return reg
Exemplo n.º 9
0
 def unregister_no_wait(
         reg_id=Namespace().get_namespace_sig(), type=Namespace.DAQSERVER):
     try:
         # print(f"unregister:{reg_id}")
         # reg = DAQRegistration.objects.get(reg_id2=reg_id2, daq_type=type)
         reg = DAQRegistration.objects.get(reg_id=reg_id, daq_type=type)
         # print(f"Unregistering: {reg}")
         reg.delete()
         # print(f"success")
     except DAQRegistration.DoesNotExist:
         # print(f"unregister: reg_id does not exist {reg_id}")
         pass
Exemplo n.º 10
0
 def get_namespace(self):
     # ns = Namespace(name=self.name, host=self.host, ns_type=Namespace.DAQSERVER)
     if self.server:
         parent_ns = self.server.get_namespace()
     elif self.parent_controller:
         parent_ns = self.parent_controller.get_namespace()
     else:
         return None
     ns = Namespace(name=self.name,
                    ns_type=Namespace.CONTROLLER,
                    parent_ns=parent_ns)
     # print(f"ns::: {ns.to_dict()}")
     return ns
Exemplo n.º 11
0
    def get_registration_no_wait(
            reg_id=Namespace().get_namespace_sig(), type=Namespace.DAQSERVER):
        # theoretically, we should not be pinging local here
        # if not regkey and config and (regkey in config):
        #     regkey = config["regkey"]
        # if regkey:
        print(f"get_reg: {reg_id}")
        try:
            # reg = DAQRegistration.objects.get(reg_id2=reg_id2, daq_type=type)
            reg = DAQRegistration.objects.get(reg_id=reg_id, daq_type=type)
            # reg.status = "CONNECTED"
            # srv = ServiceRegistration.objects.get(regkey=config["regkey"])
            # reg.save(do_update=True)  # update modified time stamp
            return reg.get_registration()
        except DAQRegistration.DoesNotExist:
            pass

        return None
Exemplo n.º 12
0
    def read_current_config(self):
        # read current config file
        fname = self.current_config_file
        if not os.path.isabs(self.current_config_file):
            fname = os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                self.current_config_file,
            )
        if not os.path.exists(fname):
            self.clear_current_config()

        try:
            # with open(self.last_config_file) as cfg:
            with open(fname) as cfg:
                self.config = json.load(cfg)
                # print(f"{self.config}")

                try:
                    self.uri = self.config["uri"]
                except KeyError:
                    pass

                try:
                    ns = self.config["namespace"]
                    self.namespace = Namespace().from_dict(ns)
                except KeyError:
                    pass

                try:
                    self.autoenable = self.config["autoenable_daq"]
                except KeyError:
                    pass
            # print(f"self.config***: {self.config}")
        except FileNotFoundError as e:
            print(e)
            pass
        except JSONDecodeError as e:
            self.config = dict()
        except TypeError:
            self.clear_current_config()
            pass
Exemplo n.º 13
0
    def ping_no_wait(
            reg_id=Namespace().get_namespace_sig(), type=Namespace.DAQSERVER):
        # theoretically, we should not be pinging local here
        # if not regkey and config and (regkey in config):
        #     regkey = config["regkey"]
        # if regkey:
        try:
            # print(f"ping server reg: {reg_id}")
            # reg = DAQRegistration.objects.get(reg_id2=reg_id2, daq_type=type)
            reg = DAQRegistration.objects.get(reg_id=reg_id, daq_type=type)
            reg.status = "CONNECTED"
            status2 = Status().from_dict(reg.status2)
            status2.set_connection_status(Status.CONNECTED)
            reg.status2 = status2.to_dict()

            # srv = ServiceRegistration.objects.get(regkey=config["regkey"])
            reg.save(do_update=True)  # update modified time stamp
            # print(f"ping success")

        except DAQRegistration.DoesNotExist:
            pass
Exemplo n.º 14
0
def index(request):


    net_map = {}
    networks = Network.objects.all()
    for net in networks:
        net_map[net.name] = {
            "description": net.description,
            "active": net.active
        }

    daq_reg_map = {}
    daq_regs = DAQRegistration.objects.all()
    for reg in daq_regs:
        ns = Namespace().from_dict(reg.namespace)

        daq_reg_map[ns.get_namespace()] = {
            "host": ns.get_host(),
            "name": ns.name,
            # "signature": ns.get_namespace_sig(),
            "type": reg.daq_type,
            # "status": reg.status
        }
        # daq_reg_map[reg.reg_id] = {
        #     "type": reg.daq_type,
        #     "status": reg.status
        # }



    context = {
        "network_map": net_map,
        "daq_reg_map": daq_reg_map,
    }


    return render(request, 'index.html', context)
Exemplo n.º 15
0
 def get_namespace(self):
     ns = Namespace(name=self.name,
                    host=self.host,
                    ns_type=Namespace.DAQSERVER)
     return ns
Exemplo n.º 16
0
    def __init__(self, config=None, server_name=None, ui_config=None):
        self.controller_list = []
        self.controller_map = dict()
        self.task_list = []
        self.ui_task_list = []
        self.last_data = {}
        self.run_flag = False
        self.registration_key = None
        self.run_state = "STOPPED"
        self.config_state = "NOT_CONFIGURED"

        # defaults
        self.server_name = ""
        self.ui_config = {
            "host": "localhost",
            "port": 8001,
        }
        self.base_file_path = "/tmp"
        self.current_run_config = {}

        # daq_id: used by ui to delineate between different daq_servers - {nodename}-{namespace}
        self.daq_id = "localhost-default"
        self.uri = "localhost"
        # self.namespace = {
        #     "daq_server": "localhost-default",
        # }
        self.namespace = Namespace(name="default",
                                   host="localhost",
                                   ns_type=Namespace.DAQSERVER)

        self.autoenable_daq = False
        self.enable_daq = True

        self.loop = asyncio.get_event_loop()

        # try to import config from daq_settings.py
        try:
            daq_settings = import_module("config.daq_settings")
            server_config = daq_settings.server_config

            if "name" in server_config:
                self.server_name = server_config["name"]

            if "ui_config" in server_config:
                self.ui_config = server_config["ui_config"]

            if "base_file_path" in server_config:
                self.base_file_path = server_config["base_file_path"]

            if "daq_id" in server_config:
                fqdn = server_config["daq_id"]["fqdn"]
                namespace = server_config["daq_id"]["namespace"]
                # self.uri = fqdn
                # self.daq_id = f"{fqdn.split('.')[0]}-{namespace}"
                # self.namespace["daq_server"] = self.daq_id.replace(" ", "")

                self.namespace.host = fqdn
                self.namespace.name = namespace.replace(" ", "")

                self.daq_id = self.namespace.get_namespace_sig()["name"]
                self.uri = self.namespace.get_namespace_sig()["host"]

            self.current_config_file = "./current_config.json"
            if "current_config_file" in server_config:
                self.current_config_file = server_config["current_config_file"]

            self.config = dict()
            # if "last_config_file" in server_config:
            #     # dir_path = os.path.dirname(os.path.realpath(__file__))
            #     # print(dir_path)
            #     self.last_config_file = server_config["last_config_file"]
            #     fname = self.last_config_file
            #     if not os.path.isabs(self.last_config_file):
            #         fname = os.path.join(
            #             os.path.dirname(os.path.realpath(__file__)),
            #             self.last_config_file,
            #         )
            #     try:
            #         # with open(self.last_config_file) as cfg:
            #         with open(fname) as cfg:
            #             self.config = json.load(cfg)
            #     except FileNotFoundError as e:
            #         print(e)
            #         pass
            #     except TypeError:
            #         pass

            # if self.config:
            #     self.config_state = "CONFIGURED"

            # if "current_run_config" in server_config:
            #     self.current_run_config = server_config["current_run_config"]
            #     if self.current_run_config:
            #         self.config = self.current_run_config

        except ModuleNotFoundError:
            print("settings file not found, using defaults")
            self.server_name = ""

        # TODO I don't think these override any more...get rid of them?
        # override config file settings
        if server_name:
            self.server_name = server_name
        if ui_config:
            self.ui_config = ui_config
        if config:
            self.config = config

        # self.do_ui_connection = auto_connect_ui
        self.do_ui_connection = False  # change this when try config
        self.reg_client = None
        self.ui_client = None

        self.status = {
            # 'run_status': 'STOPPED',
            "ready_to_run": False,
            "connected_to_ui": False,
            "health": "OK",
        }

        self.status2 = Status()

        # start managers
        SysManager.start()

        # Create message buffers
        self.to_ui_buf = None
        self.from_child_buf = None
        self.create_msg_buffers()

        # Begin startup
        self.run_task = None
        self.start_daq()
Exemplo n.º 17
0
    def register_no_wait(
            reg_id=Namespace().get_namespace_sig(),
            namespace=Namespace().to_dict(),
            type=Namespace.DAQSERVER,
            config=dict(),
    ):
        # print(f"register daq: {config}")
        # if config:
        # print(config["host"])

        # if not reg_id2:
        #     reg_id2 = Namespace().get_namespace_sig()

        try:
            # print(f'{config["HOST"]}, {config["PORT"]}')
            # registration = DAQRegistration.objects.get(reg_id2=reg_id2, daq_type=type)
            registration = DAQRegistration.objects.get(reg_id=reg_id,
                                                       daq_type=type)
            if registration:
                registration.delete()
        except DAQRegistration.MultipleObjectsReturned:
            # result = DAQRegistration.objects.filter(reg_id2=reg_id2, daq_type=type)
            result = DAQRegistration.objects.filter(reg_id=reg_id,
                                                    daq_type=type)
            for s in result:
                s.delete()
        except DAQRegistration.DoesNotExist:
            pass

        network = "default"
        # if local:
        #     network = ServiceRegistry.local_network.name
        # else:
        #     try:
        #         network = config["network"]
        #     except KeyError:
        #         pass # defaults to "default"

        # create new Reg
        # registration = DAQRegistration(
        #     reg_id=reg_id,
        #     reg_id2=reg_id2,
        #     namespace=namespace,
        #     daq_type=type,
        #     config=config,
        #     config2=config2,
        #     status="CONNECTED",
        # )
        status2 = Status()
        status2.set_connection_status(Status.CONNECTED)
        print(f"register: {reg_id}, {namespace}")
        registration = DAQRegistration(
            reg_id=reg_id,
            namespace=namespace,
            daq_type=type,
            config=config,
            status="CONNECTED",
            status2=status2.to_dict(),
        )
        registration.save()
        # TODO: update service definition to include this reg

        registration = registration.get_registration()
        return registration
Exemplo n.º 18
0
    def update_registration_no_wait(
            reg_id=Namespace().get_namespace_sig(),
            namespace=Namespace().to_dict(),
            type=Namespace.DAQSERVER,
            config=dict(),
            registration=None,
    ):
        # if config:

        # network = "default"
        # if local:
        #     network = ServiceRegistry.local_network.name
        # else:
        #     try:
        #         network = config["network"]
        #     except KeyError:
        #         pass  # defaults to "default"

        try:
            # srv = ServiceRegistration.objects.get(regkey=config["regkey"])
            # reg = DAQRegistration.objects.get(reg_id2=reg_id2, daq_type=type)
            reg = DAQRegistration.objects.get(reg_id=reg_id, daq_type=type)
        except DAQRegistration.DoesNotExist:
            reg = None
            # if reg.get_age() > DAQRegistry.auto_clean_limit:
            #     reg.delete()
            # elif config and reg.regkey in config and config["regkey"] != reg.regkey:
            #     reg.delete()
            # else:
        config = {}
        # regkey = None
        if registration:
            config = registration["config"]
            # config2 = registration["config2"]
            # regkey = registration["regkey"]
        if not reg:
            # reg = DAQRegistration(
            #     reg_id2=reg_id2,
            #     namespace=namespace,
            #     daq_type=type,
            #     config=config,
            #     config2=config2,
            # )
            reg = DAQRegistration(
                reg_id=reg_id,
                namespace=namespace,
                daq_type=type,
                config=config,
            )
        if reg:
            # reg.reg_id2 = reg_id2
            reg.reg_id = reg_id
            reg.namespace = namespace
            reg.daq_type = type
            reg.config = config
            # reg.config2 = config2
            reg.status = "CONNECTED"

            status2 = Status()
            status2.set_connection_status(Status.CONNECTED)
            reg.status2 = status2.to_dict()
            # if regkey:
            #     reg.regkey = regkey

            # srv.service_list = config.service_list
            reg.save(do_update=True)
            # TODO: update service

            # reg.add_services(config["service_list"])
            # srv.save()
            # if local:
            #     ServiceRegistry.local_network.add_registration(srv)
            # reg.join_network(ServiceRegistry.get_network_name(local, config))
            return reg.get_registration()
            # print(f"3:{registration}")
            # return registration
        else:
            return None
Exemplo n.º 19
0
 async def ping(
         reg_id=Namespace().get_namespace_sig(), type=Namespace.DAQSERVER):
     await DAQRegistry.ping_no_wait(reg_id, type)
Exemplo n.º 20
0
    def __init__(
        self,
        config,
        ui_config=None,
        base_file_path=None,
        auto_connect_ui=True,
        namespace=None,
        **kwargs,
    ):
        # non-abstract DAQ children need to set this to True
        #   in order for system to recognize as valid
        # TODO: make an abstract method to retrieve value
        #       to force children to deal with it?

        # TODO: Should DAQ have generic in/out buffers?
        print("init DAQ")
        self.loop = asyncio.get_event_loop()
        self.config = config
        self.ui_config = ui_config
        self.do_ui_connection = auto_connect_ui
        self.task_list = []
        self.ui_task_list = []
        self.enable_task_list = []
        self.message_loop_tasks = []
        self.registration_key = None

        self.plot_app = None

        self.datafile = None

        self.keepalive_ping = False

        # set base file path for all file saves
        # base_file_path = '/tmp'
        if base_file_path:
            self.base_file_path = base_file_path

        # parameters to include metadata in output
        self.include_metadata = True
        # set interval to 0 to always send metadata
        self.include_metadata_interval = 60

        # self.daq_definition = dict()
        # self.daq_definition['DEFINITION'] = dict()

        # self.INSTANTIABLE = False

        self.name = None
        self.label = None
        if "LABEL" in config:
            self.label = config["LABEL"]
        # print(f"id: {self.get_id()}")

        # print(f"16namespace = {self.namespace} - {self.alias}")

        # if namespace is passed, store a copy
        self.namespace = Namespace()
        if namespace:
            self.namespace = Namespace().from_dict(namespace)
            # self.namespace = namespace.copy()
        # self.parent_id = "parent-default"
        # if parent_id:
        #     self.parent_id = parent_id
        # self.daq_id = f"{self.parent_id}-default"

        # in case we want to add heierarchy
        # parent = {
        #   class: 'CONTROLLER',
        #   id: <controller_name>,
        # }
        self.parent = None
        self.parent_map = dict()

        self.data_request_list = []

        # Message buffers (Queues)
        #   to/from parent
        self.to_parent_buf = None
        self.from_parent_buf = None
        #   to/from child
        self.to_child_buf = None
        self.from_child_buf = None
        #   to/from gui
        self.to_ui_buf = None
        self.from_ui_buf = None
        self.create_msg_buffers()

        # ui client
        self.ui_client = None

        # controls
        self.controls = dict()
        self.status = {
            "run_status": "STOPPED",
            "ready_to_run": False,
            "connected_to_ui": False,
            "health": "OK",
        }
        self.status2 = Status()
        self.current_run_settings = dict()
Exemplo n.º 21
0
    def add_apps(config):
        # PlotManager.start_server()
        # print(f'##### add_apps -> {config}')
        server_namespace = ""
        if ("namespace") in config:
            ns = Namespace().from_dict(config["namespace"])
            # server_id = PlotManager.get_server_id(ns)
            # server_id = ns.get_namespace_comp_sig()

            server_id = PlotManager.get_server_id(ns)

            if "daq_server" in config["namespace"]:
                server_namespace += config["namespace"]["daq_server"]
            if "controller" in config["namespace"]:
                server_namespace += "-" + config["namespace"]["controller"]
        else:
            ns = Namespace()

        if "plot_meta" in config and "plots" in config["plot_meta"]:
            app_list = []
            for plot_name, plot_def in config["plot_meta"]["plots"].items():
                # print(f'####### add_plots: {plot_name} - {plot_def}')
                if "app_type" in plot_def:
                    if plot_def["app_type"] == "TimeSeries1D":
                        PlotManager.add_app(
                            TimeSeries1D(
                                # config,
                                plot_def,
                                # server_id=server_id,
                                plot_name=plot_name,
                                app_name=plot_def["app_name"],
                            ),
                            server_id=ns,
                            start_after_add=False,
                        )
                        app_list.append(plot_def["app_name"])
                        PlotManager.register_app_source(plot_def)
                    elif plot_def["app_type"] == "SizeDistribution":
                        # print(f'SizeDistribution add app')
                        PlotManager.add_app(
                            SizeDistribution(
                                # config,
                                plot_def,
                                plot_name=plot_name,
                                app_name=plot_def["app_name"],
                            ),
                            server_id=ns,
                            start_after_add=False,
                        )
                        app_list.append(plot_def["app_name"])
                        PlotManager.register_app_source(plot_def)
                    elif plot_def["app_type"] == "GeoMapPlot":
                        # print(f'SizeDistribution add app')
                        PlotManager.add_app(
                            GeoMapPlot(
                                # config,
                                plot_def,
                                plot_name=plot_name,
                                app_name=plot_def["app_name"],
                            ),
                            server_id=ns,
                            start_after_add=False,
                        )
                        app_list.append(plot_def["app_name"])
                        PlotManager.register_app_source(plot_def)

            key = config["NAME"]
            if "alias" in config:
                key = config["alias"]["name"]
            # print(f'{app_list}, {PlotManager.__app_list_map}')
            PlotManager.__app_list_map[key] = app_list
Exemplo n.º 22
0
 async def unregister(
         reg_id=Namespace().get_namespace_sig(), type=Namespace.DAQSERVER):
     await DAQRegistry.unregister_no_wait(reg_id=reg_id, type=type)