SHOW:
|
|
- or go back to the newest paste.
| 1 | - | local HOST = "bridge.computercraft.ru:1111" |
| 1 | + | local HOST = "bridge.computercraft.ru:1111" |
| 2 | - | local commandIdent = "+#" |
| 2 | + | local commandIdent = "+#" |
| 3 | - | local deviceCommandIdent = "+$" |
| 3 | + | local deviceCommandIdent = "+$" |
| 4 | - | local internet |
| 4 | + | local internet |
| 5 | - | local socket, connectedToBridge, connectedToDevice, KEY |
| 5 | + | local socket, connectedToBridge, connectedToDevice, KEY |
| 6 | - | local msgBuffer = {} |
| 6 | + | local msgBuffer = {}
|
| 7 | - | local BR = {} |
| 7 | + | local BR = {}
|
| 8 | - | |
| 8 | + | |
| 9 | - | function BR.split(source, delimiters) |
| 9 | + | function BR.split(source, delimiters) |
| 10 | - | local elements = {} |
| 10 | + | local elements = {}
|
| 11 | - | local pattern = '([^'..delimiters..']+)' |
| 11 | + | local pattern = '([^'..delimiters..']+)' |
| 12 | - | string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end); |
| 12 | + | string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end); |
| 13 | - | return elements |
| 13 | + | return elements |
| 14 | - | end |
| 14 | + | end |
| 15 | - | |
| 15 | + | |
| 16 | - | local function tableToJSON(arr) |
| 16 | + | local function tableToJSON(arr) |
| 17 | - | local json = "{" |
| 17 | + | local json = "{"
|
| 18 | - | local delim = "" |
| 18 | + | local delim = "" |
| 19 | - | for k, v in pairs(arr) do |
| 19 | + | for k, v in pairs(arr) do |
| 20 | - | json = json .. delim .."\"" .. tostring(k) .. "\":\"" .. tostring(v) .. "\"" |
| 20 | + | json = json .. delim .."\"" .. tostring(k) .. "\":\"" .. tostring(v) .. "\"" |
| 21 | - | delim = "," |
| 21 | + | delim = "," |
| 22 | - | end |
| 22 | + | end |
| 23 | - | json = json.."}" |
| 23 | + | json = json.."}" |
| 24 | - | return json |
| 24 | + | return json |
| 25 | - | end |
| 25 | + | end |
| 26 | - | |
| 26 | + | |
| 27 | - | local function enqueue(array, n) |
| 27 | + | local function enqueue(array, n) |
| 28 | - | n = n or 1 |
| 28 | + | n = n or 1 |
| 29 | - | for i = n, #array do |
| 29 | + | for i = n, #array do |
| 30 | - | table.insert(msgBuffer, array[i]) |
| 30 | + | table.insert(msgBuffer, array[i]) |
| 31 | - | end |
| 31 | + | end |
| 32 | - | end |
| 32 | + | end |
| 33 | - | |
| 33 | + | |
| 34 | - | function BR.isBridgeConnected() return connectedToBridge end |
| 34 | + | function BR.isBridgeConnected() return connectedToBridge end |
| 35 | - | function BR.isPairConnected() return connectedToDevice end |
| 35 | + | function BR.isPairConnected() return connectedToDevice end |
| 36 | - | |
| 36 | + | |
| 37 | - | function BR.sendNotification(notif) |
| 37 | + | function BR.sendNotification(notif) |
| 38 | - | BR.send(deviceCommandIdent .. "31 " .. tableToJSON(notif)) |
| 38 | + | BR.send(deviceCommandIdent .. "31 " .. tableToJSON(notif)) |
| 39 | - | end |
| 39 | + | end |
| 40 | - | |
| 40 | + | |
| 41 | - | function BR.sendToast(text, duration) |
| 41 | + | function BR.sendToast(text, duration) |
| 42 | - | duration = duration or 0 |
| 42 | + | duration = duration or 0 |
| 43 | - | BR.send(deviceCommandIdent .. "30 " .. tostring(duration) .. " " .. text) |
| 43 | + | BR.send(deviceCommandIdent .. "30 " .. tostring(duration) .. " " .. text) |
| 44 | - | end |
| 44 | + | end |
| 45 | - | |
| 45 | + | |
| 46 | - | function BR.send(str) |
| 46 | + | function BR.send(str) |
| 47 | - | if not str or str == "" then return false end |
| 47 | + | if not str or str == "" then return false end |
| 48 | - | local data = str.."\0" |
| 48 | + | local data = str.."\0" |
| 49 | - | local buff |
| 49 | + | local buff |
| 50 | - | local i = 0 |
| 50 | + | local i = 0 |
| 51 | - | while i < 6 do |
| 51 | + | while i < 6 do |
| 52 | - | buff = socket.write(data) |
| 52 | + | buff = socket.write(data) |
| 53 | - | if not buff then |
| 53 | + | if not buff then |
| 54 | - | connectedToBridge = false |
| 54 | + | connectedToBridge = false |
| 55 | - | return false |
| 55 | + | return false |
| 56 | - | elseif buff > 0 then |
| 56 | + | elseif buff > 0 then |
| 57 | - | return true |
| 57 | + | return true |
| 58 | - | end |
| 58 | + | end |
| 59 | - | -- RETRY IF BUFF IS 0 |
| 59 | + | -- RETRY IF BUFF IS 0 |
| 60 | - | i = i + 1 |
| 60 | + | i = i + 1 |
| 61 | - | end |
| 61 | + | end |
| 62 | - | |
| 62 | + | |
| 63 | - | return true |
| 63 | + | return true |
| 64 | - | end |
| 64 | + | end |
| 65 | - | |
| 65 | + | |
| 66 | - | local function execCommand(cmd) |
| 66 | + | local function execCommand(cmd) |
| 67 | - | local cmdNum = string.sub(cmd, 3, 4) |
| 67 | + | local cmdNum = string.sub(cmd, 3, 4) |
| 68 | - | |
| 68 | + | |
| 69 | - | if cmdNum == "00" then |
| 69 | + | if cmdNum == "00" then |
| 70 | - | --ping |
| 70 | + | --ping |
| 71 | - | BR.send(string.sub(cmd, 1, 2) .. "01") --REPLY TO SENDER |
| 71 | + | BR.send(string.sub(cmd, 1, 2) .. "01") --REPLY TO SENDER |
| 72 | - | elseif cmdNum == "01" then |
| 72 | + | elseif cmdNum == "01" then |
| 73 | - | --pong |
| 73 | + | --pong |
| 74 | - | elseif cmdNum == "02" then |
| 74 | + | elseif cmdNum == "02" then |
| 75 | - | connectedToBridge = false |
| 75 | + | connectedToBridge = false |
| 76 | - | --bridge stopped |
| 76 | + | --bridge stopped |
| 77 | - | elseif cmdNum == "10" then |
| 77 | + | elseif cmdNum == "10" then |
| 78 | - | connectedToDevice = false |
| 78 | + | connectedToDevice = false |
| 79 | - | --no pair |
| 79 | + | --no pair |
| 80 | - | elseif cmdNum == "20" then |
| 80 | + | elseif cmdNum == "20" then |
| 81 | - | connectedToDevice = true |
| 81 | + | connectedToDevice = true |
| 82 | - | --pair found |
| 82 | + | --pair found |
| 83 | - | elseif cmdNum == "21" then |
| 83 | + | elseif cmdNum == "21" then |
| 84 | - | connectedToDevice = false |
| 84 | + | connectedToDevice = false |
| 85 | - | end |
| 85 | + | end |
| 86 | - | end |
| 86 | + | end |
| 87 | - | |
| 87 | + | |
| 88 | - | local function isCommand(str) |
| 88 | + | local function isCommand(str) |
| 89 | - | if string.sub(str, 1, 1) == "+" then |
| 89 | + | if string.sub(str, 1, 1) == "+" then |
| 90 | - | return true |
| 90 | + | return true |
| 91 | - | end |
| 91 | + | end |
| 92 | - | return false |
| 92 | + | return false |
| 93 | - | end |
| 93 | + | end |
| 94 | - | |
| 94 | + | |
| 95 | - | function BR.receive() |
| 95 | + | function BR.receive() |
| 96 | - | local buff |
| 96 | + | local buff |
| 97 | - | if connectedToBridge then |
| 97 | + | if connectedToBridge then |
| 98 | - | buff = socket.read() |
| 98 | + | buff = socket.read() |
| 99 | - | if buff == nil then --CONNECTION LOST |
| 99 | + | if buff == nil then --CONNECTION LOST |
| 100 | - | connectedToBridge = false |
| 100 | + | connectedToBridge = false |
| 101 | - | connectedToDevice = false |
| 101 | + | connectedToDevice = false |
| 102 | - | return nil, false |
| 102 | + | return nil, false |
| 103 | - | elseif #msgBuffer > 0 then --IF BUFFER HAS DATA |
| 103 | + | elseif #msgBuffer > 0 then --IF BUFFER HAS DATA |
| 104 | - | if buff ~= "" then --IF MESSAGE IS NOT EMPTY |
| 104 | + | if buff ~= "" then --IF MESSAGE IS NOT EMPTY |
| 105 | - | enqueue(BR.split(buff, "\0")) |
| 105 | + | enqueue(BR.split(buff, "\0")) |
| 106 | - | end |
| 106 | + | end |
| 107 | - | |
| 107 | + | |
| 108 | - | buff = table.remove(msgBuffer, 1) --GET FIRST MESSAGE FROM BUFFER |
| 108 | + | buff = table.remove(msgBuffer, 1) --GET FIRST MESSAGE FROM BUFFER |
| 109 | - | elseif buff == "" then |
| 109 | + | elseif buff == "" then |
| 110 | - | return buff, connectedToDevice |
| 110 | + | return buff, connectedToDevice |
| 111 | - | end |
| 111 | + | end |
| 112 | - | |
| 112 | + | |
| 113 | - | local m = BR.split(buff, "\0") |
| 113 | + | local m = BR.split(buff, "\0") |
| 114 | - | if #m > 1 then |
| 114 | + | if #m > 1 then |
| 115 | - | enqueue(m, 2) |
| 115 | + | enqueue(m, 2) |
| 116 | - | buff = m[1] |
| 116 | + | buff = m[1] |
| 117 | - | end |
| 117 | + | end |
| 118 | - | |
| 118 | + | |
| 119 | - | while true do |
| 119 | + | while true do |
| 120 | - | if isCommand(buff) then |
| 120 | + | if isCommand(buff) then |
| 121 | - | execCommand(buff) |
| 121 | + | execCommand(buff) |
| 122 | - | --return "", connectedToDevice |
| 122 | + | --return "", connectedToDevice |
| 123 | - | if(#msgBuffer > 0) then |
| 123 | + | if(#msgBuffer > 0) then |
| 124 | - | buff = table.remove(msgBuffer, 1) |
| 124 | + | buff = table.remove(msgBuffer, 1) |
| 125 | - | else |
| 125 | + | else |
| 126 | - | return "", connectedToDevice |
| 126 | + | return "", connectedToDevice |
| 127 | - | end |
| 127 | + | end |
| 128 | - | else |
| 128 | + | else |
| 129 | - | buff = BR.split(buff, "\0")[1] --CUT \0 |
| 129 | + | buff = BR.split(buff, "\0")[1] --CUT \0 |
| 130 | - | return string.sub(buff, 2, #buff), connectedToDevice |
| 130 | + | return string.sub(buff, 2, #buff), connectedToDevice |
| 131 | - | end |
| 131 | + | end |
| 132 | - | end |
| 132 | + | end |
| 133 | - | end |
| 133 | + | end |
| 134 | - | return nil, false |
| 134 | + | return nil, false |
| 135 | - | end |
| 135 | + | end |
| 136 | - | |
| 136 | + | |
| 137 | - | function BR.init(key) |
| 137 | + | function BR.init(key) |
| 138 | - | if not key or key == "" then return false end |
| 138 | + | if not key or key == "" then return false end |
| 139 | - | KEY = key |
| 139 | + | KEY = key |
| 140 | - | internet = require("component").internet |
| 140 | + | internet = require("component").internet
|
| 141 | - | socket = internet.connect(HOST) |
| 141 | + | socket = internet.connect(HOST) |
| 142 | - | if not socket then return false end |
| 142 | + | if not socket then return false end |
| 143 | - | local i = 0 |
| 143 | + | local i = 0 |
| 144 | - | while not socket.finishConnect() do |
| 144 | + | while not socket.finishConnect() do |
| 145 | - | if i >= 5 then |
| 145 | + | if i >= 5 then |
| 146 | - | return false |
| 146 | + | return false |
| 147 | - | end |
| 147 | + | end |
| 148 | - | i = i + 1 |
| 148 | + | i = i + 1 |
| 149 | - | os.sleep(0.1) |
| 149 | + | os.sleep(0.1) |
| 150 | - | end |
| 150 | + | end |
| 151 | - | |
| 151 | + | |
| 152 | - | BR.send("KOC-"..key) |
| 152 | + | BR.send("KOC-"..key)
|
| 153 | - | connectedToDevice = false |
| 153 | + | connectedToDevice = false |
| 154 | - | connectedToBridge = true |
| 154 | + | connectedToBridge = true |
| 155 | - | return true |
| 155 | + | return true |
| 156 | - | end |
| 156 | + | end |
| 157 | - | |
| 157 | + | |
| 158 | return BR |