-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
54 lines (47 loc) · 1.61 KB
/
__init__.py
File metadata and controls
54 lines (47 loc) · 1.61 KB
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
import urllib.request
import numpy as np
import requests
import time
import cv2
import os
if not os.environ.get('TELEGRAM_BOT_TOKEN'):
print('Please set the TELEGRAM_BOT_TOKEN environment variable')
exit(1)
def telegram_bot_sendtext(bot_message):
bot_token = os.environ.get('TELEGRAM_BOT_TOKEN')
bot_chatID = '@olkkaristatus'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
def get_image():
arr = np.zeros((1,1))
try:
req = urllib.request.urlopen(f'https://athene.fi/olocam/latest.jpg?{int(round(time.time() * 1000))}')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
return cv2.cvtColor(cv2.imdecode(arr, -1), cv2.COLOR_BGR2GRAY)
except:
return np.zeros((1,1))
def loop():
last_open = False
diff_count = 0
while True:
img = get_image()
if np.mean(img) > 90:
if not last_open:
diff_count += 1
if diff_count > 5:
print('Open')
telegram_bot_sendtext('🟢🟢🟢 now *open* 🟢🟢🟢')
last_open = True
diff_count = 0
else:
if last_open:
diff_count += 1
if diff_count > 5:
print('Closed')
telegram_bot_sendtext('❌❌ now *closed* ❌❌')
last_open = False
diff_count = 0
time.sleep(5)
if __name__ == '__main__':
loop()