|
| 1 | +import sys |
| 2 | +import time |
| 3 | +import json |
| 4 | +import httplib |
| 5 | + |
| 6 | +HOST = 'iot.espressif.cn' |
| 7 | +USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36' |
| 8 | +ACCEPT = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' |
| 9 | + |
| 10 | +PATH = '/v1/datastreams/plug-status/datapoint/?deliver_to_device=true' |
| 11 | +DATASTREAM = 'plug-status' |
| 12 | +OWNER_KEY = 'd3b2f1d0e308c42515adb328973c56c16dbc5872' |
| 13 | +ON = {"datapoint":{"x":1}} |
| 14 | +OFF = {"datapoint":{"x":0}} |
| 15 | +GET, POST = 'GET', 'POST' |
| 16 | +LOOP = 100 |
| 17 | +PERIOD = 3 |
| 18 | + |
| 19 | +global total_count, failed_count, on_failed, off_failed, query_failed |
| 20 | +total_count, failed_count, on_failed, off_failed, query_failed = 0, 0, 0, 0, 0 |
| 21 | + |
| 22 | +def get_rest_resp(method, path, params, token=None): |
| 23 | + if token is None: |
| 24 | + token = '' |
| 25 | + if isinstance(params, dict): |
| 26 | + params = json.dumps(params) |
| 27 | + headers = {'Host': HOST, 'User-Agent': USER_AGENT, 'Accept': ACCEPT, 'Authorization': 'token %s' % token} |
| 28 | + conn = httplib.HTTPConnection(HOST, port=80) |
| 29 | + try: |
| 30 | + conn.request(method, path, params, headers) |
| 31 | + response = conn.getresponse() |
| 32 | + data = response.read() |
| 33 | + return json.loads(data), True |
| 34 | + finally: |
| 35 | + conn.close() |
| 36 | + return None, False |
| 37 | + |
| 38 | +def on(): |
| 39 | + resp, r = get_rest_resp(POST, PATH, ON, OWNER_KEY) |
| 40 | + if not r: |
| 41 | + return False |
| 42 | + return resp['status'] == 200 |
| 43 | + |
| 44 | +def off(): |
| 45 | + resp, r = get_rest_resp(POST, PATH, OFF, OWNER_KEY) |
| 46 | + if not r: |
| 47 | + return False |
| 48 | + return resp['status'] == 200 |
| 49 | + |
| 50 | +def query(): |
| 51 | + resp, r = get_rest_resp(GET, PATH, '', OWNER_KEY) |
| 52 | + if not r or resp['status'] != 200: |
| 53 | + return -1 |
| 54 | + return resp['datapoint']['x'] |
| 55 | + |
| 56 | +def test(): |
| 57 | + global total_count, failed_count, on_failed, off_failed, query_failed |
| 58 | + if not on(): |
| 59 | + on_failed += 1 |
| 60 | + return False |
| 61 | + time.sleep(1) |
| 62 | + status = query() |
| 63 | + if status != 1: |
| 64 | + print 'status != 1' |
| 65 | + query_failed += 1 |
| 66 | + return False |
| 67 | + if not off(): |
| 68 | + off_failed += 1 |
| 69 | + return False |
| 70 | + status = query() |
| 71 | + if status != 0: |
| 72 | + query_failed += 1 |
| 73 | + return False |
| 74 | + return True |
| 75 | + |
| 76 | +l = 0 |
| 77 | +while l < LOOP: |
| 78 | + time.sleep(PERIOD) |
| 79 | + total_count += 1 |
| 80 | + r = test() |
| 81 | + if not r: |
| 82 | + failed_count += 1 |
| 83 | + percent = int((1-failed_count*1.0/total_count)*10000) / 100.0 |
| 84 | + log = "\rtotal_count: %s, failed_count:%s, percent: %s%%, on_failed: %s, off_failed: %s, query_failed: %s" % (total_count, failed_count, percent, on_failed, off_failed, query_failed) |
| 85 | + sys.stdout.write(log) |
| 86 | + sys.stdout.flush() |
| 87 | + l += 1 |
0 commit comments