Exemplo n.º 1
0
class USBDevice(SerialCommunicator):
    AVAILABLE_QUERIES = {}

    def __init__(self, port=None, auto_port=True, interpreter=None, **kwargs):
        SerialCommunicator.__init__(
            self, port=port, auto_port=auto_port, interpreter=interpreter, **kwargs
        )
        if not hasattr(self, "config"):
            self.config = JsonDict()

    def set_device_property(self, name, value):
        self.config.put("device", "properties", name, value=value)

    def get_device_property(self, name):
        return self.config.get("device", "properties", name, autosave=False)

    def set_device_status(self, name, value):
        self.config.put("device", "status", name, value=value)

    def get_device_status(self, name):
        return self.config.get("device", "status", name, autosave=False)
Exemplo n.º 2
0
def plug_in(appconfig, config=None):
    # plugin app
    global CONFIG
    if CONFIG is None:
        if config is not None:
            CONFIG = config.getsubdict(preamble=["plug_in_django_server"])
        else:
            CONFIG = JsonDict(
                os.path.join(
                    os.path.join(os.path.expanduser("~"),
                                 ".plug_in_django_server"),
                    "plug_in_django_server_config.json",
                ))

    if config is not None:
        appconfig.config = config.getsubdict(preamble=[appconfig.name])
    else:
        if appconfig.config is None:
            appconfig.config = CONFIG.getsubdict(preamble=[appconfig.name])
    logger.info("plug in {} with the config-path:'{}'".format(
        appconfig.name, appconfig.config.file))

    if not hasattr(appconfig, "baseurl"):
        setattr(appconfig, "baseurl", getattr(appconfig, "name"))

    apps = CONFIG.get("django_settings", "apps", "additional", default={})
    if getattr(appconfig, "data_dir", False) is True:
        appconfig.data_dir = os.path.join(os.path.dirname(CONFIG.file),
                                          "{}_data".format(appconfig.name))
        appconfig.data_dir_url = ("/" + appconfig.baseurl +
                                  ("/" if len(appconfig.baseurl) > 0 else "") +
                                  "{}_data".format(appconfig.name))
    else:
        appconfig.data_dir = False

    apps[appconfig.name] = {
        "name": appconfig.name,
        "baseurl": getattr(appconfig, "baseurl"),
        "data_dir": appconfig.data_dir,
    }
    if appconfig.data_dir is not False:
        os.makedirs(appconfig.data_dir, exist_ok=True)

    CONFIG.put("django_settings", "apps", "additional", value=apps)
    CONFIG.save()
def main():
    os.makedirs(BASE_DIR, exist_ok=True)
    config = JsonDict(os.path.join(BASE_DIR, SNAKE_NAME + "_config.json"))

    logging_fmt = (
        "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    )
    logging.basicConfig(
        level=config.get(
            "basic",
            "logging",
            "level",
            default=logging.DEBUG if DEBUGGING else logging.INFO,
        ),
        format=logging_fmt,
        datefmt="(%H:%M:%S)",
    )

    rotating_handler = RotatingFileHandler(
        os.path.join(BASE_DIR, "log.log"),
        maxBytes=config.get("basic", "logging", "max_bytes", default=2 ** 19),
        backupCount=config.get("basic", "logging", "backup_count", default=10),
    )
    rotating_handler.setFormatter(logging.Formatter(logging_fmt))
    logging.getLogger("").addHandler(rotating_handler)

    logger = logging.getLogger(BASENAME)

    coloredlogs.install(level="DEBUG", fmt=logging_fmt)

    logger.info("Use basedir: " + os.path.abspath(BASE_DIR))

    #    board_collection.

    # plugin to django
    plug_in_django_manage.plug_in(DjangoArduinoControllerConfig, config)

    plug_in_django_manage.CONFIG.put("django_settings", "apps", "channels", value=True)

    # set site parameters
    plug_in_django_manage.CONFIG.put("public", "site", "title", value=BASENAME)
    plug_in_django_manage.CONFIG.put("django_settings", "DEBUG", value=DEBUGGING)
    plug_in_django_manage.CONFIG.put("django_settings", "BASE_DIR", value=BASE_DIR)

    # login required
    plug_in_django_manage.CONFIG.put(
        "django_settings",
        "manual",
        "add_to_list",
        "MIDDLEWARE",
        value=[
            #     "global_login_required.GlobalLoginRequiredMiddleware"
        ],
    )
    # if login is required accounds neet to be public
    plug_in_django_manage.CONFIG.put(
        "django_settings",
        "manual",
        "add_to_list",
        "PUBLIC_PATHS",
        value=[r"^/accounts/.*"],
    )

    plug_in_django_manage.run(
        sys.argv[0],
        "runserver",
        "--noreload",
        "0.0.0.0:"
        + str(
            plug_in_django_manage.CONFIG.get("django_settings", "port", default=8000)
        ),
    )
Exemplo n.º 4
0
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

if os.path.exists("/django_settings.json"):
    config = JsonDict("/django_settings.json")
else:
    config = JsonDict("django_settings.json")

SECRET_KEY = config.get(
    'base_settings',
    'SECRET_KEY',
    default="".join([
        random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)")
        for i in range(50)
    ]))
#'l5!r&kcwz5r+#%**eq8j*6p-6@nw&0h)1uf&9c^27sw)9_s^-='

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config.get('base_settings', 'DEBUG', default=False)

ALLOWED_HOSTS = config.get('base_settings', 'ALLOWED_HOSTS', default=[])
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
Exemplo n.º 5
0
def main():
    # main data dir
    os.makedirs(BASE_DIR, exist_ok=True)

    config = JsonDict(os.path.join(BASE_DIR, SNAKE_NAME + "_config.json"))

    # create basic logger
    logging_fmt = "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    logging.basicConfig(
        level=config.get("basic", "logging", "level", default=logging.DEBUG),
        format=logging_fmt,
        datefmt="(%H:%M:%S)",
    )

    rotating_handler = RotatingFileHandler(os.path.join(BASE_DIR, 'log.log'),
                                           maxBytes=config.get("basic",
                                                               "logging",
                                                               "max_bytes",
                                                               default=2**19),
                                           backupCount=config.get(
                                               "basic",
                                               "logging",
                                               "backup_count",
                                               default=10))
    rotating_handler.setFormatter(logging.Formatter(logging_fmt))
    logging.getLogger('').addHandler(rotating_handler)
    # logger.addHandler(logging.StreamHandler())

    logger = logging.getLogger(BASENAME)

    coloredlogs.install(level='DEBUG', fmt=logging_fmt)

    logger.info("Use basedir: " + os.path.abspath(BASE_DIR))

    # set server logger.
    from multi_purpose_arduino_controller.controll_server import manage as controll_server_manage
    # controll_server_manage.logger = logger

    # set_server_config
    controll_server_manage.CONFIG = config.getsubdict(
        preamble=['controll_server'])

    controll_server_manage.CONFIG.put("public",
                                      "site",
                                      "title",
                                      value=BASENAME)

    if config.get("basic", "mode", default=DEFAULT_MODE) == DEVELOPMENT_MODE:
        controll_server_manage.CONFIG.put("django_settings",
                                          "debug",
                                          value=True)
    else:
        controll_server_manage.CONFIG.put("django_settings",
                                          "debug",
                                          value=False)

    apps = [
        "custom_accounts_django",
        "multi_purpose_arduino_controller.controll_server.board_controller",
        "django_websocket_server", "column_switcher.django_app"
    ]
    # apps.append("serverapps.board_creator")
    controll_server_manage.CONFIG.put("django_settings",
                                      "apps",
                                      "additional",
                                      value=apps)

    # create pending migrations if in development mode
    if config.get("basic", "mode", default=DEFAULT_MODE) == DEVELOPMENT_MODE:
        controll_server_manage.run(sys.argv[0], "makemigrations")
        controll_server_manage.run(sys.argv[0], "migrate")

        from django.contrib.auth.models import User
        superusers = User.objects.filter(is_superuser=True)
        if len(superusers) == 0:
            print("No Superuser specified")
            unsername = input("Enter Username: "******"Mail: ")
            pass1 = input("Password: "******"django_settings",
        "static_files",
        "dirs",
        value=[
            os.path.abspath(
                os.path.join(os.path.dirname(__file__), "apis", "static")),
            # socketserver_instance.get_www_data_path(),
            # socketserver.TEMPDIR
        ],
    )

    python_communicator = PythonCommunicator()

    # websocket server
    socketserver_instance = websocket_server.connect_to_first_free_port(
        data_dir=BASE_DIR,
        logger=logging.getLogger("websocket_server"),
        start_in_background=True,
        startport=config.get("websocket", "startport", default=9001),
        disable_encryption=config.get("websocket",
                                      "security",
                                      "disable_encryption",
                                      default=False))

    # parse for boards
    parse_path_for_boards(os.path.join(os.path.dirname(__file__), "boards"))

    board_controller_api = BoardControllerAPI(
        python_communicator=python_communicator,
        websocket_server=socketserver_instance,
        data_dir=os.path.join(BASE_DIR, "BoardControllerAPI"))

    column_switcher_api = ColumnSwitcherAPI(
        python_communicator=python_communicator,
        websocket_server=socketserver_instance,
        data_dir=os.path.join(BASE_DIR, "ColumnSwitcherAPI"))

    # SerialReader
    sr = serialreader.SerialReader(communicator=python_communicator,
                                   start_in_background=True,
                                   config=config.getsubdict(["portdata"]))

    # starts django
    controll_server_manage.run(
        sys.argv[0], "runserver", "--noreload", "0.0.0.0:" + str(
            config.get(
                "controll_server", "django_settings", "port", default=8000)))
class BaseApp:
    DEBUGGING = False
    BASENAME = "BaseApp"
    SNAKE_NAME = BASENAME.lower().replace(" ", "_")
    BASE_DIR = os.path.join(expanduser("~"), "." + SNAKE_NAME)
    app_configs = []
    logging_fmt = (
        "%(asctime)s %(filename)s %(lineno)d %(name)s %(levelname)-8s  %(message)s"
    )

    login_required = True

    def __init__(self):
        self.to_migrate = False
        if not os.path.exists(self.BASE_DIR):
            self.to_migrate = True
        os.makedirs(self.BASE_DIR, exist_ok=True)
        self.config = JsonDict(
            os.path.join(self.BASE_DIR, self.SNAKE_NAME + "_config.json"))

        logging.basicConfig(
            level=self.config.get(
                "basic",
                "logging",
                "level",
                default=logging.DEBUG if self.DEBUGGING else logging.INFO,
            ),
            format=self.logging_fmt,
            datefmt="(%H:%M:%S)",
        )

        rotating_handler = RotatingFileHandler(
            os.path.join(self.BASE_DIR, "log.log"),
            maxBytes=self.config.get("basic",
                                     "logging",
                                     "max_bytes",
                                     default=2**19),
            backupCount=self.config.get("basic",
                                        "logging",
                                        "backup_count",
                                        default=10),
        )

        rotating_handler.setFormatter(logging.Formatter(self.logging_fmt))
        logging.getLogger("").addHandler(rotating_handler)

        logger = logging.getLogger(self.BASENAME)

        coloredlogs.install(level="DEBUG", fmt=self.logging_fmt)

        logger.info("Use basedir: " + os.path.abspath(self.BASE_DIR))

        # plugin to django
        plug_in_django_manage.plug_in(DjangoArduinoControllerConfig,
                                      self.config)
        plug_in_django_manage.CONFIG.put("django_settings",
                                         "apps",
                                         "channels",
                                         value=True)

        # set site parameters
        plug_in_django_manage.CONFIG.put("public",
                                         "site",
                                         "title",
                                         value=self.BASENAME)
        plug_in_django_manage.CONFIG.put("django_settings",
                                         "DEBUG",
                                         value=self.DEBUGGING)
        plug_in_django_manage.CONFIG.put("django_settings",
                                         "BASE_DIR",
                                         value=self.BASE_DIR)

        if self.login_required:
            # login required
            plug_in_django_manage.CONFIG.put(
                "django_settings",
                "manual",
                "add_to_list",
                "MIDDLEWARE",
                value=[
                    #     "global_login_required.GlobalLoginRequiredMiddleware"
                ],
            )
            # if login is required accounds neet to be public
            plug_in_django_manage.CONFIG.put(
                "django_settings",
                "manual",
                "add_to_list",
                "PUBLIC_PATHS",
                value=[r"^/accounts/.*"],
            )

        for app_config in self.app_configs:
            plug_in_django_manage.plug_in(app_config, self.config)

    def migrate(self):
        plug_in_django_manage.run(sys.argv[0], "makemigrations")
        plug_in_django_manage.run(sys.argv[0], "migrate")

    def run(self, open_browser=False, open_data_dir=False):
        self.migrate()
        if self.to_migrate:
            self.migrate()
        if open_browser:

            def check_thread():
                import urllib.request

                while (not urllib.request.urlopen("http://localhost:{}".format(
                        plug_in_django_manage.CONFIG.get(
                            "django_settings", "port",
                            default=8000))).getcode() == 200):
                    time.sleep(200)
                import webbrowser

                webbrowser.open(
                    "http://localhost:{}".format(
                        plug_in_django_manage.CONFIG.get("django_settings",
                                                         "port",
                                                         default=8000)),
                    new=2,
                )

            import threading

            threading.Thread(target=check_thread).start()
        if open_data_dir:
            import webbrowser

            if sys.platform == "darwin":
                webbrowser.open(self.BASE_DIR)
            elif sys.platform == "linux2":
                webbrowser.open(self.BASE_DIR)
            elif sys.platform == "win32":
                webbrowser.open(self.BASE_DIR)
            else:
                webbrowser.open(self.BASE_DIR)
        plug_in_django_manage.run(
            sys.argv[0],
            "runserver",
            "--noreload",
            "0.0.0.0:" + str(
                plug_in_django_manage.CONFIG.get(
                    "django_settings", "port", default=8000)),
        )
class SerialReader:
    def __init__(self,
                 config: JsonDict = None,
                 communicator: python_communicator.PythonCommunicator = None,
                 logger=None,
                 autocheckports=AUTOCHECKPORTS,
                 port_check_time=PORTCHECKTIME,
                 permanently_ignored_ports=None,
                 datalogger: DataLogger = None,
                 start_in_background=False):

        self.ignored_ports = set()
        self.deadports = set()
        self.connected_ports = set()
        self.available_ports = set()
        self.identified_ports = set()

        self.datalogger = DataLogger() if datalogger is None else datalogger

        self.config = JsonDict("portdata.json") if config is None else config

        if permanently_ignored_ports is None:
            permanently_ignored_ports = []

        self.port_check_time = port_check_time
        self.autocheckports = autocheckports
        self.permanently_ignored_ports = set(permanently_ignored_ports)

        self.set_communicator(python_communicator.PythonCommunicator(
        ) if communicator is None else communicator)

        self.logger = logging.getLogger(
            "SerialReader") if logger is None else logger

        if start_in_background:
            self.run_in_background()

    def reactivate_port(self, port=None):
        if port is None:
            return
        try:
            self.ignored_ports.remove(port)
        except:
            pass

        try:
            self.deadports.remove(port)
        except:
            pass

    def deactivate_port(self, port=None):
        if port is None:
            return
        self.ignored_ports.add(port)
        self._communicator.cmd_out(targets=[port], cmd="stop_read")

    def run_in_background(self):
        threading.Thread(target=self.read_forever).start()

    def set_communicator(self,
                         communicator: python_communicator.PythonCommunicator):
        self._communicator = communicator
        self._communicator.add_node("serialreader", self)

    def get_communicator(self):
        return self._communicator

    communicator = property(get_communicator, set_communicator)

    def sendports(self, data_target=None):
        try:
            self._communicator.cmd_out(
                cmd="set_ports",
                sender="serialreader",
                targets=(None if data_target is None else
                         (data_target if isinstance(data_target, list) else
                          [data_target])),
                available_ports=list(self.available_ports),
                ignored_ports=list(self.ignored_ports
                                   | self.permanently_ignored_ports),
                connected_ports=[sp.port for sp in self.connected_ports],
                identified_ports=[sp.port for sp in self.identified_ports],
            )
        except python_communicator.TargetNotFoundException:
            pass

    get_ports = sendports

    def read_forever(self):
        while 1:
            if self.autocheckports:
                self.available_ports, self.ignored_ports = serialdetector.get_avalable_serial_ports(
                    ignore=self.ignored_ports | self.permanently_ignored_ports
                    | set([sp.port for sp in self.connected_ports]))
                self.deadports = self.available_ports.intersection(
                    self.deadports)
                newports = self.available_ports - (
                    self.ignored_ports | self.deadports
                    | self.permanently_ignored_ports)
                self.logger.debug(
                    "available Ports: " + str(self.available_ports) +
                    "; new Ports: " + str(newports) + "; ignored Ports: " +
                    str(self.ignored_ports | self.permanently_ignored_ports) +
                    "; ignored Ports: " +
                    str([sp.port for sp in self.connected_ports]) +
                    "; ignored Ports: " +
                    str([sp.port for sp in self.identified_ports]), )

                self.sendports()
                for port in newports.copy():
                    try:
                        self.open_port(port)
                    except Exception as e:
                        self.logger.exception(e)
                        pass
            time.sleep(self.port_check_time)

    def open_port(self, port):
        try:
            self.available_ports.remove(port)
        except:
            pass
        try:
            self.ignored_ports.remove(port)
        except:
            pass
        try:
            self.permanently_ignored_ports.remove(port)
        except:
            pass

        from ..serialport import SerialPort
        t = threading.Thread(
            target=SerialPort,
            kwargs={
                **{
                    "communicator":
                    self._communicator,
                    "serialreader":
                    self,
                    "config":
                    self.config,
                    "port":
                    port,
                    "baudrate":
                    self.config.get("portdata", port, "baud", default=9600),
                },
                # **kwargs,
            },
        )
        t.start()
Exemplo n.º 8
0
    if not os.path.exists(os.path.join(BASE_DIR, "config.json")):
        copyfile(os.path.join(BASE_DIR, "config_basic.json"),
                 os.path.join(BASE_DIR, "config.json"))
except:
    pass

JCONFIG = JsonDict(os.path.join(BASE_DIR, "config.json"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = JCONFIG.get(
    "settings",
    "server",
    "secret_key",
    default=''.join(random.SystemRandom().choice(
        'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
                    for i in range(50)))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = JCONFIG.get("settings", "server", "debug", default=True)

ALLOWED_HOSTS = JCONFIG.get("settings", "server", "allowed_hosts", default=[])

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
Exemplo n.º 9
0
class DictTest(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.temp_file = os.path.join(self.temp_dir, "d")
        with open(self.temp_file, "wb+") as f:
            f.write(b'{"test":"file"}')
        with open(self.temp_file, "r") as f:
            print(f.read(), self.temp_file)
        self.d = JsonDict(self.temp_file, backup=False)

    def tearDown(self):
        os.remove(self.temp_file)
        os.rmdir(self.temp_dir)

    def test_create_dict(self):
        assert list(self.d.data.keys()) == ["test"]
        assert self.d.get("test") == "file"

    def test_result_is_dict_to_json_dict(self):
        self.d.put("test", value={})
        assert isinstance(self.d.get("test"), JsonSubDict)

    def test_create_subdict(self):
        print("A")
        print(self.d._file_size)
        sub1 = self.d.getsubdict("testsub", "dict")
        print("B")
        sub2 = self.d.getsubdict(["testsub", "dict"])
        assert len(self.d.data.keys()) == 2
        assert len(self.d["testsub"].keys()) == 1

    def test_data_is_dict(self):
        sd = self.d.getsubdict("testsub", "dict")
        assert isinstance(self.d.data, dict)
        assert isinstance(sd.data, dict), (type(sd.data), sd.data)

    def test_double_open_file(self):
        d2 = JsonDict(self.d.file, backup=False)
        d2.put("second_entry", value=1)
        assert len(d2.data.keys()) == len(self.d.data.keys())

    def test_list(self):
        self.d.put("list", value=[0, 1, 2])
        l: json_dict.JsonList = self.d.get("list")
        l[1] = 10
        l.reverse()
        assert all([l[i] == k for i, k in enumerate([2, 10, 0])]), str(l)
        assert l.pop(-1) == 0
        assert all([l[i] == k for i, k in enumerate([2, 10])]), str(l)

    def test_time(self):
        t = time.time()
        json_dict.VERBOSE = False
        d = 1000
        for i in range(d):
            self.d.put("second_entry",
                       value=self.d.get("second_entry", default=0) + 1)
        print(time.time() - t)
        assert self.d.get("second_entry", default=1) == d
        json_dict.VERBOSE = True

    def test_empty_key(self):
        dic = {
            "data": {
                "": {
                    "inempty": ["test"],
                }
            }
        }
        self.d.data = dic
        ds = JsonDict(self.d.file)
        print(ds)