forked from satcar77/miband4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiband4_http_api.py
109 lines (92 loc) · 3.11 KB
/
miband4_http_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import time
from flask import Flask
from flask_restful import Api, Resource, reqparse
# !/usr/bin/env python3
# This script demonstrates the usage, capability and features of the library.
import argparse
from bluepy.btle import BTLEDisconnectError
from cursesmenu import *
from cursesmenu.items import *
from miband import miband
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--mac', required=False, help='Set mac address of the device')
parser.add_argument('-k', '--authkey', required=False, help='Set Auth Key for the device')
args = parser.parse_args()
# Try to obtain MAC from the file
try:
with open("mac.txt", "r") as f:
mac_from_file = f.read().strip()
except FileNotFoundError:
mac_from_file = None
# Use appropriate MAC
if args.mac:
MAC_ADDR = args.mac
elif mac_from_file:
MAC_ADDR = mac_from_file
else:
print("Error:")
print(" Please specify MAC address of the MiBand")
print(" Pass the --mac option with MAC address or put your MAC to 'mac.txt' file")
print(" Example of the MAC: a1:c2:3d:4e:f5:6a")
exit(1)
# Validate MAC address
if 1 < len(MAC_ADDR) != 17:
print("Error:")
print(" Your MAC length is not 17, please check the format")
print(" Example of the MAC: a1:c2:3d:4e:f5:6a")
exit(1)
# Try to obtain Auth Key from file
try:
with open("auth_key.txt", "r") as f:
auth_key_from_file = f.read().strip()
except FileNotFoundError:
auth_key_from_file = None
# Use appropriate Auth Key
if args.authkey:
AUTH_KEY = args.authkey
elif auth_key_from_file:
AUTH_KEY = auth_key_from_file
else:
print("Warning:")
print(
" To use additional features of this script please put your Auth Key to 'auth_key.txt' or pass the --authkey option with your Auth Key")
print()
AUTH_KEY = None
# Validate Auth Key
if AUTH_KEY:
if 1 < len(AUTH_KEY) != 32:
print("Error:")
print(" Your AUTH KEY length is not 32, please check the format")
print(" Example of the Auth Key: 8fa9b42078627a654d22beff985655db")
exit(1)
# Convert Auth Key from hex to byte format
if AUTH_KEY:
AUTH_KEY = bytes.fromhex(AUTH_KEY)
class MiBand4_Http_Api(Resource):
def get(self):
success = False
attempts = 0
while attempts < 3 and not success:
try:
if (AUTH_KEY):
band = miband(MAC_ADDR, AUTH_KEY, debug=True)
success = band.initialize()
steps = band.get_steps()
band.disconnect()
return steps, 200
except BTLEDisconnectError:
attempts += 1
if attempts == 3:
return 'Connection to the MIBand failed', 500
print('Connection to the MIBand failed. Trying out again in 3 seconds')
time.sleep(3)
continue
if __name__ == '__main__':
app = Flask(__name__)
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
api = Api(app)
api.add_resource(MiBand4_Http_Api, "/getMiBandStat")
app.run(
host='0.0.0.0',
port=90,
debug=True)