forked from Shmoopty/rpi-appliance-monitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvibration.py
executable file
·189 lines (159 loc) · 5.91 KB
/
vibration.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import sys
import time
import threading
import RPi.GPIO as GPIO
import requests
import json
import tweepy
from time import localtime, strftime
import urllib
from ConfigParser import SafeConfigParser
from tweepy import OAuthHandler as TweetHandler
from slackclient import SlackClient
def pushbullet(cfg, msg):
try:
data_send = {"type": "note", "title": "VibrationBot", "body": msg}
requests.post(
'https://api.pushbullet.com/v2/pushes',
data=json.dumps(data_send),
headers={'Authorization': 'Bearer ' + cfg,
'Content-Type': 'application/json'})
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
def pushover(user_key, app_key, msg):
try:
data_send = {"user": user_key, "token": app_key, "message": msg}
requests.post(
'https://api.pushover.net/1/messages.json',
data=json.dumps(data_send),
headers={'Content-Type': 'application/json'})
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
def iftt(msg):
try:
iftt_url = "https://maker.ifttt.com/trigger/{}/with/key/{}".format(iftt_maker_channel_event,
iftt_maker_channel_key)
report = {"value1" : msg}
resp = requests.post(iftt_url, data=report)
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
def slack_webhook(msg):
try:
payload = urllib.urlencode({'payload': '{"text": "' + msg+ '"}'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
response = requests.request("POST", slack_webhook , data=payload, headers=headers)
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
def tweet(msg):
try:
# Twitter is the only API that NEEDS something like a timestamp,
# since it will reject identical tweets.
tweet = msg + ' ' + strftime("%Y-%m-%d %H:%M:%S", localtime())
auth = TweetHandler(twitter_api_key, twitter_api_secret)
auth.set_access_token(twitter_access_token,
twitter_access_token_secret)
tweepy.API(auth).update_status(status=tweet)
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
def slack(msg):
try:
slack = msg + ' ' + strftime("%Y-%m-%d %H:%M:%S", localtime())
sc = SlackClient(slack_api_token)
sc.api_call(
'chat.postMessage', channel='#random', text=slack)
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
def send_alert(message):
if len(message) > 1:
print message
if len(pushover_user_key) > 0 and len(pushover_app_key) > 0:
pushover(pushover_user_key, pushover_app_key, message)
if len(pushbullet_api_key) > 0:
pushbullet(pushbullet_api_key, message)
if len(pushbullet_api_key2) > 0:
pushbullet(pushbullet_api_key2, message)
if len(twitter_api_key) > 0:
tweet(message)
if len(slack_api_token) > 0:
slack(message)
if len (slack_webhook) > 0:
slack_webhook(message)
if len(iftt_maker_channel_key) > 0:
iftt(message)
def send_appliance_active_message():
send_alert(start_message)
global appliance_active
appliance_active = True
def send_appliance_inactive_message():
send_alert(end_message)
global appliance_active
appliance_active = False
def vibrated(x):
global vibrating
global last_vibration_time
global start_vibration_time
print 'Vibrated'
last_vibration_time = time.time()
if not vibrating:
start_vibration_time = last_vibration_time
vibrating = True
def heartbeat():
current_time = time.time()
print "HB at {}".format(current_time)
global vibrating
delta_vibration = last_vibration_time - start_vibration_time
if (vibrating and delta_vibration > begin_seconds
and not appliance_active):
send_appliance_active_message()
if (not vibrating and appliance_active
and current_time - last_vibration_time > end_seconds):
send_appliance_inactive_message()
vibrating = current_time - last_vibration_time < 2
threading.Timer(1, heartbeat).start()
if len(sys.argv) == 1:
print "No config file specified"
sys.exit()
vibrating = False
appliance_active = False
last_vibration_time = time.time()
start_vibration_time = last_vibration_time
config = SafeConfigParser()
config.read(sys.argv[1])
sensor_pin = config.getint('main', 'SENSOR_PIN')
begin_seconds = config.getint('main', 'SECONDS_TO_START')
end_seconds = config.getint('main', 'SECONDS_TO_END')
pushbullet_api_key = config.get('pushbullet', 'API_KEY')
pushover_user_key = config.get('pushover', 'user_api_key')
pushover_app_key = config.get('pushover', 'app_api_key')
pushbullet_api_key2 = config.get('pushbullet', 'API_KEY2')
start_message = config.get('main', 'START_MESSAGE')
end_message = config.get('main', 'END_MESSAGE')
twitter_api_key = config.get('twitter', 'api_key')
twitter_api_secret = config.get('twitter', 'api_secret')
twitter_access_token = config.get('twitter', 'access_token')
twitter_access_token_secret = config.get('twitter', 'access_token_secret')
slack_api_token = config.get('slack', 'api_token')
slack_webhook = config.get('slack','webhook_url')
iftt_maker_channel_event = config.get('iftt','maker_channel_event')
iftt_maker_channel_key = config.get('iftt','maker_channel_key')
send_alert(config.get('main', 'BOOT_MESSAGE'))
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(sensor_pin, GPIO.RISING)
GPIO.add_event_callback(sensor_pin, vibrated)
print 'Running config file {} monitoring GPIO pin {}'\
.format(sys.argv[1], str(sensor_pin))
threading.Timer(1, heartbeat).start()