Created
May 10, 2016 01:58
-
-
Save asaga/c9273fcf69c4a6077c8c1e83b838f2fa to your computer and use it in GitHub Desktop.
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
#coding: utf-8 | |
# spi, time ライブラリをインポート | |
import spidev | |
import time | |
# センサーデータのJSON化のためにインポート | |
import json | |
from datetime import datetime | |
# HTTPリクエスト生成のためにインポート | |
import requests | |
import time | |
import math | |
# SpiDev オブジェクトのインスタンスを生成 | |
spi = spidev.SpiDev() | |
# ポート0、デバイス0のSPI をオープン | |
spi.open(0, 0) | |
# 最大クロックスピードを1MHz に設定 | |
spi.max_speed_hz=1000000 | |
# 1 ワードあたり8ビットに設定 | |
spi.bits_per_word=8 | |
# ダミーデータを設定(1111 1111) | |
dummy = 0xff | |
# スタートビットを設定(0100 0111) | |
start = 0x47 | |
# シングルエンドモードを設定 (0010 0000) | |
sgl = 0x20 | |
# ch0 を選択(0000 0000) | |
ch0 = 0x00 | |
# ch1 を選択(0001 0000) | |
ch1 = 0x10 | |
# MSB ファーストモードを選択(0000 1000) | |
msbf = 0x08 | |
# IC からデータを取得する関数を定義 | |
def measure(ch): | |
# SPI インターフェイスでデータの送受信を行う | |
ad = spi.xfer2( [ (start + sgl + ch + msbf), dummy ] ) | |
# | |
val = ((ad[0] & 0x03) << 8) + ad[1] | |
# 受信した2バイトのデータを10 ビットデータにまとめる | |
voltage = ( val * 3.3 ) / 1023 | |
# 結果を返す | |
return val, voltage | |
API_TOKEN = '' | |
APP_ID = 999 | |
SUBDOMAIN = '' | |
# 例外を検して終了 | |
try: | |
# レコード取得時のヘッダー | |
headers_get = {'X-Cybozu-API-Token': API_TOKEN} | |
# レコード登録・削除時のヘッダー | |
headers = {'X-Cybozu-API-Token': API_TOKEN, 'Content-Type': 'application/json'} | |
# 無限ループ | |
while 1: | |
# 関数を呼び出してch1 のデータを取得 | |
ch1_val, ch1_voltage = measure(ch1) | |
# 照度が10未満になったら全レコード削除 | |
if ch1_val < 10: | |
print("レコード削除実行") | |
# レコード件数を取得 | |
payload = {'app': APP_ID, 'query': 'order by $id asc limit 1', 'fields': ["$id"], 'totalCount': 'true'} | |
r = requests.get('https://' + SUBDOMAIN + '.cybozu.com/k/v1/records.json', params=payload, headers=headers_get) | |
print(r.text) | |
jdata = json.loads(r.text) | |
total = int(jdata["totalCount"]) | |
print("レコード数:" + str(total)) | |
# kintoneから全レコードを取得 | |
limit = 100 #削除APIのデフォルト値 | |
offset = 0 | |
# limitからAPIの実行回数を計算 | |
runcount = int(math.ceil(float(total) / limit)) | |
print("削除API実行回数:" + str(runcount)) | |
count = 0 | |
while count < runcount: | |
payload_get = {'app': APP_ID, 'query': 'limit ' + str(limit), 'fields': ["$id"]} | |
print(payload_get) | |
r = requests.get('https://' + SUBDOMAIN + '.cybozu.com/k/v1/records.json', params=payload_get, headers=headers_get) | |
print(r.text) | |
jdata = json.loads(r.text) | |
# 取得したレコード番号を配列に入れる | |
idlist = [] | |
for _id in jdata["records"]: | |
idlist.append(_id["$id"]["value"]) | |
# 配列に対してレコード削除 | |
headers_del = {'X-Cybozu-API-Token': API_TOKEN, 'Content-Type': 'application/json'} | |
payload_del = {'app': APP_ID, 'ids': idlist } | |
print(payload_del) | |
r = requests.delete('https://' + SUBDOMAIN + '.cybozu.com/k/v1/records.json', data=json.dumps(payload_del), headers=headers) | |
print(r.text) | |
count += 1 | |
else: | |
print("レコード登録実行") | |
# kintoneへの送信 | |
headers_post = {'X-Cybozu-API-Token': API_TOKEN, 'Content-Type': 'application/json'} | |
data = {"time_sensor": {'value': datetime.now().strftime('%Y-%m-%dT%H:%M:%S+09:00')}, "ID": {'value': "id000"}, "brightness": {'value': ch1_val}} | |
payload = {'app': APP_ID, 'record': data } | |
r = requests.post('https://' + SUBDOMAIN + '.cybozu.com/k/v1/record.json', data=json.dumps(payload), headers=headers) | |
print(payload) | |
print(r.text) | |
# 3秒待つ | |
time.sleep(3) | |
# キーボード例外を検出 | |
except KeyboardInterrupt: | |
# 何も処理をしない | |
pass | |
# SPI を開放 | |
spi.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment