Skip to content

Instantly share code, notes, and snippets.

@yujiterada
Last active August 12, 2022 00:13
Show Gist options
  • Save yujiterada/f0d5cd94951be00fffdd3f15f650e4f1 to your computer and use it in GitHub Desktop.
Save yujiterada/f0d5cd94951be00fffdd3f15f650e4f1 to your computer and use it in GitHub Desktop.
import os
import datetime
import meraki
TIMEZONE_OFFSETS = {
'Australia/LHI': 10.5,
'Australia/Lord Howe': 10,
'Australia/ACT': 10,
'Australia/Canberra': 10,
'Australia/Curie': 10,
'Australia/NSW': 10,
'Australia/Queensland': 10,
'Australia/Tasmania': 10,
'Australia/Victoria': 10,
'Australia/Hobart': 10,
'Australia/Melbourne': 10.5,
'Australia/Sydney': 10,
'Australia/Brisbane': 10,
'Australia/Lindeman': 10,
'Australia/North': 9.5,
'Australia/South': 9.5,
'Australia/Yancowinna': 9.5,
'Australia/Broken Hill': 9.5,
'Australia/Adelaide': 9.5,
'Australia/Darwin': 9.5,
'Australia/Eucla': 8.75,
'Australia/West': 8.0,
'Australia/Perth': 8.0
}
DATA_FOR_THE_PAST_DAYS = 1 # 1 is yesterday, 2 is the day before yesterday
MERAKI_API_KEY = os.environ.get('MERAKI_API_KEY', None)
MERAKI_ORG_ID = os.environ.get('MERAKI_ORG_ID', 734417)
NETWORK_TO_EXCLUDE = []
if MERAKI_API_KEY:
# Initialize Meraki API Client
dashboard = meraki.DashboardAPI(api_key=MERAKI_API_KEY, suppress_logging=True, output_log=False)
if MERAKI_ORG_ID == None:
organizations = dashboard.organizations.getOrganizations()
print('Configure MERAKI_ORG_ID using the relevant ID below')
for org in organizations:
print('{} {}'.format(org['id'], org['name']))
else:
# Obtain all networks in organization
networks = dashboard.organizations.getOrganizationNetworks(organizationId=MERAKI_ORG_ID)
start_time_local = (datetime.datetime.today() - datetime.timedelta(DATA_FOR_THE_PAST_DAYS)).replace(microsecond=0, second=0, minute=0, hour=0)
# Open file
f = open("{}.csv".format(start_time_local.isoformat().split('T')[0]), "w")
f.write('Store Code,Zone,Time,Quantity/2\n')
# Iterate through the networks and check if MX exists in network
for network in networks:
network_id = network['id']
# If the network ID is not in the list to exclude
if network_id not in NETWORK_TO_EXCLUDE and "camera" in network['productTypes']:
network_timezone = network['timeZone']
offset = TIMEZONE_OFFSETS.get(network_timezone, None)
if offset == None:
print('Timezone not in TIMEZONE_OFFSETS. Configure a timezone from Network-wide > General > Timezone.')
continue
start_time_utc = start_time_local - datetime.timedelta(hours=TIMEZONE_OFFSETS[network_timezone])
# print(start_time_local.isoformat(), start_time_utc.isoformat())
# If the network includes an MV, obtain all devices in the network
meraki_devices = dashboard.networks.getNetworkDevices(networkId=network_id)
for device in meraki_devices:
if "MV" in device['model']:
mv_serial_num = device['serial']
mv_name = device.get('name', mv_serial_num)
mv_address = device.get('address')
if mv_address == '':
print('Address not configured. Configure an address from Cameras > [Select Camera] > Network > Address.')
continue
zones = dashboard.camera.getDeviceCameraAnalyticsZones(mv_serial_num)
for zone in zones:
zone_id = zone['zoneId']
zone_name = zone.get('label', zone_id)
print(mv_address, mv_serial_num, zone_name)
t0 = start_time_utc
for i in range(0,24):
count = 0
analytics = dashboard.camera.getDeviceCameraAnalyticsZoneHistory(serial=mv_serial_num, zoneId=zone_id, t0=t0.isoformat() + 'Z', resolution=60, timespan=3600)
for analytic in analytics:
count += int(analytic['entrances'])
# print('{},{},{}:00,{}'.format(mv_address, zone_name, (t0 + datetime.timedelta(hours=TIMEZONE_OFFSETS[network_timezone])).hour, round(count/2)))
f.write('{},{},{}:00,{}\n'.format(mv_address, zone_name, (t0 + datetime.timedelta(hours=TIMEZONE_OFFSETS[network_timezone])).hour, round(count/2)))
t0 = t0 + datetime.timedelta(hours=1)
f.close()
else:
print('Obtain a Meraki API key from the Dashboard. Click on your account on the upper right after logging in > My profile > API access.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment