Last active
November 22, 2015 00:44
-
-
Save ezeeetm/0ac9aee6bfd0e6a1f4cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
wally.py - version 1.0 tested using Python 2.7.5 [MSC v.1500 32 bit (Intel)] on Windows 7 x64 | |
- Accepts command line arguments | |
- Opens simple IP:socket on a 4U2SEE LED wallboard | |
- Sends messages to control the behaviour of the 4U2SEE LED wallboard, defined by command line arguments and stdin | |
- Closes the socket | |
###################### | |
TODO | |
- add some minimal try/except error handling | |
- add minor debug verbose output | |
- a unit test | |
- better comments | |
""" | |
import argparse | |
import socket | |
import sys | |
#read command line args from stdin, to be used to define the wallboard object parameters | |
def readArgs(): | |
argParser = argparse.ArgumentParser() | |
argParser.add_argument('-i', '--ip-address', action='store', dest='ipAddress', | |
help='Assigns the value specified to the target IP Address of the wallboard') | |
argParser.add_argument('-p', '--port', action='store', default='1024', dest='port', | |
help='Assigns the value specified to override the default port (1024)') | |
argParser.add_argument('-s', '--sign-address', action='store', default='00', dest='signAddress', | |
help='Assigns the value specified to override the default sign address (00)') | |
argParser.add_argument('-m', '--message', action='store', dest='custMsg', | |
help='Assigns the "<string>" specified to a custom message to be sent to the 4U2SEE wallboard.') | |
#argParser.add_argument('-d', '--debug', action='store_true', default=False, dest='debug', | |
#help='Turn on verbose debugging') | |
argParser.add_argument('-r', '--restore', action='store_true', default=False, dest='restore', | |
help='Restore factory defaults on the 4U2SEE LED wallboard') | |
argParser.add_argument('-c', '--clear-ram', action='store_true', default=False, dest='clear', | |
help='Clear RAM on the 4U2SEE LED wallboard') | |
argParser.add_argument('-C', '--clear-flash-ram', action='store_true', default=False, dest='clearFR', | |
help='Clear Flash and RAM on the 4U2SEE LED wallboard ') | |
argParser.add_argument('-b', '--blank', action='store_true', default=False, dest='blank', | |
help='Blank the 4U2SEE LED wallboard') | |
argParser.add_argument('-t', '--test', action='store_true', default=False, dest='test', | |
help='Send a known good visual feedback test message to the 4U2SEE wallboard, 12:00 AM') | |
argParser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') | |
#assign values for all action='store' args | |
results = argParser.parse_args() | |
ipAddress = results.ipAddress | |
port = results.port | |
signAddress = results.signAddress | |
custMsg = results.custMsg | |
#assign values for all boolean action='store_true' args | |
msgTypeCount = 0 #used to ensure message args == 1 | |
msgTypes = {'RESTORE':results.restore, 'CLEAR':results.clear, 'CLEARFR':results.clearFR, 'BLANK':results.blank, 'TEST':results.test} | |
if custMsg is not None: | |
msgTypeCount +=1 | |
msgType = "CUSTOM" | |
for type, results in msgTypes.iteritems(): | |
if results == True: | |
msgTypeCount += 1 | |
msgType = type | |
#test for != 1 message arg | |
if msgTypeCount > 1: | |
print 'Too many message arguments specified. Do not combine -m, -r, -c, -C, -b, or -t. wally.py -h for help' | |
sys.exit() | |
if msgTypeCount == 0: | |
print 'No message arguments specified. wally.py -h for help' | |
sys.exit() | |
return (ipAddress,port,signAddress,msgType,custMsg) | |
class Wallboard(object): | |
def __init__(self, ipAddress,port,signAddress): | |
self._headerStart = '\x01' | |
self._preFilter = 'z' | |
self._startOfText = '\x02' | |
self._end = '\x04' | |
self._cmdHeader = self._headerStart + self._preFilter + signAddress + self._startOfText | |
self._command = { | |
'RESTORE' : 'E#', | |
'CLEAR' : 'E$', | |
'CLEARFR' : 'E$$$$', | |
'BLANK' : 'A\x0fET00', | |
'TEST' : 'A\x0fET0012:00 AM', | |
'SEND_STRING' : 'AA' | |
} | |
self._socket = socket.create_connection((ipAddress,port)) | |
def restore(self): | |
self._socket.send(self._cmdHeader + self._command['RESTORE'] + self._end) | |
def clear(self): | |
self._socket.send(self._cmdHeader + self._command['CLEAR'] + self._end) | |
def clearFR(self): | |
self._socket.send(self._cmdHeader + self._command['CLEARFR'] + self._end) | |
def blank(self): | |
self._socket.send(self._cmdHeader + self._command['BLANK'] + self._end) | |
def test(self): | |
self.clear() | |
self._socket.send(self._cmdHeader + self._command['TEST'] + self._end) | |
def send(self, string): | |
self.clear() #comment this out if you want to "stack" subsequent sent messages in rotation | |
self._socket.send(self._cmdHeader + self._command['SEND_STRING'] + string + self._end) | |
def close(self): | |
self._socket.close() | |
if __name__ == '__main__': | |
#read args and build the wallboard object | |
(ipAddress,port,signAddress,msgType,custMsg) = readArgs() | |
myBoard = Wallboard(ipAddress,port,signAddress) | |
#send the message specified by arguments | |
if msgType == 'RESTORE': myBoard.restore() | |
if msgType == 'CLEAR': myBoard.clear() | |
if msgType == 'CLEARFR': myBoard.clearFR() | |
if msgType == 'BLANK': myBoard.blank() | |
if msgType == 'TEST': myBoard.test() | |
if custMsg is not None: myBoard.send(custMsg) | |
#a little console output, and finish | |
print msgType +' message sent to wallboard @ ip: '+ipAddress+' port: '+port+' sign address: '+signAddress | |
if custMsg is not None: print 'Custom Message: ' + custMsg | |
myBoard.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment