Beacon_Decoder.py

If the folder Binary_Beacons exists, this script will read in any binary (raw) beacons in the folder, in alphanumeric order. The beacon data is then placed in the database and the web-app can be run.

import sys
import socket
import struct
import datetime
import os
from threading import Thread
import binascii
import sqlite3
import Beacon_Receiver

# ---------- Configurable Options ------------

beacon_files = 'Binary_Beacons/'

files_dir = 'Beacon_Files/'
files_dir2 = 'Individual_Beacons/'

knownSequence = True

# ---------- Setup File Directory ----------------

if not os.path.exists(files_dir):
    os.makedirs(files_dir)
    
if not os.path.exists(files_dir+files_dir2):
    os.makedirs(files_dir+files_dir2)

# ---------- Receive and Parse Beacons -----------
    
def receive_messages():
    (conn, cursor) = Beacon_Receiver.create_gb()
    sequence = 0
    for filename in sorted(os.listdir(beacon_files)):
        data = open(beacon_files+filename).read()
        if len(data) == 472:
            if not knownSequence:
                seq = struct.pack('!h', sequence)
                data = seq + data[2:]
            print "Reading in file",beacon_files+filename
            Beacon_Receiver.receive_message(conn, cursor, data)
            sequence += 1
        else:
            print "Beacon is wrong size: ", len(data), " bytes"

receive_messages()