import os
import sys
import string
import base64
import httplib
from httplib import *

def parse_xml(string, substring):
    output = ""
    retA = string.find("<" + substring + ">")
    if retA > 0:
        retB = string.find("</" + substring + ">")
        output = string[retA + len(substring) + 2:retB]
    
    return output  


targetlist = []
update_list = []
msg_part = "/ws/DeviceCore/?condition=devConnectwareId="
idigi_addr = "app.idigi.com"
USERNAME = "admin"
PASSWORD = "!idigiadmin!"
USERNAMEB = "tkim"
PASSWORDB = "password"

fd = open("iplist.txt", 'r')
data = fd.read()
targetlist = data.split('\n')
fd.close()

fd = open("output.txt", "w")

count = 0
auth = base64.b64encode("%s:%s" % (USERNAME, PASSWORD))

for ip in targetlist:
    if ip == "":
        continue
    
    count = count + 1
    
    print "[" + str(count) + "] Working on: " + ip
    
    ipfd = open("c:\\logs\\" + ip + ".txt", 'r')
    data = ipfd.read()
    data_lines = data.split('\n')
    
    mac_str = ""
    
    for blob in data_lines:
        retval = blob.find("00:40:9D")
        
        if retval > 0:
            mac_str = blob[retval + 9: retval + 17]
            bleh_list = mac_str.split(':')
            mac_str = ""
            for meh in bleh_list:
                mac_str = mac_str + meh
            break
    
    if mac_str == "":
        writeline = "(" + ip + ")\tUnable to Find MAC!\n"
        fd.write(writeline)
        continue
    
    dev_id = "00000000-00000000-00409DFF-FF" + mac_str
    
    connection = httplib.HTTPConnection(idigi_addr)
    connection.putrequest("GET", msg_part + "'" + dev_id + "'")
    connection.putheader("Authorization", "Basic " + auth)
    connection.endheaders()
    connection.send("")
    response = connection.getresponse()
    data = response.read()
    connection.close()
    
    result = parse_xml(data, "resultTotalRows")
    
    if result == "" or result == "0":
        writeline = "(" + ip + ")\tUnable to Find Device ID on iDigi!\n"
        fd.write(writeline)
        update_list.append(dev_id)
        #add it against the Nalco account???
    else:
        writeline = "(" + ip + ")\tDevice ID on iDigi!\n"
        fd.write(writeline)

fd.close()

auth = base64.b64encode("%s:%s" % (USERNAMEB, PASSWORDB))

count = 0
for dev in update_list:
    count = count + 1
    print "[" + str(count) + "] Adding: " + dev
    msg = "<DeviceCore><devConnectwareId>" + dev + "</devConnectwareId></DeviceCore>"
    connection = httplib.HTTPConnection(idigi_addr)
    connection.putrequest("POST", "/ws/DeviceCore")
    connection.putheader("Authorization", "Basic " + auth)
    connection.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
    connection.putheader("Content-length", "%d" % len(msg))    
    connection.endheaders()
    connection.send(msg)
    resp = connection.getresponse()
    respdata = resp.read()
    connection.close()
    
    retA = respdata.find("<error")
    if retA >= 0: #error condition
        retB = respdata.find("</error>")
        print "Error " + respdata[retA:retB+8]

print "Complete"
