private int SplitIn(Switch sw, int?s, int?t1, int?t2) { sw.sourceLight = true; if (!sw.sourceActive) { sw.t1Light = false; sw.t2Light = false; return((int)sw.currentState); } else if (!sw.t1Active) { sw.t1Light = false; sw.t2Light = true; if (sw.currentState != sw.targetBlockId2) { sw.changeSwitchState(); } return((int)sw.currentState); } else if (!sw.t2Active) { sw.t1Light = true; sw.t2Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } return((int)sw.currentState); } if (s > 0) { return(-1); } //need to decide which gets precedence, will go with t1 by default if (t1 > 0 && t2 > 0) { sw.t1Light = true; sw.t2Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } } else if (t1 > 0) { sw.t1Light = true; sw.t2Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } } else if (t2 > 0) { sw.t1Light = false; sw.t2Light = true; if (sw.currentState != sw.targetBlockId2) { sw.changeSwitchState(); } } else { sw.t1Light = true; sw.t2Light = true; } return((int)sw.currentState); }
private int BiLoop(Switch sw, int?s, int?t1, int?t2) { if (!sw.sourceActive) { sw.sourceLight = false; sw.t1Light = false; sw.t2Light = false; return((int)sw.currentState); } else if (!sw.t1Active && !sw.t2Active) { sw.sourceLight = false; sw.t1Light = false; sw.t2Light = false; return((int)sw.currentState); } else if (!sw.t1Active) { sw.sourceLight = true; sw.t1Light = false; sw.t2Light = true; if (sw.currentState != sw.targetBlockId2) { sw.changeSwitchState(); } return((int)sw.currentState); } else if (!sw.t2Active) { sw.sourceLight = true; sw.t1Light = true; sw.t2Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } return((int)sw.currentState); } //if train approaches switch if (s > 0) { //if train approaches from target 1, and another train is leaving switch if (t1 > 0 && t2 < 0) { sw.sourceLight = true; sw.t1Light = false; sw.t2Light = false; if (sw.currentState != sw.targetBlockId2) { sw.changeSwitchState(); } } else if (t1 < 0 && t2 > 0) { sw.sourceLight = true; sw.t2Light = false; sw.t1Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } } else if (t1 > 0) { sw.sourceLight = true; sw.t1Light = false; sw.t2Light = false; if (sw.currentState != sw.targetBlockId2) { sw.changeSwitchState(); } } else if (t2 > 0) { sw.sourceLight = true; sw.t2Light = false; sw.t1Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } } //else, the switch is in safe state, it is up to track engineer to route train else { } } else if (t1 > 0) { sw.sourceLight = false; sw.t1Light = true; sw.t2Light = false; if (sw.currentState != sw.targetBlockId1) { sw.changeSwitchState(); } } else if (t2 > 0) { sw.sourceLight = false; sw.t1Light = false; sw.t2Light = true; if (sw.currentState != sw.targetBlockId2) { sw.changeSwitchState(); } } return((int)sw.currentState); }
//id is switch id //which branch determines whether source, t1, or t2 is to be gotten public int?getSwitchDirection(Switch sw, int whichBranch) { //Switch sw = switches.Find(x => x.switchId == id); SwitchTypes type = switchTypes[sw]; if (whichBranch > 2 || whichBranch < 0) { return(null); } switch (type) { case SwitchTypes.Loop1: if (whichBranch == 0) { return(0); } else if (whichBranch == 1) { return(-1); } else if (whichBranch == 2) { return(1); } break; case SwitchTypes.Loop2: if (whichBranch == 0) { return(0); } else if (whichBranch == 1) { return(1); } else if (whichBranch == 2) { return(-1); } break; case SwitchTypes.BiLoop: if (whichBranch == 0) { return(0); } else if (whichBranch == 1) { return(0); } else if (whichBranch == 2) { return(0); } break; case SwitchTypes.SplitIn: if (whichBranch == 0) { return(-1); } else if (whichBranch == 1) { return(1); } else if (whichBranch == 2) { return(1); } break; case SwitchTypes.SplitOut: if (whichBranch == 0) { return(1); } else if (whichBranch == 1) { return(-1); } else if (whichBranch == 2) { return(-1); } break; default: return(null); } return(null); }
public Boolean handleLine(String plcLine) { if (switches.Count == 0) { return(false); } String[] split = plcLine.Split(','); if (split.Length != 4) { return(false); } int switchId = Convert.ToInt32(split[0]); Switch s = switches.Find(x => x.switchId == switchId); if (switchTypes.ContainsKey(s)) { switchTypes.Remove(s); } if (split[1] == "bi") { //BiLoop if (split[2] == "bi" && split[3] == "bi") { switchTypes.Add(s, SwitchTypes.BiLoop); } //loop1 else if (split[2] == "in" && split[3] == "out") { switchTypes.Add(s, SwitchTypes.Loop2); } //loop2 else if (split[2] == "out" && split[3] == "in") { switchTypes.Add(s, SwitchTypes.Loop1); } else { return(false); } } else if (split[1] == "in") { //split in if (split[2] == "out" && split[3] == "out") { switchTypes.Add(s, SwitchTypes.SplitOut); } else { return(false); } } else if (split[1] == "out") { //split in if (split[2] == "in" && split[3] == "in") { switchTypes.Add(s, SwitchTypes.SplitIn); } else { return(false); } } else { return(false); } return(true); }
public void addSwitch(Switch s) { switches.Add(s); }
//given a train, switch, and a branch, determines if //the train is heading towards the switch public int?trainHeadingTowardsSwitch(Train t, Switch s, int whichBranch) { if (s.switchId == 3) { } if (t.direction == 0 || t.direction == null) { //Console.WriteLine("Error. Train has no direction"); } switch (whichBranch) { case 0: //if train is contained in source branch if (checkWithinRange(t.currBlock, (int)s.sourceBlockId, (int)s.sourceBlockId_end)) { int srcDir = (int)TrackControllerWindow.plc.getSwitchDirection(s, 0); if (srcDir > 0 || srcDir < 0) { return(srcDir); } else if (srcDir == 0) { if (t.direction > 0 && s.sourceBlockId > s.sourceBlockId_end) { return(1); } else if (t.direction < 0 && s.sourceBlockId < s.sourceBlockId_end) { return(1); } else { return(-1); } } } break; case 1: //if train is contained in source branch if (checkWithinRange(t.currBlock, (int)s.targetBlockId1, (int)s.targetBlockId1_end)) { int t1Dir = (int)TrackControllerWindow.plc.getSwitchDirection(s, 1); if (t1Dir > 0 || t1Dir < 0) { return(t1Dir); } else if (t1Dir == 0) { if (t.direction > 0 && s.targetBlockId1 > s.targetBlockId1_end) { return(1); } else if (t.direction < 0 && s.targetBlockId1 < s.targetBlockId1_end) { return(1); } else { return(-1); } } } break; case 2: if (checkWithinRange(t.currBlock, (int)s.targetBlockId2, (int)s.targetBlockId2_end)) { int t2Dir = (int)TrackControllerWindow.plc.getSwitchDirection(s, 2); if (t2Dir > 0 || t2Dir < 0) { return(t2Dir); } else if (t2Dir == 0) { if (t.direction > 0 && s.targetBlockId2 > s.targetBlockId2_end) { return(1); } else if (t.direction < 0 && s.targetBlockId2 < s.targetBlockId2_end) { return(1); } else { return(-1); } } } break; default: break; } return(null); }