Created
June 8, 2020 21:11
-
-
Save ngaranko/16a4ac7d1e62fa4c341b858b3f54516f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import json | |
import os | |
import sys | |
import time | |
import urllib | |
import urllib2 | |
MASTER = "http://localhost:8000/beacons/" | |
RC_FILE = os.path.expanduser("~/.beaconrc") | |
LOG_FILE = os.path.expanduser("~/beacon_log") | |
TIMEOUT = 5*60 # 5 minutes | |
def init_beacon(): | |
""" | |
Check if beacon is already set up. | |
Ask for beacon name. | |
Register beacon via API. | |
Put beacon ID into .beaconrc | |
Quit. | |
""" | |
try: | |
with open(RC_FILE, "r") as beaconrc: | |
config = json.loads(beaconrc.read()) | |
if "name" in config and "beacon_id" in config: | |
print("Beacon already configured: ") | |
print(" Name: {}".format(config["name"])) | |
print(" ID: {}".format(config["beacon_id"])) | |
if not confirm("Do you want to re-configure beacon [Y/N]? "): | |
print("Done. ID: {}".format(config["beacon_id"])) | |
return | |
except IOError: | |
pass | |
name = raw_input("Please enter name for this beacon: ") | |
print("* Initializing: {}".format(name)) | |
data = urllib.urlencode({"name": name}) | |
req = urllib2.Request(MASTER, data) | |
try: | |
response = urllib2.urlopen(req) | |
except urllib2.HTTPError as e: | |
print("Failed to register beacon! {}".format(e.read())) | |
else: | |
the_page = response.read() | |
try: | |
beacon_id = json.loads(the_page)["id"] | |
except Exception: | |
print("Failed to read Master response: {}".format(the_page)) | |
else: | |
with open(RC_FILE, "w") as beaconrc: | |
beaconrc.write(json.dumps(dict( | |
name=name, | |
beacon_id=beacon_id | |
))) | |
print("Done. ID: {}".format(beacon_id)) | |
def ping(): | |
""" | |
Get beacon details or bail out. | |
Ping master. | |
Sleep for 5 minutes. | |
""" | |
beacon_id = None | |
try: | |
with open(RC_FILE, "r") as beaconrc: | |
config = json.loads(beaconrc.read()) | |
beacon_id = config["beacon_id"] | |
except Exception as e: | |
with open(LOG_FILE, "a+") as log: | |
log.write("Failed to open config. {}\r\n".format(e)) | |
return | |
data = urllib.urlencode({"test": "1"}) | |
req = urllib2.Request("{}{}/".format(MASTER, beacon_id), data) | |
try: | |
urllib2.urlopen(req) | |
except urllib2.HTTPError as e: | |
with open(LOG_FILE, "a+") as log: | |
log.write("Failed to ping master. {}\r\n".format(e)) | |
time.sleep(TIMEOUT) | |
ping() | |
def confirm(message): | |
""" | |
Ask user to enter Y or N (case-insensitive). | |
:return: True if the answer is Y. | |
:rtype: bool | |
""" | |
answer = "" | |
while answer not in ["y", "n"]: | |
answer = raw_input(message).lower() | |
return answer == "y" | |
if __name__ == '__main__': | |
if "--init" in sys.argv: | |
init_beacon() | |
else: | |
ping() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment