##
# Copyright (c) 2021 Xilinx, Inc.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
# associated documentation files (the "Software"), to deal in the Software without restriction, including 
# without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to 
# the following conditions: 
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial 
# portions of the Software. 
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
#
#
##
#!/usr/bin/env python
import os,sys,inspect,importlib,json,datetime
import threading
from multiprocessing import Process

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
apidir = os.path.join(parentdir,"api")
backdir = os.path.join(parentdir,"backend")
sys.path.insert(0,parentdir)
from api import users,settings
from backend import ysocket
from backend import tcpclient
# from backend import ysocket
from backend import header
from backend import rabbitmq_config
from backend import receiver
from time import sleep
import string
import logging
# import numpy as np
from pymongo import MongoClient

# reduce log level
connection=""
channel = ""


def myconverter(o):
    if isinstance(o, datetime.datetime):
        return o.__str__()

class PikaClient:
    def __init__(self, threadname="",client="",active_user="",tab="",readperformance="",delay=0.5,tabstatus=True,log="",baseaddress="",steps="",location="",tempobj = ""):
        self.threadname = threadname
        self.performancethread = ""
        self.tab = tab
        self.readperformance = readperformance
        self.delay = delay
        self.client = client
        self.active_user = active_user
        self.tabstatus = tabstatus
        self.log = log
        self.connectionstatus = 0
        self.baseaddress = baseaddress
        self.steps = steps
        self.location = location
        self.tempobj = tempobj
        self.sample = 0
        self.thread_createtime = datetime.datetime.now().timestamp()

    def updateconnectionstatus(self):
        user = users.get_user(self.active_user, "connectionstatus")
        self.connectionstatus = user["connectionstatus"]
        
    def createthread(self):
        self.performancethread = threading.Thread(target=self.newconnection, args=())
        self.performancethread.deamon = True
        self.performancethread.start()

    def newconnection(self):
        self.updateconnectionstatus()
        mongoClient = MongoClient(settings.MONGO_URI)
        if self.tab == "overlay" and self.connectionstatus and self.tabstatus:
            createoverlayfile(self.readperformance,self.log,self.active_user,self.client,self.tempobj,self.threadname)
        while self.connectionstatus and self.tabstatus:
            self.updateconnectionstatus()
            stat = True
            if self.tab == "overlay":
                message =readoverlay(self.log,self.active_user,self.client,self.threadname,self.thread_createtime)
            else:
                for i in range(len(rabbitmq_config.queues)):
                    if rabbitmq_config.queues[i]["config"]["tabname"].lower() == self.tab.lower():
                        if "efficiency" in rabbitmq_config.queues[i]["config"]["tabname"].lower():
                            message = self.getefficiency(rabbitmq_config.queues[i],self.log,self.active_user,self.client,self.threadname)
                        else:
                            message = eval(self.readperformance)(rabbitmq_config.queues[i],self.log,self.active_user,self.client,self.threadname)
                        stat = False
                        break
                if stat:
                    message = self.readsysaiwireless()
            receiver.dbdump(mongoClient,self.active_user,message)
            sleep(float(self.delay))
            print("delay", self.delay)
            print("time", datetime.datetime.now())
        print("closing channel")
        mongoClient.close()
        return 1

    def readsysaiwireless(self):
        sysaiwirelessArr = {"threadname":self.threadname,"tabname": self.tab + "_" + self.active_user, "data": {}}
        sysaiwirelessArr["data"]["key"] = self.threadname
        sysaiwirelessArr["data"]["time"] = datetime.datetime.now().timestamp()
        sysaiwirelessArr["data"]["data"] = []
        sysaiwirelessArr["time"] = sysaiwirelessArr["data"]["time"]
        if self.baseaddress != "" and "0x" in self.baseaddress:
            dataarr = []
            skipindex = int(int(self.steps) / 4)
            if int(self.location) > 0:
                address = self.baseaddress
                try:
                    tempdata = (tcpclient.runcommandfromui(("mrd %s %s" % (address, str(self.location))), self.log,self.client))
                except Exception as e:
                    print(str(e))
                dataarr = dataarr + tempdata
            tempdataarr = dataarr[0::skipindex]
            diff_logic = 0
            for i in range(len(tempdataarr)):
                temp = tempdataarr[i].split(':')
                if len(temp) == 2:
                    if all(c in 'xX' + string.hexdigits for c in temp[1].strip()):
                        if("0x8010" in self.baseaddress):
                            diff_logic = 1
                            a = temp[1].strip()
                            imgI = "0x%s" % a[0:4]
                            realI = "0x%s" % a[4:8]
                            i1 = np.int16(np.int(imgI, 16))
                            r1 = np.int16(np.int(realI, 16))
                            sysaiwirelessArr["data"]["data"].append({"address":temp[0].strip(),"y":int(np.sqrt(pow(i1, 2) + pow(r1, 2) ))})
                        else:
                            temp[1] = temp[1].strip()
                            temp[1] = "0x" + temp[1][4:]
                            new_val = np.int16(np.int(temp[1], 16))
                            temp[1] = int(new_val)
                            # sysaiwirelessArr["data"]["data"].append({"address":temp[0].strip(),"value":temp[1]})
                            sysaiwirelessArr["data"]["data"].append({"address":temp[0].strip(),"y":temp[1]})
                # address = hex(int(address,16)+int(self.steps))
        if diff_logic == 1:
            maxabs = max(sysaiwirelessArr["data"]["data"])
            newdata = [round(x/maxabs, 1) for x in sysaiwirelessArr["data"]["data"]]
            sysaiwirelessArr["data"]["data"] = newdata
        return sysaiwirelessArr
    def getefficiency(self, exchange, log, active_user, client,threadname):
        try:
            perfArr = {"threadname":threadname, "tabname": "ddr_efficiency_"+exchange["name"].split("_efficiency")[0] + "_" + active_user, "data": []}
            for i in range(len(exchange["config"]["instances"])):
                filename = os.path.join(apidir, "%s.tcl" % (str(exchange["config"]["file_name"])))
                mrdNoc = open(filename, "r")
                lines = mrdNoc.readlines()
                mrdNoc.close()
                timenow = datetime.datetime.now().timestamp()
                if self.sample != 0:
                    self.sample = timenow - self.sample
                else:
                    self.sample = timenow
                perfArr["time"] = timenow
                for line in lines:
                    keys = line.replace("\n", "").split(",")
                    newobj = {}
                    command_data = tcpclient.runcommandfromui("mrd %s 3" % keys[5], log, client)
                    tcpclient.runcommandfromui("mwr %s 0x0 3" % keys[5], log, client)
                    if (type(command_data) is list) and (len(command_data) >= 3):
                        newobj = {'accumulatedlatency': command_data[0].replace(' ', '').split(':')[-1],
                                  'burstcount': command_data[1].replace(' ', '').split(':')[-1],
                                  'transactioncount': command_data[2].replace(' ', '').split(':')[-1]
                                  }
                        newobj["throughput"] = int(newobj["transactioncount"], 16) * int(newobj["burstcount"], 16) * 1024 * (
                                    self.sample / 1000)
                        newobj["theoritical_throughput"] = 24
                        newobj["efficiency"] = newobj["throughput"] / newobj["theoritical_throughput"]
                        newobj["time"] = timenow
                        perfArr["data"].append({'module': keys[0],
                                                'submodule': keys[1],
                                                'monitor': keys[2],
                                                'key': "_".join(keys[0:3]),
                                                'data': [newobj]
                                                })
                    else:
                        perfArr["error"] = "Error on reading data from board"
            return perfArr
        except Exception as e:
            import traceback
            tb = traceback.format_exc()
            print("tb------>>", tb)

def set_bit(v, index, x):
  """Set the index:th bit of v to 1 if x is truthy, else to 0, and return the new value."""
  v = int(v, 16)
  mask = 1 << index   # Compute mask, an integer with just bit 'index' set.
  v &= ~mask          # Clear the bit indicated by the mask (if x is False)
  if x:
    v |= mask         # If x was True, set the bit indicated by the mask.
  p = hex(v)[2:]
  while len(p) <8:
      p = str(0)+p
  return p

def getperf(exchange,log,active_user,client,threadname):
    perfArr = {"threadname":threadname,"tabname":exchange["name"]+"_"+active_user, "data":[]}
    for i in range(len(exchange["config"]["instances"])):
        filename = os.path.join(apidir, "%s.tcl" %(str(exchange["config"]["file_name"])))
        mrdNoc = open(filename, "r")
        lines = mrdNoc.readlines()
        mrdNoc.close()
        timenow = datetime.datetime.now().timestamp()
        perfArr["time"] = timenow
        for line in lines:
            keys = line.replace("\n", "").split(",")
            newobj = {}
            command_data = tcpclient.runcommandfromui("mrd %s 3" % keys[5], log, client)
            if (type(command_data) is list) and (len(command_data) >= 3):
                newobj = {'accumulatedlatency': command_data[0].replace(' ', '').split(':')[-1],
                          'burstcount': command_data[1].replace(' ', '').split(':')[-1],
                          'transactioncount': command_data[2].replace(' ', '').split(':')[-1]
                         }
                overflow = []
                for key, val in newobj.items():
                    if ( int(val, 16) & (1<<31) ):
                        overflow.append(key)
                    newobj[key] = str("%x" % (int(val,  16) & int("0x7FFFFFFF", 16)))

                newobj.update({'overflow': overflow,
                               'time': timenow
                              })
                perfArr["data"].append({'module': keys[0],
                                        'submodule': keys[1],
                                        'monitor': keys[2],
                                        'key': "_".join(keys[0:3]),
                                        'data': [newobj]
                                       })
            else:
                perfArr["error"] = "Error on reading data from board"
    return perfArr

def getlpdcounters(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir,str(header.READ_LPD_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname":"lpd_"+active_user,"data":[]}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(
                        ("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                        {"time": timenow, str(keys[0]): str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr
def getpmccounters(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir,str(header.READ_PMC_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname":"pmc_"+active_user,"data":[]}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(
                        ("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] ="Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                    {"time": timenow, str(keys[0]): str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getfpdcounters(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir,str(header.READ_FPD_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname":"fpd_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time":timenow, str(keys[0]): str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getcpmcounters(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir,str(header.READ_CPM_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname":"cpm_"+active_user,"data":[]}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] ="Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                    {"time": timenow, str(keys[0]): str("%x" % (int(str(tempvar),  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getcpmnsu(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir, str(header.READ_CPM_NSU))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname": "cpm_nsu_" + active_user, "data": []}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log, client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str(
                            "%x" % (int(str(tempvar), 16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log, client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                        {"time": timenow, str(keys[0]): str("%x" % (int(str(tempvar), 16) & int("0x0000FFFF", 16))),
                         "overflow": []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getdcperformance(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir, str(header.PERF_DDR_DC))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname":"dc_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    overflow = []
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        if (int(str(tempvar),  16) & (1<<31)):
                            counterArr["data"][i]["data"][0]["overflow"].append(str(keys[0]))
                        else:
                            val = str(keys[0])
                            counterArr["data"][i]["data"][0]["overflow"] = [value for value in (counterArr["data"][i]["data"][0]["overflow"]) if value != val]
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(str(tempvar),  16) & int("0x7FFFFFFF", 16)))
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    if (int(str(tempvar),  16) & (1<<31)):
                        overflow.append(str(keys[0]))
                    else:
                        val = str(keys[0])
                        overflow = [value for value in overflow if value != val]
                    obj["data"].append({"time":timenow, str(keys[0]): str("%x" % (int(str(tempvar),  16) & int("0x7FFFFFFF", 16))), "overflow" : overflow})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getapmperformance(exchange,log,active_user,client,threadname):
    filename = os.path.join(apidir,str(header.READ_APM))
    readcounters = open(filename, "r")
    counterArr = {"threadname":threadname,"tabname":"pl_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time": timenow, str(keys[0]): tempvar})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getnocperformance(exchange,log,active_user,client,threadname):
    counterArr = {"threadname":threadname,"tabname":"noc_"+active_user,"data":[]}
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    dataobj = {}
    filename = os.path.join(apidir, str(header.READ_NMU_REG))
    readnmu = open(filename, "r")
    for line in readnmu:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if (len(keys) >= 3) and (line[0] != "#"):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]) and counterArr["data"][i]["key"] == "nmu_"+str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False
            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = "nmu_"+str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time":timenow, str(keys[0]):tempvar, "Accumulated_latency":""})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
            for i in range(len(counterArr["data"])):
                if (counterArr["data"][i]["data"][0]) and ("Accumulated_latency_upper" in counterArr["data"][i]["data"][0]) and ("Accumulated_latency_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["Accumulated_latency"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["Accumulated_latency_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["Accumulated_latency_lower"])
                if (counterArr["data"][i]["data"][0]) and ("byte_count_upper" in counterArr["data"][i]["data"][0]) and ("byte_count_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["byte_count"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["byte_count_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["byte_count_lower"])
    readnmu.close()
    status = False
    filename = os.path.join(apidir,str(header.READ_NSU_REG))
    readnsu = open(filename, "r")
    for line in readnsu:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if (len(keys) >= 3) and (line[0] != "#"):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]) and counterArr["data"][i]["key"] == "nsu_"+str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = "nsu_"+str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time":timenow, str(keys[0]):tempvar})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
            for i in range(len(counterArr["data"])):
                if (counterArr["data"][i]["data"][0]) and ("Accumulated_latency_upper" in counterArr["data"][i]["data"][0]) and ("Accumulated_latency_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["Accumulated_latency"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["Accumulated_latency_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["Accumulated_latency_lower"])
                if (counterArr["data"][i]["data"][0]) and ("byte_count_upper" in counterArr["data"][i]["data"][0]) and ("byte_count_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["byte_count"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["byte_count_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["byte_count_lower"])
    readnsu.close()

    npsdataarr = []
    for j in range(0, ysocket.numberofnps):
        status = False
        filename = os.path.join(apidir, str("%s"%header.READ_NPS_REG+str(j)+".tcl"))
        readnps = open(filename, "r")
        for line in readnps:
            line = line.replace("\n", "")
            keys = line.split(" ")
            if (len(keys) >= 3) and (line[0] != "#"):
                for i in range(len(counterArr["data"])):
                    if  counterArr["data"][i]["key"] == "nps_"+str(j):
                        status = True
                        if (str(keys[0]) == "counter_1"):
                            prevvalue = counterArr["data"][i]["data"][0][str(keys[2])]
                            tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[1])),log,client)
                            if all(c in 'xX' + string.hexdigits for c in tempvar):
                                counterArr["data"][i]["data"][0][str(keys[2])] = str(set_bit(tempvar, 31, 0))
                                counterArr["data"][i]["data"][0][str(keys[2])] = counterArr["data"][i]["data"][0][
                                                                                     str(keys[2])] + "" + prevvalue
                            else:
                                counterArr["error"] = "Error on reading data from board"
                        else:
                            tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                            if all(c in 'xX' + string.hexdigits for c in tempvar):
                                counterArr["data"][i]["data"][0][str(keys[2])] = tempvar
                            else:
                                counterArr["error"] = "Error on reading data from board"
                        break
                    else:
                        status = False

                if not status:
                    obj = {}
                    obj["data"] = []
                    obj["displayname"] = str(keys[0])
                    obj["key"] = "nps_"+str(j)
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[1])),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        obj["data"].append({"time":timenow, str(keys[2]):tempvar})
                        counterArr["data"].append(obj)
                    else:
                        counterArr["error"] = "Error on reading data from board"
        readnps.close()
    return counterArr

def getmeperf(exchange,log,active_user,client,threadname):
    counterArr = {"threadname":threadname,"tabname":"aiengine_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    status = False

    for j in range(0,len(client.mebaseaddress)):
        timenow = datetime.datetime.now().timestamp()
        counterArr["time"] = timenow
        filename = os.path.join(apidir, str(header.READ_ME))
        readcounters = open(filename, "r")
        for line in readcounters:
            line = line.replace("\n", "")
            keys = line.split(" ")
            if ((len(keys) >= 3) and (line[0] != '#')):
                for i in range(len(counterArr["data"])):
                    if counterArr["data"][i]["displayname"] == str(keys[2])+str(j):
                        status = True
                        address = hex(int(client.mebaseaddress[j], 16) + int(str(keys[1]), 16))
                        tempvar = tcpclient.getcommandresponse(("mrd %s\n" %address),log,client)
                        if all(c in 'xX' + string.hexdigits for c in tempvar):
                            counterArr["data"][i]["data"][0][str(keys[0])] = tempvar
                        else:
                            counterArr["error"] = "Error on reading data from board"
                        break
                    else:
                        status = False

                if not status:
                    obj = {}
                    obj["data"] = []
                    obj["displayname"] = str(keys[2]) + str(j)
                    obj["key"] = str(keys[2])+str(j)
                    address = hex(int(client.mebaseaddress[j], 16) + int(str(keys[1]), 16))
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" %address),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        obj["data"].append({"time": timenow, str(keys[0]): tempvar})
                        counterArr["data"].append(obj)
                    else:
                        counterArr["error"] = "Error on reading data from board"
        readcounters.close()
    return counterArr

#create .tcl file for read overlay performance
def createoverlayfile(readperformance,log,active_user,client,tempobj,threadname):
    methodarr = readperformance.split("/")
    open(threadname+".tcl", 'w').close()
    newfp = open(threadname+".tcl","a")
    for i in range(len(methodarr)):
        for j in range(len(rabbitmq_config.queues)):
            if methodarr[i] != "" and methodarr[i] == rabbitmq_config.queues[j]["config"]["method"]:
                filename = os.path.join(apidir, str(getattr(header,rabbitmq_config.queues[j]["config"]["file_name"])))
                fp = open(filename, "r")
                for performanceindex in range(len(tempobj["performance"])):
                    if tempobj["performance"][performanceindex]["subtab"] == rabbitmq_config.queues[j]["name"]:
                        for line in fp:
                            line = line.replace("\n", "")
                            if methodarr[i]!= "getperf":
                                keys = line.split(" ")
                                if ((len(keys) >= 3) and (line[0] != '#')):
                                    if any(x in line for x in tempobj["performance"][performanceindex]["plot"]):
                                        line = keys[0]+"_"+keys[2]+" "+keys[1]+" "+tempobj["key"]
                                        newfp.write(line + "\n")
                            else:
                                keys = line.split(",")
                                if len(keys)==6:
                                    mrdkeys = ["accumulatedlatency","burstcount","transactioncount"]
                                    for x in range(0,3):
                                        keys[3] = mrdkeys[x]
                                        line = keys[0]+"_"+keys[1]+"_"+keys[2]+" "+keys[5]+" "+tempobj["key"]
                                        if ((len(keys) >= 3) and (line[0] != '#')):
                                            if any(x in line for x in tempobj["performance"][performanceindex]["plot"]):
                                                newfp.write(line + "\n")

                fp.close()
    newfp.close()

def readoverlay(log,active_user,client,threadname,thread_createtime):
    filename = os.path.join(apidir, str(threadname+".tcl"))
    readcounters = open(filename, "r")
    counterArr = {"tabname": "overlay_" + active_user, "data": [],"threadname":threadname,"thread_createtime":thread_createtime}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now().timestamp()
    counterArr["time"] = timenow
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log, client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log, client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time": timenow, str(keys[0]): tempvar})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr