// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;
import "./HighwayStations.sol";
import "./CertGeneration.sol";contract VehicleRegister {
address public owner;HighwayStations public highwayStations;
CertGeneration public certGeneration;struct Registration {
uint256 etcID;
string carNumber;
string carImage;
uint256 carCoding;
uint256 timeStamp;
uint256 stationID;
HighwayStations.Type _type;
}
struct StationTraffic {
uint256 stationID;
string[] vehicles;
}struct ExitRegistration {
uint256 stationInletID;
uint256 stationRoadID;
uint256 etcID;
string carNumber;
string carImage;
uint256 carCoding;
uint256 timeStamp;
uint256 stationExportID;
uint256 expenses;
HighwayStations.Type _type;
uint256 registerCounter;
}
uint256 public etcID;
uint256 public stationInletID;
uint256 public stationRoadID;
uint256 public stationExportID;uint256 public registerCounter;
uint256 public exitNumber;
uint256 public carNumber;
uint256 public carRouteNumber;
uint256 public nowTime;
string public carImage;
uint256 public carCoding;
uint256 public timeStamp;
uint256 public expenses;
uint256 public totalExpenses;
uint256[] public routeRegister;
uint256[][] public routeRegisterArr;
mapping (uint256 => Registration) public entranceRegistrations;
mapping (uint256 => Registration) public routeRegistrations;
mapping (uint256 => ExitRegistration) public exitRegistrations;
mapping (uint256 => uint256[][]) public routeRegisterArrMapper;
mapping (uint256 => StationTraffic) public stationTraffic;
mapping (string => uint256[]) public vehicleRoutes;constructor(address _stationAddress, address _certGeneration) public{
owner = msg.sender;
highwayStations = HighwayStations(_stationAddress);
certGeneration = CertGeneration(_certGeneration);
}modifier onlyOwner() {
require(msg.sender == owner, "您没有权限");
_;
}function registerEntrance(uint256 _etcID, string memory _carNumber, string memory _carImage, uint256 _carCoding, uint256 _stationID) public onlyOwner {
// require(highwayStations.stations(_stationID).stationID != 0, "无效的站点");
entranceRegistrations[registerCounter] = Registration({
etcID: _etcID,
carNumber: _carNumber,
carImage: _carImage,
carCoding: _carCoding,
timeStamp: block.timestamp,
stationID: _stationID,
_type: HighwayStations.Type.inlet
});
registerCounter++;
vehiclePassedStation(_stationID, _carNumber);
vehicleRoutes[_carNumber].push(_stationID);
}function registerRoute(uint256 _etcID, string memory _carNumber, string memory _carImage, uint256 _carCoding, uint256 _stationID) public onlyOwner {
// require(highwayStations.stations(_stationID).stationID != 0, "无效的站点");
routeRegistrations[registerCounter] = Registration({
etcID: _etcID,
carNumber: _carNumber,
carImage: _carImage,
carCoding: _carCoding,
timeStamp: block.timestamp,
stationID: _stationID,
_type: HighwayStations.Type.road
});
registerCounter++;
vehiclePassedStation(_stationID, _carNumber);
vehicleRoutes[_carNumber].push(_stationID);
}function registerExit(uint256 _etcID, string memory _carNumber, string memory _carImage, uint256 _carCoding, uint256 _stationExportID, uint256 _expenses) public onlyOwner {
// require(highwayStations.stations(_stationID).stationID != 0, "无效的站点");
//计费方法nowTime = block.timestamp;
certGeneration.generateCert(_carNumber, nowTime);
registerCounter++;
uint256 s;
for(uint i = 0; i < exitNumber; i++){
s = carNumber + s;
carNumber = routeRegister.length;
}
carRouteNumber = registerCounter - s;
uint256[] storage routeRegisters;
for(uint256 i = 0; i <= carRouteNumber; i++){
routeRegisters[i] = routeRegister[i+s];
}routeRegisterArr.push(routeRegisters);
exitNumber++;stationInletID = s + 1;
stationExportID = routeRegisters[carRouteNumber];
exitRegistrations[registerCounter] = ExitRegistration({
registerCounter: registerCounter,
etcID: _etcID,
carNumber: _carNumber,
carImage: _carImage,
carCoding: _carCoding,
timeStamp: block.timestamp,
stationInletID: stationInletID,
stationRoadID: stationRoadID,
stationExportID: _stationExportID,
expenses: _expenses,
_type: HighwayStations.Type.export
});routeRegisterArrMapper[nowTime] = routeRegisterArr;
vehiclePassedStation(_stationExportID, _carNumber);
vehicleRoutes[_carNumber].push(_stationExportID);
}function vehiclePassedStation(uint256 stationID, string memory vehicleID) internal onlyOwner {
uint256 stationCounters = highwayStations.getStations();
require(stationID < stationCounters, "无效的站点ID");
stationTraffic[stationID].vehicles.push(vehicleID);
}function getNowRegisterCounter() view external returns(uint256){
return registerCounter;
}function getCarAllRoutes(string memory _carNumber) public view returns(uint256[] memory) {
return vehicleRoutes[_carNumber];
}function getStateCars(uint256 stationID) public view returns(string[] memory) {
require(stationID < highwayStations.stationCounter(), "无效的站点ID");
return stationTraffic[stationID].vehicles;
}}
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;contract HighwayStations {
address public owner;uint256 public stationCounter;
enum Type {
inlet,
road,
export
}struct Station {
uint256 stationID;
string name;
Type types;
}mapping(uint256 => Station) public stations;
mapping(string => uint256) public stationNameToID;constructor() public{
owner = msg.sender;
}modifier onlyOwner() {
require(msg.sender == owner, "您没有权限");
_;
}function addStation(string memory _name, Type _type) public onlyOwner {
stations[stationCounter] = Station({
stationID: stationCounter,
name: _name,
types: _type
});
stationNameToID[_name] = stationCounter;
stationCounter++;
}function getStations() public view returns(uint256) {
return stationCounter;
}function getStationName(uint256 stationID) public view returns(string memory) {
require(stationID < stationCounter, "站点ID不存在");
return stations[stationID].name;
}function getStationIDByName(string memory _name) public view returns (uint256) {
return stationNameToID[_name];
}}
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;import "./HighwayStations.sol";
import "./VehicleRegister.sol";contract CertGeneration {
HighwayStations public highwayStations;
VehicleRegister public vehicleRegister;
struct VehicleData {
uint256 stationInletID;
uint256 stationRoadID;
uint256 stationExportID;
string carNumber;
uint256 time;
string carImage;
uint256 carCoding;
uint256 etcID;
uint256 totalExpenses;
}struct Certificate {
VehicleData[] data;
}mapping (string => Certificate) carCertificates;
constructor(address _stationAddress, address _vehicleRegister) public {
highwayStations = HighwayStations(_stationAddress);
vehicleRegister = VehicleRegister(_vehicleRegister);
}function generateCert(string memory _carNumber, uint256 _timeStamp) external {
Certificate storage cert = carCertificates[_carNumber];
// uint256 registerCounters = vehicleRegister.getNowRegisterCounter();
VehicleData memory newVehicleData = VehicleData({
stationInletID: vehicleRegister.stationInletID(),
stationRoadID: vehicleRegister.stationRoadID(),
stationExportID: vehicleRegister.stationExportID(),
carNumber: _carNumber,
time: _timeStamp,
carImage: vehicleRegister.carImage(),
carCoding: vehicleRegister.carCoding(),
etcID: vehicleRegister.etcID(),
totalExpenses: vehicleRegister.totalExpenses()
});cert.data.push(newVehicleData);
}function getVehicleData(string memory _carNumber, uint256 index) public view returns (VehicleData memory) {
Certificate storage cert = carCertificates[_carNumber];
require(index < cert.data.length, "Index out of bounds");
return cert.data[index];
}
}