it
Accedi

← Torna al riferimento dell'API

Script The script itself

Everything a script can write without an object in front: the layout's collections (trains, blocks, stations …), the layout as a whole (layout), the model clock (clock), and a few helpers: messages, addresses, flags, routes and time.

A cosa serve: You use it all the time: every script starts from one of these names. Inside a class of your own, prefix them with Trax., since Groovy does not know them there.

say "The scripts are running"
def lamp = output(40)
every(60) { log "another minute" }

Proprietà

trains → Things<Train>

Every train on the layout by its name.

The collection of every train made up on the layout, as Things<Train>. Look up by name (not case-sensitive), use find when the name may be missing, iterate with each or values(). Read-only.

trains.each { name, train ->
   log "${name}: in South? ${train.isIn(stations.South)}"
}

blocks → Things<Block>

Every block by its name.

Every block of the diagram, by name. A block without a name has no key in the collection.

def occupied = blocks.values().findAll { it.occupied }
log "occupied: ${occupied*.name}"

places → Things<Place>

Every station, yard, staging yard, siding and depot.

Every place of every kind: stations, shunting yards, staging yards, sidings and depots. stations, yards and staging are subsets of it; kind says which kind a place is.

places.each { name, place ->
   log "${name} is a ${place.kind} with ${place.blocks.size()} tracks"
}

stations → Things<Place>

The stations.

Only the places of kind station. The same objects as in places: stations.South and places.South are one object.

stations.values().each { Place station ->
   station.onArrival { Train train -> log "${train.name} at ${station.name}" }
}

yards → Things<Place>

The shunting yards.

Only the shunting yards (kind yard).

log "shunting yards: ${yards.keySet()}"

staging → Things<Place>

The staging yards.

Only the staging yards (kind staging).

def free = staging.values().collectMany { it.freeTracks }
log "${free.size()} tracks free in staging"

turnouts → Things<Turnout>

Every turnout with a name.

Every turnout with a name. A turnout without a name in the diagram cannot be reached from a script: give it a name in the diagram editor.

turnouts.values().each { Turnout t ->
   log "${t.name}: ${t.isStraight() ? "straight" : "diverging"}"
}

duties → Things<Duty>

Every duty.

Every duty of the layout, to hand to startAgent.

trains.IC.startAgent(duties.Shuttle)

signals → Things<Signal>

Every signal placed on the layout.

Every signal placed on the layout, by name.

signals.each { name, Signal signal ->
   signal.onRed { log "${name} red" }
}

devices → Things<Device>

Every tablet, phone or other computer registered with TRAX Control.

Every registered device (tablets, phones, other computers), by the name it registered under. The computer TRAX Control runs on is not in it.

def here = devices.values().findAll { it.isHere() }
log "here: ${here*.name}"

layout → Layout

The layout itself: start, shutdown, messages, power.

The object of the layout as a whole: events for starting, stopping, routes, command station, track power and devices, and the actions for the whole layout. See the object Layout.

layout.onStart {
   log "the scripts are running"
}

clock → ModelClock

The model clock: its time, and every model day at a time.

The layout's model clock. See the object ModelClock.

log "model time: ${clock.time}, ${clock.rate}x, ${clock.running ? "running" : "stopped"}"

Metodi

at

Every day at this time of this computer's clock.

A timer on this computer's clock: the closure runs every day at that time. Time as "HH:mm". For the model clock use clock.at.

at("23:30") {
   say "Half past eleven: time to wrap up"
}

input → Input

A feedback address: a detector's or a push button's.

Makes an Input for a feedback address: the number as your command station or feedback module reports it. For block detectors use the block instead; input is for buttons and switches that are no block.

input(12).onOn {
   log "button 12 pressed"
}

output → Output

An accessory address: a light, a signal, an uncoupler.

Makes an Output for an accessory address. green() drives the green (straight) output, red() the red one. Nothing is reported back: the script does not know whether it arrived.

def lamp = output(40)
lamp.green()

flag → Flag

A flag, which rules can ask too.

A flag by name: a boolean that scripts and rules share. A new name is a new flag, which is off. Flags live only in TRAX Control's memory.

flag("maintenance").on()
if (flag("maintenance").isOn()) {
   log "maintenance mode"
}

route → Route

The first way from one block to another.

A Route from one block to another. Nothing is set yet: set() does that. The route finder takes the first way that keeps its direction and needs no turnout by hand.

route(blocks.B1, blocks.B4).set()

every

Again and again, every so many seconds.

A repeating timer: the closure runs every so many seconds (at least 1), on a thread of its own. Use it only when there is no event for what you want to know; every read of a live property is a request to TRAX Control.

every(300) {
   log "${blocks.values().count { it.occupied }} blocks occupied"
}

between → boolean

Whether this computer's clock is between two times.

Whether this computer's clock is between two times. Runs over midnight when the first time is later than the second: between("22:00", "06:00") is true at 23:00 and at 05:00.

if (between("22:00", "06:00")) {
   log "night"
}

say

A message on every screen.

A message (INFO) on every screen, which stays a moment and goes into the list of alerts. Needs no right to work the layout.

say "Shunting in North is done"

announce

An announcement: shown and spoken on every screen.

An announcement: shown on every screen and spoken with the device's voice. Needs no right to work the layout.

announce "The train to South leaves from track 3"

log

A line in this script's log.

A line in this script's log, under the editor, in the Scripts panel and in TRAX Control's log book. println writes here too. The argument becomes text with toString().

log "start: ${trains.size()} trains, ${blocks.size()} blocks"

power

The track power on or off.

Switches track power on (true) or off (false), as the button in the top bar does. Goes through the scripts' rights.

layout.onStationConnected {
   power(true)
}

stopAll

The emergency stop: every train, at once.

The emergency stop: every train stops at once, as the red button does. Meant for a real emergency, such as an emergency button on the layout.

input(99).onOn {
   stopAll()
}