Skip to content

Commit

Permalink
car status
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimeGoose committed Jun 12, 2022
1 parent 6e98400 commit 44aee1d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 26 deletions.
57 changes: 31 additions & 26 deletions src/F1_22_UDP.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {createSocket,Socket,RemoteInfo} from 'node:dgram';
import {PacketCarStatusDataParser} from './parsers/CarStatus/parsers';
import {createSocket, RemoteInfo, Socket} from 'node:dgram';
import {EventEmitter} from 'node:stream';
import {PacketMotionDataParser} from './parsers/Motion/parsers';
import {PacketSessionDataParser} from './parsers/Session/parsers';
import {packetSize} from './constants';
import {PacketLapDataParser} from './parsers/LapData/parsers';
import {PacketEventDataParser} from './parsers/Event/parsers';
import {PacketParticipantsParser} from './parsers/Participants/parsers';
import {PacketCarSetupDataParser} from './parsers/CarSetup/parsers';
import {PacketCarTelemetryDataParser} from './parsers/CarTelemetry/parsers';
import {PacketEventDataParser} from './parsers/Event/parsers';
import {PacketLapDataParser} from './parsers/LapData/parsers';
import {PacketMotionDataParser} from './parsers/Motion/parsers';
import {PacketParticipantsParser} from './parsers/Participants/parsers';
import {PacketSessionDataParser} from './parsers/Session/parsers';

export class F122UDP extends EventEmitter {
private socket: Socket;
Expand All @@ -19,68 +20,72 @@ export class F122UDP extends EventEmitter {
// create socket
start() {
// if socket is not created, create it
if(!this.socket) {
if (!this.socket) {
this.socket = createSocket('udp4');
}
this.socket.bind({port: 20777,address: '192.168.88.200'});
this.socket.bind({port: 20777, address: '192.168.88.200'});
console.log('start');
this.socket.on('listening',(): void => {
console.log('F122UDP listening on: ',this.socket.address().address,':',this.socket.address().port);
this.socket.on('message',(msg: Buffer,rinfo: RemoteInfo): void => {
switch(rinfo.size) {
this.socket.on('listening', (): void => {
console.log('F122UDP listening on: ', this.socket.address().address, ':', this.socket.address().port);
this.socket.on('message', (msg: Buffer, rinfo: RemoteInfo): void => {
switch (rinfo.size) {
case packetSize.Motion: {
const {data} = new PacketMotionDataParser(msg);
this.emit('motion',data);
this.emit('motion', data);
// console.log(motionDataPacket);
break;
}
case packetSize.Session:
{
console.log('Session');
const {data} = new PacketSessionDataParser(msg);
this.emit('session',data);
this.emit('session', data);
// console.log(data);
}

break;
case packetSize.LapData: {
const {data} = new PacketLapDataParser(msg);
this.emit('lap',data);
this.emit('lap', data);
// console.log(data);
break;
}
case packetSize.Event: {
const {data} = new PacketEventDataParser(msg);
this.emit('event',data);
this.emit('event', data);

break;
}
case packetSize.Participants: {
console.log('Participants');
const {data} = new PacketParticipantsParser(msg);
this.emit('participants',data);
this.emit('participants', data);
// console.log(data);

break;
}
case packetSize.CarSetups: {
// console.log("CarSetups");
const {data} = new PacketCarSetupDataParser(msg,false)
this.emit('carSetups',data);
const {data} = new PacketCarSetupDataParser(msg, false);
this.emit('carSetups', data);

break;
}
case packetSize.CarTelemetry:{
case packetSize.CarTelemetry: {
// console.log("CarTelemetry");
const {data} = new PacketCarTelemetryDataParser(msg,false)
this.emit('carTelemetry',data);
console.log(data);
const {data} = new PacketCarTelemetryDataParser(msg, false);
this.emit('carTelemetry', data);
// console.log(data);

break;
}
case packetSize.CarStatus:
case packetSize.CarStatus: {
// console.log("CarStatus");
const {data} = new PacketCarStatusDataParser(msg, true);
this.emit('carStatus', data);
// console.log(data);
break;
}
case packetSize.LobbyInfo:
// console.log("LobbyInfo");
break;
Expand All @@ -107,7 +112,7 @@ export class F122UDP extends EventEmitter {
}

// process exit on ctrl+c
process.on('SIGINT',() => {
process.on('SIGINT', () => {
console.log('SIGINT');
// process.exit(1);
});
41 changes: 41 additions & 0 deletions src/parsers/CarStatus/CarStatus.spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Car Status Packet

This packet details car statuses for all the cars in the race.

Frequency: Rate as specified in menus
Size: 1058 bytes
Version: 1

struct CarStatusData
{
uint8 m_tractionControl; // Traction control - 0 = off, 1 = medium, 2 = full
uint8 m_antiLockBrakes; // 0 (off) - 1 (on)
uint8 m_fuelMix; // Fuel mix - 0 = lean, 1 = standard, 2 = rich, 3 = max
uint8 m_frontBrakeBias; // Front brake bias (percentage)
uint8 m_pitLimiterStatus; // Pit limiter status - 0 = off, 1 = on
float m_fuelInTank; // Current fuel mass
float m_fuelCapacity; // Fuel capacity
float m_fuelRemainingLaps; // Fuel remaining in terms of laps (value on MFD)
uint16 m_maxRPM; // Cars max RPM, point of rev limiter
uint16 m_idleRPM; // Cars idle RPM
uint8 m_maxGears; // Maximum number of gears
uint8 m_drsAllowed; // 0 = not allowed, 1 = allowed
uint16 m_drsActivationDistance; // 0 = DRS not available, non-zero - DRS will be available
uint8 m_actualTyreCompound; // F1 Modern - 16 = C5, 17 = C4, 18 = C3, 19 = C2, 20 = C1
uint8 m_visualTyreCompound; // F1 visual (can be different from actual compound)
uint8 m_tyresAgeLaps; // Age in laps of the current set of tyres
int8 m_vehicleFiaFlags; // -1 = invalid/unknown, 0 = none, 1 = green
float m_ersStoreEnergy; // ERS energy store in Joules
uint8 m_ersDeployMode; // ERS deployment mode, 0 = none, 1 = medium
float m_ersHarvestedThisLapMGUK; // ERS energy harvested this lap by MGU-K
float m_ersHarvestedThisLapMGUH; // ERS energy harvested this lap by MGU-H
float m_ersDeployedThisLap; // ERS energy deployed this lap
uint8 m_networkPaused; // Whether the car is paused in a network game
};

struct PacketCarStatusData
{
PacketHeader m_header; // Header

CarStatusData m_carStatusData[22];
};
53 changes: 53 additions & 0 deletions src/parsers/CarStatus/parsers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {F1Parser} from '../../f1.parser';

export class CarStatusDataParser extends F1Parser {
constructor() {
super();
this
/*uint8*/ .uint8('m_tractionControl')
/*uint8*/ .uint8('m_antiLockBrakes')
/*uint8*/ .uint8('m_fuelMix')
/*uint8*/ .uint8('m_frontBrakeBias')
/*uint8*/ .uint8('m_pitLimiterStatus')
/*float*/ .floatle('m_fuelInTank')
/*float*/ .floatle('m_fuelCapacity')
/*float*/ .floatle('m_fuelRemainingLaps')
/*uint16*/ .uint16le('m_maxRPM')
/*uint16*/ .uint16le('m_idleRPM')
/*uint8*/ .uint8('m_maxGears')
/*uint8*/ .uint8('m_drsAllowed')
/*uint16*/ .uint16le('m_drsActivationDistance')
/*uint8*/ .uint8('m_actualTyreCompound')
/*uint8*/ .uint8('m_visualTyreCompound')
/*uint8*/ .uint8('m_tyresAgeLaps')
/*int8*/ .int8('m_vehicleFiaFlags')
/*float*/ .floatle('m_ersStoreEnergy')
/*uint8*/ .uint8('m_ersDeployMode')
/*float*/ .floatle('m_ersHarvestedThisLapMGUK')
/*float*/ .floatle('m_ersHarvestedThisLapMGUH')
/*float*/ .floatle('m_ersDeployedThisLap')
/*uint8*/ .uint8('m_networkPaused');
}
}

import {PacketHeaderParser} from '../../PacketHeader/parser';
import {PacketCarStatusData} from '../types';

export class PacketCarStatusDataParser extends F1Parser {
data: PacketCarStatusData;

constructor(buffer: Buffer, bigintEnabled: boolean) {
super();

this.endianess('little')
.nest('m_header', {
type: new PacketHeaderParser(bigintEnabled),
})
.array('m_carStatusData', {
length: 22,
type: new CarStatusDataParser(),
});

this.data = this.fromBuffer(buffer) as PacketCarStatusData;
}
}
32 changes: 32 additions & 0 deletions src/parsers/CarStatus/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {PacketHeader} from '@Type/PacketHeader';

export interface CarStatusData {
/*uint8*/ m_tractionControl: number;
/*uint8*/ m_antiLockBrakes: number;
/*uint8*/ m_fuelMix: number;
/*uint8*/ m_frontBrakeBias: number;
/*uint8*/ m_pitLimiterStatus: number;
/*float*/ m_fuelInTank: number;
/*float*/ m_fuelCapacity: number;
/*float*/ m_fuelRemainingLaps: number;
/*uint16*/ m_maxRPM: number;
/*uint16*/ m_idleRPM: number;
/*uint8*/ m_maxGears: number;
/*uint8*/ m_drsAllowed: number;
/*uint16*/ m_drsActivationDistance: number;
/*uint8*/ m_actualTyreCompound: number;
/*uint8*/ m_visualTyreCompound: number;
/*uint8*/ m_tyresAgeLaps: number;
/*int8*/ m_vehicleFiaFlags: number;
/*float*/ m_ersStoreEnergy: number;
/*uint8*/ m_ersDeployMode: number;
/*float*/ m_ersHarvestedThisLapMGUK: number;
/*float*/ m_ersHarvestedThisLapMGUH: number;
/*float*/ m_ersDeployedThisLap: number;
/*uint8*/ m_networkPaused: number;
}

export interface PacketCarStatusData {
m_header: PacketHeader;
m_carStatusData: CarStatusData[];
}

0 comments on commit 44aee1d

Please sign in to comment.