#!/usr/bin/python3
#-*- coding: utf-8 -*-

import urllib.parse
import json
import base64
import requests
import logging
class Network():
    LOGIN_URL = 'http://192.168.211.101/portal/pws?t=li'
    BEAT_URL = 'http://192.168.211.101/portal/page/doHeartBeat.jsp'
    COMMON_HERADERS = {
    'Accept-Language': 'en-US',
    'Accept': 'text/html'
    }
    def __init__(self, username, password):
        b64Password = base64.b64encode(bytes(password,'utf8'))
        self.data = {'userName': username, 'userPwd': b64Password}

    def login(self):
        logging.info('login:%s'%(self.data))
        response = requests.post(Network.LOGIN_URL, data=self.data,
         headers=Network.COMMON_HERADERS, timeout=3)
        responseText = base64.b64decode(response.text + '==')
        responseJson = urllib.parse.unquote(responseText.decode('utf8'))
        jsonDict = json.loads(responseJson)
        heartBeatCyc = jsonDict.get('heartBeatCyc')
        if heartBeatCyc == None:
            raise BaseException(responseJson)
        logging.info('login seccuss: %s'%(responseJson))
        self.heartBeatCyc = int(heartBeatCyc)
        self.serialNo = jsonDict.get('serialNo')
        return self.heartBeatCyc

    def beat(self):
        response = requests.post(Network.BEAT_URL, data={'serialNo': self.serialNo},
         headers=Network.COMMON_HERADERS, timeout=3)
        if response.text.find('v_failedTimes') is -1:
            raise BaseException(response.text)

[(21, 'logging.info', 'logging.info', (["('login:%s' % self.data)"], {}), False, 'import logging\n'), (22, 'requests.post', 'requests.post', (['Network.LOGIN_URL'], {'data': 'self.data', 'headers': 'Network.COMMON_HERADERS', 'timeout': '(3)'}), False, 'import requests\n'), (24, 'base64.b64decode', 'base64.b64decode', (["(response.text + '==')"], {}), False, 'import base64\n'), (26, 'json.loads', 'json.loads', (['responseJson'], {}), False, 'import json\n'), (30, 'logging.info', 'logging.info', (["('login seccuss: %s' % responseJson)"], {}), False, 'import logging\n'), (36, 'requests.post', 'requests.post', (['Network.BEAT_URL'], {'data': "{'serialNo': self.serialNo}", 'headers': 'Network.COMMON_HERADERS', 'timeout': '(3)'}), False, 'import requests\n')]