|
| 1 | +Sides = require("sides") |
| 2 | +Colors = require("colors") |
| 3 | +local conf = {} |
| 4 | +local light = {} |
| 5 | +local door = {} |
| 6 | + |
| 7 | +local pathtoconf = "/home/Serverconf.lua" |
| 8 | + |
| 9 | +dofile(pathtoconf) |
| 10 | + |
| 11 | +-- Setup of the lights and doors |
| 12 | + |
| 13 | +function conf.setlights() |
| 14 | + for _, data in pairs(Conf.Lights) do |
| 15 | + light.setTable(data[1], data[2], data[3], data[4]) |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +function conf.setdoors() |
| 20 | + for _, data in pairs(Conf.Doors) do |
| 21 | + door.setTable(data[1], data[2], data[3], data[4], data[5]) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | + |
| 26 | +local term = require("term") |
| 27 | +local component = require("component") |
| 28 | +local rs = component.redstone |
| 29 | +local gpu = component.gpu |
| 30 | +local modem = component.modem |
| 31 | +local event = require("event") |
| 32 | +local filesystem = require("filesystem") |
| 33 | +local unicode = require("unicode") |
| 34 | +local sign = {} |
| 35 | +local write = {} |
| 36 | +local lights = {} |
| 37 | +local doors = {} |
| 38 | + |
| 39 | +gpu.setResolution(Conf.Resolution[1], Conf.Resolution[2]) |
| 40 | +term.clear() |
| 41 | + |
| 42 | +local alarm = {} |
| 43 | + |
| 44 | + |
| 45 | +function sign.running(minx, miny, maxx, maxy, message) |
| 46 | + gpu.set(minx, miny, unicode.char(0x2552)) |
| 47 | + gpu.set(minx + 1, miny, string.rep(unicode.char(0x2550), maxx - minx - 1)) |
| 48 | + gpu.set(maxx, miny, unicode.char(0x2555)) |
| 49 | + local frameheight = maxy - miny - 1 |
| 50 | + gpu.fill(minx, miny + 1, 1, frameheight, unicode.char(0x2502)) |
| 51 | + gpu.fill(maxx, miny + 1, 1, frameheight, unicode.char(0x2502)) |
| 52 | + gpu.set(minx, maxy, unicode.char(0x2514)) |
| 53 | + gpu.set(minx + 1, maxy, string.rep(unicode.char(0x2500), maxx - minx - 1)) |
| 54 | + gpu.set(maxx, maxy, unicode.char(0x2518)) |
| 55 | + local y = (miny + maxy) / 2 |
| 56 | + local x = math.ceil((maxx - minx - #message) / 2) + minx |
| 57 | + gpu.set(x, y, message) |
| 58 | +end |
| 59 | + |
| 60 | +sign.running(1, 1, 160, 5, Conf.Serverrunningmessage) |
| 61 | +term.setCursor(1, 6) |
| 62 | + |
| 63 | +function write.log(message) |
| 64 | + Log = io.open(Conf.Logfile, "a") |
| 65 | + if filesystem.exists(Conf.Logfile) == true then |
| 66 | + Log:write("\n"..message) |
| 67 | + else |
| 68 | + Log:write(message) |
| 69 | + end |
| 70 | + print(message) |
| 71 | + Log:close() |
| 72 | +end |
| 73 | + |
| 74 | +function light.setTable(room, logname, side, color) |
| 75 | + lights[room] = {} |
| 76 | + lights[room]["room"] = room |
| 77 | + lights[room]["logname"] = logname |
| 78 | + lights[room]["side"] = side |
| 79 | + lights[room]["color"] = color |
| 80 | +end |
| 81 | + |
| 82 | +function light.clearTable() |
| 83 | + lights = {} |
| 84 | +end |
| 85 | + |
| 86 | +function light.check(room) |
| 87 | + for _, data in pairs(lights) do |
| 88 | + if room == data["room"] then |
| 89 | + Currstate = rs.getBundledOutput(data["side"], data["color"]) |
| 90 | + end |
| 91 | + end |
| 92 | + if Currstate == 255 then State = "on" end |
| 93 | + if Currstate == 0 then State = "off" end |
| 94 | + modem.send(Sender, Conf.Port, State) |
| 95 | + write.log(Sender .. " " .. Conf.Logmessages.Checklightmessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Checklightmessage[2]) |
| 96 | +end |
| 97 | + |
| 98 | +function light.action(room, action) |
| 99 | + if action == "turn on" then Strength = 255 end |
| 100 | + if action == "turn off" then Strength = 0 end |
| 101 | + for _, data in pairs(lights) do |
| 102 | + if room == data["room"] then |
| 103 | + rs.setBundledOutput(data["side"], data["color"], Strength) |
| 104 | + Currstate = rs.getBundledOutput(data["side"], data["color"]) |
| 105 | + end |
| 106 | + end |
| 107 | + if action == "turn on" and Currstate == 255 then Check = "turned on" write.log(Sender .. " " .. Conf.Logmessages.Turnonlightmessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Turnonlightmessage[2]) |
| 108 | + else if action == "turn off" and Currstate == 0 then Check = "turned off" write.log(Sender .. " " .. Conf.Logmessages.Turnofflightmessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Turnofflightmessage[2]) |
| 109 | + else Check = "failed" end end |
| 110 | + modem.send(Sender, Conf.Port, Check) |
| 111 | +end |
| 112 | + |
| 113 | +function light.turnalloff() |
| 114 | + for _, data in pairs(lights) do |
| 115 | + rs.setBundledOutput(data["side"], data["color"], 0) |
| 116 | + end |
| 117 | + write.log(Sender .. " " .. Conf.Logmessages.Turnalllightsoffmessage[1]) |
| 118 | +end |
| 119 | + |
| 120 | +function door.setTable(name, logname, side, color, pass) |
| 121 | + doors[name] = {} |
| 122 | + doors[name]["name"] = name |
| 123 | + doors[name]["logname"] = logname |
| 124 | + doors[name]["side"] = side |
| 125 | + doors[name]["color"] = color |
| 126 | + doors[name]["pass"] = pass |
| 127 | +end |
| 128 | + |
| 129 | +function door.check(door) |
| 130 | + if door == "garage door" then Open = rs.getBundledOutput(Conf.Garage.Garageopenside, Conf.Garage.Garageopencolor) Close = rs.getBundledOutput(Conf.Garage.Garagecloseside, Conf.Garage.Garageclosecolor) |
| 131 | + if Open == 255 and Close == 0 then State = "opened" end |
| 132 | + if Open == 0 and Close == 255 then State = "closed" end |
| 133 | + modem.send(Sender, Conf.Port, State) |
| 134 | + write.log(Sender .. " " .. Conf.Logmessages.Checkgaragemessage[1]) |
| 135 | + else |
| 136 | + for _, data in pairs(doors) do |
| 137 | + if door == data["name"] then |
| 138 | + Side = data["side"] |
| 139 | + Color = data["color"] |
| 140 | + end |
| 141 | + end |
| 142 | + local currstate = rs.getBundledOutput(Side, Color) |
| 143 | + if currstate == 0 then State = "closed" end |
| 144 | + if currstate == 255 then State = "opened" end |
| 145 | + modem.send(Sender, Conf.Port, State) |
| 146 | + write.log(Sender .. " " .. Conf.Logmessages.Checkdoormessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Checkdoormessage[2]) |
| 147 | + end |
| 148 | +end |
| 149 | + |
| 150 | +function door.lockhouse(pass) |
| 151 | + if pass == Conf.Passcodes.Lockhousepass then |
| 152 | + rs.setBundledOutput(Conf.Garage.Garageopenside, Conf.Garage.Garageopencolor, 0) rs.setBundledOutput(Conf.Garage.Garagecloseside, Conf.Garage.Garageclosecolor, 255) |
| 153 | + for _, data in pairs(doors) do |
| 154 | + rs.setBundledOutput(data["side"], data["color"], 0) |
| 155 | + end |
| 156 | + alarm.action("enable alarm") |
| 157 | + modem.send(Sender, Conf.Port, "correct") |
| 158 | + write.log(Sender .. " " .. Conf.Logmessages.Lockhousemessage[1]) |
| 159 | + else modem.send(Sender, Conf.Port, "wrong") |
| 160 | + end |
| 161 | +end |
| 162 | + |
| 163 | +function door.action(door, action, pass) |
| 164 | + if door == "garage door" then |
| 165 | + if pass == Conf.Passcodes.Garagepass then |
| 166 | + if action == "open" then rs.setBundledOutput(Conf.Garage.Garagecloseside, Conf.Garage.Garageclosecolor, 0) rs.setBundledOutput(Conf.Garage.Garageopenside, Conf.Garage.Garageopencolor, 255) modem.send(Sender, Conf.Port, "correct", "was opened") alarm.action("disable alarm", Conf.Passcodes.Alarmpass) write.log(Sender .. " " .. Conf.Logmessages.Opengaragemessage[1]) end |
| 167 | + if action == "close" then rs.setBundledOutput(Conf.Garage.Garageopenside, Conf.Garage.Garageopencolor, 0) rs.setBundledOutput(Conf.Garage.Garagecloseside, Conf.Garage.Garageclosecolor, 255) modem.send(Sender, Conf.Port, "correct", "was closed") write.log(Sender .. " " .. Conf.Logmessages.Closegaragemessage[1]) end |
| 168 | + else |
| 169 | + modem.send(Sender, Conf.Port, "wrong") |
| 170 | + write.log(Sender .. " " .. Conf.Logmessages.Actiongaragewrongcodemessage[1]) |
| 171 | + end |
| 172 | + else |
| 173 | + for _, data in pairs(doors) do |
| 174 | + if door == data["name"] then |
| 175 | + Side = data["side"] |
| 176 | + Color = data["color"] |
| 177 | + Doorpass = data["pass"] |
| 178 | + end |
| 179 | + end |
| 180 | + if action == "no" then |
| 181 | + else |
| 182 | + if pass == Doorpass then |
| 183 | + if action == "close" then Strength = 0 end |
| 184 | + if action == "open" then Strength = 255 end |
| 185 | + rs.setBundledOutput(Side, Color, Strength) |
| 186 | + local currstate = rs.getBundledOutput(Side, Color) |
| 187 | + if action == "close" and currstate == 0 then Check = "was closed" write.log(Sender .. " " .. Conf.Logmessages.Closedoormessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Closedoormessage[2]) |
| 188 | + elseif action == "open" and currstate == 255 then alarm.action("disable alarm", Conf.Passcodes.Alarmpass) Check = "was opened" write.log(Sender .. " " .. Conf.Logmessages.Opendoormessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Opendoormessage[2]) |
| 189 | + else Check = "failed" end |
| 190 | + modem.send(Sender, Conf.Port, "correct", Check) |
| 191 | + else modem.send(Sender, Conf.Port, "wrong") |
| 192 | + write.log(Sender .. " " .. Conf.Logmessages.Actiondoorwrongcodemessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Actiondoorwrongcodemessage[2]) |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | +end |
| 197 | + |
| 198 | +function door.code(door, pass) |
| 199 | + for _, data in pairs(doors) do |
| 200 | + if door == data["name"] then |
| 201 | + Doorpass = data["pass"] |
| 202 | + Side = data["side"] |
| 203 | + Color = data["color"] |
| 204 | + end |
| 205 | + end |
| 206 | + if pass == Doorpass then |
| 207 | + local currstate = rs.getBundledOutput(Side, Color) |
| 208 | + if currstate == 0 then rs.setBundledOutput(Side, Color, 255) write.log(Sender .. " " .. Conf.Logmessages.Opendoormessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Opendoormessage[2]) alarm.action("disable alarm", Conf.Passcodes.Alarmpass) |
| 209 | + elseif currstate == 255 then rs.setBundledOutput(Side, Color, 0) write.log(Sender .. " " .. Conf.Logmessages.Closedoormessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Closedoormessage[2]) end |
| 210 | + modem.send(Sender, Conf.Port, "correct") |
| 211 | + else |
| 212 | + modem.send(Sender, Conf.Port, "wrong") |
| 213 | + write.log(Sender .. " " .. Conf.Logmessages.Actiondoorwrongcodemessage[1] .. " " .. Logname .. " " .. Conf.Logmessages.Actiondoorwrongcodemessage[2]) |
| 214 | + end |
| 215 | +end |
| 216 | + |
| 217 | +function alarm.check() |
| 218 | + local currstate = rs.getBundledInput(Conf.Alarm.Alarmside, Conf.Alarm.Alarmcolor) |
| 219 | + if currstate >= 0 then State = "alarm triggered" end |
| 220 | + if currstate == 0 then State = "alarm not triggered" end |
| 221 | + modem.send(Sender, Conf.Port, State) |
| 222 | + write.log(Sender .. " " .. Conf.Logmessages.Checkalarmmessage[1]) |
| 223 | +end |
| 224 | + |
| 225 | +function alarm.action(action, pass, modemuse) |
| 226 | + if action == "enable alarm" then |
| 227 | + rs.setBundledOutput(Conf.Alarm.Alarmresetside, Conf.Alarm.Alarmresetcolor, 0) rs.setBundledOutput(Conf.Alarm.Alarmenableside, Conf.Alarm.Alarmenablecolor, 255) |
| 228 | + else |
| 229 | + if pass == Conf.Passcodes.Alarmpass then |
| 230 | + modem.send(Sender, Conf.Port, "correct") |
| 231 | + if action == "disable alarm" then |
| 232 | + rs.setBundledOutput(Conf.Alarm.Alarmenableside, Conf.Alarm.Alarmenablecolor, 0) rs.setBundledOutput(Conf.Alarm.Alarmresetside, Conf.Alarm.Alarmresetcolor, 255) |
| 233 | + if rs.getBundledInput(Conf.Alarm.Alarmside, Conf.Alarm.Alarmcolor) == 0 and rs.getBundledOutput(Conf.Alarm.Alarmresetside, Conf.Alarm.Alarmresetcolor) == 255 and rs.getBundledOutput(Conf.Alarm.Alarmenableside, Conf.Alarm.Alarmenablecolor) == 0 then |
| 234 | + if modemuse then modem.send(Sender, Conf.Port, "correct", "alarm disabled") write.log(Sender .. " " .. Conf.Logmessages.Disablealarmmessage[1]) end |
| 235 | + end |
| 236 | + end |
| 237 | + if action == "reset alarm" then |
| 238 | + rs.setBundledOutput(Conf.Alarm.Alarmresetside, Conf.Alarm.Alarmresetcolor, 255) os.sleep(1) rs.setBundledOutput(Conf.Alarm.Alarmresetside, Conf.Alarm.Alarmresetcolor, 0) |
| 239 | + if rs.getBundledInput(Conf.Alarm.Alarmside, Conf.Alarm.Alarmcolor) == 0 then modem.send(Sender, Conf.Port, "correct", "alarm reset") write.log(Sender .. " " .. Conf.Logmessages.Resetalarmmessage[1]) end |
| 240 | + end |
| 241 | + else |
| 242 | + modem.send(Sender, Conf.Port, "wrong") |
| 243 | + write.log(Sender .. " " .. Conf.Logmessages.Actionalarmwrongcodemessage[1]) |
| 244 | + end |
| 245 | + end |
| 246 | +end |
| 247 | + |
| 248 | +while true do |
| 249 | + |
| 250 | + local program = nil |
| 251 | + Object = nil |
| 252 | + local action = nil |
| 253 | + local pass = nil |
| 254 | + |
| 255 | + |
| 256 | + conf.setlights() |
| 257 | + conf.setdoors() |
| 258 | + |
| 259 | + modem.open(Conf.Port) |
| 260 | + _, _, Sender, _, _, program, Object, action, pass = event.pull("modem_message") |
| 261 | + |
| 262 | + for _, data in pairs(lights) do |
| 263 | + if Object == data["room"] then |
| 264 | + Logname = data["logname"] |
| 265 | + end |
| 266 | + end |
| 267 | + |
| 268 | + for _, data in pairs(doors) do |
| 269 | + if Object == data["name"] then |
| 270 | + Logname = data["logname"] |
| 271 | + end |
| 272 | + end |
| 273 | + |
| 274 | + if Logname == nil then Logname = "" end |
| 275 | + |
| 276 | + if program == "check server" then modem.send(Sender, Conf.Port, "server ok") write.log(Sender .. " " .. Conf.Logmessages.Checkedservermessage[1]) end |
| 277 | + |
| 278 | + if program == "light" then |
| 279 | + if action == nil and Object ~= "turn all off" then light.check(Object) |
| 280 | + elseif Object == "turn all off" then light.turnalloff() |
| 281 | + elseif action ~= nil and Object ~= "turn all off" then light.action(Object, action) |
| 282 | + end |
| 283 | + end |
| 284 | + |
| 285 | + if program == "door" then |
| 286 | + if action == nil and Object ~= "lock house" then door.check(Object) |
| 287 | + elseif Object == "lock house" then door.lockhouse(pass) |
| 288 | + else door.action(Object, action, pass) |
| 289 | + end |
| 290 | + end |
| 291 | + |
| 292 | + if program == "alarm" then |
| 293 | + if action == nil then alarm.check() |
| 294 | + else alarm.action(action, pass, true) |
| 295 | + end |
| 296 | + end |
| 297 | + |
| 298 | + if program == "code" then |
| 299 | + door.code(Object, pass) |
| 300 | + end |
| 301 | + |
| 302 | + sign.running(1, 1, 160, 5, Conf.Serverrunningmessage) |
| 303 | + |
| 304 | +end |
0 commit comments