import sys
import argparse
import json
import RXTools as rx
import datetime

if __name__ == "__main__":

  ######################################################################
  # Parse arguments
  parser = argparse.ArgumentParser(description='Clear receiver error/warning log')
  parser.add_argument('-s','--stations', help='Filename of the station JSON e.g. --stations global.json')
  args = parser.parse_args()
  ######################################################################

  # Load the list of stations
  if(args.stations):
    with open(args.stations,'r') as f: 
      data = json.load(f)

    stations = []
    gotStations = False
    # New JSON format
    for i in range(len(data)):
      if('RX' in data[i]):
        stations = data[i]['RX']
        gotStations = True
    
    # Old format
    if(gotStations == False):
      stations = data
  else:
    print('require a station JSON file')
    sys.exit(1)
 
  for thisStation in stations:
    nowStr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(nowStr,':',thisStation.get("addr"))
    try:
      rx.ClearErrorLog(thisStation.get("addr"), 
                       thisStation.get("user"), 
                       thisStation.get("pw"))
    except:
      print('Problem with:',thisStation.get("addr"))


