Esempio n. 1
0
def _get_cf_instance_number():
    num = ''
    endpoint = config.get_tap_auth_endpoint()
    api = config.get_tap_api_endpoint()
    if endpoint and api:
        proxy = config.get_all_proxy()
        client = ApiClient(endpoint, proxy)
        client.add_header('content-type', 'application/x-www-form-urlencoded;charset=utf-8')
        client.add_header('accept', 'application/json;charset=utf-8')
        client.add_header('authorization', 'Basic Y2Y6')
        data = {
                    'grant_type': 'password',
                    'username': config.get_tap_uname(),
                    'password': config.get_tap_pwd(),
                }
        ret = client.post('/oauth/token', data)
        if ret.ok():
            at = ret.content.get('access_token')
            cc = ApiClient(api, proxy)
            cc.add_header('authorization', 'bearer ' + at)
            resp = cc.get('/v2/apps?q=name:{}'.format(config.get_tap_app_name()))
            if resp.ok():
                num = resp.content['resources'][0]['entity']['instances']
        else:
            print "response: " + str(ret.content)
            print "status code:" + str(ret.status_code)
            print "post data:" + str(data)
    return str(num)
Esempio n. 2
0
def _get_cf_instance_number():
    num = ''
    endpoint = config.get_tap_auth_endpoint()
    api = config.get_tap_api_endpoint()
    if endpoint and api:
        proxy = config.get_all_proxy()
        client = ApiClient(endpoint, proxy)
        client.add_header('content-type',
                          'application/x-www-form-urlencoded;charset=utf-8')
        client.add_header('accept', 'application/json;charset=utf-8')
        client.add_header('authorization', 'Basic Y2Y6')
        data = {
            'grant_type': 'password',
            'username': config.get_tap_uname(),
            'password': config.get_tap_pwd(),
        }
        ret = client.post('/oauth/token', data)
        if ret.ok():
            at = ret.content.get('access_token')
            cc = ApiClient(api, proxy)
            cc.add_header('authorization', 'bearer ' + at)
            resp = cc.get('/v2/apps?q=name:{}'.format(
                config.get_tap_app_name()))
            if resp.ok():
                num = resp.content['resources'][0]['entity']['instances']
        else:
            print "response: " + str(ret.content)
            print "status code:" + str(ret.status_code)
            print "post data:" + str(data)
    return str(num)
Esempio n. 3
0
 def connect(self, gateway_id):
     """
     Connect to IoT web service
     """
     if self._client is None:
         self._client = IoTClient(gateway_id,
                                  proxies=config.get_all_proxy())
Esempio n. 4
0
class NexmoClient(object):
    api_key = config.get_api_key()
    api_secret = config.get_api_secret()
    interval = config.get_sms_interval()
    proxy = config.get_all_proxy()

    @staticmethod
    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
Esempio n. 5
0
 def connect(self, username):
     """
     Connect to IoT web service
     """
     if self._client is None:
         self._client = IoTClient(username, proxies=config.get_all_proxy())
Esempio n. 6
0
 def connect(self, username):
     """
     Connect to IoT web service
     """
     if self._client is None:
         self._client = IoTClient(username, proxies=config.get_all_proxy())