text stringlengths 0 828 |
|---|
1261,"def bind(self, devices_to_bind): |
"""""" This function allows an entity to list the devices to subscribe for data. This function must be called |
at least once, before doing a subscribe. Subscribe function will listen to devices that are bound here. |
Args: |
devices_to_bind (list): an array of devices to listen to. |
Example bind([""test100"",""testDemo""]) |
"""""" |
if self.entity_api_key == """": |
return {'status': 'failure', 'response': 'No API key found in request'} |
url = self.base_url + ""api/0.1.0/subscribe/bind"" |
headers = {""apikey"": self.entity_api_key} |
data = { |
""exchange"": ""amq.topic"", |
""keys"": devices_to_bind, |
""queue"": self.entity_id |
} |
with self.no_ssl_verification(): |
r = requests.post(url, json=data, headers=headers) |
response = dict() |
if ""No API key"" in str(r.content.decode(""utf-8"")): |
response[""status""] = ""failure"" |
r = json.loads(r.content.decode(""utf-8""))['message'] |
elif 'bind queue ok' in str(r.content.decode(""utf-8"")): |
response[""status""] = ""success"" |
r = r.content.decode(""utf-8"") |
else: |
response[""status""] = ""failure"" |
r = r.content.decode(""utf-8"") |
response[""response""] = str(r) |
return response" |
1262,"def unbind(self, devices_to_unbind): |
"""""" This function allows an entity to unbound devices that are already bound. |
Args: |
devices_to_unbind (list): an array of devices that are to be unbound ( stop listening) |
Example unbind([""test10"",""testDemo105""]) |
"""""" |
if self.entity_api_key == """": |
return {'status': 'failure', 'response': 'No API key found in request'} |
url = self.base_url + ""api/0.1.0/subscribe/unbind"" |
headers = {""apikey"": self.entity_api_key} |
data = { |
""exchange"": ""amq.topic"", |
""keys"": devices_to_unbind, |
""queue"": self.entity_id |
} |
with self.no_ssl_verification(): |
r = requests.delete(url, json=data, headers=headers) |
print(r) |
response = dict() |
if ""No API key"" in str(r.content.decode(""utf-8"")): |
response[""status""] = ""failure"" |
r = json.loads(r.content.decode(""utf-8""))['message'] |
elif 'unbind' in str(r.content.decode(""utf-8"")): |
response[""status""] = ""success"" |
r = r.content.decode(""utf-8"") |
else: |
response[""status""] = ""failure"" |
r = r.content.decode(""utf-8"") |
response[""response""] = str(r) |
return response" |
1263,"def subscribe(self, devices_to_bind=[]): |
"""""" This function allows an entity to subscribe for data from the devices specified in the bind operation. It |
creates a thread with an event loop to manager the tasks created in start_subscribe_worker. |
Args: |
devices_to_bind (list): an array of devices to listen to |
"""""" |
if self.entity_api_key == """": |
return {'status': 'failure', 'response': 'No API key found in request'} |
self.bind(devices_to_bind) |
loop = asyncio.new_event_loop() |
t1 = threading.Thread(target=self.start_subscribe_worker, args=(loop,)) |
t1.daemon = True |
t1.start()" |
1264,"def start_subscribe_worker(self, loop): |
"""""" Switch to new event loop as a thread and run until complete. """""" |
url = self.base_url + ""api/0.1.0/subscribe"" |
task = loop.create_task(self.asynchronously_get_data(url + ""?name={0}"".format(self.entity_id))) |
asyncio.set_event_loop(loop) |
loop.run_until_complete(task) |
self.event_loop = loop" |
1265,"async def asynchronously_get_data(self, url): |
"""""" Asynchronously get data from Chunked transfer encoding of https://smartcity.rbccps.org/api/0.1.0/subscribe. |
(Only this function requires Python 3. Rest of the functions can be run in python2. |
Args: |
url (string): url to subscribe |
"""""" |
headers = {""apikey"": self.entity_api_key} |
try: |
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session: |
async with session.get(url, headers=headers, timeout=3000) as response: |
while True: # loop over for each chunk of data |
chunk = await response.content.readchunk() |
if not chunk: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.