Django for IoT: From hackathon to production (DjangoCon US)

43
Django for Internet of Things: from hackathon to production Anna Schneider DjangoCon US, 18 July 2016 @windupanna

Transcript of Django for IoT: From hackathon to production (DjangoCon US)

Page 1: Django for IoT: From hackathon to production (DjangoCon US)

Django for Internet of Things: from hackathon

to production

Anna Schneider DjangoCon US, 18 July 2016

@windupanna

Page 2: Django for IoT: From hackathon to production (DjangoCon US)

I’m Anna :)

Co-founder and CTO at WattTime

WattTime.org @wattTime

Page 3: Django for IoT: From hackathon to production (DjangoCon US)

UnconsciousBiasProject.org @UBP_STEM

Page 4: Django for IoT: From hackathon to production (DjangoCon US)

what??

Django for Internet of Things: from hackathon

to production

Page 5: Django for IoT: From hackathon to production (DjangoCon US)

everyone’s favorite framework

🎸🐍Django

Page 6: Django for IoT: From hackathon to production (DjangoCon US)

Internet of Things (IoT)when a thing you don’t normally think of as a computer can transmit data and respond to controls in real time

🏡🚗📡🎮⏰

Page 7: Django for IoT: From hackathon to production (DjangoCon US)

some people like to write the code on the thing, I like to talk to things through their APIs

Internet of Things (IoT)

☁️ 🚗☁️you them the thing

Page 8: Django for IoT: From hackathon to production (DjangoCon US)

a really fun way to write really fragile code really quickly

hackathon

💻☕️👏⚠️

Page 9: Django for IoT: From hackathon to production (DjangoCon US)

when you can ship your code and trust it while you’re having fun at DjangoCon

production

💻🍎🚢😎

Page 10: Django for IoT: From hackathon to production (DjangoCon US)

design patterns (and anti-patterns) for writing and deploying Django projects

to monitor and control an IoT device using its API so you can get started fast then build well

what you’ll learn

☕️ 🚢

😱

Page 11: Django for IoT: From hackathon to production (DjangoCon US)

python manage.py startproject awesome_iot_hackathon

💻☕️👏⚠️

Page 12: Django for IoT: From hackathon to production (DjangoCon US)

models!

Page 13: Django for IoT: From hackathon to production (DjangoCon US)

the books app

Book title

author year

Book title

author year

Author name

hometown

Book title

author year

Book title

author year

Author name

hometown

Author name

hometown

Page 14: Django for IoT: From hackathon to production (DjangoCon US)

the books IoT app

Observation value

device timestamp

Device name

location

Device name

location

Device name

location

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Page 15: Django for IoT: From hackathon to production (DjangoCon US)

the IoT appDevice name

location vendor ID

Device name

location vendor ID

Device name

location vendor ID

☕️🚢

vendor’s unique ID(s) whatever data you need

to send to their API

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Page 16: Django for IoT: From hackathon to production (DjangoCon US)

Observation value

device timestamp

the IoT app

plan for big(gish) time series data eg use db_index

🚢

Device name

location vendor ID

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Device name

location vendor ID

Device name

location vendor ID

Page 17: Django for IoT: From hackathon to production (DjangoCon US)

the IoT app

Attribute vs Status numerical vs str/bool

values

Observation value

device timestamp

Device name

location vendor ID

Observation value

device timestamp

Observation value

device timestamp

Attribute value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Attribute value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Attribute value

device timestamp

Device name

location vendor ID

Device name

location vendor ID

Observation value

device timestamp

Observation value

device timestamp

Observation value

device timestamp

Status is_on

device timestamp

☕️🚢

Page 18: Django for IoT: From hackathon to production (DjangoCon US)

views?models

Page 19: Django for IoT: From hackathon to production (DjangoCon US)

views? tasks!

Page 20: Django for IoT: From hackathon to production (DjangoCon US)

☁️ 🚗☁️you them the thing

request

response

☁️ 💻you them

request

response

normal views

IoT tasks

Page 21: Django for IoT: From hackathon to production (DjangoCon US)

what tasks do we need to do?

pull new attributes pull new on/off statuses

set attributes set on/off statuses

monitor control

Page 22: Django for IoT: From hackathon to production (DjangoCon US)

tasks.pydef do_something_awesome(device):

# decide to turn on or off is_on = run_decision_criterion()

# set status using device API set_status(device, is_on)

# create status in db status = device.status_set.create( is_on=is_on, valid_at=timezone.now(), )

# return return status

☕️

the awesome part

Page 23: Django for IoT: From hackathon to production (DjangoCon US)

tasks.pydef do_something_awesome(device):

# decide to turn on or off is_on = run_decision_criterion()

# set status using device API set_status(device, is_on)

# create status in db status = device.status_set.create( is_on=is_on, valid_at=timezone.now(), )

# return return status

🚢

bad for asynchronous

😱

Page 24: Django for IoT: From hackathon to production (DjangoCon US)

tasks.pydef do_something_awesome(device_id): # get device from pk device = Device.objects.get( pk=device_id )

# decide to turn on or off is_on = run_decision_criterion()

# set status using device API set_status(device, is_on)

# create status in db status = device.status_set.create( is_on=is_on, valid_at=timezone.now(), )

# return return [status.pk]

pass pks pro: don’t rely on database state

con: may be extra queries

🚢

Page 25: Django for IoT: From hackathon to production (DjangoCon US)

put it together!

models

tasks

Page 26: Django for IoT: From hackathon to production (DjangoCon US)

the hackathon appmyapp

models.py Device Attribute Status

tasks.py pull_status set_status pull_attributes set_attributes

client.py admin.py views.py tests.py

☕️

Page 27: Django for IoT: From hackathon to production (DjangoCon US)

the production apps

devices models.py admin.py views.py tests.py

interactions tasks.py views.py tests.py

vendor client.py tests.py

observations models.py admin.py views.py tests.py

views for adding/removing devices

analytics, DRF for dashboards

models for logging, views for clickable tasks

swappable vendors

🚢

Page 28: Django for IoT: From hackathon to production (DjangoCon US)

deploy!

models

tasks

apps

Page 29: Django for IoT: From hackathon to production (DjangoCon US)

deploy tasks?

goals: • run any task (control or monitor) • at frequent, deterministic times • outside of request/response cycle

cron in the cloud

Page 30: Django for IoT: From hackathon to production (DjangoCon US)

1) the hackathon way

two ways to automate

management commands + Heroku Scheduler

☕️

python manage.py do_something_awesome --device_id=1

Page 31: Django for IoT: From hackathon to production (DjangoCon US)

Heroku Scheduler

pros: • easy to set up

cons: • limited frequencies

(daily, hourly, 10 min) • “best effort” service

(may skip some)

☕️

Page 32: Django for IoT: From hackathon to production (DjangoCon US)

2) the production waytask functions + celery periodic task scheduler

🚢two ways to automate

Page 33: Django for IoT: From hackathon to production (DjangoCon US)

Celery

distributed message queuing system for asynchronous stuff

slow event-driven tasks

send the user sign-up email

scheduled periodic tasks

run the daily report

🚢

Page 34: Django for IoT: From hackathon to production (DjangoCon US)

web servers

Celery architecture (how)

tasks

tasks

worker servers

messages results result storemessage broker transport queue

messages

tasks

scheduler

🚢

Page 35: Django for IoT: From hackathon to production (DjangoCon US)

Celery architecture (how)

schedulerdo_awesome do_awesome

workerstatus.pk

☁️turn on

on!Status(is_on=True)

🚢

Page 36: Django for IoT: From hackathon to production (DjangoCon US)

from celery import shared_task

@shared_task def set_status(device_id): ...

@shared_task def do_something_awesome(device_id): ...

just add decorator :)

tasks.py

🚢

(what)

Page 37: Django for IoT: From hackathon to production (DjangoCon US)

from celery.schedules import crontab

SCHEDULE = { 'run_task': { # use the actual path 'task': ‘do_something_awesome’,

# every 5 minutes 'schedule': crontab(minute='*/5'),

# args and kwargs 'args': [], 'kwargs': {},

}, }

very close to crontab once per minute - once per year

schedule.py

🚢

(when)

Page 38: Django for IoT: From hackathon to production (DjangoCon US)

from celery.schedules import crontab

SCHEDULE = { 'run_task': { # use the actual path 'task': ‘do_something_awesome’,

# every 5 minutes 'schedule': crontab(minute='*/5'),

# args and kwargs 'args': [], 'kwargs': {'device_id': d.pk},

} for d in Device.objects.all() }

schedule.py

🚢

static arguments only code is only evaluated once at run time, better to have one task spawn daughters

(when)

😱

Page 39: Django for IoT: From hackathon to production (DjangoCon US)

from celery.schedules import crontab

SCHEDULE = { 'run_task': { # use the actual path 'task': ‘run_all',

# every 5 minutes 'schedule': crontab(minute='*/5'),

# args and kwargs 'args': [], 'kwargs': {},

} }

@shared_task def run_all(): for d in Device.objects.all(): do_something_awesome(d.pk)

schedule.py

🚢

static arguments only code is only evaluated once at run time, better to have one task spawn daughters

(when)

Page 40: Django for IoT: From hackathon to production (DjangoCon US)

production, Celerified

devices models.py admin.py views.py tests.py

interactions tasks.py schedule.py views.py tests.py

brand client.py tests.py

observations models.py admin.py views.py tests.py

🚢

django_iot __init__.py celery.py settings.py wsgi.py urls.py

django_iot Procfile requirements.txt manage.py

Page 41: Django for IoT: From hackathon to production (DjangoCon US)

cookiecutter https://github.com/aschn/cookiecutter-django-iot.git

☕️🚢

Page 42: Django for IoT: From hackathon to production (DjangoCon US)

• think time series data: mind your models • async not request/response: tasks not views • ☕ ️easy but rigid: Heroku Scheduler • 🚢 flexible but complex: celery periodic tasks • hack better+faster: cookiecutter-django-iot • IoT can be for good, not just for fun and profit

what have we learned?

Page 43: Django for IoT: From hackathon to production (DjangoCon US)

Thanks!!

Anna Schneider @windupanna

@wattTime

cookiecutter https://github.com/aschn/cookiecutter-django-iot.git