import RXTools as rx
import time
import argparse 
import random
import sys

# Default values
user     = 'admin'
password = 'password'
port     = 5020
NumMax   = 2

#
# Controlled acquisiton - Turns off/on each satellite system at a time
# Hostile re-acquistion - Uses the Trimcomm A2 command to tweak the signal and force a loss of lock.
#                         However, Stinger is not informed, so transitions through loss of lock
#                         logic
# Randomly force a set of channels from PLL to FLL (and then allow then to upgrade back to PLL)
#
# Needs web access *and* an open TCP/IP port!
#
# Copyright Trimble Inc 2022
#
parser = argparse.ArgumentParser(description="Stresses Stinger tracking by forcing relocks",
                                 formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument("IPAddr", help="IP Address of the receiver")
parser.add_argument("--user", help="Username")
parser.add_argument("--port", help="Open TCP/IP port")
parser.add_argument("--password", help="Password")
parser.add_argument("--NumMax", help="Number of Maxwells - 1 or 2 (default 2)")
args = parser.parse_args()

IPAddr = args.IPAddr

if(args.NumMax is not None):
  num = int(args.NumMax)
  if( (num == 1) or (num == 2) ):
    NumMax = num
  else:
    print('Invalid number of Maxwells',num)
    sys.exit(1)

MaxChan = int(0x74/NumMax)

if(args.port is not None):
  port = int(args.port)

if(args.user is not None):
  user = args.user

if(args.password is not None):
  password = args.password

print('IP Address:',IPAddr)
print('Username:',user)
print('Password:',password)
print('TrimComm Port:',port)
print('Number of Maxwells: %d\n'  %NumMax)

satSys = ['GPS','GLONASS','BeiDou','Galileo','QZSS']

sysDelay = 20

while(True):
  for thisSys in satSys:
    print(thisSys)
   
    while(True):
      try:
        rx.disableGNSS(IPAddr,user,password,thisSys)
        break
      except:
        time.sleep(5)
        pass

    time.sleep(sysDelay)

    while(True):
      try:
        rx.enableGNSS(IPAddr,user,password,thisSys)
        break
      except:
        time.sleep(5)
        pass
        
  # We've cycled the systems, now mess with the samples a 
  # channel group at a time

  chanNum = 10 # How many to kill at a time
  for index in range( int(MaxChan / chanNum) + 1):
    
    start = index * chanNum
    end = start + chanNum
    if(end > MaxChan):
      end = MaxChan

    while(True):
      try:
        cmd = rx.formDColAntennaOffCmd(start_chan=start,end_chan=end)
        print('Ant Off',start,end)
        rx.sendDColCommand(IPAddr,port,cmd)
        time.sleep(2)

        cmd = rx.formDColAntennaOnCmd(start_chan=start,end_chan=end)
        print('Ant On',start,end)
        rx.sendDColCommand(IPAddr,port,cmd)
        time.sleep(5)
        break

      except:
        print('Error sleep')
        time.sleep(5)
        pass

  # Force a random set of channels to FLL and back 
  # First time we send the A2:12 the channel will downgrade to an FLL. It 
  # appears to stick there until you send another A2:12 to recover
  print('Force FLL')
  for index in range(20):
    cmd = bytearray(0)
    for chan in range(1,MaxChan+1):
      if(random.random() > 0.5):
        print('Chan',chan)
        cmd += rx.formDColCommand( 0xA2, [12, chan] )

    try:
      # Force to FLL
      rx.sendDColCommand(IPAddr,port,cmd)
      time.sleep(5)
      # Allow to recover to an PLL
      rx.sendDColCommand(IPAddr,port,cmd)
    except:
      print('Command Issue')
      pass

    time.sleep(5)



