diff --git a/DeviceAPI/API_AwairSensor.py b/DeviceAPI/API_AwairSensor.py new file mode 100644 index 0000000000000000000000000000000000000000..3bc5883d8b88457e246264642a7eb838ccfbada6 --- /dev/null +++ b/DeviceAPI/API_AwairSensor.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +from __future__ import division +from bemoss_lib.utils.BEMOSS_ONTOLOGY import BEMOSS_ONTOLOGY +from DeviceAPI.BaseAPI import baseAPI +from settings import AWAIR_TOKEN +import requests +import urllib2 +import datetime +import json + +class API(baseAPI): + def __init__(self,**kwargs): + super(API, self).__init__(**kwargs) + self.set_variable('connection_renew_interval',6000) + self.device_supports_auto = False + self.devices_url = 'https://beta-api.awair.is/v1/users/self/devices' + self._debug = False + + def API_info(self): + #TODO: create chart_template and html_template for awair + return [{'device_model' : 'Awair+', 'vendor_name' : 'Awair', 'communication' : 'WiFi', + 'device_type_id' : 4,'api_name': 'API_AwairSensor', 'html_template': '---', 'agent_type': 'BasicAgent', + 'identifiable': False, 'authorizable': False, 'is_cloud_device' : True, + 'schedule_weekday_period': '4', 'schedule_weekend_period': '4', + 'allow_schedule_period_delete': False, 'chart_template': '---'}] + + def dashboard_view(self): + return {"top": {"type": "number", "value": "co2"}, "center": {"type": "number", "value": "voc"}, + "bottom": {"type": "number", "value": "dust"}} + + def ontology(self): + return {"co2":BEMOSS_ONTOLOGY.CO2,"dust":BEMOSS_ONTOLOGY.DUST, + "temperature":BEMOSS_ONTOLOGY.TEMPERATURE,"VOC":BEMOSS_ONTOLOGY.VOC, + "humidity":BEMOSS_ONTOLOGY.RELATIVE_HUMIDITY} + + def discover(self, username, password): + self.token = password + head_auth = {'Authorization' : self.token} + discovered_devices = list() + try: + devices_req = requests.get(self.devices_url, headers = head_auth, timeout=10) + except Exception as er: + print "AwairSensor API fails HTTP request" + print er + return discovered_devices + + if devices_req is not None: + devices_json = devices_req.json() + model_vendor = self.getModelVendor() + discovered_devices = [{'address': str(device['device_id']), 'mac': str(device['device_id'])} for device in devices_json['data']] + for device in discovered_devices: + device.update(model_vendor) + + if self._debug: + print discovered_devices + + return discovered_devices + + def getModelVendor(self): + return {'model': 'Awair+', 'vendor': 'Awair'} + + # GET Open the URL and read the data + def getDataFromDevice(self): + awair_id = self.get_variable('address') + head_auth = {'Authorization': self.get_variable('password')} + + device_url = 'https://beta-api.awair.is/v1/devices/' + str(awair_id) + '/events/15min-avg' + start = (datetime.datetime.now()-datetime.timedelta(minutes=15)).isoformat() + end = datetime.datetime.now().isoformat() + timespan = {'from': start, 'to': end} + try: + data_req = requests.get(device_url,headers = head_auth, params = timespan) + + data_json = data_req.json() + for row in data_json['data']: + data = row['sensor'].values() + + self.set_variable('dust', data[0]) + self.set_variable('co2', data[1]) + self.set_variable('humidity', data[2]) + self.set_variable('temperature', data[3]*1.8+32) #convert to fahrenheit + self.set_variable('voc', data[4]) + + except Exception as er: + print "AwairAPI could not get device data" + print er + + + +# This main method will not be executed when this class is used as a module +def main(): + # Step1: create an object with initialized data from DeviceDiscovery Agent + # requirements for instantiation1. model, 2.type, 3.api, 4. address + Awair = API(model='Awair+',agent_id='iaqsensor',api='API_AwairSensor',password='Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTUxOSJ9.3YE0APseF-aLMcjTMSbN4bHgE--ZgcuWBEXVs-5TTO4') + print("{0}agent is initialzed for {1} using API={2}".format(Awair.get_variable('agent_id'),Awair.get_variable('model'),Awair.get_variable('api'))) + #Awair.getModelVendor() + Awair.getDeviceStatus() + +if __name__ == "__main__": main() \ No newline at end of file diff --git a/Web_Server/webapps/discovery/models.py b/Web_Server/webapps/discovery/models.py index 5b8b9237717f3b26f737742f3286d58a3505c1b8..aa196cce8aa945be400af18934c4eb77dd869707 100644 --- a/Web_Server/webapps/discovery/models.py +++ b/Web_Server/webapps/discovery/models.py @@ -59,7 +59,7 @@ kwargs = {'subscribe_address': __.SUB_SOCKET, class PasswordsManager(models.Model): device_model = models.ForeignKey(SupportedDevices, db_column="device_model") username = models.CharField(max_length=128) - password = models.CharField(_('password'), max_length=128) + password = models.CharField(_('password'), max_length=256) last_modified = models.DateTimeField() class Meta: diff --git a/bemoss_lib/utils/BEMOSS_ONTOLOGY.py b/bemoss_lib/utils/BEMOSS_ONTOLOGY.py index 886fd6af01caa654c23c5105a96c1fed23f31183..313896c6e747bc3db2f654f3f1db69dbb47fbde6 100644 --- a/bemoss_lib/utils/BEMOSS_ONTOLOGY.py +++ b/bemoss_lib/utils/BEMOSS_ONTOLOGY.py @@ -313,6 +313,19 @@ class BEMOSS_ONTOLOGY: ALTERNATE_NAMES = ['Carbondioxide'] SPOKEN_NAMES = ['carbon dioxide','carbon dioxide level'] + class VOC: + NAME = 'voc' + TYPE = 'float' + UNIT = 'ppb' + ALTERNATE_NAMES = ['VOC'] + SPOKEN_NAMES = ['volatile organic chemicals'] + + class DUST: + NAME = 'dust' + TYPE = 'float' + UNIT = 'ug/m^3' + ALTERNATE_NAMES = ['PM10'] + class NOISE: NAME = 'noise' TYPE = 'float' diff --git a/settings.py b/settings.py index 3188c06ee21c651fdf52b0cf52cf30ad24ec9299..b63755cbcb9b8d91586e621a1a3852685a285502 100644 --- a/settings.py +++ b/settings.py @@ -161,9 +161,6 @@ LANGUAGE_CODE = 'en-us' # Make this unique, and don't share it with anybody. SECRET_KEY = 'b4nr@$=^2)_g!_vz-nm_1$_!!jfh&2yn$6#a9klqyh28g*vjl%' -# Alexa Authentication PIN: -ALEXA_KEY = '93712' - #Weather Underground Key WUNDERGROUND_KEY = 'underground-key'