Created
June 10, 2012 11:18
-
-
Save jdecaron/2904994 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
import serial | |
import socket | |
import string | |
import struct | |
import zlib | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.bind(('', 7040)) | |
mreq = struct.pack("=4sl", socket.inet_aton("230.0.0.1"), socket.INADDR_ANY) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) | |
arduino = serial.Serial("COM5") | |
def cut(s): | |
a = [] | |
while len(s) > 190 - 1: | |
a.append(s[0:190-1]) | |
s = s[190:len(s)] | |
if len(s) > 0: | |
a.append(s) | |
return a | |
def extract_skillset(s): | |
s = s[127:len(s)] | |
p = string.find(s, "\x00\x00") | |
if p != -1 and p > 0: | |
s = s[0:p] | |
s = string.replace(s, "\x00", "") | |
return s | |
def extract_stats(s): | |
#21 | |
return s[21] | |
def is_client(client): | |
clients = ['AL_PMG', 'NOVELIS'] | |
for c in clients: | |
if string.find(client, c) >= 0: | |
return 1 | |
return 0 | |
def remove_first_characters(s): | |
c = 0 | |
while s[c] == "\x00": | |
c = c + 1 | |
return s[c:len(s)] | |
c = 0 | |
while True: | |
try: | |
s = sock.recv(16384) | |
#f = open(str(c), 'w') | |
s = zlib.decompress(s[72:len(s)]) | |
s2 = s | |
s = remove_first_characters(s) | |
s = cut(s) | |
clients = 0 | |
found_skillsets = {} | |
for i in s: | |
skillset = extract_skillset(i) | |
if is_client(skillset): | |
stats = int(ord(extract_stats(i))) | |
if stats >= 1: | |
print skillset | |
if string.find(skillset, 'AL_PMG') >= 0: | |
found_skillsets['AL_PMG'] = 'x' | |
elif string.find(skillset, 'NOVELIS') >= 0: | |
found_skillsets['NOVELIS'] = 'y' | |
print found_skillsets | |
if len(found_skillsets) == 2: | |
clients = 'z' | |
else: | |
for i in found_skillsets: | |
clients = found_skillsets[i] | |
arduino.write(clients) | |
#f.write(s2) | |
#f.close() | |
c = c + 1 | |
except: | |
print "Error: UDP or ZLIB." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment