def nodes_trigger_callback(): """ Trigger Callback Called when node gpio triggered --- tags: - nodemcu responses: 200: description: Returns node data """ payload = request.get_json() id = int(payload.get("id")) data = payload.get("data") node = Node.get_by_id(id) if node.printer_id: printer = Printer.get_by_id(node.printer_id) if printer.status in ["completed", "cancelled"]: t = Command(printer.id, hub.log, "clear", hub.Webapi) else: t = Command(printer.id, hub.log, "cancel", hub.Webapi) t.start() for sensor in node.sensors: if sensor.sensor_type == "LED": url = sensor.led_on() t = threading.Thread(target=requests.get, args=(url,)) t.start() #r = requests.get(url, timeout=10) d = {'id': id, 'data': data} return json.jsonify(d)
def activate_node(payload = None): """ Activate Node Called by node to activate itself on the hub --- tags: - nodemcu responses: 200: description: Returns a list of sensors """ global nodes id = int(request.args.get("id")) ip = str(request.args.get("ip")) port = int(request.args.get("port", 80)) log = hub.log listener = hub.node_listeners node = Node.get_by_id(id) if node: node.update(ip=ip) if not listener.is_alive(id): t = NodeCollector(id, hub.Webapi, hub.log) t.start() listener.add_thread(id, t) log.log("Node " + str(id) + " is now online.") return json.jsonify({'message': "Node " + str(id) + " is now online."}),201 if listener.is_alive(id): log.log("Node " + str(id) + " is already online but tried" + " to activate again, Updated it's data") return json.jsonify({'message': "Node " + str(id) + " was already online"}),201 else: node = Node(id, ip) updates = {"nodes": []} node_updates = updates.get("nodes") for node in Node.get_all(fresh=True): if node.printer_id == None: node_updates.append(node.id) t = threading.Thread(target=hub.Webapi.update_nodes,args=updates) t.start() return json.jsonify({"message": str(id) + " has been activated."}),201
def sensor_delete(sensor_id): """ Delete a Sensor --- tags: - nodes parameters: - name: sensor_id in: path description: id of sensor required: true type: integer responses: 200: description: Returns "Deleted" """ sensor = Sensor.get_by_webid(sensor_id) node = Node.get_by_id(sensor.node_id) node.remove_sensor(sensor.id) return json.jsonify({'message': 'Deleted'}), 201
def login(): """ Log in to Wink --- tags: - wink responses: 200: description: Returns success """ username = request.json.get('username') password = request.json.get('password') values = dumps({ "client_id": "quirky_wink_android_app", "client_secret": "e749124ad386a5a35c0ab554a4f2c045", "username": username, "password": password, "grant_type": "password", }).encode('utf-8') headers = { "Content-Type": "application/json", "Connection": "keep-alive", "X-API-VERSION": "1.0 User-Agent: Wink/1.1.9 (iPhone; iOS 7.0.4; Scale/2.00)" } req = Request("https://winkapi.quirky.com/oauth2/token", data=values, headers=headers) response_body = urlopen(req).read() data = json.loads(response_body) access_token = data['access_token'] refresh_token = data['refresh_token'] token_endpoint = data['token_endpoint'] if access_token and refresh_token and token_endpoint: account = Account.get_by_name('wink') if account: account.update(web_token=access_token, web_refresh_token=refresh_token) else: account = Account('wink', access_token, refresh_token, token_endpoint) devices = get_all_devices(account) channels = [] sub_key = '' node_id = '' node_frienly_id = '' for device in devices['data']: if 'hub_id' in device: node_id = device['hub_id'] node_frienly_id = device['name'] break if node_id: node = Node.get_by_id(node_id) if node is None: node = Node(node_id, '0', node_frienly_id) #register node for device in devices['data']: sub = device['subscription'] if sub: channels.append(sub['pubnub']['channel']) sub_key = sub['pubnub']['subscribe_key'] sensor = Sensor.get_by_webid(device['uuid']) if sensor is None: sensor = Sensor(node.id, 'wink') sensor.webid = device['uuid'] sensor.value = json.dumps(device['desired_state']) sensor.raw = json.dumps(device) sensor.friendly_id = json.dumps(device['name']).replace('"', '') sensor.state = "CONNECTED" if json.dumps( device['last_reading'] ['connection']) == 'true' else "DISCONNECTED" sensor.update() subcribe_devices_to_pub_nub(sub_key, channels) return "success" else: abort(404)
def run(self): """TODO: Docstring for run. :returns: TODO """ id = self.node_id webapi = self.webapi log = self.log node = Node.get_by_id(id) log.log("NodeCollector starting for node " + str(id)) sleep(10) failures = 0 while (True): node = Node.get_by_id(id, fresh=True) ip = node.ip sensors = node.sensors if self.stopped == True: log.log("NodeCollector for node " + str(id) + " was requested to stop. Exiting...") return 0 if ip != '0': for sensor in sensors: pin = sensor.pin webid = sensor.webid sensor_type = sensor.sensor_type if pin == None or sensor_type == None: continue if sensor_type in ["TEMP", "DOOR", "HUMI", "TRIG"]: url = sensor.get_url() elif sensor_type in ["LED"]: printer = Printer.get_by_id(node.printer_id) if printer: if printer.status in ["completed", "cancelled"]: url = sensor.led_flash() else: url = sensor.led_on() else: continue elif sensor_type in ["POWER"]: url = sensor.power_on() else: continue try: response = requests.get(url, timeout=10) except requests.ConnectionError: log.log("ERROR: Could not connect to " + url) response = None except requests.exceptions.Timeout: log.log("ERROR: Timeout occured on " + url) response = None if response == None: failures += 1 if failures > 10: log.log("ERROR: Could not collect" + " sensor data from node " + str(id) + ". NodeCollector exiting...") return -1 continue if response.status_code != 200: log.log("ERROR Response from " + str(id) + " returned status code " + str(response.status_code) + " on " + response.url) else: recent_json = response.json() ret = sensor.set_value(recent_json) data = sensor.to_web() if data != None and data.get("id") != None: webapi.add_data(data) sleep(3) sleep(3)