'''Some basic examples of using Espec North Americas Web Controller RESTful API.These examples require firmware version at least version 2.3.'''importrequests,json,timedefparse(rsp):ifrsp.status_code<200orrsp.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:returnrsp.json()exceptException:# Response was not json return the raw textreturnrsp.textdefpprint(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}'forloopinrsp['loops']:printtemplate.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.forloopin['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']printu'\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 modeparse(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']fori,programinenumerate(programs):ifnotprogram['empty']:parse(requests.post('{}/api/edge/chambers/1/operations/program/{}'.format(host,program['number'])))printpprint(parse(requests.get(program['uri'])))breakiflen(programs)-1==i:print'No program to run'
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.