1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-25 19:38:07 +11:00

Compare commits

..

3 Commits

8 changed files with 276 additions and 0 deletions

View File

@ -138,4 +138,41 @@
authors = "Izaya", authors = "Izaya",
repo = "tree/master/" repo = "tree/master/"
}, },
["realtime"] = {
files = {
["master/realtime/OpenOS/usr/lib/realtime.lua"] = "/lib",
["master/realtime/OpenOS/usr/man/wolbeacon"] = "/man",
},
name = "realtime",
description = "Real-time library for OpenOS",
authors = "Izaya",
repo = "tree/master/"
},
["realtime-sync"] = {
files = {
["master/realtime/OpenOS/etc/rc.d/realtime-sync.lua"] = "//etc/rc.d",
["master/realtime/OpenOS/usr/man/realtime-sync"] = "/man",
},
dependencies = {
["realtime"] = ""
},
name = "realtime-sync",
description = "Internet-based real-time synchronisation daemon for OpenOS",
authors = "Izaya",
repo = "tree/master/"
},
["realtime-relay"] = {
files = {
["master/realtime/OpenOS/etc/rc.d/realtime-relay.lua"] = "//etc/rc.d",
["master/realtime/OpenOS/usr/man/realtime-relay"] = "/man",
},
dependencies = {
["realtime"] = "",
["minitel"] = ""
},
name = "realtime-relay",
description = "Minitel-based real-time synchronisation daemon for OpenOS",
authors = "Izaya",
repo = "tree/master/"
},
} }

View File

@ -0,0 +1,68 @@
local serial = require "serialization"
local computer = require "computer"
local realtime = require "realtime"
local minitel = require "minitel"
local event = require "event"
local timer, listener = false, false
local cfg = {}
local startUptime = computer.uptime()
cfg.host = ""
cfg.port = 37
cfg.sync = false
cfg.frequency = 300
cfg.offset = 0
local function saveConfig()
local f = io.open("/etc/realtime-relay.cfg","wb")
if not f then
return false
end
f:write(serial.serialize(cfg))
f:close()
end
local function loadConfig()
local f = io.open("/etc/realtime-relay.cfg","rb")
if not f then
saveConfig()
return false
end
for k,v in pairs(serial.unserialize(f:read("*a"))) do
cfg[k] = v
end
f:close()
end
local function updateTime()
startUptime = computer.uptime()
minitel.rsend(cfg.host,cfg.port,"rqtime",true)
end
local function recvMsg(_,from,port,data)
if port == cfg.port and from == cfg.host then
epoch = tonumber(data)
if not epoch then return false end
local ut = (computer.uptime() - startUptime)/2
realtime.update(epoch, ut, cfg.offset*60*60)
elseif port == cfg.port and not tonumber(data) then
minitel.rsend(from,port,realtime.time(true),true)
end
end
function start()
loadConfig()
if not timer and cfg.sync then
timer = event.timer(cfg.frequency, updateTime, math.huge)
end
if not listener then
listener = event.listen("net_msg",recvMsg)
end
updateTime()
end
function stop()
event.cancel(timer)
timer = nil
event.ignore("net_msg",recvMsg)
listener = false
end

View File

@ -0,0 +1,70 @@
local serial = require "serialization"
local component = require "component"
local computer = require "computer"
local realtime = require "realtime"
local event = require "event"
local timer = nil
local cfg = {}
cfg.url = "https://google.com"
cfg.frequency = 300
cfg.offset = 0
local function saveConfig()
local f = io.open("/etc/realtime-sync.cfg","wb")
if not f then
return false
end
f:write(serial.serialize(cfg))
f:close()
end
local function loadConfig()
local f = io.open("/etc/realtime-sync.cfg","rb")
if not f then
saveConfig()
return false
end
for k,v in pairs(serial.unserialize(f:read("*a"))) do
cfg[k] = v
end
f:close()
end
local function convertToEpoch(s)
p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
day,month,year,hour,min,sec=s:match(p)
months={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
month=months[month]
return os.time({day=day,month=month,year=year,hour=hour,min=min,sec=sec})
end
local function updateTime()
local ut,rq = computer.uptime(), component.internet.request(cfg.url)
while true do
if rq.finishConnect() then
break
end
end
local _, _, headers = rq.response()
local date = headers.Date
if type(date) == "table" then
date=date[1]
end
realtime.update(convertToEpoch(date), ut, cfg.offset*60*60)
rq.close()
end
function sync()
updateTime()
print(os.date("%Y-%m-%d %H:%M",realtime.time()))
end
function start()
loadConfig()
if not timer then
timer = event.timer(cfg.frequency, updateTime, math.huge)
end
sync()
end
function stop()
event.cancel(timer)
timer = nil
end

View File

@ -0,0 +1,22 @@
local computer = require "computer"
local realtime = {}
realtime.epoch, realtime.uptime, realtime.offset = 0, computer.uptime(), 0
function realtime.update(epoch,uptime,offset)
if type(epoch) ~= "number" or type(uptime) ~= "number" then
return false
end
realtime.epoch, realtime.uptime, realtime.offset = epoch, uptime, offset or realtime.offset
return true
end
function realtime.time(utc)
local ofs = realtime.offset
if utc then
ofs = 0
end
local ut = computer.uptime()
return (realtime.epoch+(ut-realtime.uptime))+ofs
end
return realtime

View File

@ -0,0 +1,13 @@
# realtime
realtime is a library for OpenOS providing a reasonably accurate current real-world time, given the right input (see realtime-sync and realtime-relay).
## API
### realtime.time(*utc*)
Returns the current time, either respecting the current offset, or if *utc* is set, with no offset.
### realtime.update(*epoch*, *uptime*, *offset*)
Updates the currently known real time, with the time as *epoch*, measured at *uptime*. Optionally, an offset can be provided.

View File

@ -0,0 +1,26 @@
# realtime-relay
realtime relay daemon for OpenOS
realtime-relay is both a Minitel realtime server and client; allowing both synchronisation with another machine, and other machines to synchronise with the local machine's clock.
## Usage
realtime-relay is implemented as an rc service, and as such, to start it one can use the usual rc commands:
rc realtime-relay start
rc realtime-relay stop
rc realtime-relay enable
rc realtime-relay disable
## Configuration
realtime-relay includes a configuration file at */etc/realtime-relay.cfg*; as a Lua table. It contains the following fields:
- **host**: The host to synchronise the local real-time clock with.
- **port**: The port to communicate with *host* on.
- **sync**: Whether to synchronise clocks with *host*.
- **frequency**: The time between automatic time synchronisations.
- **offset**: The offset time, in hours.
By default, realtime-relay will only act as a server, as using it as a client requires both a *host* being set, and *sync* being enabled.

View File

@ -0,0 +1,28 @@
# realtime-sync
realtime synchronisation daemon for OpenOS
realtime-sync uses the Date: header in HTTP responses, using the Internet card, to ascertain the current real time in UTC, and updates the realtime library with this information.
## Usage
realtime-sync is implemented as an rc service, and as such, to start it one can use the usual rc commands:
rc realtime-sync start
rc realtime-sync stop
rc realtime-sync enable
rc realtime-sync disable
Additionally, realtime-sync includes
rc realtime-sync sync
to force a re-synchronisation.
## Configuration
realtime-sync includes a configuration file at */etc/realtime-sync.cfg*; as a Lua table. It contains the following fields:
- **url**: The URL to request to get the time. Pick one that returns UTC dates in the headers, or leave the default.
- **offset**: The timezone offset, in hours. For example, for AEDT, use 11.
- **frequency**: The time between automatic time synchronisations.

12
realtime/README.md Normal file
View File

@ -0,0 +1,12 @@
# realtime for OpenComputers
Given the lack of a good way to get the real-world time from an OpenComputers machine, the *realtime* library and protocol were created.
## OpenOS
The realtime package for OpenOS includes:
- The [realtime library](OpenOS/usr/lib/realtime.lua) itself, for OpenOS
- [realtime-sync](OpenOS/etc/rc.d/realtime-sync.lua), for synchronising with the real world
- [realtime-relay](OpenOS/etc/rc.d/realtime-relay.lua), for synchronising with another OpenComputers machine
These can be installed as oppm packages, and documentation can be found [here](OpenOS/usr/man)