input{
udp {
    port => 5000
    codec => plain { charset => 'ISO-8859-1' }
}}

filter {
    ruby {
        code => "
		event.set('binary', event.get('message').encode('iso-8859-1'))
		event.set('m_packetFormat', event.get('binary')[0..1].unpack('S')[0])
		event.set('m_gameMajorVersion', event.get('binary')[2..2].unpack('C')[0])
		event.set('m_gameMinorVersion', event.get('binary')[3..3].unpack('C')[0])
		event.set('m_PacketVersion', event.get('binary')[4..4].unpack('C')[0])
		event.set('m_packetID', event.get('binary')[5..5].unpack('C')[0])
#		event.set('m_sessionUID', event.get('binary')[6..13].unpack('Q')[0])
		event.set('m_sessionTime', event.get('binary')[14..17].unpack('e')[0])
		event.set('m_frameIdentifier', event.get('binary')[18..21].unpack('L')[0])
		event.set('m_playerCarIndex', event.get('binary')[22..22].unpack('C')[0])
		event.set('m_secondaryPlayerCarIndex', event.get('binary')[23..23].unpack('C')[0])

	if event.get('m_packetID') == 0 then # Motion
		event.set('m_worldPositionX', event.get('binary')[24..27].unpack('e')[0])
		event.set('m_worldPositionY', event.get('binary')[28..31].unpack('e')[0])
		event.set('m_worldPositionZ', event.get('binary')[32..35].unpack('e')[0])
		event.set('m_worldVelocityX', event.get('binary')[36..39].unpack('e')[0])
		event.set('m_worldVelocityY', event.get('binary')[40..43].unpack('e')[0])
		event.set('m_worldVelocityZ', event.get('binary')[44..47].unpack('e')[0])
		event.set('m_worldForwardDirX', event.get('binary')[48..49].unpack('s')[0])
		event.set('m_worldForwardDirY', event.get('binary')[50..51].unpack('s')[0])
		event.set('m_worldForwardDirZ', event.get('binary')[52..53].unpack('s')[0])
		event.set('m_worldRightDirX', event.get('binary')[54..55].unpack('s')[0])
		event.set('m_worldRightDirY', event.get('binary')[56..57].unpack('s')[0])
		event.set('m_worldRightDirZ', event.get('binary')[58..59].unpack('s')[0])
		event.set('m_gForceLateral', event.get('binary')[60..63].unpack('e')[0])
		event.set('m_gForceLongitudinal', event.get('binary')[64..67].unpack('e')[0])
		event.set('m_gForceVertical', event.get('binary')[68..71].unpack('e')[0])
		event.set('m_yaw', event.get('binary')[72..75].unpack('e')[0])
		event.set('m_pitch', event.get('binary')[76..79].unpack('e')[0])
		event.set('m_roll', event.get('binary')[80..83].unpack('e')[0])

	elsif event.get('m_packetID') == 1 then # Session
		event.set('m_weather', event.get('binary')[24..24].unpack('C')[0])
		event.set('m_trackTemperature', event.get('binary')[25..25].unpack('c')[0])
		event.set('m_airTemperature', event.get('binary')[26..26].unpack('c')[0])
		event.set('m_totalLaps', event.get('binary')[27..27].unpack('C')[0])
		event.set('m_trackLength', event.get('binary')[28..29].unpack('S')[0])
		event.set('m_sessionType', event.get('binary')[30..30].unpack('C')[0])
		event.set('m_trackId', event.get('binary')[31..31].unpack('c')[0])
		event.set('m_formula', event.get('binary')[32..32].unpack('C')[0])
		event.set('m_sessionTimeLeft', event.get('binary')[33..34].unpack('S')[0])
		event.set('m_sessionDuration', event.get('binary')[35..36].unpack('S')[0])
		event.set('m_pitSpeedLimit', event.get('binary')[37..37].unpack('C')[0])
		event.set('m_gamePaused', event.get('binary')[38..38].unpack('C')[0])
		
	elsif event.get('m_packetID') == 2 then # Lap Data
		event.set('m_lastLapTime', event.get('binary')[24..27].unpack('e')[0])
		event.set('m_currentLapTime', event.get('binary')[28..31].unpack('e')[0])
		event.set('m_sector1TimeInMS', event.get('binary')[32..33].unpack('S')[0])
		event.set('m_sector2TimeInMS', event.get('binary')[34..35].unpack('S')[0])
		event.set('m_bestLapTime', event.get('binary')[36..39].unpack('e')[0])
		event.set('m_bestLapNum', event.get('binary')[40..40].unpack('C')[0])
		event.set('m_bestLapSector1TimeInMS', event.get('binary')[41..42].unpack('S')[0])
		event.set('m_bestLapSector2TimeInMS', event.get('binary')[43..44].unpack('S')[0])
		event.set('m_bestLapSector3TimeInMS', event.get('binary')[45..46].unpack('S')[0])
		event.set('m_bestOverallSector1TimeInMS', event.get('binary')[47..48].unpack('S')[0])
		event.set('m_bestOverallSector1LapNum', event.get('binary')[49..49].unpack('C')[0])
		event.set('m_bestOverallSector2TimeInMS', event.get('binary')[50..51].unpack('S')[0])
		event.set('m_bestOverallSector2LapNum', event.get('binary')[52..52].unpack('C')[0])
		event.set('m_bestOverallSector3TimeInMS', event.get('binary')[53..54].unpack('S')[0])
		event.set('m_bestOverallSector3LapNum', event.get('binary')[55..55].unpack('C')[0])
		event.set('m_lapDistance', event.get('binary')[56..59].unpack('e')[0])
		event.set('m_totalDistance', event.get('binary')[60..63].unpack('e')[0])
		event.set('m_safetyCarDelta', event.get('binary')[64..67].unpack('e')[0])
		event.set('m_carPosition', event.get('binary')[68..68].unpack('C')[0])
		event.set('m_currentLapNum', event.get('binary')[69..69].unpack('C')[0])
		event.set('m_pitStatus', event.get('binary')[70..70].unpack('C')[0])
		event.set('m_sector', event.get('binary')[71..71].unpack('C')[0])
		event.set('m_currentLapInvalid', event.get('binary')[72..72].unpack('C')[0])
		event.set('m_penalties', event.get('binary')[73..73].unpack('C')[0])
		event.set('m_gridPosition', event.get('binary')[74..74].unpack('C')[0])
		event.set('m_driverStatus', event.get('binary')[75..75].unpack('C')[0])
		event.set('m_resultStatus', event.get('binary')[76..76].unpack('C')[0])

	elsif event.get('m_packetID') == 3 then # Event
		event.set('vehicleIdx', event.get('binary')[24..24].unpack('C')[0])
		event.set('lapTime', event.get('binary')[25..28].unpack('e')[0])
		event.set('vehicleIdx', event.get('binary')[29..29].unpack('C')[0])
		event.set('vehicleIdx', event.get('binary')[30..30].unpack('C')[0])
		event.set('vehicleIdx', event.get('binary')[31..31].unpack('C')[0])
		event.set('penaltyType', event.get('binary')[32..32].unpack('C')[0])
		event.set('infringementType', event.get('binary')[33..33].unpack('C')[0])
		event.set('vehicleIdx', event.get('binary')[34..34].unpack('C')[0])
		event.set('otherVehicleIdx', event.get('binary')[35..35].unpack('C')[0])
		event.set('time', event.get('binary')[36..36].unpack('C')[0])
		event.set('lapNum', event.get('binary')[37..37].unpack('C')[0])
		event.set('placesGained', event.get('binary')[38..38].unpack('C')[0])
		event.set('vehicleIdx', event.get('binary')[39..39].unpack('C')[0])
		event.set('speed', event.get('binary')[40..43].unpack('e')[0])

	elsif event.get('m_packetID') == 4 then # Participants
		event.set('m_aiControlled', event.get('binary')[24..24].unpack('C')[0])
		event.set('m_driverId', event.get('binary')[25..25].unpack('C')[0])
		event.set('m_teamId', event.get('binary')[26..26].unpack('C')[0])
		event.set('m_raceNumber', event.get('binary')[27..27].unpack('C')[0])
		event.set('m_nationality', event.get('binary')[28..28].unpack('C')[0])
		  
	elsif event.get('m_packetID') == 5 then # Car Setups
		event.set('m_frontWing', event.get('binary')[24..24].unpack('C')[0])
		event.set('m_rearWing', event.get('binary')[25..25].unpack('C')[0])
		event.set('m_onThrottle', event.get('binary')[26..26].unpack('C')[0])
		event.set('m_offThrottle', event.get('binary')[27..27].unpack('C')[0])
		event.set('m_frontCamber', event.get('binary')[28..31].unpack('e')[0])
		event.set('m_rearCamber', event.get('binary')[32..35].unpack('e')[0])
		event.set('m_frontToe', event.get('binary')[36..39].unpack('e')[0])
		event.set('m_rearToe', event.get('binary')[40..43].unpack('e')[0])
		event.set('m_frontSuspension', event.get('binary')[44..44].unpack('C')[0])
		event.set('m_rearSuspension', event.get('binary')[45..45].unpack('C')[0])
		event.set('m_frontAntiRollBar', event.get('binary')[46..46].unpack('C')[0])
		event.set('m_rearAntiRollBar', event.get('binary')[47..47].unpack('C')[0])
		event.set('m_frontSuspensionHeight', event.get('binary')[48..48].unpack('C')[0])
		event.set('m_rearSuspensionHeight', event.get('binary')[49..49].unpack('C')[0])
		event.set('m_brakePressure', event.get('binary')[50..50].unpack('C')[0])
		event.set('m_brakeBias', event.get('binary')[51..51].unpack('C')[0])
		event.set('m_rearLeftTyrePressure', event.get('binary')[52..55].unpack('e')[0])
		event.set('m_rearRightTyrePressure', event.get('binary')[56..59].unpack('e')[0])
		event.set('m_frontLeftTyrePressure', event.get('binary')[60..63].unpack('e')[0])
		event.set('m_frontRightTyrePressure', event.get('binary')[64..67].unpack('e')[0])
		event.set('m_ballast', event.get('binary')[68..68].unpack('C')[0])
		event.set('m_fuelLoad', event.get('binary')[69..72].unpack('e')[0])

	elsif event.get('m_packetID') == 6 then # Car Telemetry
		event.set('m_speed', event.get('binary')[24..25].unpack('S')[0])
		event.set('m_throttle', event.get('binary')[26..29].unpack('e')[0])
		event.set('m_steer', event.get('binary')[30..33].unpack('e')[0])
		event.set('m_brake', event.get('binary')[34..37].unpack('e')[0])
		event.set('m_clutch', event.get('binary')[38..38].unpack('C')[0])
		event.set('m_gear', event.get('binary')[39..39].unpack('c')[0])
		event.set('m_engineRPM', event.get('binary')[40..41].unpack('S')[0])
		event.set('m_drs', event.get('binary')[42..42].unpack('C')[0])
		event.set('m_revLightsPercent', event.get('binary')[43..43].unpack('C')[0])
		event.set('m_brakesTemperatureRL', event.get('binary')[44..45].unpack('S')[0])
		event.set('m_brakesTemperatureRR', event.get('binary')[46..47].unpack('S')[0])
		event.set('m_brakesTemperatureFL', event.get('binary')[48..49].unpack('S')[0])
		event.set('m_brakesTemperatureFR', event.get('binary')[50..51].unpack('S')[0])
		event.set('m_tyresSurfaceTemperatureRL', event.get('binary')[52..52].unpack('C')[0])
		event.set('m_tyresSurfaceTemperatureRR', event.get('binary')[53..53].unpack('C')[0])
		event.set('m_tyresSurfaceTemperatureFL', event.get('binary')[54..54].unpack('C')[0])
		event.set('m_tyresSurfaceTemperatureFR', event.get('binary')[55..55].unpack('C')[0])
		event.set('m_tyresInnerTemperatureRL', event.get('binary')[56..56].unpack('C')[0])
		event.set('m_tyresInnerTemperatureRR', event.get('binary')[57..57].unpack('C')[0])
		event.set('m_tyresInnerTemperatureFL', event.get('binary')[58..58].unpack('C')[0])
		event.set('m_tyresInnerTemperatureFR', event.get('binary')[59..59].unpack('C')[0])
		event.set('m_engineTemperature', event.get('binary')[60..61].unpack('C')[0])
		event.set('m_tyresPressureRL', event.get('binary')[62..65].unpack('e')[0])
		event.set('m_tyresPressureRR', event.get('binary')[66..69].unpack('e')[0])
		event.set('m_tyresPressureFL', event.get('binary')[70..73].unpack('e')[0])
		event.set('m_tyresPressureFR', event.get('binary')[74..77].unpack('e')[0])
		event.set('m_surfaceTypeRL', event.get('binary')[78..78].unpack('C')[0])
		event.set('m_surfaceTypeRR', event.get('binary')[79..79].unpack('C')[0])
		event.set('m_surfaceTypeFL', event.get('binary')[80..80].unpack('C')[0])
		event.set('m_surfaceTypeFR', event.get('binary')[81..81].unpack('C')[0])

	elsif event.get('m_packetID') == 7 then # Car Status
		event.set('m_tractionControl', event.get('binary')[24..24].unpack('C')[0])
		event.set('m_antiLockBrakes', event.get('binary')[25..25].unpack('C')[0])
		event.set('m_fuelMix', event.get('binary')[26..26].unpack('C')[0])
		event.set('m_frontBrakeBias', event.get('binary')[27..27].unpack('C')[0])
		event.set('m_pitLimiterStatus', event.get('binary')[28..28].unpack('C')[0])
		event.set('m_fuelInTank', event.get('binary')[29..32].unpack('e')[0])
		event.set('m_fuelCapacity', event.get('binary')[33..36].unpack('e')[0])
		event.set('m_fuelRemainingLaps', event.get('binary')[37..40].unpack('e')[0])
		event.set('m_maxRPM', event.get('binary')[41..42].unpack('S')[0])
		event.set('m_idleRPM', event.get('binary')[43..44].unpack('S')[0])
		event.set('m_maxGears', event.get('binary')[45..45].unpack('C')[0])
		event.set('m_drsAllowed', event.get('binary')[46..46].unpack('C')[0])
		event.set('m_drsActivationDistance', event.get('binary')[47..48].unpack('S')[0])
		event.set('m_tyresWearRL', event.get('binary')[49..49].unpack('C')[0])
		event.set('m_tyresWearRR', event.get('binary')[50..50].unpack('C')[0])
		event.set('m_tyresWearFL', event.get('binary')[51..51].unpack('C')[0])
		event.set('m_tyresWearFR', event.get('binary')[52..52].unpack('C')[0])
		event.set('m_actualTyreCompound', event.get('binary')[53..53].unpack('C')[0])
		event.set('m_visualTyreCompound', event.get('binary')[54..54].unpack('C')[0])
		event.set('m_tyresAgeLaps', event.get('binary')[55..55].unpack('C')[0])
		event.set('m_tyresDamageRL', event.get('binary')[56..56].unpack('C')[0])
		event.set('m_tyresDamageRR', event.get('binary')[57..57].unpack('C')[0])
		event.set('m_tyresDamageFL', event.get('binary')[58..58].unpack('C')[0])
		event.set('m_tyresDamageFR', event.get('binary')[59..59].unpack('C')[0])
		event.set('m_frontLeftWingDamage', event.get('binary')[60..60].unpack('C')[0])
		event.set('m_frontRightWingDamage', event.get('binary')[61..61].unpack('C')[0])
		event.set('m_rearWingDamage', event.get('binary')[62..62].unpack('C')[0])
		event.set('m_drsFault', event.get('binary')[63..63].unpack('C')[0])
		event.set('m_engineDamage', event.get('binary')[64..64].unpack('C')[0])
		event.set('m_gearBoxDamage', event.get('binary')[65..65].unpack('C')[0])
		event.set('m_vehicleFiaFlags', event.get('binary')[66..66].unpack('c')[0])
		event.set('m_ersStoreEnergy', event.get('binary')[67..70].unpack('e')[0])
		event.set('m_ersDeployMode', event.get('binary')[71..71].unpack('C')[0])
		event.set('m_ersHarvestedThisLapMGUK', event.get('binary')[72..75].unpack('e')[0])
		event.set('m_ersHarvestedThisLapMGUH', event.get('binary')[76..79].unpack('e')[0])
		event.set('m_ersDeployedThisLap', event.get('binary')[80..83].unpack('e')[0])
		  
	elsif event.get('m_packetID') == 8 then # Final Classification
		event.set('m_position', event.get('binary')[24..24].unpack('C')[0])
		event.set('m_numLaps', event.get('binary')[25..25].unpack('C')[0])
		event.set('m_gridPosition', event.get('binary')[26..26].unpack('C')[0])
		event.set('m_points', event.get('binary')[27..27].unpack('C')[0])			 
		event.set('m_numPitStops', event.get('binary')[28..28].unpack('C')[0])
		event.set('m_resultStatus', event.get('binary')[29..29].unpack('C')[0])
		event.set('m_bestLapTime', event.get('binary')[30..33].unpack('e')[0])
		event.set('m_totalRaceTime', event.get('binary')[34..37].unpack('E')[0])
		event.set('m_penaltiesTime', event.get('binary')[38..38].unpack('C')[0])
		event.set('m_numPenalties', event.get('binary')[39..39].unpack('C')[0])
		event.set('m_numTyreStints', event.get('binary')[40..40].unpack('C')[0])
		event.set('m_tyreStintsActual', event.get('binary')[41..41].unpack('C')[0])
		event.set('m_tyreStintsVisual', event.get('binary')[42..42].unpack('C')[0])
		  
	elsif event.get('m_packetID') == 9 then # Lobby Info
		event.set('m_aiControlled', event.get('binary')[24..24].unpack('C')[0])
		event.set('m_teamId', event.get('binary')[25..25].unpack('C')[0])
		event.set('m_nationality', event.get('binary')[26..26].unpack('C')[0])			  
				
	    else
	        event.set('event', 'other m_packetID')
	    end
"
    }
 mutate {
   remove_field => ["message","binary"]
}
}

output {
  elasticsearch{ 
    hosts => ["localhost:9200"]
    action => "index"
    index => "f1_data"
	user => "username"
	password => "password"}   
 }
