def getValidPosition(self): """Returns an arbitrary valid position on the scenery. Returns None, if no valid position has been found.""" for ti in self._trackItems.values(): if isinstance(ti, lineitem.LineItem): return position.Position(ti, ti.previousItem, 0.0) return None
def updateData(self, msg): self.nextPlaceIndex = msg["nextPlaceIndex"] self.serviceCode = msg["serviceCode"] self._speed = msg["speed"] self._status = msg["status"] self._trainType = self.simulation.trainTypes[msg["trainTypeCode"]] self._trainHead = position.Position(parameters=msg["trainHead"]) self._trainHead.initialize(self.simulation) self.trainStatusChanged.emit(self.trainId)
def _setTrainHeadStr(self, value): """Setter function for the trainHeadStr property.""" if self.simulation.context == utils.Context.EDITOR_TRAINS: values = eval(value.strip('()')) if len(values) != 3: return tiId, ptiId, posOnTI = eval(value.strip('()')) trackItem = self.simulation.trackItem(str(tiId)) previousTI = self.simulation.trackItem(str(ptiId)) self.trainHead = position.Position(trackItem, previousTI, posOnTI)
def json_hook(dct): """Hook method for json.load().""" if not dct.get('__type__'): return dct elif dct['__type__'] == "Simulation": return Simulation(dct['options'], dct['trackItems'], dct['routes'], dct['trainTypes'], dct['services'], dct['trains'], dct['messageLogger']) elif dct['__type__'] == "SignalItem": return signalitem.SignalItem(parameters=dct) elif dct['__type__'] == "EndItem": return enditem.EndItem(parameters=dct) elif dct['__type__'] == "InvisibleLinkItem": return invisiblelinkitem.InvisibleLinkItem(parameters=dct) elif dct['__type__'] == "LineItem": return lineitem.LineItem(parameters=dct) elif dct['__type__'] == "Place": return placeitem.Place(parameters=dct) elif dct['__type__'] == "PlatformItem": return platformitem.PlatformItem(parameters=dct) elif dct['__type__'] == "PointsItem": return pointsitem.PointsItem(parameters=dct) elif dct['__type__'] == "TextItem": return textitem.TextItem(parameters=dct) elif dct['__type__'] == "Route": return route.Route(parameters=dct) elif dct['__type__'] == "Position": return position.Position(parameters=dct) elif dct['__type__'] == "TrainType": return trains.TrainType(parameters=dct) elif dct['__type__'] == "Service": return trains.Service(parameters=dct) elif dct['__type__'] == "ServiceLine": return trains.ServiceLine(parameters=dct) elif dct['__type__'] == "Train": return trains.Train(parameters=dct) elif dct['__type__'] == "MessageLogger": return logger.MessageLogger(parameters=dct) elif dct['__type__'] == "Message": return logger.Message(dct) else: raise utils.FormatException( translate("json_hook", "Unknown __type__ '%s' in JSON file") % dct['__type__'])
def prepareRoute(self, signalId): """Prepares the route starting with the SignalItem given by _selectedSignal and ending at signalId. Sets _selectedSignal to signalId if it is not set. Preparation means: - Check that the route leads to signalId, using the current directions of each PointsItem. - Set _preparedRoute to this route - Highlights the route if valid""" if self.context == utils.Context.EDITOR_ROUTES: si = self.trackItem(signalId) if self._selectedSignal is None or self._selectedSignal == si: # First signal selected self._selectedSignal = si else: pos = position.Position(self._selectedSignal, self._selectedSignal.previousItem, 0) directions = {} cur = pos.next() while not isinstance(cur.trackItem, enditem.EndItem): ti = cur.trackItem if isinstance(ti, pointsitem.PointsItem): directions[ti.tiId] = int(ti.pointsReversed) if ti == si: if ti.isOnPosition(cur): self._preparedRoute = route.Route({ "routeNum": self._nextRouteId, "beginSignal": self._selectedSignal.tiId, "endSignal": signalId, "directions": directions, "initialState": 0 }) self._preparedRoute.initialize(self) self.selectedRoute = self._preparedRoute self._nextRouteId += 1 self._selectedSignal = None si.unselect() return cur = cur.next()
def setupTrainsFromServices(self): """Removes all trains instances and creates a train for each relevant service, that is each service which is not following another one (i.e a service which is not the nextService of another service).""" self._trains = [] serviceList = list(self.services.keys()) for s in self.services.values(): if s.nextServiceCode is not None and \ s.nextServiceCode != "": try: serviceList.remove(s.nextServiceCode) except ValueError: QtCore.qDebug("nextServiceCode: %s does not exist" % s.nextServiceCode) for sc in serviceList: train = self.addNewTrain() train.serviceCode = sc service = self.service(sc) placeCode, trackCode = service.getEntryPlaceData() entryLineItem = self.getLineItem(placeCode, trackCode) if entryLineItem is not None: if isinstance(entryLineItem.nextItem, enditem.EndItem): previousTI = entryLineItem.nextItem else: previousTI = entryLineItem.previousItem pos = position.Position(entryLineItem, previousTI, max(0, entryLineItem.realLength - 1)) initialSpeed = entryLineItem.maxSpeed else: pos = self.getValidPosition() initialSpeed = 0 train.trainHead = pos train.initialSpeed = initialSpeed train.trainTypeCode = service.plannedTrainType if service.lines[0].scheduledArrivalTimeStr != "": train.appearTimeStr = service.lines[0].scheduledArrivalTimeStr else: train.appearTimeStr = "00:00:00"
def loadTrains(self, trns): for dct in trns: th = position.Position(parameters=dct["trainHead"]) dct["trainHead"] = th self._trains.append(trains.Train(dct))