-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.lua
172 lines (153 loc) · 5.78 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
local utils = {}
local Config = require("config")
local internet = require("internet")
local json = require("json")
local controllers = require("controllers")
-- Function: utils.calcSwitchTextPos
-- Description: Calculate the position of the switch text
-- Parameters: switchTbl - table containing the switch data
-- Returns: table containing the x and y position of the switch text
utils.calcSwitchTextPos = function(switchTbl)
local result = {}
local x, y = switchTbl[1], switchTbl[2]
if switchTbl[3] == "╗" or switchTbl[4] == "╗" then
y = switchTbl[2] - 1
elseif switchTbl[3] == "╝" or switchTbl[4] == "╝" then
y = switchTbl[2] + 1
elseif switchTbl[3] == "╚" or switchTbl[4] == "╚" then
y = switchTbl[2] + 1
elseif switchTbl[3] == "╔" or switchTbl[4] == "╔" then
y = switchTbl[2] - 1
end
x = switchTbl[5]:len() == 1 and x or x - 1
result["x"] = x
result["y"] = y
return result
end
-- Function: utils.calcSignalTextPos
-- Description: Calculate the position of the signal text
-- Parameters: signalTbl - table containing the signal data
-- Returns: table containing the x and y position of the signal text
utils.calcSignalTextPos = function(signalTbl)
local result = {}
local x, y = signalTbl[1], signalTbl[2]
if signalTbl[4] == "<" or signalTbl[4] == "◀" or signalTbl[4] == "◁" then
x = signalTbl[1] - (string.len(signalTbl[3]) / 2)
if string.len(signalTbl[3]) % 2 == 0 then x = x + 1 end
y = signalTbl[2] - 1
elseif signalTbl[4] == ">" or signalTbl[4] == "▶" or signalTbl[4] == "▷" then
x = signalTbl[1] - (string.len(signalTbl[3]) / 2)
if string.len(signalTbl[3]) % 2 == 0 then x = x + 1 end
y = signalTbl[2] + 1
elseif signalTbl[4] == "^" or signalTbl[4] == "▲" or signalTbl[4] == "△" then
x = signalTbl[1] + 1
y = signalTbl[2]
elseif signalTbl[4] == "V" or signalTbl[4] == "▼" or signalTbl[4] == "▽" then
x = signalTbl[1] - string.len(signalTbl[3])
y = signalTbl[2]
end
result["x"] = x
result["y"] = y
return result
end
-- Function: string.split
-- Description: Splits a string by a separator
-- Parameters: inputstr - the string to split
-- sep - the separator
-- Returns: table containing the split strings
function string.split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
-- Function: utils.debugPrint
-- Description: Logs a message to the log file if debug is enabled
-- Parameters: message - the message to print
utils.debugPrint = function(message)
if not Config.Debug then return end
local file = io.open("log.txt", "a")
if not file then
print("Failed to open log file")
return os.exit()
end
local rawTime = json.decode(internet.request("https://worldtimeapi.org/api/timezone/Europe/Prague")())["datetime"]
local time = string.split(rawTime, "T")[1] .. " " .. string.split(string.split(rawTime, "T")[2], ".")[1]
file:write(time .. " " .. tostring(message) .. "\n")
file:close()
end
-- Define connected controllers
local switchesConnected = controllers.isConnected("Switches")
local signalsConnected = controllers.isConnected("Signals")
local crossingsConnected = controllers.isConnected("Crossings")
-- Function: utils.resetLayout
-- Description: Resets the switches and signals according to the default layout
utils.resetLayout = function()
if switchesConnected then
for _, switch in pairs(Config.Switches) do
if switch[3] == "╗" or switch[3] == "╝" or switch[3] == "╚" or switch[3] == "╔" or switch[3] == "╚" then
controllers.Switches.setAspect(switch[5], 1)
else
controllers.Switches.setAspect(switch[5], 5)
end
end
end
if signalsConnected then controllers.Signals.setEveryState("Stuj") end
end
-- Function: utils.toggleSwitch
-- Description: Toggles the switch
-- Parameters: switchName - the name of the switch
utils.toggleSwitch = function(switchName)
if not switchesConnected then return end
if controllers.Switches.getAspect(switchName) == 1 then
controllers.Switches.setAspect(switchName, 5)
else
controllers.Switches.setAspect(switchName, 1)
end
end
utils.toggleCrossing = function(switchName)
if not crossingsConnected then return end
if controllers.Crossings.getAspect(switchName) == 5 then
controllers.Crossings.setAspect(switchName, 1)
else
controllers.Crossings.setAspect(switchName, 5)
end
end
-- Function: utils.sendStateToExpectSig
-- Description: Sends the state of the signal to the expect signal
-- Parameters: signalName - the name of the signal
-- state - the state of the signal
utils.sendStateToExpectSig = function(signalName, state)
if not signalsConnected then return end
if state == "Stuj" then
state = "Vystraha"
elseif state == "PN" then
state = "Vystraha"
elseif state == "Vystraha" then
state = "Volno"
elseif state == "Volno" then
state = "Volno"
elseif string.sub(state, 1, 3) == "R40" then
state = "Ocek40"
elseif string.sub(state, 1, 3) == "R60" then
state = "Ocek60"
elseif string.sub(state, 1, 3) == "R80" then
state = "Ocek80"
elseif state.sub(state, 1, 4) == "Ocek" then
state = "Volno"
elseif state.sub(state, 1, 4) == "Opak" then
state = state.sub(state, 5)
else
state = "Vystraha"
end
controllers.Signals.setState("Pr" .. signalName, state)
end
-- Simple shallow copy of a table
function table.clone(org)
return {table.unpack(org)}
end
return utils, table.clone, string.split