Module pyinsim
[hide private]
[frames] | no frames]

Module pyinsim

source code

An InSim module for the Python programming language.

Classes [hide private]
  IS_ISI
InSim Init - packet to initialise the InSim system
  IS_VER
VERsion.
  IS_TINY
General purpose packet.
  IS_SMALL
General purpose packet.
  IS_STA
STAte packet, sent whenever the data in the packet changes.
  IS_SCH
Single CHaracter
  IS_SFP
State Flags Pack.
  IS_SCC
Set Car Camera - Simplified camera packet (not SHIFT+U mode)
  IS_CPP
Cam Pos Pack - Full camera packet (in car or SHIFT+U mode)
  IS_ISM
InSim Multi
  IS_MSO
MSg Out - system messages and user messages
  IS_III
InsIm Info - /i message from user to host's InSim
  IS_MST
MSg Type - send to LFS to type message or command
  IS_MTC
Msg To Connection - hosts only - send to a connection or a player
  IS_MOD
MODe : send to LFS to change screen mode
  IS_VTN
VoTe Notify
  IS_RST
Race STart
  IS_NCN
New ConN
  IS_CNL
ConN Leave
  IS_CPR
Conn Player Rename
  IS_NPL
New PLayer joining race (if PLID already exists, then leaving pits)
  IS_PLP
PLayer Pits (go to settings - stays in player list)
  IS_PLL
PLayer Leave race (spectate - removed from player list)
  IS_LAP
LAP time
  IS_SPX
SPlit X time
  IS_PIT
PIT stop (stop at pit garage)
  IS_PSF
Pit Stop Finished
  IS_PLA
Pit LAne
  IS_CCH
Camera CHange
  IS_PEN
PENalty (given or cleared)
  IS_TOC
Take Over Car
  IS_FLG
FLaG (yellow or blue flag changed)
  IS_PFL
Player FLags (help flags changed)
  IS_FIN
FINished race notification (not a final result - use IS_RES)
  IS_RES
RESult (qualify or confirmed finish)
  IS_REO
REOrder (when race restarts after qualifying)
  NodeLap
Car info in 6 bytes - there is an array of these in the NLP (below)
  IS_NLP
Node and Lap Packet - variable size
  CompCar
Car info in 28 bytes - there is an array of these in the MCI (below)
  IS_MCI
Multi Car Info - if more than 8 in race then more than one of these is sent
  IS_MSX
MSg eXtended - like MST but longer (not for commands)
  IS_MSL
MSg Local - message to appear on local computer only
  IS_CRS
Car ReSet
  IS_BFN
Button FunctioN - delete buttons / receive button requests
  IS_AXI
AutoX Info
  IS_AXO
AutoX Object
  IS_BTN
BuTtoN - button header - followed by 0 to 240 characters
  IS_BTC
BuTton Click - sent back when user clicks a button
  IS_BTT
BuTton Type - sent back when user types into a text entry button
  IS_RIP
Replay Information Packet
  IS_SSH
ScreenSHot
  OutSimPack
External Motion simulator support
  OutGaugePack
External dashboard support.
  _Buffer
Class to manage the receive buffer.
  InSim
Class to manage an InSim connection with LFS.
  OutReceiver
Class to handle incoming UDP packets from LFS, OutSim, OutGauge and IS_MCI/IS_NLP packets.
  OutSim
Motion Simulator Support.
  OutGauge
External dashboard support.
Functions [hide private]
boolean
checkIP(ipStr)
Check if a string contains a valid IP address.
source code
boolean
checkPort(portStr)
Check if a string contains a valid port number.
source code
boolean
checkAdmin(adminStr)
Check if a string contains a valid admin password.
source code
tuple
parseCommand(mso, prefix)
Parse a command string from an MSO message (EG '!admin DarkTimes') and return it as a tuple.
source code
unicode string
strToUnicode(str_, default='L', cols=True)
Convert a LFS encoded string to unicode.
source code
string
stripColours(lfsStr)
Strip colour codes (^1, ^3 etc..) from a string.
source code
number
speedToMps(speed)
Convert speed to meters per second.
source code
number
speedToKph(speed)
Convert speed to kilometers per hour.
source code
number
mpsToKph(mps)
Convert meters per second to kilometers per hour.
source code
number
speedToMph(speed)
Convert speed to miles per hour.
source code
number
mpsToMph(mps)
Convert meters per second to miles per hour.
source code
number
metersToMiles(meters)
Convert meters to miles.
source code
number
metersToKilometers(meters)
Convert meters to kilometers.
source code
number
directionToDegrees(direction)
Convert direction to degrees.
source code
number
lengthToMetres(length)
Convert length to meters.
source code
number
distance(a=(0, 0, 0), b=(0, 0, 0))
Calculate the distance between two points.
source code
number
radiansToDegrees(radians)
Convert radians to degrees.
source code
number
radiansToRpm(radians)
Convert radians to RPM.
source code
InSim
insimConnect(host='localhost', port=29999, **values)
Short-hand for initailising an InSim connection.
source code
OutReceiver
startOutReceiver(host='localhost', port=30000, mode=0, timeout=10.0)
Short-hand for initailising an OutReceiver().
source code
tuple
time(ms)
Convert milliseconds into hours, minutes, seconds and thousanths.
source code
string
timeStr(ms, hours=False)
Convert milliseconds into a formatted time string (EG h:m:s.t).
source code
 
_packetSize(data)
Get the size of the packet.
source code
 
_packetType(data)
Get the packet type from the ISP_ enumeration.
source code
 
_tinyType(data)
Get the tiny type from the TINY_ enumeration.
source code
 
_eatNullChars(str_)
Remove NULL terminating line characters.
source code
Variables [hide private]
  _INSIM_BUFFER_SIZE = 1024
  INSIM_TCP = 1
  INSIM_UDP = 2
  PYINSIM_VERSION = '1.5.5'
  _IP_PATTERN = '\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25...
  _ENCODING_MAP = {'B': 'iso8859_13', 'C': 'cp1251', 'E': 'iso88...
  _ESCAPE_MAP = {'^': '^', 'a': '*', 'c': ':', 'd': '\\', 'l': '...
  MAX_PORT = 65535
  MIN_PORT = 1
  ADMIN_LEN = 16
  CLOSE_REQUEST = 0
  CLOSE_LFS = 1
  EVT_CLOSE = 253
  EVT_ERROR = 254
  EVT_TIMEOUT = 255
  OUT_INSIM = 0
  OUT_OUTSIM = 1
  OUT_OUTGAUGE = 2
  ISP_NONE = 0
  ISP_ISI = 1
  ISP_VER = 2
  ISP_TINY = 3
  ISP_SMALL = 4
  ISP_STA = 5
  ISP_SCH = 6
  ISP_SFP = 7
  ISP_SCC = 8
  ISP_CPP = 9
  ISP_ISM = 10
  ISP_MSO = 11
  ISP_III = 12
  ISP_MST = 13
  ISP_MTC = 14
  ISP_MOD = 15
  ISP_VTN = 16
  ISP_RST = 17
  ISP_NCN = 18
  ISP_CNL = 19
  ISP_CPR = 20
  ISP_NPL = 21
  ISP_PLP = 22
  ISP_PLL = 23
  ISP_LAP = 24
  ISP_SPX = 25
  ISP_PIT = 26
  ISP_PSF = 27
  ISP_PLA = 28
  ISP_CCH = 29
  ISP_PEN = 30
  ISP_TOC = 31
  ISP_FLG = 32
  ISP_PFL = 33
  ISP_FIN = 34
  ISP_RES = 35
  ISP_REO = 36
  ISP_NLP = 37
  ISP_MCI = 38
  ISP_MSX = 39
  ISP_MSL = 40
  ISP_CRS = 41
  ISP_BFN = 42
  ISP_AXI = 43
  ISP_AXO = 44
  ISP_BTN = 45
  ISP_BTC = 46
  ISP_BTT = 47
  ISP_RIP = 48
  ISP_SSH = 49
  ISP_OUTGAUGE = 50
  ISP_OUTSIM = 51
  TINY_NONE = 0
  TINY_VER = 1
  TINY_CLOSE = 2
  TINY_PING = 3
  TINY_REPLY = 4
  TINY_VTC = 5
  TINY_SCP = 6
  TINY_SST = 7
  TINY_GTH = 8
  TINY_MPE = 9
  TINY_ISM = 10
  TINY_REN = 11
  TINY_CLR = 12
  TINY_NCN = 13
  TINY_NPL = 14
  TINY_RES = 15
  TINY_NLP = 16
  TINY_MCI = 17
  TINY_REO = 18
  TINY_RST = 19
  TINY_AXI = 20
  TINY_AXC = 21
  TINY_RIP = 22
  SMALL_NONE = 0
  SMALL_SSP = 1
  SMALL_SSG = 2
  SMALL_VTA = 3
  SMALL_TMS = 4
  SMALL_STP = 5
  SMALL_RTP = 6
  SMALL_NLI = 7
  ISF_RES_0 = 1
  ISF_RES_1 = 2
  ISF_LOCAL = 4
  ISF_MSO_COLS = 8
  ISF_NLP = 16
  ISF_MCI = 32
  MSO_SYSTEM = 0
  MSO_USER = 1
  MSO_PREFIX = 2
  MSO_O = 3
  SND_SILENT = 0
  SND_MESSAGE = 1
  SND_SYSMESSAGE = 2
  SND_INVALIDKEY = 3
  SND_ERROR = 4
  VOTE_NONE = 0
  VOTE_END = 1
  VOTE_RESTART = 2
  VOTE_QUALIFY = 3
  PITLANE_EXIT = 0
  PITLANE_ENTER = 1
  PITLANE_NO_PURPOSE = 2
  PITLANE_DT = 3
  PITLANE_SG = 4
  VIEW_FOLLOW = 0
  VIEW_HELI = 1
  VIEW_CAM = 2
  VIEW_DRIVER = 3
  VIEW_CUSTOM = 4
  VIEW_ANOTHER = 255
  LEAVR_DISCO = 0
  LEAVR_TIMEOUT = 1
  LEAVR_LOSTCONN = 2
  LEAVR_KICKED = 3
  LEAVR_BANNED = 4
  LEAVR_SECURITY = 5
  PENALTY_NONE = 0
  PENALTY_DT = 1
  PENALTY_DT_VALID = 2
  PENALTY_SG = 3
  PENALTY_SG_VALID = 4
  PENALTY_30 = 5
  PENALTY_45 = 6
  PENR_UNKNOWN = 1
  PENR_ADMIN = 2
  PENR_WRONG_WAY = 3
  PENR_FALSE_START = 4
  PENR_SPEEDING = 5
  PENR_STOP_SHORT = 6
  PENR_STOP_LATE = 7
  TYRE_R1 = 0
  TYRE_R2 = 1
  TYRE_R3 = 2
  TYRE_R4 = 3
  TYRE_ROAD_SUPER = 4
  TYRE_ROAD_NORMAL = 5
  TYRE_HYBRID = 6
  TYRE_KNOBBLY = 7
  TYRE_NOT_CHANGED = 255
  ISS_GAME = 1
  ISS_REPLAY = 2
  ISS_PAUSED = 4
  ISS_SHIFTU = 8
  ISS_SHIFTU_HIGH = 16
  ISS_SHIFTU_FOLLOW = 32
  ISS_SHIFTU_NO_OPT = 64
  ISS_SHOW_2D = 128
  ISS_FRONT_END = 256
  ISS_MULTI = 512
  ISS_MPSPEEDUP = 1024
  ISS_WINDOWED = 2048
  ISS_SOUND_MUTE = 4096
  ISS_VIEW_OVERRIDE = 8192
  ISS_VISIBLE = 16384
  PSE_NOTHING = 1
  PSE_STOP = 2
  PSE_FR_DAM = 4
  PSE_FR_WHL = 8
  PSE_LE_FR_DAM = 16
  PSE_LE_FR_WHL = 32
  PSE_RI_FR_DAM = 64
  PSE_RI_FR_WHL = 128
  PSE_RE_DAM = 256
  PSE_RE_WHL = 512
  PSE_LE_RE_DAM = 1024
  PSE_LE_RE_WHL = 2048
  PSE_RI_RE_DAM = 4096
  PSE_RI_RE_WHL = 8192
  PSE_BODY_MINOR = 16384
  PSE_BODY_MAJOR = 32768
  PSE_SETUP = 65536
  PSE_REFUEL = 131072
  PSE_NUM = 262144
  PIF_SWAPSIDE = 1
  PIF_RESERVED_2 = 2
  PIF_RESERVED_4 = 4
  PIF_AUTOGEARS = 8
  PIF_SHIFTER = 16
  PIF_RESERVED_32 = 32
  PIF_HELP_B = 64
  PIF_AXIS_CLUTCH = 128
  PIF_INPITS = 256
  PIF_AUTOCLUTCH = 512
  PIF_MOUSE = 1024
  PIF_KB_NO_HELP = 2048
  PIF_KB_STABILISED = 4096
  PIF_CUSTOM_VIEW = 8192
  CONF_MENTIONED = 1
  CONF_CONFIRMED = 2
  CONF_PENALTY_DT = 4
  CONF_PENALTY_SG = 8
  CONF_PENALTY_30 = 16
  CONF_PENALTY_45 = 32
  CONF_DID_NOT_PIT = 64
  CONF_DISQ = 76
  CONF_TIME = 48
  HOSTF_CAN_VOTE = 1
  HOSTF_CAN_SELECT = 2
  HOSTF_MID_RACE = 32
  HOSTF_MUST_PIT = 64
  HOSTF_CAN_RESET = 128
  HOSTF_FCV = 256
  HOSTF_CRUISE = 512
  CCI_BLUE = 1
  CCI_YELLOW = 2
  CCI_LAG = 32
  CCI_FIRST = 64
  CCI_LAST = 128
  IS_X_MIN = 0
  IS_X_MAX = 110
  IS_Y_MIN = 30
  IS_Y_MAX = 170
  BFN_DEL_BTN = 0
  BFN_CLEAR = 1
  BFN_USER_CLEAR = 2
  BFN_REQUEST = 3
  INST_ALWAYS_ON = 128
  ISB_C1 = 1
  ISB_C2 = 2
  ISB_C4 = 4
  ISB_CLICK = 8
  ISB_LIGHT = 16
  ISB_DARK = 32
  ISB_LEFT = 64
  ISB_RIGHT = 128
  ISB_LMB = 1
  ISB_RMB = 2
  ISB_CTRL = 4
  ISB_SHIFT = 8
  WEA_BRIGHTCLEAR = 0
  WEA_OVERCAST = 1
  WEA_CLOUDYSUNSETDUSK = 2
  WND_NONE = 0
  WND_LOW = 1
  WND_HIGH = 2
  FLG_BLUE = 1
  FLG_YELLOW = 2
  FLG_ON = 1
  FLG_OFF = 0
  RIP_OK = 0
  RIP_ALREADY = 1
  RIP_DEDICATED = 2
  RIP_WRONG_MODE = 3
  RIP_NOT_REPLAY = 4
  RIP_CORRUPTED = 5
  RIP_NOT_FOUND = 6
  RIP_UNLOADABLE = 7
  RIP_DEST_OOB = 8
  RIP_UNKNOWN = 9
  RIP_USER = 10
  RIP_OOS = 11
  RIPOPT_LOOP = 1
  RIPOPT_SKINS = 2
  SSH_OK = 0
  SSH_DEDICATED = 1
  SSH_CORRUPTED = 2
  SSH_NO_SAVE = 3
  SETF_SYMM_WHEELS = 1
  SETF_TC_ENABLE = 2
  SETF_ABS_ENABLE = 4
  COL_BLACK = '^0'
  COL_RED = '^1'
  COL_LIGHT_GREEN = '^2'
  COL_YELLOW = '^3'
  COL_BLUE = '^4'
  COL_PURPLE = '^5'
  COL_LIGHT_BLUE = '^6'
  COL_WHITE = '^7'
  COL_DARK_GREEN = '^8'
  COL_ORIGINAL = '^9'
  OG_TURBO = 8192
  OG_KM = 16384
  OG_BAR = 32768
  DL_SHIFT = 1
  DL_FULLBEAM = 2
  DL_HANDBRAKE = 4
  DL_PITSPEED = 8
  DL_TC = 16
  DL_SIGNAL_L = 32
  DL_SIGNAL_R = 64
  DL_SIGNAL_ANY = 128
  DL_OILWARN = 256
  DL_BATTERY = 512
  DL_ABS = 1024
  DL_SPARE = 2048
  DL_NUM = 4096
  _PACKET_STRS = {'ISP_AXI': 43, 'ISP_AXO': 44, 'ISP_BFN': 42, '...
  _PACKET_DEFS = {ISP_ISI: IS_ISI, ISP_VER: IS_VER, ISP_TINY: IS...
  __package__ = None
Function Details [hide private]

checkIP(ipStr)

source code 

Check if a string contains a valid IP address.

Parameters:
  • ipStr (string) - The host IP string to check.
Returns: boolean
True if the IP is valid.

checkPort(portStr)

source code 

Check if a string contains a valid port number.

Parameters:
  • portStr (string) - The port string to check.
Returns: boolean
True if the port number is valid.

checkAdmin(adminStr)

source code 

Check if a string contains a valid admin password.

Parameters:
  • adminStr (string) - The admin password string to check.
Returns: boolean
True if the admin password is valid.

parseCommand(mso, prefix)

source code 

Parse a command string from an MSO message (EG '!admin DarkTimes') and return it as a tuple.

Parameters:
  • mso (IS_MSO) - An IS_MSO packet, possibly containing a command.
  • prefix (string) - The prefix (E.G. '!') the command begins with.
Returns: tuple
A tuple, with the command as the first element, any following command string as the second element, or () if no command could be parsed.

strToUnicode(str_, default='L', cols=True)

source code 

Convert a LFS encoded string to unicode.

Parameters:
  • str_ (string) - The LFS encoded string.
  • default (string) - The default encoding to start with (E.G. L, G, J etc..).
  • cols (boolean) - Whether to keep colour codes in the string.
Returns: unicode string
The string encoded to unicode.

stripColours(lfsStr)

source code 

Strip colour codes (^1, ^3 etc..) from a string.

Parameters:
  • lfsStr (string) - The string to strip.
Returns: string
The string sans colour codes.

speedToMps(speed)

source code 

Convert speed to meters per second.

Parameters:
  • speed (number) - The speed to convert.
Returns: number
Meters per second.

speedToKph(speed)

source code 

Convert speed to kilometers per hour.

Parameters:
  • speed (number) - The speed to convert.
Returns: number
Kilometers per hour.

mpsToKph(mps)

source code 

Convert meters per second to kilometers per hour.

Parameters:
  • mps (number) - Meters per second.
Returns: number
Kilometers per hour.

speedToMph(speed)

source code 

Convert speed to miles per hour.

Parameters:
  • speed (number) - The speed to convert.
Returns: number
Miles per hour.

mpsToMph(mps)

source code 

Convert meters per second to miles per hour.

Parameters:
  • mps (number) - Meters per second.
Returns: number
Miles per hour.

metersToMiles(meters)

source code 

Convert meters to miles.

Parameters:
  • meters (number) - The meters to convert.
Returns: number
Miles.

metersToKilometers(meters)

source code 

Convert meters to kilometers.

Parameters:
  • meters (number) - The meters to convert.
Returns: number
Kilometers.

directionToDegrees(direction)

source code 

Convert direction to degrees.

Parameters:
  • direction (number) - The direction to convert.
Returns: number
Degrees.

lengthToMetres(length)

source code 

Convert length to meters.

Parameters:
  • length (number) - The length to convert.
Returns: number
Meters

distance(a=(0, 0, 0), b=(0, 0, 0))

source code 

Calculate the distance between two points.

Parameters:
  • a (tuple) - The points (X, Y, Z) coordinates.
  • b (tuple) - The points (X, Y, Z) coordinates.
Returns: number
The distance.

radiansToDegrees(radians)

source code 

Convert radians to degrees.

Parameters:
  • radians (number) - The radians to convert.
Returns: number
Degrees.

radiansToRpm(radians)

source code 

Convert radians to RPM.

Parameters:
  • radians (number) - The radians to convert.
Returns: number
RPM.

insimConnect(host='localhost', port=29999, **values)

source code 

Short-hand for initailising an InSim connection. It creates an Insim object, calls its connect() method, and sends an ISI packet.

>>> # Example.
>>> insim = pyinsim.insimConnect('localhost', 29999, Admin='pass')
Parameters:
  • host (string) - The host to connect to.
  • port (number) - The port to connect to the host through.
  • values (args) - The values to initailise the ISI packet with.
Returns: InSim
The initailised InSim object.

startOutReceiver(host='localhost', port=30000, mode=0, timeout=10.0)

source code 

Short-hand for initailising an OutReceiver().

Parameters:
  • host (string) - The host to connect to.
  • port (number) - The port to connect to the host through.
  • mode (enum) - The mode from the OUT_ enumeration.
  • timeout (number) - The seconds to wait before timing out.
Returns: OutReceiver
The initailise OutReceiver object.

time(ms)

source code 

Convert milliseconds into hours, minutes, seconds and thousanths.

Parameters:
  • ms (number) - The time in milliseconds
Returns: tuple
A tuple containing the (hours, mins, secs, thousandths).

timeStr(ms, hours=False)

source code 

Convert milliseconds into a formatted time string (EG h:m:s.t).

Parameters:
  • ms (number) - The time in milliseconds.
  • hours (boolean) - Set to True to force use of the hours componant of the time.
Returns: string
The formatted time string (E.G. hh:mm:ss.ttt)

Variables Details [hide private]

_IP_PATTERN

Value:
'''\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2\\
[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\
\.(25\\
[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b'''

_ENCODING_MAP

Value:
{'B': 'iso8859_13',
 'C': 'cp1251',
 'E': 'iso8859_2',
 'G': 'iso8859_7',
 'H': 'cp950',
 'J': 'Shift-JIS',
 'K': 'cp949',
 'L': 'cp1252',
...

_ESCAPE_MAP

Value:
{'^': '^',
 'a': '*',
 'c': ':',
 'd': '\\',
 'l': '<',
 'q': '?',
 'r': '>',
 's': '/',
...

_PACKET_STRS

Value:
{'ISP_AXI': 43,
 'ISP_AXO': 44,
 'ISP_BFN': 42,
 'ISP_BTC': 46,
 'ISP_BTN': 45,
 'ISP_BTT': 47,
 'ISP_CCH': 29,
 'ISP_CNL': 19,
...

_PACKET_DEFS

Value:
{ISP_ISI: IS_ISI, ISP_VER: IS_VER, ISP_TINY: IS_TINY, ISP_SMALL: IS_SM\
ALL, ISP_STA: IS_STA, ISP_SCH: IS_SCH, ISP_SFP: IS_SFP, ISP_SCC: IS_SC\
C, ISP_CPP: IS_CPP, ISP_ISM: IS_ISM, ISP_MSO: IS_MSO, ISP_III: IS_III,\
 ISP_MST: IS_MST, ISP_MTC: IS_MTC, ISP_MOD: IS_MOD, ISP_VTN: IS_VTN, I\
SP_RST: IS_RST, ISP_NCN: IS_NCN, ISP_CNL: IS_CNL, ISP_CPR: IS_CPR, ISP\
_NPL: IS_NPL, ISP_PLP: IS_PLP, ISP_PLL: IS_PLL, ISP_LAP: IS_LAP, ISP_S\
PX: IS_SPX, ISP_PIT: IS_PIT, ISP_PSF: IS_PSF, ISP_PLA: IS_PLA, ISP_CCH\
: IS_CCH, ISP_PEN: IS_PEN, ISP_TOC: IS_TOC, ISP_FLG: IS_FLG, ISP_PFL: \
...