-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
54 lines (46 loc) · 1.43 KB
/
__main__.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
# Standard library imports
import json
import random
import datetime
import websockets
import asyncio
import configparser
import functools
import sys
import miio
# Reader imports
from miio import Walkingpad, DeviceException
from miio.walkingpad import (
OperationMode,
OperationSensitivity,
WalkingpadException,
WalkingpadStatus,
)
async def getWalkingPadState(walkingpad):
state = walkingpad.status()
v['sp'] = state.speed
v['step'] = state.step_count
v['cal'] = state.calories
v['time'] = state.walking_time
v['dist'] = state.distance
return v
async def mainLoop(websocket, path, walkingpad):
while True:
values = await getWalkingPadState(walkingpad)
await websocket.send(str(json.dumps(values)))
await asyncio.sleep(1)
def main():
parser = configparser.ConfigParser()
parser.read_file(open(r'config.txt'))
CONFIG_TOKEN = parser.get('walking-pad-twitch', 'token')
CONFIG_IP = parser.get('walking-pad-twitch', 'ip')
print("Config loaded...")
print("Walkingpad connecting...")
walkingpad = Walkingpad(CONFIG_IP, CONFIG_TOKEN)
main_loop_handler = functools.partial(mainLoop, walkingpad=walkingpad)
start_server = websockets.serve(main_loop_handler, 'localhost', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
print("Websocket Server started...")
if __name__ == "__main__":
main()