summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-system/msg_client.py
blob: 8978e86419e08b769a95ea67e9cb75dfaff9b4be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python2
#client for  msg_daemon

import socket
import sys
import argparse
import pickle
import time
# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = '/run/msg_socket'
def send_message(message):
    #print >>sys.stderr, 'connecting to %s' % server_address
    try:
        sock.connect(server_address)
    except socket.error, msg:
        print >>sys.stderr, msg
        sys.exit(1)
#try:
    sock.sendall(message)
    #amount_received = 0
    #amount_expected = len(message)
    print "Waiting for response..."
    if cmd == 'list':
        time.sleep(1)
    data = sock.recv(1024)
    print data

#finally:
    #print >>sys.stderr, 'error occured closing socket'

    sock.close()
def usage():
    line = '''
    Usage:
    msg_client.py is used to add, remove, or list items in the queue of messages that
    will be displayed on screen. Optionally messages can be given an identifier or "tag".
    This identifier does not have to be unique.

    Items are processed in the order they arrive, based off their slot number.
    The lowest slot number will be displayed first.
    After the message is displayed it is removed from the queue.


    ADD:
      To add items to the queue (--tag is optional):
        msg_client.py --msg "My first message" --tag "tag1"

      To add a message with a line break use \\n:
        msg_client.py --msg "My first\\nmessage"


    REMOVE:
      To remove all items from the queue that match the tag "tag1":
        msg_client.py --clear --tag "tag1"

      To remove all items from the queue:
        msg_client.py --clear

      To immediately stop displaying the current message:
        msg_client.py --kill


    LIST:
      To list items in the queue or to get the total item count:
        msg_client.py --print_list
            >      -------------
            >      msg : slot 1
            >      slot : 1
            >      cmd : msg
            >      tag : None
            >      timeout : None


    APPEARANCE:
      Display profiles define the font color, display timeout, screen position,
        and font. Display profiles are defined in /usr/MythVantage/etc/msg.cfg

      To display the message using a profile:
        msg_client.py --msg "Display middle profile.|middle"

    '''
    print line
    sys.exit(0)
if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument('--tag', action='store', dest='tag',help='Add a tag to the message')
    parser.add_argument('--timeout', action='store', dest='timeout',help='Time to display the message in seconds')
    action = parser.add_mutually_exclusive_group(required=True)
    action.add_argument('--msg', action='store', dest='msg', help='Add a message to the queue')
    action.add_argument('--clear', action='store_true', help='Remove messages from queue that match tag. No tag will remove all messages in the queue')
    action.add_argument('--kill', action='store_true', help='Kill the current message that is displayed')
    action.add_argument('--print_list', action='store_true', help='Print the current queue')
    action.add_argument('--usage', action='store_true', help='Print usage screen')

    results = parser.parse_args()
    resultsdict = vars(results)

    if results.msg :
        cmd = "msg"
    elif results.clear:
        cmd = "clear"
    elif results.kill:
        cmd = "kill"
    elif results.print_list:
        cmd = "list"
    elif results.usage:
        usage()

    arg_dict = {'cmd':cmd , 'msg':resultsdict['msg'] , 'tag':resultsdict['tag'] , 'timeout':resultsdict['timeout']}
    send_message(pickle.dumps(arg_dict))
    #sys.exit(0)