def init_pin_modes(): app.logger.info("----START init pin modes START----") sensors = SensorInteractor.get_all_active() if sensors: for sensor in sensors: if sensor.pin.arduino_pin[0] == "D": try: requests.get("http://%s%s%s/%s/%s" % ( settings.LOCAL, settings.REST_ROOT, settings.MODE, sensor.pin.pin[1:], sensor.pin.io ), timeout = 5 ) app.logger.info("Pin %s mode successfully changed to %s!" % (sensor.pin.arduino_pin, sensor.pin.io)) except ConnectionError: app.logger.error("Cannot connect to %s!" % settings.IP_DNS) continue except Timeout: app.logger.error("Request timed out. Wrong IP?") continue app.logger.info("----END init pin modes END----")
def ip_cron(): import json from app.arduino.common import PublicIPInteractor from urllib2 import urlopen ip = PublicIPInteractor.get() currentIP = "%s:%s" % ( json.load(urlopen('http://httpbin.org/ip'))['origin'].rstrip(), settings.PORT ) app.logger.info("---- START IP cron START----") app.logger.info("IP ADDRESS - %s" % currentIP) if ( not ip.address or ip.address != currentIP ): try: for gateway in GatewayInteractor.get_all_device_registered(): r = request_helper.delete_device(gateway.address, gateway.post_authorization) if r != False: app.logger.info("Delete dev: %d" % r.status_code) ip.address = currentIP ip.save() r = request_helper.init_device(gateway.address, gateway.post_authorization) if r != False: app.logger.info("Init dev: %d" % r.status_code) r = request_helper.init_descriptor(gateway.address, gateway.post_authorization) if r != False: app.logger.info("Init descriptor: %d" % r.status_code) r = request_helper.send_descriptor(gateway.address, gateway.post_authorization) if r != False: app.logger.info("Descriptor: %d" % r.status_code) for sensor in SensorInteractor.get_all_active(): for sensor_method in sensor.sensor_methods: r = request_helper.delete_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path) if r != False: app.logger.info("Delete sensor method %s: %d" % (sensor_method.method.path, r.status_code)) r = request_helper.init_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path) if r != False: app.logger.info("Init sensor method %s: %d" % (sensor_method.method.path, r.status_code)) if sensor_method.method.type in ["read", "write"]: r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value) if r != False: app.logger.info("Send method value %s: %d" % (sensor_method.method.path, r.status_code)) log = "%s - %s %s %s - %s (%s)" % ( gateway.address, sensor.module.hardware.name, sensor.module.name, sensor.identificator, sensor_method.method.path, sensor_method.value ) app.logger.info(log) else: ip.address = "" ip.save() else: ip.address = "" ip.save() else: ip.address = "" ip.save() except: app.logger.error( "%s" % sys.exc_info()[0] ) ip.address = "" ip.save() else: app.logger.warning("address wasn't changed") app.logger.info("----END IP cron END----") return make_response()
def register_device_on_gateway(gateway_id): gateway = GatewayInteractor.get(gateway_id) if gateway: r = request_helper.init_device(gateway.address, gateway.post_authorization) if r != False: if r.status_code == 201 or r.status_code == 409: r = request_helper.init_descriptor(gateway.address, gateway.post_authorization) if r != False: r = request_helper.send_descriptor(gateway.address, gateway.post_authorization) if r != False: sensors = SensorInteractor.get_all_active() if sensors: for sensor in sensors: if sensor.active: sensor.save() for sensor_method in sensor.sensor_methods: r = request_helper.init_sensor( gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, ) if ( r != False and sensor_method.method.type in ["read", "write"] and sensor_method.value ): request_helper.send_sensor_value( gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value, ) flash("Device successfully registered!", category={"theme": "success"}) gateway.device_registered = True gateway.save() elif r.status_code == 400 or r.status_code == 401: flash("Wrong authorization for registration!", category={"theme": "error"}) app.logger.error("Registering device: Wrong authorization") else: flash("Something went wrong!", category={"theme": "error"}) app.logger.error("Registering device: Unknown error during device initialization") else: flash("Gateway does not exist!", category={"theme": "error"}) app.logger.error("Registering device: Gateway doesn't exist") return redirect("/")
def cron(): app.logger.info("----START cron START----") try: for gateway in GatewayInteractor.get_all_device_registered(): for sensor in SensorInteractor.get_all_active(): for sensor_method in sensor.sensor_methods: if sensor_method.method.type in ["read", "write"]: if sensor_method.method.type == "read": r = request_helper.get_sensor_value(sensor, sensor_method.method.path) if r != False: sensor_method.value = r sensor_method.save() if sensor_method.value: r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value) if r != False: log = "%s - %s %s %s - %s (%s)" % ( gateway.address, sensor.module.hardware.name, sensor.module.name, sensor.identificator, sensor_method.method.path, sensor_method.value ) app.logger.info(log) else: app.logger.error("CRON getting value: Couldn't connect to YunServer - sensor %s (method %s) - path %s" % (sensor.id, sensor_method.method.id, sensor_method.method.path)) return make_response() except: app.logger.error( "%s" % sys.exc_info()[0] ) app.logger.info("----END cron END----") return make_response()
def send_descriptor(address, post_authorization): headers = {'Authorization': 'Basic %s' % post_authorization, "content-type":"application/xml"} xml = '<?xml version="1.0" encoding="UTF-8"?>\ <obj xmlns="http://obix.org/ns/schema/1.1">\ <str name="applicationID" val="%(app_id)s"/>\ <list name="interfaces">\ <obj>\ <str name="interfaceID" val="Basic.Srv"/>\ </obj>\ <obj>\ <str name="interfaceID" val="Identify.Srv"/>\ </obj>' % { "app_id" : settings.DEVICE_ID } sensors = SensorInteractor.get_all_active() if sensors: sub_xml = '' for sensor in sensors: for sensor_method in sensor.sensor_methods: sub_xml = sub_xml + '<obj>\ <str name="interfaceID" val="%(sensor_identificator)s_%(method_path)s.Srv" />' % { "sensor_identificator" : sensor.identificator,"method_path" : sensor_method.method.path } if sensor_method.method.type in ["read", "write"]: sub_xml = sub_xml + \ '<%(method_value_type)s href="%(sensor_href)s" name="m2mMeasuredValue" unit="obix:units/%(method_unit)s" />' %\ { "method_value_type": sensor_method.method.value_type, "method_unit" : sensor_method.method.unit, "sensor_href" : '%s/%s%s/%s_%s%s%s' % (settings.APPLICATIONS, settings.DEVICE_ID, settings.CONTAINERS, sensor.identificator, sensor_method.method.path, settings.CONTENT_INSTANCES, settings.LATEST_CONTENT) } if sensor_method.method.type in ["write", "call"]: path = sensor_method.method.path if sensor_method.method.type == "write": path += "/[value-to-be-written]" sub_xml = sub_xml + \ '<op type="%(method_type)s" name="%(method_name)s" href="/m2m/applications/%(app_id)s/retargeting1/sensors/%(sensor_id)s/%(method_path)s" />'\ % { "method_type" : sensor_method.method.type, "method_name" : sensor_method.method.path, "method_path" : path, "app_id" : settings.DEVICE_ID, "sensor_id" : sensor.identificator} sub_xml = sub_xml + '</obj>' init_sensor(address, post_authorization, sensor.identificator, sensor_method.method.path) xml = xml + sub_xml else: flash("No active sensors to send values for after DESCRIPTOR initiation!", category={ 'theme' : 'warning' } ) xml = xml + '</list></obj>' try: r = requests.post("%s%s/%s%s%s%s" % ( address, settings.APPLICATIONS, settings.DEVICE_ID, settings.CONTAINERS, settings.DESCRIPTOR, settings.CONTENT_INSTANCES ), headers = headers, timeout = 5, data = xml ) return r except ConnectionError as e: flash("Cannot connect to gateway %s!" % address, category={ 'theme': 'error' } ) app.logger.error("Sending descriptor: %s" % (str(e), )) return False except Timeout as e: flash("Request timed out. Wrong IP?", category={ 'theme': 'error' } ) app.logger.error("Sending descriptor: %s" % (str(e), )) return False