import os
import sys
import string

import os.path
DIGI_DIA_ARCHIVE = os.path.join('WEB/python', "dia.zip")
if DIGI_DIA_ARCHIVE not in sys.path:
    sys.path.insert(0, DIGI_DIA_ARCHIVE)
    sys.path.insert(0, os.path.join(DIGI_DIA_ARCHIVE, "src"))
    sys.path.insert(0, os.path.join(DIGI_DIA_ARCHIVE, "lib"))

import urllib2
import rci
from socket import *
import select
import time
import thread

class cooler_controller_settings:
    ip = "192.168.1.2"
    port = 80
    username = "nalco"
    password = "12345"
    dns = "10.160.16.66"
    mail = "usnapsmtp01.nalco.one.net"
    mail_int = "60"

def buildPacket(string):
    retstring = "GET " + string + " HTTP/1.1\r\n"
    retstring = retstring + "Accept: */*\r\n"
    retstring = retstring + "Accept-Language: en-us\r\n"
    retstring = retstring + "Connection: Keep-Alive\r\n\r\n"
    return retstring

def SendRequest(string, ip, port):
    data = ""
    try:
        sock = socket(AF_INET, SOCK_STREAM)
        sock.connect((ip, port))
        packet = buildPacket(string)
        sock.send(packet)
        selectList = [sock]
        receivedEvent = select.select(selectList, [], [], 2)
        for r in receivedEvent:
            if r == []:
                continue
            data = sock.recv(2000)
            break
        sock.close()
    except Exception, e:
        data = "Error: %s" % e
    
    return data  

def rci_cb_setc_settings(input):
    settings = cooler_controller_settings()
    
    if input == "":
        settings.ip = "192.168.1.2"
    else:
        settings.ip = input
    
    #todo, parse input and set it to the settings appropriately??
    
    AUTH_REQUEST = "/Validate.cgi?txtUserName=3DSupport_" + settings.username + \
        "&txtPassword=" + settings.password
    MAIL_REQUEST = "/CommSettings.cgi?mailserver=" + settings.mail
    DNS_REQUEST = "/CommSettings.cgi?dns=" + settings.dns
    EMAIL_REQUEST = "/ControlInit.cgi?datamail_interval=" + settings.mail_int
    REBOOT_REQUEST = "/ControlInit.cgi?reset=1"
    
    response = SendRequest(AUTH_REQUEST, settings.ip, settings.port)
    response = response + SendRequest(MAIL_REQUEST, settings.ip, settings.port)
    response = response + SendRequest(DNS_REQUEST, settings.ip, settings.port)
    response = response + SendRequest(EMAIL_REQUEST, settings.ip, settings.port)
    response = response + SendRequest(REBOOT_REQUEST, settings.ip, settings.port)
    
    return "Complete: " + response

def rci_cb_getc_settings(input):
    if input == "":
        storedip = "192.168.1.2"
    else:
        storedip = input

    dataurl = "http://" + storedip + "/Communications.xml"

    try:
        f = urllib2.urlopen(dataurl)
        data = f.read()
        f.close()
        ret = data.find("<communications>")
        if ret > 0:
            data = data[ret:]
        
    except Exception, e:
        data = "<error>" + "%s" % e
        data = data + "</error>"

    return data

def rci_cb_getb_settings(input):
    if input == "":
        storedip = "192.168.1.2"
    else:
        storedip = input
        
    #todo, parse input and set it to the settings appropriately??
    PORT = 80
    
    VAR_CMD = "/c:/shell_cmd.cgi?vars "
    
    #MAIL_REQUEST = VAR_GET_CMD + "mail1"
    DNS_REQUEST =  VAR_CMD + "dns1"
    #EMAIL_REQUEST = "/ControlInit.cgi?datamail_interval=" + settings.mail_int
    
    response = ""
    #response = response + SendRequest(MAIL_REQUEST, settings.ip, settings.port)
    response = response + SendRequest(DNS_REQUEST, storedip, PORT)
    #response = response + SendRequest(EMAIL_REQUEST, settings.ip, settings.port)
    
    return "Complete: " + response

def rci_cb_setb_settings(input):
    if input == "":
        storedip = "192.168.1.2"
    else:
        storedip = input
        
    #todo, parse input and set it to the settings appropriately??
    PORT = 80
    
    VAR_CMD = "/c:/shell_cmd.cgi?vars "
    DNS_IP = "10.160.16.66"
    
    #MAIL_REQUEST = VAR_GET_CMD + "mail1"
    DNS_REQUEST =  VAR_CMD + "dns1 " + DNS_IP
    #EMAIL_REQUEST = "/ControlInit.cgi?datamail_interval=" + settings.mail_int
    #REBOOT_REQUEST = "/ControlInit.cgi?reset=1"
    
    response = ""
    #response = response + SendRequest(MAIL_REQUEST, settings.ip, settings.port)
    response = response + SendRequest(DNS_REQUEST, storedip, PORT)
    #response = response + SendRequest(EMAIL_REQUEST, settings.ip, settings.port)
    #response = response + SendRequest(REBOOT_REQUEST, settings.ip, settings.port)
    
    return "Complete: " + response

def callback_thread_A():
    rci.add_rci_callback("get_cooler_settings", rci_cb_getc_settings)

def callback_thread_B():
    rci.add_rci_callback("set_cooler_settings", rci_cb_setc_settings)

def callback_thread_C():
    rci.add_rci_callback("get_boiler_settings", rci_cb_getb_settings)

def callback_thread_D():
    rci.add_rci_callback("set_boiler_settings", rci_cb_setb_settings)
    
#meant to be imported
thread.start_new_thread(callback_thread_A, ())
thread.start_new_thread(callback_thread_B, ())
thread.start_new_thread(callback_thread_C, ())
thread.start_new_thread(callback_thread_D, ())
