#!/usr/bin/python

import sys, getopt
sys.path.append('/usr/local/bin/thrift-ice-py')
 
from ice import Ice
from ice.ttypes import *
 
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
 
def main(argv):
  try:
    opts, args = getopt.getopt(sys.argv[1:],"hil:p:ag", ["help", "interfaces", "listpeers=", "setpeers=", "autogenpeers", "gps"])
  except getopt.GetoptError as err:
    print str(err)
    Usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-i", "--interfaces"):
       SYSGetInterfaces()
       sys.exit()
    elif opt in ("-l", "--listpeers"):
       SYSListPeers(arg)
       sys.exit()
    elif opt in ("-s", "--setpeers"):
       SYSSetPeers()
       sys.exit()
    elif opt in ("-a", "--autogenpeers"):
       SYSGenPeers()
       sys.exit()
    elif opt in ("-g", "--gps"):
       GetGPS()
       sys.exit()
    elif opt in ("-h", "--help"):
      Usage()
      sys.exit()
    else:
      Usage()
      sys.exit(2)

def GetGPS():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.GetGPSInfo()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def SYSGetInterfaces():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.SYS_GetInterfaceList()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def SYSListPeers(interface):
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.SYS_GetPeerList(interface)
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def SYSSetPeers():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
   # print client.stopRemoteSupport()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def SYSGenPeers():
  try:

    vPeerList = []
    sPeer0 = stPeerDevice()
    sPeer0.type = "type0";
    sPeer0.pn = "pn0";
    sPeer0.sn = "sn0";
    sPeer0.software_version = "sv0";
    sPeer0.hardware_version = "hv0";
    sPeer0.id = "id0";
    sPeer0.addr = "addr0";
    sPeer0.ecuid = "ecuid0";
    sPeer0.swid = "swid0";
    vPeerList.append(sPeer0)

    sPeer1 = stPeerDevice()
    sPeer1.type = "type1";
    sPeer1.pn = "pn1";
    sPeer1.sn = "sn1";
    sPeer1.software_version = "sv1";
    sPeer1.hardware_version = "hv1";
    sPeer1.id = "id1";
    sPeer1.addr = "addr1";
    sPeer1.ecuid = "ecuid1";
    sPeer1.swid = "swid1";
    vPeerList.append(sPeer1)

    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.SYS_SetPeerList("TestInterface",vPeerList)
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def Usage():
	print 'ice-rs [option]'
	print '   --interfaces              Show all reporting interfaces'
	print '   --listpeers [inteface]    List peers associated with interface'
	print '   --setpeers [inteface]     Set peers list on interface'
	print '   --autogenpeers            Create a test interface and list'
	print '   --gps                     Print current GPS info'

if __name__ == "__main__":
   main(sys.argv[1:])
