Skip to content

Commit

Permalink
send only grid data to cityIO on interval
Browse files Browse the repository at this point in the history
  • Loading branch information
RELNO committed Mar 21, 2020
1 parent 5171e09 commit 9cdf080
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
31 changes: 21 additions & 10 deletions cityscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,23 +395,17 @@ def create_data_json(self, multiprocess_shared_dict):
old_scan_results = [-1]
SEND_INTERVAL = timedelta(milliseconds=SEND_INTERVAL)
last_sent = datetime.now()

while True:
scan_results = multiprocess_shared_dict['scan']
from_last_sent = datetime.now() - last_sent
if (scan_results != old_scan_results) and from_last_sent > SEND_INTERVAL:
try:
# convert to json
json_struct = self.table_settings
json_struct['grid'] = scan_results
if self.table_settings['objects']['cityscopy']['cityio'] is True:
cityIO_json = json.dumps(json_struct)
self.send_json_to_cityIO(cityIO_json)
self.send_json_to_cityIO(json.dumps(scan_results))
else:
self.send_json_to_UDP(scan_results)
except Exception as e:
print(e)

except Exception as ERR:
print(ERR)
# match the two grid after send
old_scan_results = scan_results
last_sent = datetime.now()
Expand All @@ -422,14 +416,31 @@ def create_data_json(self, multiprocess_shared_dict):
##################################################

def send_json_to_cityIO(self, cityIO_json):
'''
sends the grid to cityIO
'''
# defining the api-endpoint
API_ENDPOINT = "https://cityio.media.mit.edu/api/table/update/" + \
self.table_settings['header']['name'] + "/grid/"
# sending post request and saving response as response object
req = requests.post(url=API_ENDPOINT, data=cityIO_json)
if req.status_code != 200:
print("cityIO might be down. so sad.")
print("sending grid to", API_ENDPOINT, req)

##################################################

def init_table(self):
json_struct = self.table_settings
cityIO_json = json.dumps(json_struct)
# defining the api-endpoint
API_ENDPOINT = "https://cityio.media.mit.edu/api/table/update/" + \
self.table_settings['header']['name']
# sending post request and saving response as response object
req = requests.post(url=API_ENDPOINT, data=cityIO_json)
if req.status_code != 200:
print("cityIO might be down. so sad.")
print(req)
print("init table on cityIO", req)

##################################################

Expand Down
6 changes: 6 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
# init cityscopy class
cityscopy = Cityscopy(cityscopy_settings_path)
# run CityScopy main methods

# keystone the scanned area
# cityscopy.keystone()
# create new table instance on cityIO
cityscopy.init_table()
# scan the grid and send to cityIO
cityscopy.scan()
# start local UDP comms
# # cityscopy.udp_listener()

0 comments on commit 9cdf080

Please sign in to comment.