Commit b81b0aeb authored by mancini's avatar mancini

Implement Server and Client

parent 2ec97a93
import logging
import argparse
from lib.opc_interface import connect_to_server, DEFAULT_URI
import numpy
# ----------------------------------MAIN CODE LOGIC
def setup_logging():
"""
......@@ -27,14 +28,30 @@ def setup_command_argument_parser():
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Read cross correlation statistics and offers it through OPC UA")
parser.add_argument('url', help='url to connect to', type=int)
parser.add_argument('--url', help='url to connect to', type=str, default='localhost')
parser.add_argument('--port', help='url to connect to', type=int, default=55555)
parser.add_argument('--path', help='sub path', type=str, default='/LOFAR2.0/CROSSECHO')
return parser
def main():
init()
parser = setup_command_argument_parser()
arguments = parser.parse_args()
client = connect_to_server(arguments.url, arguments.port)
objects = client.get_objects_node()
idx = client.get_namespace_index(DEFAULT_URI)
# Now getting a variable node using its browse path
obj = client.get_root_node().get_child(["0:Objects",
"{}:StationMetrics".format(idx),
"{}:RCU".format(idx)])
result = obj.call_method("{}:record_cross".format(idx), 100, 2)
print("Timestamp is ", result[0])
print("Crosscorrelations are ", numpy.array(result[1]))
client.close_session()
client.close_secure_channel()
if __name__=='__main__':
main()
\ No newline at end of file
import logging
from opcua import ua, uamethod, Server
from opcua import ua, uamethod, Server, Client
from .rspctl_probe import produce_xcstatistics, set_xcsubband
import numpy
DEFAULT_URI = "http://lofar.eu"
......@@ -9,9 +10,19 @@ DEFAULT_URI = "http://lofar.eu"
@uamethod
def get_crosslet_statistics(parent, subband, integration_time):
import time
print("Ask to record subband {} with integration time {}", subband,
integration_time)
crosslets = numpy.zeros([96, 96]).tolist()
print("Ask to record subband {} with integration time {}".format(subband,
integration_time))
set_xcsubband(subband)
data = produce_xcstatistics(integration_time=integration_time, duration=integration_time)['data']
num_rcu = 96
time_slots = int(len(data) / num_rcu / num_rcu)
crosslets_complex = data.reshape((time_slots, num_rcu, num_rcu))
crosslets = numpy.zeros((2, num_rcu, num_rcu), dtype=float)
crosslets[0, :, :] = crosslets_complex.real
crosslets[1, :, :] = crosslets_complex.imag
crosslets = crosslets.tolist()
timestamp = time.localtime()
return timestamp, crosslets
......@@ -34,14 +45,14 @@ def define_get_crosslet_statistics_parameters(object, idx):
cross = ua.Argument()
cross.Name = "crosslet_statistics"
cross.DataType = ua.NodeId(ua.ObjectIds.ComplexNumberType)
cross.ValueRank = 2
cross.ArrayDimensions = [96, 96]
cross.DataType = ua.NodeId(ua.ObjectIds.Float)
cross.ValueRank = 3
cross.ArrayDimensions = [96, 96, 2]
cross.Description = ua.LocalizedText("crosscorrelation statistics")
timestamp = ua.Argument()
timestamp.Name = "timestamp"
timestamp.DataType = ua.NodeId(ua.ObjectIds.Int64)
timestamp.DataType = ua.NodeId(ua.ObjectIds.DateTime)
timestamp.ValueRank = -1
timestamp.ArrayDimensions = []
timestamp.Description = ua.LocalizedText("timestamp")
......@@ -50,6 +61,11 @@ def define_get_crosslet_statistics_parameters(object, idx):
[subband, integration_time],
[timestamp, cross])
def connect_to_server(url, port):
client = Client("opc.tcp://{}:{}/".format(url, port))
client.connect()
client.load_type_definitions()
return client
def setup_server_metadata(server=None, port=55555):
if server is None:
......
......@@ -12,6 +12,7 @@ import tempfile
import shutil
import time
import socket
import numpy
import traceback
from functools import reduce
......@@ -605,9 +606,6 @@ def query_xcstatistics(options):
temporary_output_directory = tempfile.mkdtemp(prefix="rspctlprobe_tmp")
options['directory'] = temporary_output_directory
integration = options['integration']
duration = options['duration']
logger.info("query xcstatistics and storing them into directory %s", options['directory'])
......@@ -620,27 +618,13 @@ def query_xcstatistics(options):
res["timestamp"] = timestamp
filename = timestamp + filename
tmp_file_out = os.path.join(temporary_output_directory, file_list)
shutil.copy(tmp_file_out, os.path.join(final_directory, filename))
data = numpy.fromfile(filename, dtype=numpy.complex128)
shutil.copy(os.path.join(temporary_output_directory, file_list), os.path.join(final_directory, filename))
shutil.rmtree(temporary_output_directory)
rcus = res["rcus"]
header = ["RCUID", "delay", "attenuation", "mode", "status", "xcsub_bands"]
ids = [[header[0]] + list(map(str, list(rcus.keys())))] # Create the id column of the file
table = [[key] + [str(rcus[i][key]) for i in rcus] for key in header[1:]]
table = ids + table
fileout = os.path.join(final_directory, "summary.info")
with open(fileout, "a") as out:
out.write("\n")
out.write("timestamp = {} , mode = {} , xcsubband = {}, integration = {}, duration = {}\n".format(
res["timestamp"],
res["mode"],
res["xcsub_band"],
integration,
duration))
table_print_out_table(out.write, table)
res["data"] = data
return res
......@@ -718,7 +702,7 @@ def produce_xcstatistics(integration_time=1, duration=1, add_options=None, outpu
add_options["directory"] = output_directory
res = query_xcstatistics(add_options)
return res
return
def batch_produce_xcstatistics(integration_time,
......@@ -760,7 +744,3 @@ def batch_produce_xcstatistics(integration_time,
time.sleep(w)
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment