Skip to content

main

main() #

Starts the copilot.

During installation of cogip-tools, setuptools is configured to create the cogip-monitor script using this function as entrypoint.

Source code in cogip/tools/monitor/main.py
177
178
179
180
181
182
183
184
def main():
    """
    Starts the copilot.

    During installation of cogip-tools, `setuptools` is configured
    to create the `cogip-monitor` script using this function as entrypoint.
    """
    typer.run(main_opt)

main_opt(url=typer.Argument('http://localhost:8091', envvar='COGIP_SOCKETIO_SERVER_URL', help='Socket.IO Server URL')) #

Launch COGIP Monitor.

Source code in cogip/tools/monitor/main.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def main_opt(
    url: str = typer.Argument(
        "http://localhost:8091",
        envvar="COGIP_SOCKETIO_SERVER_URL",
        help="Socket.IO Server URL",
    ),
) -> None:
    """
    Launch COGIP Monitor.
    """
    if os.getenv("QT_QPA_PLATFORM") is None:
        os.environ["QT_QPA_PLATFORM"] = "xcb"

    QCoreApplication.setOrganizationName("cogip")
    QCoreApplication.setOrganizationDomain("cogip.tools")
    QCoreApplication.setApplicationName("COGIP Monitor")

    sio_client = SocketioClient(url)

    app = QGuiApplication(sys.argv)
    app.setFont(QFont("Ubuntu", 11))

    QQuickStyle.setStyle("Fusion")

    palette = app.palette()
    palette.setColor(QPalette.ColorRole.Window, "#2a2a2a")
    palette.setColor(QPalette.ColorRole.WindowText, "#ffffff")
    palette.setColor(QPalette.ColorRole.Base, "#2a2a2a")
    palette.setColor(QPalette.ColorRole.AlternateBase, "#272727")
    palette.setColor(QPalette.ColorRole.ToolTipBase, "#ffffdc")
    palette.setColor(QPalette.ColorRole.ToolTipText, "#000000")
    palette.setColor(QPalette.ColorRole.Text, "#ffffff")
    palette.setColor(QPalette.ColorRole.Button, "#2a2a2a")
    palette.setColor(QPalette.ColorRole.ButtonText, "#ffffff")
    palette.setColor(QPalette.ColorRole.BrightText, "#ffffff")
    palette.setColor(QPalette.ColorRole.Highlight, "#e95420")
    palette.setColor(QPalette.ColorRole.HighlightedText, "#ffffff")
    palette.setColor(QPalette.ColorRole.Light, "#343434")
    palette.setColor(QPalette.ColorRole.Midlight, "#2f2f2f")
    palette.setColor(QPalette.ColorRole.Dark, "#252525")
    palette.setColor(QPalette.ColorRole.Mid, "#2f2f2f")
    palette.setColor(QPalette.ColorRole.Shadow, "#020202")
    palette.setColor(QPalette.ColorRole.Link, "#308cc6")
    palette.setColor(QPalette.ColorRole.LinkVisited, "#ff00ff")
    palette.setColor(QPalette.ColorRole.NoRole, "#000000")
    palette.setColor(QPalette.ColorRole.PlaceholderText, "#9b9b9b")
    palette.setColor(QPalette.ColorRole.Accent, "#308cc6")
    app.setPalette(palette)

    qmlRegisterType(View3DBackend, "View3DBackend", 1, 0, "View3DBackend")

    engine = QQmlApplicationEngine()

    # Add PySide6's QML import path
    engine.addImportPath(PySide6.__path__[0])

    qml_file = Path(__file__).parent / "main.qml"
    engine.load(QUrl.fromLocalFile(str(qml_file)))

    if not engine.rootObjects():
        logger.error("No root objects loaded, exiting.")
        sys.exit(-1)

    root: QWindow = engine.rootObjects()[0]
    scene_qml = root.findChild(QObject, "Scene")
    view3d_backend = View3DBackend(scene_qml)
    view3d_qml = scene_qml.findChild(QObject, "view")
    view3d_qml.setProperty("view3DBackend", view3d_backend)
    root.setProperty("isConnected", False)
    root.setProperty("starterChecked", False)
    root.setProperty("socketClient", sio_client)
    root.setProperty("toolMenu", {"name": "", "entries": []})
    obstacle_storage = ObstacleStorage()
    root.setProperty("obstacleStorage", obstacle_storage)

    table = Table(view3d_qml.findChild(QObject, "table"))
    root.setProperty("table", table)

    def request_quit() -> None:
        QtCore.QTimer.singleShot(0, app.quit)

    def handle_connected(connected: bool) -> None:
        def update_state() -> None:
            root.setProperty("isConnected", connected)
            if not connected:
                root.setProperty("toolMenu", {"name": "", "entries": []})
                root.setProperty("starterChecked", False)
                QtCore.QMetaObject.invokeMethod(
                    root,
                    "closeAllConfigWindows",
                    QtCore.Qt.ConnectionType.QueuedConnection,
                )
                QtCore.QMetaObject.invokeMethod(
                    root,
                    "closeWizardWindow",
                    QtCore.Qt.ConnectionType.QueuedConnection,
                )

        QtCore.QTimer.singleShot(0, update_state)

    def handle_tool_menu(menu: models.ShellMenu) -> None:
        filtered_entries: list[dict[str, str]] = []
        for entry in menu.entries:
            if entry.cmd.startswith("_"):
                continue
            entry_dict = {"cmd": entry.cmd, "desc": entry.desc}
            if entry.cmd == "exit":
                filtered_entries.insert(0, entry_dict)
            else:
                filtered_entries.append(entry_dict)
        payload = {
            "name": menu.name,
            "entries": filtered_entries,
        }

        QtCore.QTimer.singleShot(0, lambda: root.setProperty("toolMenu", payload))

    def handle_wizard_request(data: dict[str, Any]) -> None:
        QtCore.QMetaObject.invokeMethod(
            root,
            "openWizardWindow",
            QtCore.Qt.ConnectionType.QueuedConnection,
            QtCore.Q_ARG("QVariant", data),
        )

    def handle_close_wizard() -> None:
        QtCore.QMetaObject.invokeMethod(
            root,
            "closeWizardWindow",
            QtCore.Qt.ConnectionType.QueuedConnection,
        )

    def handle_starter_changed(pushed: bool) -> None:
        QtCore.QTimer.singleShot(0, lambda: root.setProperty("starterChecked", pushed))

    sio_client.signal_connected.connect(handle_connected)
    sio_client.signal_exit.connect(request_quit)
    sio_client.signal_add_robot.connect(view3d_backend.handle_add_robot)
    sio_client.signal_del_robot.connect(view3d_backend.handle_del_robot)
    sio_client.signal_pose_current.connect(view3d_backend.handle_pose_current)
    sio_client.signal_robot_path.connect(view3d_backend.handle_robot_path)
    sio_client.signal_tool_menu.connect(handle_tool_menu)
    sio_client.signal_wizard_request.connect(handle_wizard_request)
    sio_client.signal_close_wizard.connect(handle_close_wizard)
    sio_client.signal_starter_changed.connect(handle_starter_changed)

    sio_client.start()

    ret = app.exec()

    sio_client.stop()

    sys.exit(ret)