Created
June 27, 2020 06:05
-
-
Save iseebi/b26da36e501dcefe9c83cfab3fb7e98a to your computer and use it in GitHub Desktop.
SwitchBotをHTTP経由で叩けるようにする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from BaseHTTPServer import BaseHTTPRequestHandler | |
import SimpleHTTPServer | |
import SocketServer | |
import binascii | |
from bluepy.btle import Peripheral, ADDR_TYPE_RANDOM | |
SWITCHBOT_BLE_ADDR = "00:00:00:00:00:00" # Set your SwitchBot BLE Address | |
PORT = 8121 | |
class DehumidifierRequestHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
r = [] | |
code = 200 | |
try: | |
p = Peripheral(SWITCHBOT_BLE_ADDR, ADDR_TYPE_RANDOM) | |
hand_service = p.getServiceByUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b") | |
hand = hand_service.getCharacteristics("cba20002-224d-11e6-9fb8-0002a5d5c51b")[0] | |
hand.write(binascii.a2b_hex("570100")) | |
p.disconnect() | |
r.append(u'{"result":true}') | |
except: | |
code = 500 | |
r.append(u'{"result":false}') | |
self.send_response(code) | |
self.send_header('Content-Type', 'application/json') | |
self.end_headers() | |
self.wfile.write('\n'.join(r)) | |
handler = DehumidifierRequestHandler | |
httpd = SocketServer.TCPServer(("", PORT), handler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment