#!/usr/bin/python
#-*-coding:utf-8-*-
import httplib,urllib;
import json
import os
import sys
import time
import random
host = "127.0.0.1:2123"

NetType_Wifi = 1
NetType_2G   = 2
NetType_3G   = 3
NetType_4G   = 4
NetType_Wire = 5
SysType_Andriod = 1
SysType_IOS     = 2
Systype_PC    = 3

def active_room(conn, headers, roomid):
  body = {}

  js = {}
  js["100"] = "1"
  js["101"] = "2"
  js["102"] = "3"

  body["roomid"] = int(roomid)
  body["active_time"] = int(time.time()) + 3600*3
  body["teachers"] = str(js).replace("'","\"")
  body["max_count"] = 100
  
  pa = json.dumps(body) 
  print "post: ", pa
  conn.request(method="POST", url="/active_room", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "发布成功!";
  else:
      print "发布失败";
  print "data:",data
  d = eval(data)
  print d

def enter_room(conn, headers, roomid, uid):
  body = {}

  client = {}
  client["brand"] = "Xiaomi"
  client["model"] = "MI 2"
  client["ver"]   = 200100
  client["net_type"] = NetType_Wifi
  client["sys_type"] = SysType_IOS

  body["roomid"] = roomid
  body["uid"] = uid
  body["ip"] = "127.0.0.3"
  body["data"] = str(client).replace("'","\"")
  
  pa = json.dumps(body) 

  print "post: ", pa
  conn.request(method="POST", url="/enter_room", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "发布成功!";
  else:
      print "发布失败";
  print "data:",data
  d = eval(data)
  print d

def set_room(conn, headers, status, num):
  body = {}

  body["roomid"] = 10001
  body["status"] = status
  body["max_count"] = num
  
  pa = json.dumps(body) 

  print "post: ", pa
  conn.request(method="POST", url="/set_room", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "发布成功!";
  else:
      print "发布失败";
  print "data:",data
  d = eval(data)
  print d

def get_roomlist(conn, headers):
  body = {}  
  pa = json.dumps(body) 

  print "post: ", pa
  conn.request(method="POST", url="/get_roomlist", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "发布成功!";
  else:
      print "发布失败";
  print "data:",data
  d = eval(data)
  print d

def update_mgr(conn, headers):
  pass

def tester(argv1, argv2):
  me = argv1
  headers = {"Content-Type":"application/json;charset=utf-8", "Connection":"Keep-Alive"}
  conn = httplib.HTTPConnection(host)
  if me == "open":
    active_room(conn, headers, argv2)
  elif me == "enter":
    enter_room(conn, headers, 10001, 101)
  elif me == "lock" or me == "unlock" or me == "close":
    set_room(conn, headers, me, argv2)
  elif me == "update":
    update_mgr(conn, headers)
  elif me == "list":
    get_roomlist(conn, headers)

def looper():
  random.seed(time.time())
  headers = {"Content-Type":"application/json;charset=utf-8", "Connection":"Keep-Alive"}
  conn = httplib.HTTPConnection(host)

  open_dict = {}
  roomid = 10
  status = ["lock", "unlock","close"]
  while True:
    time.sleep(1)
    r = random.randint(0,100)
    if r > 30:
      active_room(conn, headers, roomid)
      roomid += 1
    elif r > 50:
      num = random.randint(20,1000)
      idx = random.randint(0,2)
      set_room(conn, header, status[idx], num)
    elif r > 75:
      uid = random.randint(23,2093)
      enter_room(conn, headers, roomid - 1, uid)
    else:
      get_roomlist(conn, headers)

if __name__ == "__main__":
  
  argv1 = ""
  if len(sys.argv) >= 2:
    argv1 = sys.argv[1]
  argv2 = ""
  if len(sys.argv) > 2:
    argv2 = sys.argv[2]

  #tester(argv1, argv2)
  
  looper()

