Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | #coding: utf8 |
| 3 | #Copyright © 2016 - 2017 Copyright (c) Sprint, Inc. and others. All rights |
| 4 | #reserved. |
| 5 | # |
| 6 | #This program and the accompanying materials are made available under the |
| 7 | #terms of the Eclipse Public License v1.0 which accompanies this distribution, |
| 8 | #and is available at http://www.eclipse.org/legal/epl-v10.html |
| 9 | # |
| 10 | ############################################################################ |
| 11 | # File : rules_test.py |
| 12 | # |
| 13 | # Comments : |
| 14 | # Establish the channel with DP (Script(PUB) -> Forwarder -> DP(SUB)), |
| 15 | # And listen and push the message over socket. |
| 16 | ############################################################################ |
| 17 | |
| 18 | import signal |
| 19 | import sys |
| 20 | import zmq |
| 21 | import struct |
| 22 | import socket as socketlib |
| 23 | import datetime |
| 24 | import time |
| 25 | |
| 26 | from adc_rules import * |
| 27 | from pcc_rules import * |
| 28 | from mtr_rules import * |
| 29 | from sdf_rules import * |
| 30 | |
| 31 | conflict = False |
| 32 | topicId = None |
| 33 | |
| 34 | # TBD: Needs to handle exception |
| 35 | # TBD: Needs to handle keyboard intrrupts |
| 36 | |
| 37 | #ZMQ ports |
slowr | dd7e81b | 2018-01-05 05:58:41 -0500 | [diff] [blame] | 38 | rec_port = "5566" |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 39 | send_port = "5555" |
| 40 | # Socket to talk to server |
| 41 | context = zmq.Context() |
| 42 | socket = context.socket(zmq.SUB) |
| 43 | pub_socket = context.socket(zmq.PUB) |
| 44 | # As of not test script runs from FPC-SDN only |
| 45 | socket.connect ("tcp://192.168.105.14:%s" % rec_port) |
| 46 | pub_socket.connect("tcp://192.168.105.14:%s" % send_port) |
| 47 | topicfilter = "" |
| 48 | controller_topic= 252 |
| 49 | socket.setsockopt(zmq.SUBSCRIBE, topicfilter) |
| 50 | print "Listening to port ", rec_port |
| 51 | print "Publisher on port ", send_port |
| 52 | print "Ready to receive messages. Press Ctrl+C when ready to exit." |
| 53 | |
| 54 | for update_nbr in range(900000): |
| 55 | # TBD: Needs to handle exception |
| 56 | string = socket.recv() |
| 57 | ts = time.time() |
| 58 | st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') |
| 59 | |
| 60 | topic, msgnum, ID = struct.unpack('!BBB', string[:3]) |
| 61 | print"\n topic,msg,ID:%s,%s,%s" % (topic, msgnum, ID) |
| 62 | |
| 63 | #Listen to topic |
| 64 | if topic == 1 and msgnum == 10:#Assign_Id |
| 65 | top, msg, topId = struct.unpack('!BBB', string[:3]) |
| 66 | print "\n topId :", topId |
| 67 | topicId = topId |
| 68 | |
| 69 | #Listen to ack |
| 70 | if topic == 2 and msgnum == 12: |
| 71 | top, msg, topId_t = struct.unpack('!BBB', string[:3]) |
| 72 | |
| 73 | if topicId == topId_t: |
| 74 | # TBD: Needs to handle exception |
| 75 | |
| 76 | parse_adc_values(pub_socket, topicId) |
| 77 | time.sleep(1) |
| 78 | parse_mtr_values(pub_socket, topicId) |
| 79 | time.sleep(1) |
| 80 | parse_pcc_values(pub_socket, topicId) |
| 81 | time.sleep(1) |
| 82 | parse_sdf_values(pub_socket, topicId) |
| 83 | time.sleep(1) |
| 84 | socket.close() |
| 85 | pub_socket.close() |
| 86 | sys.exit(0) |
| 87 | |