LuaScripts/linkedGet.lua

60 lines
1.5 KiB
Lua
Raw Normal View History

2021-01-07 23:30:28 +00:00
local event = require("event")
local keyboard = require("keyboard")
local component = require("component")
2021-01-08 14:43:49 +00:00
local internet = require("internet")
2021-01-07 23:30:28 +00:00
local tunnel = component.tunnel
2021-01-08 01:09:46 +00:00
2021-01-08 01:44:49 +00:00
local success = "console: команда выполнена успешно"
local fail = "console: неизвестная команда - пиши help"
2021-01-08 14:47:57 +00:00
local erius = "b7585fb7-1ea4-4f94-b1a2-05e1cef76a1f"
local DummyWalrus = "0952a2dd-23fa-4dd2-aefe-92e7cd6c77ce"
2021-01-08 01:09:46 +00:00
2021-01-08 14:09:44 +00:00
function getTime()
2021-01-08 14:41:59 +00:00
local handle = internet.request("http://api.timezonedb.com/v2.1/get-time-zone?key=INWE0H77BDN6&by=zone&zone=Europe/Moscow&format=json")
local result = ""
for chunk in handle do
result = result..chunk
end
local f = io.open("time.txt", "w")
f:write(result)
f:close()
f = io.open("time.txt", "r")
f:seek("set", 245)
local time = f:read(19)
f:close()
2021-01-08 14:09:44 +00:00
return time
2021-01-08 00:04:54 +00:00
end
2021-01-08 01:09:46 +00:00
function handleMessage(msg)
2021-01-08 15:07:11 +00:00
if msg == "help" or msg == "exit" or msg == "near" then
2021-01-08 01:09:46 +00:00
return success
2021-01-08 01:24:12 +00:00
elseif msg == "rain" then
2021-01-08 01:09:46 +00:00
return success
else
return fail
end
end
2021-01-08 01:24:12 +00:00
function getUser(from)
2021-01-08 14:47:57 +00:00
if from == erius then
2021-01-08 01:24:12 +00:00
return "erius"
2021-01-08 14:47:57 +00:00
elseif from == DummyWalrus then
2021-01-08 01:24:12 +00:00
return "DummyWalrus"
else
return "???"
end
end
2021-01-07 23:30:28 +00:00
while true do
2021-01-08 01:09:46 +00:00
local _, _, from, _, _, message = event.pull("modem_message")
2021-01-08 14:49:01 +00:00
local result = handleMessage(tostring(message))
2021-01-08 14:09:44 +00:00
local time = getTime()
local user = getUser(tostring(from))
2021-01-08 14:49:01 +00:00
local log = "["..time.."] "..user..": "..message.."\n".."["..time.."] "..result
2021-01-08 00:35:28 +00:00
print(log)
2021-01-08 00:40:24 +00:00
local f = io.open("logs.txt", "a")
2021-01-08 01:24:12 +00:00
f:write(log.."\n")
2021-01-08 00:35:28 +00:00
f:close()
2021-01-07 23:30:28 +00:00
end