LoginSignup
0
3

More than 3 years have passed since last update.

pythonを使って玄関のカギを開ける。

Posted at

pythonを使って玄関のカギを開ける。
image.png

image.png

セサミ mini スマートロック + WiFiアクセスポイント 価格¥14,500

初期設定

image.png

Keyをもらう

**https://my.candyhouse.co/
image.png

実際のサンプルプログラム

sesami.py
import requests,json,time
"""
Sesame API Y.Hirata 2020/05/30
"""
class SesamiApi:
    list=[]
    stat=[]
    device_id=""
    task_id=""
    def __init__(self,header):
        self.Header=header
    """GetSesameList"""
    def GetSesameList(self):
        self.list=json.loads(requests.get('https://api.candyhouse.co/public/sesames',headers= {'Authorization':self.Header}).text)
        return self.list
    """GetSesameStatus"""
    def GetSesameStatus(self,nickname):
        self.stat=[]
        if nickname==None:
            for x in self.list:
                self.stat.append(json.loads(requests.get('https://api.candyhouse.co/public/sesame/%s'%x["device_id"],headers= {'Authorization':self.Header}).text))
            return self.stat
        else:
            for x in self.list:
                if nickname==x["nickname"]:
                    self.device_id=x["device_id"]
                    return json.loads(requests.get('https://api.candyhouse.co/public/sesame/%s'%self.device_id,headers={'Authorization':self.Header}).text)
    """Control Sesame Unlock"""
    def ControlSesameUnLock(self):
        headers = {'Authorization': self.Header,'Content-Type': 'application/json'}
        data = '{"command":"unlock"}'
        js = json.loads(requests.post('https://api.candyhouse.co/public/sesame/%s'%self.device_id, headers=headers, data=data).text)
        self.task_id=js['task_id']
        return self.task_id
    """Control Sesame lock"""
    def ControlSesameLock(self):
        headers = {'Authorization': self.Header,'Content-Type': 'application/json'}
        data = '{"command":"lock"}'
        js = json.loads(requests.post('https://api.candyhouse.co/public/sesame/%s'%self.device_id, headers=headers, data=data).text)
        self.task_id=js['task_id']
        return self.task_id

    """Query Execution Result"""
    def QueryExecutionResult(self,task_id):
        params = (('task_id', task_id),)
        return json.loads( requests.get('https://api.candyhouse.co/public/action-result', headers={'Authorization':self.Header}, params=params).text)
##登録してkeyをもらう
key='CzAHJ-OdR7mjH2iehLK8lKU.....O0VVJdeRFZq5nhy6TJ3Jc1KPB.........'
############## Sample program##########################################
s=SesamiApi(key)
res=s.GetSesameList() ##リスト形式で登録してある電気錠の情報を求める
print(res)
res=s.GetSesameStatus("玄関") ##玄関のステータスを求めろ
print(res)
id=s.ControlSesameUnLock() ##開錠する。
print(id)
############## response ################################################
[{"device_id": "ca0001ce-............", "serial": "E7055.....", "nickname": "玄関"}]
{"locked": true, "battery": 100, "responsive": true}
{"task_id": "7c756ae1-ff14-4cb9-a314-d2afe0dec3b2"}
0
3
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3