class derivedKPI(object): """ Class to implement derived KPIs calculation""" def __init__(self): """ Initialize frequently used parameters""" file_name = os.path.basename(__file__).split('.')[0] self.logger = log_function(file_name) self.config_obj = Config() user_authentication = self.config_obj.user_authentication() gateway = self.config_obj.gateway() derived_KPI = self.config_obj.derived_KPI() self.auth_URL = user_authentication['Auth_URL'] self.derivedKPI_url = derived_KPI['DerivedKPI_url'] self.derivedKPI_fetch_interval = derived_KPI['DerivedKPI_fetch_interval'] self.access_key = user_authentication['access_key'] self.user_key = user_authentication['user_key'] self.user_id = user_authentication['user_id'] self.token = user_authentication['token'] self.application_id = user_authentication['application_id'] self.auth_password = user_authentication['password'] self.gateway_id = gateway['gateway_id'] self.redis_obj_perf_data = redis_conn(db_name = 'REDIS_PERF_DATA') redis_obj_derived_kpi_data = redis_conn(db_name ='REDIS_DERIVED_KPI') try : self.api_response = eval(redis_obj_derived_kpi_data.get("Derived_KPI")) except Exception,error : self.api_response = None self.redis_obj_derived_kpi_data = redis_conn(db_name ='REDIS_DERIVED_KPI')
def __init__(self): """ From config file, get and initialization all variables. """ file_name = os.path.basename(__file__).split('.')[0] self.logger = log_function(file_name) self.redis_obj_register = redis_conn(db_name='REDIS_REGISTER') self.config_obj = Config()
def __init__(self): file_name = os.path.basename(__file__).split('.')[0] #self.logger = log_function(file_name) self.config_obj = Config() iothub = self.config_obj.iothub() self.IoTHub_IP = iothub['IP'] self.IoTHub_port = iothub['Port'] self.publisher = False self.derivedKPI_obj = derivedKPI() self.redis_obj_perf_data = redis_conn('REDIS_PERF_DATA') connection_lost = self.config_obj.wait_for_connection() self.DataSource = connection_lost['data_source'] self.ServiceName = connection_lost['service_name']
def __init__(self): self.config_obj = Config() self.restrict_data = self.config_obj.gateway()['restrict_data']
class Formatter(): def __init__(self): self.config_obj = Config() self.restrict_data = self.config_obj.gateway()['restrict_data'] def format_data(self,formatted_data,device_id = None,service_name = None, data_source = None,\ current_value = None,check_timestamp= None,sys_timestamp = None,\ is_lat = False, is_long = False,is_host_state = False,\ on_state_value = None, off_state_value = None): """ Format data in list of JSON format including keys for device_id, service_name, data_source, current_value, check_timestamp, sys_timestamp, and all formatted data store in In-memory DB Args: device_id - Registered device ID service_name - Parameter service name data_source - Parameter data source current_value - Parameter current value check_timestamp- Parameter value change time sys_timestamp - Parameter value storage time return: Formatted JSON list """ current_value = str(current_value) device_data_dic = {} global last_up_time if formatted_data == []: """ Read last up time of device for which data was sent to pick the previous values for comparing """ try: last_up_time = eval(redis_obj_perf_data.get(str('Device:'+device_id))) except Exception,error: pass state = None if device_id is not None and service_name is not None and data_source is not None : """ Condition to add 'host_status' service/data-source if a particular service can be used to determine ON/OFF status of device """ if is_host_state is not None and on_state_value is not None and off_state_value is not None : if current_value == on_state_value: state = "ON" elif current_value == off_state_value: state = "OFF" device_data_dic= { "device_id": device_id, "service_name": "host_status", "data_source": "host_status", "current_value": state, "check_timestamp": sys_timestamp , "sys_timestamp": sys_timestamp, } if self.restrict_data == 'True': try: """ Read last value of parameter and compare with current value If Value is not equal add status as "insert" Else add status as "update" In case the value is read first time add status as "insert" After reading last value update the current value in in-memory DB for reading value in next cycle """ get_value = redis_obj_perf_data.get((str(device_id) + '_' + "host_status" + '_' + "host_status" + '_' + str(last_up_time))) if eval(get_value) == eval(state): device_data_dic.update({"status":"update"}) else: device_data_dic.update({"status":"insert"}) redis_obj_perf_data.set(str(device_id) + '_' + "host_status" + '_' + "host_status" + '_' + str(sys_timestamp),state) except Exception: redis_obj_perf_data.set(str(device_id) + '_' + "host_status" + '_' + "host_status" + '_' + str(sys_timestamp),state) device_data_dic.update({"status":"insert"}) formatted_data.append(device_data_dic) device_data_dic= { "device_id": device_id, "service_name": service_name, "data_source": data_source, "current_value": current_value, "check_timestamp": check_timestamp , "sys_timestamp": sys_timestamp, } if self.restrict_data == 'True': try: get_value = redis_obj_perf_data.get((str(device_id) + '_' + service_name + '_' + data_source + '_' + str(last_up_time))) if eval(get_value) == eval(device_data_dic["current_value"]): device_data_dic.update({"status":"update"}) else: device_data_dic.update({"status":"insert"}) redis_obj_perf_data.set(str(device_id) + '_' + service_name + '_' + data_source + '_' + str(sys_timestamp),device_data_dic[ "current_value"]) except Exception, error: redis_obj_perf_data.set(str(device_id) + '_' + service_name + '_' + data_source + '_' + str(sys_timestamp),device_data_dic[ "current_value"]) device_data_dic.update({"status":"insert"}) formatted_data.append(device_data_dic) elif is_lat : """ Condition to add 'xFusion_location'/'xFusion_latitude' service/data-source if a particular service can be used to determine location of device """ device_data_dic = { "device_id": device_id, "service_name": 'xFusion_location', "data_source": 'xFusion_latitude', "current_value": current_value, "check_timestamp": check_timestamp , "sys_timestamp": sys_timestamp, } if self.restrict_data == 'True': try: get_value = eval(redis_obj_perf_data.get(str(device_id) + '_' + 'xFusion_location' + '_' + 'xFusion_latitude' + '_' + str(last_up_time))) if get_value == device_data_dic["current_value"]: device_data_dic.update({"status":"update"}) else: device_data_dic.update({"status":"insert"}) redis_obj_perf_data.set(str(device_id) + '_' + 'xFusion_location' + '_' + 'xFusion_latitude' + '_' + str(sys_timestamp),device_data_dic[ "current_value"]) except Exception, error: redis_obj_perf_data.set(str(device_id) + '_' + 'xFusion_location' + '_' + 'xFusion_latitude' + '_' + str(sys_timestamp),device_data_dic[ "current_value"]) device_data_dic.update({"status":"insert"}) formatted_data.append(device_data_dic) device_data_dic ={ "device_id": device_id, "service_name": service_name, "data_source": data_source, "current_value": current_value, "check_timestamp": check_timestamp , "sys_timestamp": sys_timestamp, } if self.restrict_data == 'True': try: get_value = redis_obj_perf_data.get((str(device_id) + '_' + service_name + '_' + data_source + '_' + str(last_up_time))) if get_value == device_data_dic["current_value"]: device_data_dic.update({"status":"update"}) else: device_data_dic.update({"status":"insert"}) redis_obj_perf_data.set(str(device_id) + '_' + service_name + '_' + data_source + '_' + str(sys_timestamp),device_data_dic[ "current_value"]) except Exception, error: redis_obj_perf_data.set(str(device_id) + '_' + service_name + '_' + data_source + '_' + str(sys_timestamp),device_data_dic[ "current_value"]) device_data_dic.update({"status":"insert"})
class PublisherIOTHub(object): def __init__(self): file_name = os.path.basename(__file__).split('.')[0] #self.logger = log_function(file_name) self.config_obj = Config() iothub = self.config_obj.iothub() self.IoTHub_IP = iothub['IP'] self.IoTHub_port = iothub['Port'] self.publisher = False self.derivedKPI_obj = derivedKPI() self.redis_obj_perf_data = redis_conn('REDIS_PERF_DATA') connection_lost = self.config_obj.wait_for_connection() self.DataSource = connection_lost['data_source'] self.ServiceName = connection_lost['service_name'] """ publish(self,deviceId,content) Function to call encryptData() and publishredis_conn_to_IoTHub() if publisher object exists and publisher object doesn't exists then data send on redis-dbs Parameters : DeviceID and Performance Data Returns : Encrypted data """ def publish(self, deviceId, connecting_string, content, sys_timestamp): formatter_obj = Formatter() check_timestamp = self.redis_obj_perf_data.get( str(deviceId) + "_check_timestamp") update_content = [] for dict_data in content: if 'host_status' in dict_data['service_name']: add_host_status = False break else: add_host_status = True if add_host_status: formatted_data = [] formatted_data = formatter_obj.format_data(formatted_data=formatted_data,device_id=deviceId,service_name='host_status',\ data_source = 'host_status',current_value = "ON",\ check_timestamp=check_timestamp,sys_timestamp=sys_timestamp) # self.redis_obj_perf_data.set("%s_check_timestamp" % str(deviceId), sys_timestamp) content.extend(formatted_data) derived_data = self.derivedKPI_obj.calculate_derived_KPI( deviceId, sys_timestamp) if derived_data: content.extend(derived_data) """ when publisher object doesn't exists we store content list on redis. and store only those records on redis which is different to previous records and data publish on IOT Hub when publisher object exists.. """ #CONNECTION_STRING = 'HostName=ttpliot.azure-devices.net;DeviceId=Thermostat;SharedAccessKey=qq77l72Y8RwBwy/SevxGZA==' #PROTOCOL = "MQTT" CONNECTION_STRING = 'HostName=ttpliot.azure-devices.net;DeviceId=Thermostat;SharedAccessKey=qq77l72Y8RwBwy/SevxGZA==' client = iothub_client_conn(connecting_string) if client: encryptedcontent = encryptData(self.IoTHub_IP, self.IoTHub_port, deviceId, str(update_content)) #print encryptedcontent iothub_client_publish_message(connecting_string, deviceId, encryptedcontent) #self.logger.debug('Published to IOTHub !!!!: %s' %(deviceId )) self.redis_obj_perf_data.set("Device:%s" % str(deviceId), sys_timestamp) self.redis_obj_perf_data.set( str(deviceId) + "_Device_last_up_time", int(time.time()))
Script to manage device state ''' from logger import log_function from datetime import datetime, timedelta import os from IotGateway.connection import redis_conn from IotGateway.config import Config from IotGateway.publishData.publisher import PublisherIOTHub file_name = (os.path.basename(__file__)).split('.')[0] redis_obj_perf_data = redis_conn('REDIS_PERF_DATA') logger = log_function(file_name) pub_obj = PublisherIOTHub() config_obj = Config() gateway_conf = config_obj.gateway() #from start.entry import app #@app.task(name='set_state_to_off', ignore_result=True) def set_state_to_off(): """ set_state_to_off(self) Function to set device state to if no packet received in device down threshold time defined in config.ini Parameters : None Returns : None """ try: device_list = redis_obj_perf_data.keys('Device:*')
''' Created on 27-May-2016 @author: TERAMATRIX/Rupam.Kumari Script to handle logging and send mail in case of exception ''' #!/usr/bin/python import os import smtplib import string import logging.handlers from datetime import datetime, timedelta from IotGateway.config import Config from string import upper config_obj = Config() format_simple = config_obj.format_simple() email_config = config_obj.email_config() last_mail_time_config = config_obj.last_mail_time() logger_set = config_obj.set() #print last_mail_time user_authentication = config_obj.user_authentication() #last_mail_time_conf=config_obj.last_mail_time_conf #print last_mail_time_conf,"last_mail_time_conf" #log_config_file = os.path.dirname(os.path.abspath(__file__)) loggers = {} class TlsSMTPHandler(logging.handlers.SMTPHandler): def __init__(self, mailhost,
class registration(object): def __init__(self): """ From config file, get and initialization all variables. """ file_name = os.path.basename(__file__).split('.')[0] self.logger = log_function(file_name) self.redis_obj_register = redis_conn(db_name='REDIS_REGISTER') self.config_obj = Config() def device_registration_push(self, device=None): """ If device id registered in gateway then get devices id from memcache otherwise call device registration api . if device registration api response status '401' then firstly call user authentication api and authenticate users. and register the devices on gateway and return device id and device id stored in memcache Args:device return: None(if device registration fail that time return None) """ try: primary_key = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnoo" secondary_key = "111222333444555666777888999000aaabbbcccdddee" device_id = self.redis_obj_register.get(device) info = self.redis_obj_register.get( str(device_id) + "azure_device_info") if device_id and info: return device_id else: gateway = self.config_obj.gateway() device_registration = self.config_obj.device_registration() user_authentication = self.config_obj.user_authentication() iot_hub = self.config_obj.iothub() data = { 'device_id': device, 'gateway_id': gateway['gateway_id'], 'protocol': iot_hub['protocol'], 'access_key': user_authentication['access_key'], } headers = { 'token': user_authentication['token'], 'user_id': user_authentication['user_id'], 'user_key': user_authentication['user_key'] } #print device_registration['device_registation_url'],data response = requests.post(device_registration['device_registation_url'],\ data = data,verify=False) if response.status_code == 401: authData = { 'user_name': user_authentication['user_id'], 'password': user_authentication['password'], 'application_id': user_authentication['application_id'] } authResponse = requests.post( user_authentication['Auth_URL'], data=authData, headers=headers, verify=False) authResponseData = json.loads(authResponse.content) if authResponseData['valid']: authResponseData = authResponseData['object'] token = authResponseData['access_token'] user_key = authResponseData['userKey'] access_key = authResponseData['access_key'] self.config_obj.write_user_authentication( token, user_key, access_key) # data['token'] = authResponseData['access_token'] # data['userKey'] = authResponseData['userKey'] # data['access_key'] = authResponseData['access_key'] headers['user_key'] = authResponseData['userKey'] headers['token'] = authResponseData['access_token'] response = requests.post(device_registration['device_registation_url'],\ data = data, headers=headers, verify=False) response_data = json.loads(response.content) print "response_data", response_data self.logger.error("--- Registration response --- :{0}".format( str(response_data))) if response_data['valid'] and len(response_data['object']) > 0: device_id = response_data['object'] id = device_id[0]['id'] print "id", id deviceId = get_device(id) print deviceId if deviceId: print "device_id" device_info = "HostName=ttpliot.azure-devices.net" + ";" + str( deviceId) + ";" + str(primary_key) self.redis_obj_register.set(device, device_id[0]['id']) self.redis_obj_register.set( str(device_id[0]['id']) + "azure_device_info", device_info) else: print "device_id[0]['id']", device_id[0][ 'id'], primary_key, secondary_key device_info = iothub_create_device( device_id[0]['id'], primary_key, secondary_key) self.redis_obj_register.set(device, device_id[0]['id']) self.redis_obj_register.set( str(device_id) + "azure_device_info", device_info) else: self.logger.error("Device Registration fail %s", response.text) return None except Exception as e: self.logger.exception('Exception in device_registration_push: %s', e)
''' Created on 06-Aug-2016 @author: TERAMATRIX\rupam.kumari ''' import os import pymongo from redis import StrictRedis from pymongo import MongoClient from logger import log_function from IotGateway.config import Config file_name = os.path.basename(__file__) file_name = file_name.split('.')[0] config_obj = Config() mongo_conf = config_obj.mongo() redis_conf = config_obj.redis() def mongo_conn(): """ Function_name : mongo_conn (function for making mongo db connection) return: db Exception: PyMongoError """ #config = ConfigObj(CONF) host = mongo_conf['host']