extends Node class_name SammiAPI signal request_completed(response: ApiResponse) @export_category("SAMMI Connection Information") @export var host: String = "127.0.0.1" @export var port: int = 9450 @export var password: String = "" var client: SammiClient var last_error: String = "" var last_error_description: String = "" func _ready() -> void: client = SammiClient.new() client.host = host client.port = port client.password = password client.request_completed.connect(_on_request_completed) func get_variable(name: String, button_id: String = "") -> Variant: var api_path: String = "/api?request=getVariable&name=%s" % name if button_id != "": api_path += "&button_id=%s" % button_id client.request(SammiClient.Method.GET, api_path, "") var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.contains("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return res else: return res["data"] else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return null func set_variable(name: String, value: Variant, button_id) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "setVariable", "name": name, "value": value, "button_id": button_id } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: Invalid JSON Returned. Body: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func delete_variable(name: String, button_id) -> bool: var api_path: String = "/api" % name var req: Dictionary = { "request": "deleteVariable", "name": name, "button_id": button_id } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func insert_array(name: String, position: int, value: Variant, button_id: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "insertArray", "name": name, "position": position, "value": value, "button_id": button_id } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func delete_array(name: String, position: int, button_id: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "deleteArray", "name": name, "position": position, "button_id": button_id } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func get_deck_status(deck_id: String) -> bool: var api_path: String = "/api?request=getDeckStatus&deck_id=%s" % deck_id client.request(SammiClient.Method.GET, api_path, "") var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: return true else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func change_deck_status(deck_id: String, status: int) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "changeDeckStatus", "deck_id": deck_id, "status": status } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func trigger_button(button_id: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "triggerButton", "button_id": button_id } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func release_button(button_id: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "releaseButton", "button_id": button_id } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func modify_button(button_id: String, text: String = "", color: int = -1, image: String = "", border: int = -1) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "modifyButton", "button_id": button_id, } if text != "": req["text"] = text if color != -1: req["color"] = color if image != "": req["image"] = image if border != -1: req["border"] = border client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func popup_message(message: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "popupMessage", "message": message } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func alert_message(message: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "alertMessage", "message": message } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func notification_message(message: String, title: String) -> bool: var api_path: String = "/api" var req: Dictionary = { "request": "notificationMessage", "message": message, "title": title } client.request(SammiClient.Method.POST, api_path, JSON.stringify(req)) var response: SammiClient.SammiResponse = await client.request_completed if response.code == 200: var res := JSON.parse_string(response.body) if res.has("data") and res["data"] == "Ok.": return true elif res.has("error"): push_error("Error: %s: %s" % [res["Error"], res["Description"]]) last_error = res["Error"] last_error_description = res["Description"] return false else: push_error("Error: %s" % response.body) last_error = "Invalid JSON Returned" last_error_description = response.body return false else: push_error("Error: %d: %s" % [response.code, response.error_message]) last_error = "HTTP Error: %d" % response.code last_error_description = response.error_message return false func _on_request_completed(response: SammiClient.SammiResponse) -> void: var resp: ApiResponse = ApiResponse.new() if response.code == 200: var res := JSON.parse_string(response.body) if res.has("error"): resp.error = res["Error"] resp.error_description = res["Description"] else: resp.data = res["data"] else: resp.error = "HTTP Error: %d" % response.code resp.error_description = response.error_message request_completed.emit(resp) class ApiResponse: var data: Variant var error: String var error_description: String