def Run(): z_outs = [] count = 50 * 60 # Count allows us to send a 'running' message ~1 a minute client = initSender("PalomAlert/acc") while True: for i in range( 0, 30 ): # The PalomAlert takes the standard deviation over the past 30 readings in order to tell whether the door is being shook - raw readings were too erratic to be used reliably z_out_L = read_byte(0x2C) z_out_H = read_byte(0x2D) z_out = convert_data(z_out_L, z_out_H) z_outs.append(z_out) deviation = stdev(z_outs) z_outs.clear() # Clear the array to ensure next reading is correct if (deviation > 1000): count = 50 * 60 # To indicate door isn't being shook again ts = time.time() # Get timestamp send(client, ts, "PalomAlert/acc/shake", qos=2) time.sleep( 3.5 ) # To ensure messages aren't being sent at too high a frequency elif (count >= 50 * 60): send( client, None, "PalomAlert/acc/running", qos=2 ) # Running message indicates to the server that the PalomAlert is live, and there is no intrusion happening count = 0 else: count = count + 1 time.sleep(0.02)
def Check(Readings): client = initSender("PalomAlert/check") # This function checks whether the door was static during calibration if (Readings["comp"][1] > 0.5 or Readings["x"][1] > 220) or (Readings["y"][1] > 220 or (Readings["z"][1] > 220 or Readings["temp"][1])) > 1.5: send(client, None, "PalomAlert/calibration/retry", qos=2) return False else: send(client, None, "PalomAlert/calibration/ok", qos=2) return True
def Run(compData): mean = compData[0] count = 600 # Count allows us to send a 'running' message ~1 a minute countOpen = 0 # Set to alert if door is open for whole minute scale = 0.92 # Scale needed to convert to degrees offset = 0.9 client = initSender("PalomAlert/comp") sendMsg = True # Allow to send message while True: # Read compass data x_out = read_word_2c(4, 3) * scale y_out = read_word_2c( 8, 7 ) * scale # Read but unused - the sensor stops updating if left unread z_out = read_word_2c(6, 5) * scale bearing = convert_2_bearing(z_out, x_out) # Checking if door is open & a msg hasn't been sent yet or if door has been open for over a minute if (isOutOfRange360(bearing, mean - offset, mean + offset) and sendMsg) or (countOpen >= 600): ts = time.time() # Get timestamp send(client, ts, "PalomAlert/comp/open", qos=2) time.sleep( 3 ) # To ensure messages aren't being sent at too high a frequency sendMsg = False count = 600 # Ensures 'running' message is sent after the door is closed countOpen = 0 # Checking if door has stayed open if isOutOfRange360(bearing, mean - offset, mean + offset) and (not sendMsg): countOpen = countOpen + 1 # CountOpen ensures no repeat messages are sent until the door has been open for a minute elif (count >= 600): send( client, None, "PalomAlert/comp/running", qos=2 ) # Running message indicates to the server that the PalomAlert is live, and there is no intrusion happening count = 0 sendMsg = True else: count = count + 1 countOpen = 0 sendMsg = True time.sleep(0.2)
def Run(tempData): offset = 10 # Offset neeeded to convert temperatures to celcius mean = tempData[0] + offset # Conversion to celcius count = 60 # Count allows us to send a 'running' message ~1 a minute tolerance = 10 # Tolerance, the variation of temperature before a message is sent client = initSender("PalomAlert/temp") sendMsg = True # Allow to send message countChange = 0 while True: # Reading temperature and converting to celcius temp = read_word_2c(0x0C, 0x0D) temp = temp >> 6 temp = temp + offset if ((temp < (mean - tolerance / 2) or temp > (mean + tolerance)) and sendMsg) or countChange >= 60: count = 60 # Ensures 'running' message is sent after the temperature returns to normal countChange = 0 sendMsg = False payload = { "temp": temp, "ts": time.time() # Send timestamp and current temperature } send(client, payload, "PalomAlert/temp/change", qos=2) time.sleep( 3 ) # To ensure messages aren't being sent at too high a frequency # Checking if temperature has stayed outside of range if (temp < (mean - tolerance / 2) or temp > (mean + tolerance)) and (not sendMsg): countChange = countChange + 1 # CountOpen ensures no repeat messages are sent until temperature remains changed for a minute elif (count >= 60): send( client, str(temp), "PalomAlert/temp/running", qos=2 ) # Running message indicates to the server that the PalomAlert is live, and there is no intrusion happening count = 0 sendMsg = True else: countChange = 0 count = count + 1 sendMsg = True time.sleep(1)
from flask import Flask, render_template, request, redirect, url_for import json import time from API_network import send, initSender client = initSender("PalomAlert/run") #read last status from status.json def get_status(): with open("status.json", "r") as f: status_dict = json.load(f) return status_dict #read logged events from log.txt def get_log(): with open("log.txt") as f: content = f.readlines() #to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] return content #get relevant information from log to display on Events Log def get_logStatus(log): output = [] t_output = [] for m_event in log: event = json.loads(m_event)
from API_network import send, initSender client = initSender("PalomAlert/halt") send(client, None, "PalomAlert/halt", qos=2)