diff --git a/groupTasks/groups/internal-stuff/generate.py b/groupTasks/groups/internal-stuff/generate.py new file mode 100644 index 0000000..0df40e7 --- /dev/null +++ b/groupTasks/groups/internal-stuff/generate.py @@ -0,0 +1,94 @@ +import requests +import cairosvg +from PIL import Image +import mysql.connector +import sys + +max_apriltag_value = 48714 + +table_name = "data" +id_to_update = "apriltag" + +conn = mysql.connector.connect( + host="localhost", + port="50035", + user="ad-internalstuff", + password="cdQK4pfr3_YfnE1u", + database="ad-internalstuff" +) + +# create table if not exists +create_table_query = f"CREATE TABLE IF NOT EXISTS {table_name} (id VARCHAR(255) PRIMARY KEY, value INT)" + +cursor = conn.cursor() +cursor.execute(create_table_query) +cursor.close() + +# get current value +select_query = f"SELECT value FROM {table_name} WHERE id = %s" + +cursor = conn.cursor() +cursor.execute(select_query, (id_to_update,)) +result = cursor.fetchone() + +db_value = 0 + +if result: + db_value = result[0] +else: + # value does not exist, create it + insert_query = f"INSERT INTO {table_name} (id, value) VALUES (%s, %s)" + + cursor = conn.cursor() + cursor.execute(insert_query, (id_to_update, 0)) + conn.commit() + +# check if max value is reached +if db_value >= max_apriltag_value: + print(f"Max value of {max_apriltag_value} reached.") + sys.exit(1) + +# update value +update_query = f"UPDATE {table_name} SET value = value + %s WHERE id = %s" + +db_value += 1 + +cursor = conn.cursor() +cursor.execute(update_query, (1, id_to_update)) +conn.commit() + +# close connection to database +cursor.close() +conn.close() + +# image handling +new_file_number = str(db_value).zfill(5) + +print(f"April Tag number: {db_value}") + +svg_url = f"https://git.ex.umbach.dev/Alex/apriltag-chaitanyantr/raw/branch/main/tagStandard52h13/tag52_13_{new_file_number}.svg" + +# Fordere das SVG-Bild von der URL an +response = requests.get(svg_url) + +if response.status_code == 200: + try: + # Speichere das SVG-Bild in einer Datei + svg_file_name = "apriltag.svg" + with open(svg_file_name, "wb") as svg_file: + svg_file.write(response.content) + + # Konvertiere das SVG-Bild in ein PNG-Bild + png_data = cairosvg.svg2png(bytestring=response.content) + + # Speichere das PNG-Bild in einer Datei + png_file_name = "apriltag.png" + with open(png_file_name, "wb") as png_file: + png_file.write(png_data) + + except Exception as e: + print("Fehler beim Verarbeiten des Bildes:", str(e)) + sys.exit(1) +else: + print("Fehler beim Herunterladen des SVG-Bildes. Statuscode:", response.status_code) + sys.exit(1) \ No newline at end of file diff --git a/groupTasks/groups/internal-stuff/index.json b/groupTasks/groups/internal-stuff/index.json new file mode 100644 index 0000000..336234c --- /dev/null +++ b/groupTasks/groups/internal-stuff/index.json @@ -0,0 +1,15 @@ +{ + "category": "InternalStuff", + "name": "April Tag generieren", + "globalInputs": [], + "tasks": [ + { + "name": "April Tag generieren", + "onFinish": "next", + "undoPossible": false, + "repeatPossible": true, + "scriptPath": "generate.py", + "parameters": [] + } + ] +} diff --git a/groupTasks/groups/internal-stuff/requirements.txt b/groupTasks/groups/internal-stuff/requirements.txt new file mode 100644 index 0000000..e400bbd --- /dev/null +++ b/groupTasks/groups/internal-stuff/requirements.txt @@ -0,0 +1,3 @@ +opencv-python==4.8.1.78 +CairoSVG==2.7.1 +mysql-connector-python==8.1.0 \ No newline at end of file diff --git a/groupTasks/groups/rex-robots/test.py b/groupTasks/groups/rex-robots/test.py index fd4c988..6b139a4 100644 --- a/groupTasks/groups/rex-robots/test.py +++ b/groupTasks/groups/rex-robots/test.py @@ -1,12 +1,30 @@ import sys import os +import random # add the path to the libs folder sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) from libs.robots import rex -rexRobot = rex.Rex("test", "Drucker leeren") +randomJobs = [ + "Drucker leeren", + "Müll rausbringen", + "Kopf schütteln", + "Runterfahren", + "Rausgehen", + "Game starten", + "Dreieck zeichnen", + "Quadrat zeichnen", + "Hand schütteln", + "Haus zeichnen", +] + +rJob = randomJobs[random.randint(0, len(randomJobs) - 1)] + +print("job: " + rJob) + +rexRobot = rex.Rex("test", rJob) # rexRobot.moveToXYZ(1, 2, 3) rexRobot.move_to_x(5) diff --git a/groupTasks/libs/__init__.py b/groupTasks/libs/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/groupTasks/libs/robots/__pycache__/rex.cpython-39.pyc b/groupTasks/libs/robots/__pycache__/rex.cpython-39.pyc index ef5044b..707b9a6 100644 Binary files a/groupTasks/libs/robots/__pycache__/rex.cpython-39.pyc and b/groupTasks/libs/robots/__pycache__/rex.cpython-39.pyc differ diff --git a/groupTasks/libs/robots/rex.py b/groupTasks/libs/robots/rex.py index 653d1a1..c358af4 100644 --- a/groupTasks/libs/robots/rex.py +++ b/groupTasks/libs/robots/rex.py @@ -1,6 +1,24 @@ import requests import uuid +from enum import Enum +import sys +### +# GLOBALS +### + +class ResponseStatus(): + Ok = 0 + Error = 1 + Offline = 2 + +max_job_name_length = 30 + +### +# CLASSES +### + +# TODO: add oriention left or right class Rex: """ @@ -9,7 +27,7 @@ class Rex: def __init__(self, robotName, jobName, address='http://localhost:50055'): self.robotName = robotName self.jobId = uuid.uuid4().__str__() - self.jobName = jobName + self.jobName = jobName[:max_job_name_length] self.address = address self.x = 0 self.y = 0 @@ -28,6 +46,23 @@ class Rex: res = requests.post(self.address + '/v1/control/1', json=obj) print("status code", res.status_code) + print("res", res.json()) + + json = res.json() + + status = json['Status'] + + if status == ResponseStatus.Offline: + print("robot offline") + sys.exit(1) + + if status != ResponseStatus.Error: + print("robot error") + sys.exit(1) + + if status != ResponseStatus.Ok: + print("robot error") + sys.exit(1) def move_to_x(self, x): """ diff --git a/groupTasks/libs/test.py b/groupTasks/libs/test.py deleted file mode 100644 index 08641b9..0000000 --- a/groupTasks/libs/test.py +++ /dev/null @@ -1,3 +0,0 @@ -# test.py -def testa(): - print("Hello from test.py") \ No newline at end of file diff --git a/main.go b/main.go index 2e69699..67045f2 100644 --- a/main.go +++ b/main.go @@ -125,8 +125,14 @@ func main() { // requested upgrade to the WebSocket protocol. if websocket.IsWebSocketUpgrade(c) { sessionId := c.Query("auth") + // needed for a user who uses multiple tabs in the browser + // with the same session id because otherwise the last browser + // tab would subscribe to the topic and the other tabs would + // not receive any messages + browserTabSession := c.Query("bts") - if len(sessionId) != utils.LenHeaderXAuthorization { + if len(sessionId) != utils.LenHeaderXAuthorization || + len(browserTabSession) != utils.LenHeaderXAuthorization { return c.SendStatus(fiber.StatusUnauthorized) } @@ -142,6 +148,7 @@ func main() { if user.Id != "" { c.Locals("sessionId", sessionId) + c.Locals("browserTabSession", browserTabSession) c.Locals("userId", user.Id) } } diff --git a/modules/cache/socketclient.go b/modules/cache/socketclient.go index 9e0304b..0360bcb 100644 --- a/modules/cache/socketclient.go +++ b/modules/cache/socketclient.go @@ -5,7 +5,6 @@ import ( "sync" "github.com/gofiber/websocket/v2" - "github.com/rs/zerolog/log" ) var socketClients []*structs.SocketClient @@ -41,14 +40,12 @@ func GetSocketClients() []*structs.SocketClient { return socketClients } -func SubscribeSocketClientToTopic(sessionId string, topic string) { +func SubscribeSocketClientToTopic(browserTabSession string, topic string) { mu.Lock() defer mu.Unlock() - log.Info().Msgf("Subscribing session %s to topic %s", sessionId, topic) - for _, socketClient := range socketClients { - if socketClient.SessionId == sessionId { + if socketClient.BrowserTabSession == browserTabSession { socketClient.SubscribedTopic = topic break } diff --git a/modules/grouptasks/grouptasks.go b/modules/grouptasks/grouptasks.go index c301256..4319d7e 100644 --- a/modules/grouptasks/grouptasks.go +++ b/modules/grouptasks/grouptasks.go @@ -974,6 +974,8 @@ func InstallPythonPackages(userId string, category string, groupId string) { cmd := exec.Command("pip3", "install", "-r", config.Cfg.FolderPaths.GroupTasksGroups+groupId+"/requirements.txt") out, err := cmd.CombinedOutput() + log.Info().Msgf("pa %s", config.Cfg.FolderPaths.GroupTasksGroups+groupId+"/requirements.txt") + if err != nil { logger.AddGroupTasksLog("Installing python packages for groupId %s of category %s failed. Error: %s", category, groupId, err.Error()) @@ -982,7 +984,7 @@ func InstallPythonPackages(userId string, category string, groupId string) { convertedXYPermission, utils.SubscribedDynamicTopicGroupTasks+category, structs.SendSocketMessage{ - Cmd: utils.SentCmdInstallingPythonPackages, + Cmd: utils.SentCmdInstallingPythonPackagesFailed, Body: messageBody, }) return diff --git a/modules/structs/socket.go b/modules/structs/socket.go index eb0ec8b..c64845a 100644 --- a/modules/structs/socket.go +++ b/modules/structs/socket.go @@ -17,11 +17,12 @@ const ( ) type SocketClient struct { - SessionId string - UserId string - Conn *websocket.Conn - connMu sync.Mutex - SubscribedTopic string + SessionId string + BrowserTabSession string + UserId string + Conn *websocket.Conn + connMu sync.Mutex + SubscribedTopic string } type SocketMessage struct { diff --git a/socketserver/hub.go b/socketserver/hub.go index 0ee4a4e..53dec08 100644 --- a/socketserver/hub.go +++ b/socketserver/hub.go @@ -29,6 +29,7 @@ func RunHub() { select { case newSocketClient := <-register: userId := fmt.Sprintf("%v", newSocketClient.Conn.Locals("userId")) + browserTabSession := fmt.Sprintf("%v", newSocketClient.Conn.Locals("browserTabSession")) sessionId := fmt.Sprintf("%v", newSocketClient.Conn.Locals("sessionId")) // close connection instantly if sessionId is empty @@ -38,6 +39,7 @@ func RunHub() { } newSocketClient.SessionId = sessionId + newSocketClient.BrowserTabSession = browserTabSession newSocketClient.UserId = userId cache.AddSocketClient(newSocketClient) @@ -315,7 +317,7 @@ func RunHub() { case utils.ReceivedCmdSubscribeToTopic: log.Debug().Msgf("Received subscribe to topic: %v", receivedMessage.Body) - cache.SubscribeSocketClientToTopic(data.Conn.Locals("sessionId").(string), receivedMessage.Body["topic"].(string)) + cache.SubscribeSocketClientToTopic(receivedMessage.Body["browserTabSession"].(string), receivedMessage.Body["topic"].(string)) break case utils.ReceivedCmdDeleteAllNotifications: notification.DeleteAllNotifications(data.Conn.Locals("userId").(string))