Snippets

Espec North America Example of Espec North America's web controller RESTful API

Created by Myles Metzler last modified
'''
Some basic examples of using Espec North Americas Web Controller RESTful API.

These examples require firmware version at least version 2.3.
'''
import requests, json, time

def parse(rsp):
    if rsp.status_code < 200 or rsp.status_code >= 300:
        print 'ERROR code: {0}, Exception: "{exception}", message: "{message}"'.format(rsp.status_code, **rsp.json())
        print 'The script will stop due to an error.'
        exit(1)
    try:
        return rsp.json()
    except Exception:
        # Response was not json return the raw text
        return rsp.text

def pprint(data):
    print(json.dumps(data, sort_keys=True, indent=4))

host = 'http://wc.espec.com'
headers = {'Content-type': 'application/json'}



print 'Get current chamber state as one request:'
rsp = parse(requests.get(host + '/api/edge/chambers/1'))
print '\tCurrent Chamber Mode: {mode}'.format(**rsp['operations'])

# The front end application (HTML/javascript/knockout.js) is just a large template that this api
# information bound to. By having this data in a list/array we can have as many structures
# as are needed for the chamber; while this does make things more complex it means
# for large custom projects NO custom software is needed.
template = u'\t{name} setpoint:{setValue[current]}{units}, processValue:{processValue[air]}{units}'
for loop in rsp['loops']:
    print template.format(**loop)



print 'Get curent chamber state as multiple requests:'
rsp = parse(requests.get(host + '/api/edge/chambers/1/operations/mode'))['data']
print '\tCurrent Chamber Mode: {}'.format(rsp)

# This example shows how not using the list/array requires more logic specific to the application.
# Meaning this section only works for a temperature/humidity chamber the other example above
# Works for ANY chamber.
for loop in ['Temperature', 'Humidity']:
    url = '{}/api/edge/chambers/1/loops/{}/{}'
    setval = parse(requests.get(url.format(host, loop, 'setValue/current')))['data']
    unit = parse(requests.get(url.format(host, loop, 'units')))['data']
    proc = parse(requests.get(url.format(host, loop, 'processValue/air')))['data']
    print u'\t{0} setpoint: {1}{2}, processValue:{3}{2}'.format(loop, setval, unit, proc)



print 'Run constant mode at 75/85'
# These two lines do exactly the same thing as the next two, some people find the next two more clear.
data = [{'name':'Temperature', 'setValue':75.0}, {'name': 'Humidity', 'setValue':85.0}]
parse(requests.post(host + '/api/edge/chambers/1/loops', data=json.dumps(data), headers=headers))
parse(requests.post(host + '/api/edge/chambers/1/loops/Temperature/setValue', data=json.dumps(75), headers=headers))
parse(requests.post(host + '/api/edge/chambers/1/loops/Humidity/setValue', data=json.dumps(85), headers=headers))

#start constant mode
parse(requests.post(host + '/api/edge/chambers/1/operations/constant'))



print 'wait 10 seconds, then stop chamber.'
time.sleep(10)
parse(requests.post(host + '/api/edge/chambers/1/operations/standby'))



print 'wait 10 seconds, then start the first available program; and print that program'
time.sleep(10)
programs = parse(requests.get(host + '/api/edge/chambers/1/programs'))['programs']
for i, program in enumerate(programs):
    if not program['empty']:
        parse(requests.post('{}/api/edge/chambers/1/operations/program/{}'.format(host, program['number'])))
        print pprint(parse(requests.get(program['uri'])))
        break
    if len(programs) - 1 == i:
        print 'No program to run'

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.