summaryrefslogtreecommitdiff
path: root/messages.lua
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-30 20:47:13 +0200
committerFxQnLr <[email protected]>2024-04-30 20:47:13 +0200
commitd95f49be9dde63de3694a56316fe7e07ad4062e7 (patch)
treee07af9156af08a15dbb33b6718359a3fc7c004d2 /messages.lua
parentab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b (diff)
downloadfunsaac-main.tar
funsaac-main.tar.gz
funsaac-main.zip
working connection and basic requestHEADmain
Diffstat (limited to 'messages.lua')
-rw-r--r--messages.lua66
1 files changed, 63 insertions, 3 deletions
diff --git a/messages.lua b/messages.lua
index 69576e1..a3f8d77 100644
--- a/messages.lua
+++ b/messages.lua
@@ -1,33 +1,93 @@
1---@diagnostic disable: duplicate-set-field
2
1local json = require("json") 3local json = require("json")
2 4
5WsClient = require("websocket").new("127.0.0.1", 12345, "/")
6
7CONNECTED = false
8
9function WsClient:onmessage(message)
10 HandleResponse(ResponseContent(message))
11 if CONNECTED == true then
12 print("StartScanning")
13 StartScanning()
14 CONNECTED = false
15 end
16end
17
18function WsClient:onopen()
19 RequestServerInfo()
20end
21
3Messages = { 22Messages = {
4 -- Handshake 23 -- Handshake
5 RequestServerInfo = { 24 RequestServerInfo = {
6 RequestServerInfo = { 25 RequestServerInfo = {
7 Id = 1, 26 Id = 1,
8 ClientName = "Funsaac v0.0.2", 27 ClientName = "Funsaac v0.0.3",
9 MessageVersion = 3 28 MessageVersion = 3
10 } 29 }
11 }, 30 },
12 31
13 -- Enumeration 32 -- Enumeration
14 33
15 StartScanning = { 34 StartScanning = {
16 StartScanning = { 35 StartScanning = {
17 Id = 1 36 Id = 1
18 } 37 }
38 },
39
40 -- Generic Device
41 ScalarCmd = {
42 ScalarCmd = {
43 Id = 1,
44 DeviceIndex = 0,
45 Scalars = {
46 {
47 Index = 0,
48 Scalar = 1,
49 ActuatorType = "Vibrate"
50 }
51 }
52 }
19 } 53 }
20} 54}
21 55
22local cnt = 1; 56local cnt = 1;
23 57
58local function getEncodedJson(msg)
59 return "[" .. json.encode(msg) .. "]"
60end
61
62---Get message with correct id
63---@param msg table Message from Messages table
24function GetMessage(msg) 64function GetMessage(msg)
25 local message = msg 65 local message = msg
26 message[next(msg)]["Id"] = cnt 66 message[next(msg)]["Id"] = cnt
27 cnt = cnt + 1 67 cnt = cnt + 1
28 return "[" .. json.encode(message) .. "]" 68 return message
69end
70
71-- REQUESTS
72
73---Request Info from the server
74function RequestServerInfo()
75 WsClient:send(getEncodedJson(GetMessage(Messages.RequestServerInfo)))
29end 76end
30 77
78function StartScanning()
79 WsClient:send(getEncodedJson(GetMessage(Messages.StartScanning)))
80end
81
82function ScalarCmd(strength)
83 local message = GetMessage(Messages.ScalarCmd)
84 -- message[next(Messages.ScalarCmd)]["Scalars"][0]["Scalar"] = strength
85 WsClient:send(getEncodedJson(message))
86end
87
88
89--RESPONSES
90
31function ResponseContent(message) 91function ResponseContent(message)
32 local msg = json.decode(message)[1] 92 local msg = json.decode(message)[1]
33 local type = next(msg) 93 local type = next(msg)