# NEW DOOR CAM Raspberry Pi surveillance kit
# by ACMECORPORATION



import os, subprocess, time
os.environ['DISPLAY'] = ":0" # check if your dispaly name launching from command line: echo $DISPLAY then change accordingly
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
import fnmatch

email = "0"

subprocess.call('xset dpms force off', shell=True)
while True:
    try:
	with open('/var/www/testmacro.txt', 'r') as motstat:
		data=motstat.read().replace('\n', '')
			
	if data == "1":
		subprocess.call('xset s reset', shell=True)
		email = "1"
		# for bult-in speaker change next line to: os.system("mpg123 -m /home/pi/example.mp3")
		os.system("sshpass -p 'REMOTE_RPI_PASSWORD' ssh pi@REMOTE_RPI_IP -p22 mpg123 -m /home/pi/example.mp3") # add remote Raspberry Pi password, IP and file path
		time.sleep(30)
	else:
		subprocess.call('xset dpms force off', shell=True)
		if email == "1":
			fromaddr = "EMAILSENDER" # insert here sender email address 
			toaddr = "EMAILRECEIVER" # insert here receiver email address
			msg = MIMEMultipart()
			msg['From'] = fromaddr
			msg['To'] = toaddr
			msg['Subject'] = "ALERT"
			body = "MOTION DETECTED"
			msg.attach(MIMEText(body, 'plain'))
			filename = "NAME.jpg"
			with open('/var/www/testmacronomefile.txt', 'r') as thumbname:
				thum=thumbname.read().replace('\n', '')
				part = MIMEBase('application', 'octet-stream')
				part.set_payload(open(thum, "rb").read())
				encoders.encode_base64(part)
				part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
				msg.attach(part)
				server = smtplib.SMTP('smtp.gmail.com', 587) # i'm using gmail
				server.starttls()
				server.login(fromaddr, "SENDER_EMAIL_PASSWORD") # insert here sender email password
				text = msg.as_string()
				server.sendmail(fromaddr, toaddr, text)
				server.quit()
				email = "0"
		time.sleep(2)

    except:
	pass
