import requests
import json

def prepare(settings):

	if settings["dhcp"] == 1.000000000:
		settings["dhcp"] = "on"
	if settings["dhcp"] == 0:
		settings["dhcp"] = "off"

	if settings["pgw_en"] == True:
		settings["pgw_en"] = "on"
	elif settings["pgw_en"] == False:
		settings["pgw_en"] = "off"

	print settings

	post_data = "&read_community=" 		+ settings["read_community"] + \
		"&write_community=" 			+ settings["write_community"] + \
		"&managerIP=" 					+ settings["managerIP"] + \
		"&managerIP2=" 					+ settings["managerIP2"] + \
		"&managerIP3=" 					+ settings["managerIP3"] + \
		"&managerIP4=" 					+ settings["managerIP4"] + \
		"&managerIP5=" 					+ settings["managerIP5"] + \
		"&di1=" 						+ settings["di1"] + \
		"&di_name1=" 					+ settings["di_name1"] + \
		"&di2=" 						+ settings["di2"] + \
		"&di_name2=" 					+ settings["di_name2"] + \
		"&type_ts1=" 					+ str(settings["type_ts1"]) + \
		"&type_ts2=" 					+ str(settings["type_ts2"]) + \
		"&ipaddr=" 						+ settings["ipaddr"] + \
		"&gw=" 							+ settings["gw"] + \
		"&mask=" 						+ settings["mask"] + \
		"&dhcp=" 						+ settings["dhcp"] + \
		"&pgw_en=" 						+ settings["pgw_en"] + \
		"&pgw_port=" 					+ settings["pgw_port"] + \
		"&pgw_baud=" 					+ str(settings["pgw_baud"]) + \
		"&pgw_par=" 					+ str(settings["pgw_par"]) + \
		"&pgw_ndata=" 					+ str(settings["pgw_ndata"]) + \
		"&pgw_nstop=" 					+ str(settings["pgw_nstop"]) + \
		"&ntp=" 						+ settings["ntp"] + \
		"&ntpservip=" 					+ settings["ntpservip"] + \
		"&ups_cell_min=" 				+ settings["ups_cell_min"] + \
		"&ups_cell_max=" 				+ settings["ups_cell_max"] + \
		"&mainvolt_high=" 				+ settings["mainvolt_high"] + \
		"&mainvolt_low=" 				+ settings["mainvolt_low"] + \
		"&mainvolt_hist=" 				+ settings["mainvolt_hist"] + \
		"&temp_high=" 					+ settings["temp_high"] + \
		"&temp_low=" 					+ settings["temp_low"] + \
		"&temp_hist=" 					+ settings["temp_hist"] + \
		"&case_temp_high=" 				+ settings["case_temp_high"] + \
		"&case_temp_low=" 				+ settings["case_temp_low"] + \
		"&case_temp_hist=" 				+ settings["case_temp_hist"]

	return post_data


def get(ip, session):
	r = session.get("http://" + ip + "/settings.cgi", cookies = session.cookies, timeout = 1)
	
	if r.status_code != 200:
		return None
	try:
		return r.json()
	except:
		return None


def post(ip, session, post_data):
	r = session.post("http://" + ip + "/settings.cgi", cookies = session.cookies, data=post_data, timeout = 1)
	if r.status_code != 200:
		return False
	
	return True

def login(ip):
	s = requests.Session()
	data = {"login":"admin", "password":"12345"}
	r = s.post("http://" + ip + "/login.cgi", data)
	if r.status_code != 200:
		return None
	
	return s


def change(ip, session, baudrate = None, bytesize = None, parity = None, stopbits = None, enable = True):
	params = get(ip, session)
	if params == None:
		return False

	#Enable by default
	params["pgw_en"] = enable

	params["pgw_baud"] = baudrate
	params["pgw_ndata"] = bytesize

	if parity == 'N':
		params["pgw_par"] = "no"
	if parity == 'E':
		params["pgw_par"] = "ev"
	if parity == 'O':
		params["pgw_par"] = "od"

	params["pgw_nstop"] = stopbits

	post(ip, session, prepare(params))

	return True