def get_sensor(): """ Get sensor data by token """ token_dict = {} default_token = util.format_datetime(util.get_utc_now() - datetime.timedelta(minutes=1)) # print default_token tlist = [name+'-token' for name in ALERT_GRP] for t in tlist: token_dict.update({t: request.headers.get(t) if request.headers.get(t) else default_token}) # print str(token_dict) ret = _get_sensor_data(token_dict) return jsonify({'data': ret}), 201
def get_sensor(): """ Get sensor data by token """ token_dict = {} default_token = util.format_datetime(util.get_utc_now() - datetime.timedelta(minutes=1)) # print default_token tlist = [name + '-token' for name in ALERT_GRP] for t in tlist: token_dict.update({ t: request.headers.get(t) if request.headers.get(t) else default_token }) # print str(token_dict) ret = _get_sensor_data(token_dict) return jsonify({'data': ret}), 201
def send_message(gateway_id, uuid, sensor_type): try: if any( [NexmoClient.api_key is None, NexmoClient.api_secret is None]): raise AuthenticationError # check sms history and decide whether it's already sent in the interval. token = get_utc_now() - timedelta( seconds=int(NexmoClient.interval)) if get_latest_by_gateway_uuid(gateway_id, uuid, token): # already sent to all users, ignore return False # get user phone number by gateway id phone_numbers = get_user_phone_by_gateway(gateway_id) # Unsupported sensor type if sensor_type not in ('gas', ): return False msg = '' if sensor_type == 'motion': msg = 'Motion was detected at your home just now. ' elif sensor_type == 'gas': msg = 'There was a gas leak detected at your home just now. ' for to in phone_numbers: params = { 'from': 'NEXMO', 'to': to, 'text': msg, } cl = Client(api_key=NexmoClient.api_key, api_secret=NexmoClient.api_secret, proxy=NexmoClient.proxy) print cl.send_message(params) print new({'gateway_id': gateway_id, 'uuid': uuid}) return True except (AuthenticationError, ClientError, ServerError): return False
def send_message(gateway_id, uuid, sensor_type): try: if any([NexmoClient.api_key is None, NexmoClient.api_secret is None]): raise AuthenticationError # check sms history and decide whether it's already sent in the interval. token = get_utc_now() - timedelta(seconds=int(NexmoClient.interval)) if get_latest_by_gateway_uuid(gateway_id, uuid, token): # already sent to all users, ignore return False # get user phone number by gateway id phone_numbers = get_user_phone_by_gateway(gateway_id) # Unsupported sensor type if sensor_type not in ('gas',): return False msg = '' if sensor_type == 'motion': msg = 'Motion was detected at your home just now. ' elif sensor_type == 'gas': msg = 'There was a gas leak detected at your home just now. ' for to in phone_numbers: params = { 'from': 'NEXMO', 'to': to, 'text': msg, } cl = Client(api_key=NexmoClient.api_key, api_secret=NexmoClient.api_secret, proxy=NexmoClient.proxy) print cl.send_message(params) print new({'gateway_id': gateway_id, 'uuid': uuid}) return True except (AuthenticationError, ClientError, ServerError): return False
def _get_sensor_data(token_dict): res = resource.list_resource(status=1, gateway_id=session['gateway_id']) default_token = util.format_datetime(util.get_utc_now() - datetime.timedelta(minutes=1)) ret = {'alert': {}, 'status': {}, 'data': {}, 'brillo': {}, 'generic': {}} for sensor in res: typ = sensor.get('sensor_type').get('mapping_class') href = sensor.get('path') resource_id = sensor.get("id") if href.startswith("/a/"): if typ in ALERT_GRP: token = token_dict.get(str(resource_id)) if str(resource_id) in token_dict.keys() \ and token_dict.get(str(resource_id)) else default_token latest_data = util.get_class( "DB.api.{}.get_latest_alert_by_gateway_uuid".format(typ))( resource_id=resource_id, token=token) latest_data = latest_data if latest_data else { "resource_id": resource_id } elif typ == 'power': latest_data = util.get_class( "DB.api.energy.get_latest_by_gateway_uuid".format(typ))( resource_id=resource_id) else: latest_data = util.get_class( "DB.api.{}.get_latest_by_gateway_uuid".format(typ))( resource_id=resource_id) if typ == 'buzzer': status_data = util.get_class( "DB.api.{}.get_latest_by_gateway_uuid".format(typ))( resource_id=resource_id) if latest_data is None: continue # print latest_data if typ in ALERT_GRP: _compose_sensor_data(typ, latest_data, 'created_at', 'alert', ret) if typ in STATUS_GRP: if typ == "rgbled": val = True if latest_data.get( 'rgbvalue') == "[255, 0, 0]" else False _compose_sensor_data(typ, latest_data, {'value': val}, 'status', ret) elif typ == 'buzzer': _compose_sensor_data(typ, status_data, 'status', 'status', ret) else: _compose_sensor_data(typ, latest_data, 'status', 'status', ret) elif typ in DATA_GRP: # extract values from the db query result if typ in ['temperature', 'illuminance']: key_words = [typ] elif typ == 'solar': key_words = ['tiltpercentage'] elif typ == 'power': key_words = ['value'] elif typ == 'environment': key_words = [ 'temperature', 'humidity', 'pressure', 'uv_index' ] for key in key_words: sensor_type = typ if typ != "environment" else key _compose_sensor_data(sensor_type, latest_data, key, 'data', ret) elif typ == "generic": _compose_sensor_data(typ, latest_data, 'json_data', 'generic', ret) elif href.startswith("/brillo/"): latest_data = util.get_class( "DB.api.{}.get_latest_by_gateway_uuid".format(typ))( resource_id=resource_id) # print latest_data if latest_data is None: uuid = sensor.get('uuid') if ret['brillo'].get(uuid) is None: ret['brillo'].update( {uuid: { typ: { 'resource_id': resource_id } }}) else: ret['brillo'][uuid].update( {typ: { 'resource_id': resource_id }}) continue if typ in BRILLO_GRP: if typ in ['brightness']: keys = [typ] elif typ == 'rgbled': keys = ['rgbvalue'] elif typ == 'audio': keys = ['volume', 'mute'] elif typ == 'mp3player': keys = ['media_states', 'playlist', 'state', 'title'] _compose_sensor_data(typ, latest_data, keys, 'brillo', ret) return ret
def _get_sensor_data(token_dict): res = resource.list_resource(status=1, gateway_id=session['gateway_id']) default_token = util.format_datetime(util.get_utc_now() - datetime.timedelta(minutes=1)) ret = { 'alert': {}, 'status': {}, 'data': {}, 'brillo': {}, 'generic': {} } for sensor in res: typ = sensor.get('sensor_type').get('mapping_class') href = sensor.get('path') resource_id = sensor.get("id") if href.startswith("/a/"): if typ in ALERT_GRP: token = token_dict.get(str(resource_id)) if str(resource_id) in token_dict.keys() \ and token_dict.get(str(resource_id)) else default_token latest_data = util.get_class("DB.api.{}.get_latest_alert_by_gateway_uuid" .format(typ))(resource_id=resource_id, token=token) latest_data = latest_data if latest_data else {"resource_id": resource_id} elif typ == 'power': latest_data = util.get_class("DB.api.energy.get_latest_by_gateway_uuid".format(typ))(resource_id=resource_id) else: latest_data = util.get_class("DB.api.{}.get_latest_by_gateway_uuid".format(typ))(resource_id=resource_id) if latest_data is None: continue # print latest_data if typ in ALERT_GRP: _compose_sensor_data(typ, latest_data, 'created_at', 'alert', ret) elif typ in STATUS_GRP: if typ == "rgbled": val = True if latest_data.get('rgbvalue') == "[255, 0, 0]" else False _compose_sensor_data(typ, latest_data, {'value': val}, 'status', ret) else: _compose_sensor_data(typ, latest_data, 'status', 'status', ret) elif typ in DATA_GRP: # extract values from the db query result if typ in ['temperature', 'illuminance']: key_words = [typ] elif typ == 'solar': key_words = ['tiltpercentage'] elif typ == 'power': key_words = ['value'] elif typ == 'environment': key_words = ['temperature', 'humidity', 'pressure', 'uv_index'] for key in key_words: sensor_type = typ if typ != "environment" else key _compose_sensor_data(sensor_type, latest_data, key, 'data', ret) elif typ == "generic": _compose_sensor_data(typ, latest_data, 'json_data', 'generic', ret) elif href.startswith("/brillo/"): latest_data = util.get_class("DB.api.{}.get_latest_by_gateway_uuid".format(typ))( resource_id=resource_id) # print latest_data if latest_data is None: continue if typ in BRILLO_GRP: if typ in ['brightness']: keys = [typ] elif typ == 'rgbled': keys = ['rgbvalue'] elif typ == 'audio': keys = ['volume', 'mute'] elif typ == 'mp3player': keys = ['media_states', 'playlist', 'state', 'title'] _compose_sensor_data(typ, latest_data, keys, 'brillo', ret) return ret