diff options
author | FxQnLr <[email protected]> | 2024-04-30 20:47:13 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-30 20:47:13 +0200 |
commit | d95f49be9dde63de3694a56316fe7e07ad4062e7 (patch) | |
tree | e07af9156af08a15dbb33b6718359a3fc7c004d2 /messages.lua | |
parent | ab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b (diff) | |
download | funsaac-main.tar funsaac-main.tar.gz funsaac-main.zip |
Diffstat (limited to 'messages.lua')
-rw-r--r-- | messages.lua | 66 |
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 | |||
1 | local json = require("json") | 3 | local json = require("json") |
2 | 4 | ||
5 | WsClient = require("websocket").new("127.0.0.1", 12345, "/") | ||
6 | |||
7 | CONNECTED = false | ||
8 | |||
9 | function WsClient:onmessage(message) | ||
10 | HandleResponse(ResponseContent(message)) | ||
11 | if CONNECTED == true then | ||
12 | print("StartScanning") | ||
13 | StartScanning() | ||
14 | CONNECTED = false | ||
15 | end | ||
16 | end | ||
17 | |||
18 | function WsClient:onopen() | ||
19 | RequestServerInfo() | ||
20 | end | ||
21 | |||
3 | Messages = { | 22 | Messages = { |
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 | ||
22 | local cnt = 1; | 56 | local cnt = 1; |
23 | 57 | ||
58 | local function getEncodedJson(msg) | ||
59 | return "[" .. json.encode(msg) .. "]" | ||
60 | end | ||
61 | |||
62 | ---Get message with correct id | ||
63 | ---@param msg table Message from Messages table | ||
24 | function GetMessage(msg) | 64 | function 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 |
69 | end | ||
70 | |||
71 | -- REQUESTS | ||
72 | |||
73 | ---Request Info from the server | ||
74 | function RequestServerInfo() | ||
75 | WsClient:send(getEncodedJson(GetMessage(Messages.RequestServerInfo))) | ||
29 | end | 76 | end |
30 | 77 | ||
78 | function StartScanning() | ||
79 | WsClient:send(getEncodedJson(GetMessage(Messages.StartScanning))) | ||
80 | end | ||
81 | |||
82 | function ScalarCmd(strength) | ||
83 | local message = GetMessage(Messages.ScalarCmd) | ||
84 | -- message[next(Messages.ScalarCmd)]["Scalars"][0]["Scalar"] = strength | ||
85 | WsClient:send(getEncodedJson(message)) | ||
86 | end | ||
87 | |||
88 | |||
89 | --RESPONSES | ||
90 | |||
31 | function ResponseContent(message) | 91 | function 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) |