#!/usr/bin/python3
"""
@file
@copyright ESO - European Southern Observatory

@brief opcua data subscription

"""
import argparse
from ifopclib import ifopc

#from ifloggerlib.iflogger import IfLogger
#IfLogger.setUpLogging(filename="ifopcread_log", console_level="INFO")
if __name__ == "__main__":

    # execute only if run as a script
    parser = argparse.ArgumentParser(description='SUBSCRIBE TO OPCUA TOPICS')
    parser.add_argument('-f','--file', help='Configuration file (yaml)', required=True )
    parser.add_argument('-d','--data' ,nargs='+', help='Data to subscribe', required=True)
    parser.add_argument('-g','--groupfile', type=str , help='File of groups')
    parser.add_argument('-sp','--shortpolling', action='store_true', help='MAL properties: 5ms polling time')
    
    args=parser.parse_args()

    # Path for configuration file is relative
    # to env variable CFGPATH
    configfile = 'config/' + args.file

    # config path added only when a groupfile argument
    # is given. Otherwise, remains None.
    groupfile = args.groupfile
    if groupfile != None:
        groupfile  = 'config/' + args.groupfile

    if args.shortpolling:
        malProperties={'opc.ps.outstandingPublishRequests':'5',
                'opc.asyncLoopExecutionPeriodMs':'5',
                'opc.asyncCallSubmitTimeoutMs':'1000',
                'opc.ps.pollingPeriodMs':'5',
                'opc.ps.requestedPublishingIntervalMs':'25',
                'opc.asyncCallRetryPeriodMs':'250'}
    else:
        malProperties={}

    print(malProperties)
    
    reply = ifopc.subscribe(configfile,args.data,groupfile,malProperties)
    if type(reply) == Exception:
        raise Exception(reply)
    
    #print(reply)    

# ___oOo___