def main(): # initialize system reading for reading environment sysreader = reading.Reading() # get reading from environment reader = sysreader.getReading() # change temp based on env variable temp = os.environ.get('NOTIFICATION_TEMPERATURE', 20) if isinstance(temp, str): try: temp = int(temp) except ValueError: temp = 20 # exit the program if the temperature is more than 20 if (reader["temperature"] > temp): print(reader["temperature"]) sys.exit(0) # Check if another notification exist today try: history = notificationdb.NotificationDB() if (history.checkTodayNotifications() != True): sys.exit(0) except FileNotFoundError: # config file not found print("config file not found. aborting") sys.exit(0) except KeyError: # config file is misconfigured / corrupted print("miconfigured key in config file") sys.exit(0) except (exceptions.InfluxDBClientError, exceptions.InfluxDBServerError) as err: # Exception from influx, print it but proceed to notify the user anyway print(err) # send push bullet notification. It's too cold! devices = [] try: pusher = pushbullet.PushBullet() pusher.pushNotification(reader["temperature"], "cold") devices = pusher.getDevices() except KeyError: print("please check your configuration file") except TypeError: print("cannot decode body") except FileNotFoundError: print("config file not found") except: print("Fail to call pushbullet API") # Write the history for all devices try: history.writeHistory(devices, reader["temperature"], reader["deviceid"]) except (exceptions.InfluxDBClientError, exceptions.InfluxDBServerError) as err: print(err)
def __init__(self, _object, metadata=None): objectType = type(_object) # Floats if (objectType is float): self.type = "Float" self.value = float(_object) # Ints elif objectType is int: self.type = "Integer" self.value = int(_object) # Strings elif objectType is str: self.type = "String" self.value = str(_object) # Booleans elif objectType is bool: self.type = "Boolean" self.value = bool(_object) # Lists/Arrays elif objectType is list: self.type = "Array" self.value = [] for item in _object: # Sensors if isinstance(item, SensorDataEntry): self.value.append(sensor.Sensor(item)) # Readings elif isinstance(item, SensorReading): self.value.append(reading.Reading(item)) # Generic objects else: self.value.append(EntityAttribute(item)) # Dictionaries elif objectType is dict: for key, value in _object.iteritems(): setattr(self, key, EntityAttribute(value)) # Other objects else: self.type = _object.__class__.__name__ tempDict = {} for key, value in _object.__dict__.iteritems(): tempDict[key] = EntityAttribute(value) self.value = tempDict # Add metadata if (metadata != None): self.metadata = EntityAttribute(metadata)
def main(): # initialize system reading for reading environment sysreader = reading.Reading() # get reading from environment reader = sysreader.getReading() history = None devices = [] sentDevices = [] eligibleMacs = [] # Check if another notification exist today try: history = bluetoothdb.BluetoothDB() devices = history.readDevices() macs = list(map(lambda d: d["MAC"], devices)) # list all macs listed in the config sentDevices = history.getHistory( ) # list all not eligible devices (already sent) eligibleMacs = list( set(macs) - set(sentDevices)) # remove all devices which already been sent devices = list(filter(lambda d: d["MAC"] in eligibleMacs, devices)) # get all eligible devices except FileNotFoundError: # config file not found print("config file not found. aborting") sys.exit(0) except KeyError: # config file is misconfigured / corrupted print("miconfigured key in config file") sys.exit(0) except (exceptions.InfluxDBClientError, exceptions.InfluxDBServerError) as err: # Exception from influx, print it but proceed to notify the user anyway print(err) pusher = None try: pusher = pushbullet.PushBullet() #instantiate pushbullet except KeyError: print("please check your configuration file") except FileNotFoundError: print("config file not found") if (history is not None and len(devices) > 0): # search bluetooth devices search(reader, devices, pusher, history)
def main(): # initialize system reading for reading environment sysreader = reading.Reading() # get reading from environment data = sysreader.getReading() dataStr = json.dumps(data) print(dataStr) try: # initialize iot connection iotConnector = iotcore.IOTCore() # publish data to iot iotConnector.publish("rmit", dataStr) # disconnect from iot iotConnector.disconnect() except KeyError: print("Invalid config file") except FileNotFoundError: print("config file not found") except: print("Cannot publish to iot core")
def create_reading(): try: print ("POST") print request req_json = request.get_json() print req_json req_json["id"] = None req_json["timestamp"] = None ourReading = reading.Reading(req_json) print ourReading insertrresult = datastore.insertreading(ourReading) if insertrresult: return jsonify({"status": "succeeded"}), 200 else: return jsonify({"status": "failed"}), 400 except Exception as e: print "data not inserted" print e return "OK"
from flask import Flask, request from flask_cors import * import json app = Flask(__name__) CORS(app, supports_credentials=True) # import complex_num import feedback import reading import simpledatabase import similarity_core import feedback_complexnumber core = similarity_core.similarity_core() tmp = reading.Reading(core) t2 = [ 3, 2, 3, 3, 3, 3, 3, 1, 2, 1, 3, 2, 1, 3, 2, 1, 3, 1, 2, 3, 2, 1, 3, 2, 3, 2, 1 ] t3 = simpledatabase.help1() @app.route('/getanswer', methods=['POST']) def hello_world(): ttt = request.get_json()["question"] number, question = tmp.classify(ttt) print(number) print(question) if number == -1: