From d95f49be9dde63de3694a56316fe7e07ad4062e7 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Tue, 30 Apr 2024 20:47:13 +0200 Subject: working connection and basic request --- messages.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'messages.lua') diff --git a/messages.lua b/messages.lua index 69576e1..a3f8d77 100644 --- a/messages.lua +++ b/messages.lua @@ -1,33 +1,93 @@ +---@diagnostic disable: duplicate-set-field + local json = require("json") +WsClient = require("websocket").new("127.0.0.1", 12345, "/") + +CONNECTED = false + +function WsClient:onmessage(message) + HandleResponse(ResponseContent(message)) + if CONNECTED == true then + print("StartScanning") + StartScanning() + CONNECTED = false + end +end + +function WsClient:onopen() + RequestServerInfo() +end + Messages = { -- Handshake RequestServerInfo = { RequestServerInfo = { Id = 1, - ClientName = "Funsaac v0.0.2", + ClientName = "Funsaac v0.0.3", MessageVersion = 3 } }, -- Enumeration - + StartScanning = { StartScanning = { Id = 1 } + }, + + -- Generic Device + ScalarCmd = { + ScalarCmd = { + Id = 1, + DeviceIndex = 0, + Scalars = { + { + Index = 0, + Scalar = 1, + ActuatorType = "Vibrate" + } + } + } } } local cnt = 1; +local function getEncodedJson(msg) + return "[" .. json.encode(msg) .. "]" +end + +---Get message with correct id +---@param msg table Message from Messages table function GetMessage(msg) local message = msg message[next(msg)]["Id"] = cnt cnt = cnt + 1 - return "[" .. json.encode(message) .. "]" + return message +end + +-- REQUESTS + +---Request Info from the server +function RequestServerInfo() + WsClient:send(getEncodedJson(GetMessage(Messages.RequestServerInfo))) end +function StartScanning() + WsClient:send(getEncodedJson(GetMessage(Messages.StartScanning))) +end + +function ScalarCmd(strength) + local message = GetMessage(Messages.ScalarCmd) + -- message[next(Messages.ScalarCmd)]["Scalars"][0]["Scalar"] = strength + WsClient:send(getEncodedJson(message)) +end + + +--RESPONSES + function ResponseContent(message) local msg = json.decode(message)[1] local type = next(msg) -- cgit v1.2.3