-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainLoop.py
122 lines (108 loc) · 3.73 KB
/
mainLoop.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
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import pingLib
import blinkLib
import logging
import threading
import ConfigParser
## Log Config
logging.basicConfig(level=logging.DEBUG, #Change it for different log level (debug, info)
format='%(asctime)s :: %(levelname)-8s :: %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='/var/log/mainLoop.log')
## Mail Config (see also mail.conf)
mailConfig = ConfigParser.ConfigParser()
mailConfig.readfp(open(r'mail.conf'))
userMail = mailConfig.get('mailSettings', 'user')
pwMail = mailConfig.get('mailSettings', 'password')
## Check Ping COnfig
pingSleeptime=10 #in sec for the check ping interval
hostPing ="192.168.1.1" # IP host to check
### END CONFIG
global ping1_flag
ping1_flag = 0
global gmail1_flag
global threadOn #Flag to kill or not the threads
threadOn = False
logging.info("Starting Python Script Monitoring daemon")
def pingTest():
global ping1_flag, threadOn
logging.debug("Starting PingTest Thread")
while not threadOn:
try:
resPing= pingLib.check_ping(hostPing)
except:
print("Ping command Failed !")
logging.error('Ping command Failed !')
if resPing == "0": #1=ok 0=Failed
logging.info('Host %s is not reachable',hostPing)
else:
logging.debug('Host %s : OK',hostPing)
ping1_flag = resPing
time.sleep(pingSleeptime)
logging.debug("Exiting PingTest Thread")
def ledNotification():
global ping1_flag, gmail1_flag, threadOn
logging.debug("Starting LedNotif Thread")
time.sleep(6)
while not threadOn:
if ping1_flag == "0":
logging.debug("Ping Flag received: Blinking !")
blinkLib.blink(19,float(0.1))
if ping1_flag == "1" and gmail1_flag > 0:
logging.debug("Gmail flag received: Blinking")
blinkLib.blink(gmail1_flag,2)
time.sleep(10)
logging.debug("Exiting PingTest Thread")
#TY to https://gist.github.com/vadviktor/3529647#file-gmail-py
def gmail_connect(username,password):
import imaplib,re
i=imaplib.IMAP4_SSL('imap.gmail.com')
try:
i.login(username,password)
x,y=i.status('INBOX','(MESSAGES UNSEEN)')
messages=int(re.search('MESSAGES\s+(\d+)',y[0]).group(1))
unseen=int(re.search('UNSEEN\s+(\d+)',y[0]).group(1))-2
return (messages,unseen)
except:
return False,0
def gmail_check():
global gmail1_flag, threadOn
time.sleep(2)
logging.debug("Starting Gmail_check Thread")
while not threadOn:
try:
messages,unread = gmail_connect(userMail,pwMail)
except:
print("Gmail Check command Failed !")
logging.error('Gmail check command Failed !')
if unread == 0:
logging.debug('Gmail Check: No new messages - Total: %i ',messages)
else:
logging.info('Gmail Check: %i new messages ! - Total: %i ',unread,messages)
gmail1_flag = unread
time.sleep(10)
logging.debug("Exiting gmailCheck Thread")
#Da Main loop
if __name__ == '__main__':
try:
print("Press Ctrl-C to quit.")
thread_pingTest = threading.Thread(name='pingTest', target=pingTest)
thread_ledNotif = threading.Thread(name='ledNotification', target=ledNotification)
thread_gmailNotif = threading.Thread(name='gmail_check', target=gmail_check)
thread_pingTest.start() #start threads
thread_ledNotif.start()
thread_gmailNotif.start()
while True: #Loop in order to wait for the ctrl-c
time.sleep(1)
except KeyboardInterrupt:
logging.info("Ctrl-C received asking thread to stopping...")
threadOn = True
else:
logging.warning("Main loop killed or something... Asking for kill remaining threads")
threadOn = True
finally:
time.sleep(1) #Waiting for the threads
logging.info("Exiting Python Script")
GPIO.cleanup()