Add more
This commit is contained in:
parent
386e76c80c
commit
2a2aa6a793
5 changed files with 1400 additions and 0 deletions
110
pcm-norma-client.lua
Normal file
110
pcm-norma-client.lua
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
local connectionURL = "wss://0a71-2a03-ec00-b14a-55a-39f7-fdb9-1340-2e18.ngrok-free.app"
|
||||
|
||||
local function splitByChunk(text, chunkSize)
|
||||
local s = {}
|
||||
for i=1, #text, chunkSize do
|
||||
s[#s+1] = text:sub(i,i+chunkSize - 1)
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
function string.starts(String,Start)
|
||||
return string.sub(String,1,string.len(Start))==Start
|
||||
end
|
||||
|
||||
local dfpwm = require("cc.audio.dfpwm")
|
||||
local ws, err = http.websocket(connectionURL)
|
||||
local speakers = {peripheral.find("speaker")}
|
||||
local monitor = peripheral.find("monitor")
|
||||
|
||||
if monitor then
|
||||
monitor.clear()
|
||||
monitor.setCursorPos(1, 1)
|
||||
monitor.write("yourfriend's music")
|
||||
monitor.setCursorPos(1, 3)
|
||||
monitor.write("currently playing:")
|
||||
monitor.setCursorPos(1, 4)
|
||||
|
||||
monitor.write("nothing")
|
||||
end
|
||||
|
||||
function restart()
|
||||
print("connection with ", connectionURL, " closed!")
|
||||
print("restarting in 1s")
|
||||
os.sleep(1)
|
||||
ws, err = http.websocket(connectionURL)
|
||||
if not ws then
|
||||
printError(err)
|
||||
restart()
|
||||
end
|
||||
end
|
||||
|
||||
if not ws then
|
||||
restart()
|
||||
printError(err)
|
||||
end
|
||||
|
||||
|
||||
parallel.waitForAll(
|
||||
function()
|
||||
while true do
|
||||
local _, url = os.pullEvent("websocket_closed")
|
||||
if url == connectionURL then
|
||||
restart()
|
||||
end
|
||||
end
|
||||
end,
|
||||
function()
|
||||
while true do
|
||||
local _, url, resp, isBinary = os.pullEvent("websocket_message")
|
||||
if url == connectionURL then
|
||||
if not string.starts(resp, "{") then
|
||||
local bytes = {("b"):rep(#resp):unpack(resp)}
|
||||
bytes[#bytes]=nil
|
||||
|
||||
local functions = {}
|
||||
|
||||
for k,v in pairs(speakers) do
|
||||
table.insert(functions, function ()
|
||||
while not v.playAudio(bytes, 3) do
|
||||
os.pullEvent("speaker_audio_empty")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
parallel.waitForAll(table.unpack(functions))
|
||||
else
|
||||
local json = textutils.unserialiseJSON(resp)
|
||||
if json.type == "update" then
|
||||
local file = io.open(shell.getRunningProgram(), "w")
|
||||
if not file then return end
|
||||
file:write(json.file)
|
||||
end
|
||||
if json.type == "playing" then
|
||||
if monitor then
|
||||
local width, height = monitor.getSize()
|
||||
for k=4,height do
|
||||
monitor.setCursorPos(1, k)
|
||||
monitor.write(string.rep(" ", width))
|
||||
end
|
||||
|
||||
for k, v in pairs(splitByChunk(json.data, width)) do
|
||||
monitor.setCursorPos(1, k+3)
|
||||
monitor.write(v)
|
||||
end
|
||||
else
|
||||
print("Playing: ")
|
||||
print(json.data)
|
||||
end
|
||||
end
|
||||
if json.type == "reboot" then
|
||||
os.reboot()
|
||||
end
|
||||
if json.type == "shutdown" then
|
||||
os.shutdown()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue