Below is some code I've put together to contol a siren for a fire service.
It works by webscraping a paging feed and looks for set triggers.
Is there a better way of doing my code or is this "pythonic" enough or can it be improved?
Im python self taught.
"""PYTHON SCRIPT TO CONTROL SIREN
This script will webscape a paging feed and
in turn activate a siren on set incidents
siren should only sound for 30secconds between 0800 -2000 hrs
this has been made so the siren only goes off for some incidents not all
by s.rees (c) 2016
"""
from scraper import Scraper
import sys
import requests
from clint.textui import puts, colored
import RPi.GPIO as GPIO
from time import sleep
import time
time = datetime.datetime.now()
print "Starting"
print "Monitoring"
#Set GPIO siren is attached too
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
def siren ():
GPIO.output(7, GPIO.HIGH)
sleep(30)
GPIO.output(7, GPIO.LOW)
if __name__ == "__main__":
# Load webscraper module
scraper = Scraper(5, recent_messages=True)
@scraper.register_handler
def handler(msg):
# get msg from scraper and make it readable
puts(colored.yellow(msg.channel), newline=False)
puts(" [", newline=False)
puts(colored.green(msg.datetime), newline=False)
puts("] ", newline=False)
if msg.response:
puts(colored.red(msg.text))
else:
puts(msg.text)
# set agency
if 'MFS' in msg.channel:
#set unit
if 'Station' in msg.channel:
print "Brigade Found"
#set response type
if 'RESPOND RUBBISH OR WASTE' in msg.text:
print "Trigger found"
if (time > datetime.time(8) and time < datetime.time(20)):
print "Activate Siren"
siren ()
print 'done'
return
else:
print "out of time restrictions"
return
if 'RESPOND GRASS FIRE' in msg.text:
print "Trigger found"
if (time > datetime.time(8) and time < datetime.time(20)):
print "Activate Siren"
siren ()
print 'done'
return
else:
print "out of time restrictions"
return
else:
print "Brigade found but no trigger"
return
else:
return
scraper.run()
GPIO.cleanup()