def __init__(self, name, core_services): self.__name = name self.__core = core_services self.wd = None self.restart_counter = 0 self.__tracer = get_tracer(name) # Settings # watchdog_interval: defines how often to stroke the watchdog in seconds # low_memory_threshold: reboot if below memory threshold (bytes) # auto_restart_interval: how long the device should run # before auto-restart in seconds. ## Settings Table Definition: settings_list = [ Setting( name='watchdog_interval', type=int, required=True, default_value=120, verify_function=lambda x: x >= 60 and x <= 3600), Setting( name='low_memory_threshold', type=long, required=False, verify_function=lambda x: x >= 40960), Setting( name='auto_restart_interval', type=long, required=False, verify_function=lambda x: x >= 600), ] ## Initialize the ServiceBase interface: ServiceBase.__init__(self, self.__name, settings_list)
def __init__(self, name, core_services): self.__name = name self.__core = core_services self.wd = None self.restart_counter = 0 self.__tracer = get_tracer(name) # Settings # watchdog_interval: defines how often to stroke the watchdog in seconds # low_memory_threshold: reboot if below memory threshold (bytes) # auto_restart_interval: how long the device should run # before auto-restart in seconds. ## Settings Table Definition: settings_list = [ Setting(name='watchdog_interval', type=int, required=True, default_value=120, verify_function=lambda x: x >= 60 and x <= 3600), Setting(name='low_memory_threshold', type=long, required=False, verify_function=lambda x: x >= 40960), Setting(name='auto_restart_interval', type=long, required=False, verify_function=lambda x: x >= 600), ] ## Initialize the ServiceBase interface: ServiceBase.__init__(self, self.__name, settings_list)
def __init__(self, name, core_services): self.__name = name self.__core = core_services from core.tracing import get_tracer self.__tracer = get_tracer(name) settings_list = [] ## Initialize settings: ServiceBase.__init__(self, name=name, settings_list=settings_list)
def __init__(self, name, core_services): self.__name = name self.__core = core_services from core.tracing import get_tracer self.__tracer = get_tracer(name) settings_list = [ ] ## Initialize settings: ServiceBase.__init__(self, name=name, settings_list=settings_list)
def __init__(self, name, core_services): """Standard :class:`ServiceBase` initialization""" self.__name = name self.__core = core_services self._channel_sources = {} self._enrolled_sources = {} settings_list = [ Setting( name='instance_list', type=list, required=False, default_value=[]), ] ServiceBase.__init__(self, name, settings_list)
def __init__(self, name, core_services): """\ Initialize TimingService Standard service arguments see :class:`ServiceBase` """ self.__name = name self.__core = core_services self._channel_sources = {} self._enrolled_sources = {} settings_list = [ Setting( name='timings', type=dict, required=False, default_value=[]), ] ServiceBase.__init__(self, name, settings_list)
def __init__(self, name, core_services): self.__name = name self.__core = core_services self.xbee_device_manager_name = None ## Settings Table Definition: settings_list = [ Setting( name='server_ip', type=str, required=False, default_value="192.168.33.106"), Setting( name='server_port', type=int, required=False, default_value="8080"), ] ## Channel Properties Definition: #property_list = [ ] ## Initialize the DeviceBase interface: ServiceBase.__init__(self, self.__name, settings_list) ## Thread initialization: self.__stopevent = threading.Event() threading.Thread.__init__(self, name=name) #threading.Thread.setDaemon(self, True) self.chs_last_ts = {} #self.device_list=device_list #it shoud be self.device_list so we can see it from others Classes/Objects. for i in device_list: device_id = i device_type = device_list[i].get('type').lower() device_name = device_list[i].get('name') device_mac = device_list[i].get('mac') device_sample_rate = device_list[i].get('sample_rate') instance_name = device_name driver_path = Driver_map[device_type].get('path') settings = {} if Driver_map[device_type].get('settings').find('xbee_device_manager') != -1: if(self.xbee_device_manager_name == None): self.xbee_device_manager_name = 'xbee_device_manager' self.driver_add('xbee_device_manager', 'devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager', settings={'worker_threads':2}) time.sleep(1) settings['xbee_device_manager'] = self.xbee_device_manager_name settings['extended_address'] = device_mac if Driver_map[device_type].get('settings').find('sample_rate_ms') != -1: if(device_sample_rate == None): device_sample_rate = 30.0 settings['sample_rate_ms'] = int(float(device_sample_rate) * 1000) elif Driver_map[device_type].get('settings').find('sample_rate_sec') != -1: if(device_sample_rate == None): device_sample_rate = 1.0 settings['sample_rate_sec'] = float(device_sample_rate) if Driver_map[device_type].get('settings').find('channel_settings') != -1: settings['channel_settings'] = Driver_map[device_type].get('channel_settings') self.driver_add(instance_name, driver_path, settings) ## itty Rest functions for get, post, put, delete requests: ## CNDEP functions: cndep_path = "/cndep/" @get(cndep_path + 'help') @get(cndep_path + 'description') def Description(request): url = 'http://dsnet.tu-plovdiv.bg/cndep/rest/CNDEP_REST_wadl' data = urllib.urlopen(url) return Response(data.read(), content_type='text/xml') @get(cndep_path + 'device/list') def GetList(request): if('full' in request.GET): full = True else: full = False xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response rows="%d" status="200">\n' % len(device_list) for i in device_list: xml_response += self.xml_creator(i, full, True) xml_response += '</response>' return Response(xml_response, content_type='text/xml') @get(cndep_path + 'device/all') def GetAll(request): if('full' in request.GET): full = True else: full = False xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response rows="%d" status="200">\n' % len(device_list) for i in device_list: xml_response += self.xml_creator(i, full) xml_response += '</response>' return Response(xml_response, content_type='text/xml') @get(cndep_path + 'device/id_from_name/(?P<device_name>\w+)') def GetId(request, device_name=None): for i in device_list: if(device_list[i].get('name') == device_name): xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="200">\n' xml_response += '<device id="%s"/>\n' % i xml_response += '</response>' return Response(xml_response, content_type='text/xml') raise NotFound('Device Name not found!') @get(cndep_path + 'device/(?P<device_id>\w+)') def GetDevice(request, device_id=None): if(device_id == None): raise NotFound('Device not found!') if('full' in request.GET): full = True else: full = False if(device_id in device_list): xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="200">\n' xml_response += self.xml_creator(device_id, full) xml_response += '</response>' return Response(xml_response, content_type='text/xml') else: raise NotFound('Device not found!') @get(cndep_path + 'device/(?P<device_id>\w+)/(?P<channel_name>\w+)') def GetSensor(request, device_id, channel_name=None): if(device_id in device_list): #device found in device_list device = device_list.get(device_id) xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="200">\n' if(device.get('type') in Device_types): instance_name = device.get('name') data = self.driver_channel_get(instance_name, channel_name) xml_response += '<device id="%s">' % device_id value = data.value unit = data.unit xml_response += '\n<sensor name="%s" value="%s" unit="%s"/>' % (channel_name, value, unit) xml_response += '\n</device>' xml_response += '\n</response>' return Response(xml_response, content_type='text/xml') else: raise NotFound('Device not found!') @put(cndep_path + 'device') def AddDevice(request): xml_request = request.body root = ET.fromstring(xml_request) device = root.find('device') device_id = device.get('id') if(device_id == None): device_id = 1 elif(not device_id.isdigit()): raise Conflict('Device Id is not integer!') else: device_id = int(device_id) device_type = device.get('type') if(device_type == None or device_type == ''): raise Conflict('Device Type is empty!') device_type = device_type.lower() if(device_type not in Device_types): raise Conflict('Device Type not found!') device_name = device.get('name') if(device_name == None): raise Conflict('Device Name is empty!') flag = 0 for i in device_list: if(device_list[i].get('name') == device_name): flag = 1 break if(flag): raise Conflict('Device Name already exist!') device_mac = device.get('mac') if(device_mac == None): raise Conflict('Device Mac is empty!') device_sample_rate = device.get('sample_rate') if(device_sample_rate == None): device_sample_rate = 2.0 else: try: device_sample_rate = float(device_sample_rate) except: raise Conflict('Device Sample Rate is not integer or float!') while(str(device_id) in device_list): #if device_id exist -> device_id++ and try again device_id += 1 device_id = str(device_id) device_list[device_id] = {'type': device_type, 'name': device_name, 'mac': device_mac, 'sample_rate': device_sample_rate} instance_name = device_name driver_path = Driver_map[device_type].get('path') settings = {} if Driver_map[device_type].get('settings').find('xbee_device_manager') != -1: if(self.xbee_device_manager_name == None): self.xbee_device_manager_name = 'xbee_device_manager' self.driver_add('xbee_device_manager', 'devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager', settings={'worker_threads':2}) time.sleep(1) settings['xbee_device_manager'] = self.xbee_device_manager_name settings['extended_address'] = device_mac if Driver_map[device_type].get('settings').find('sample_rate_ms') != -1: if(device_sample_rate == None): device_sample_rate = 30 settings['sample_rate_ms'] = int(float(device_sample_rate) * 1000) elif Driver_map[device_type].get('settings').find('sample_rate_sec') != -1: if(device_sample_rate == None): device_sample_rate = 1.0 settings['sample_rate_sec'] = float(device_sample_rate) if Driver_map[device_type].get('settings').find('channel_settings') != -1: settings['channel_settings'] = Driver_map[device_type].get('channel_settings') self.driver_add(instance_name, driver_path, settings) try: self.device_list_update() except: traceback.print_exc() xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="201">\n' xml_response += '<device id="%s" message="Device Created!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=201, content_type='text/xml') @put(cndep_path + 'device/(?P<device_id>\w+)/(?P<channel_name>\w+)') def AddChannel(request, device_id=None, channel_name=None): if(device_id in device_list): xml_request = request.body root = ET.fromstring(xml_request) sensor = root.find('sensor') ch_unit = sensor.get('unit') instance_name = device_list[device_id].get('name') if(ch_unit == None): type = device_list[device_id].get('type') try: ch_unit = Driver_map[type].get('channel_settings').split(',')[1] except: ch_unit = 'no unit' self.driver_channel_add(instance_name, channel_name, ch_unit) xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="201">\n' xml_response += '<device id="%s" message="Channel Created!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=201, content_type='text/xml') else: raise NotFound('device not found!') @post(cndep_path + 'device') def EditDevice(request): xml_request = request.body root = ET.fromstring(xml_request) device = root.find('device') device_id = device.get('id') if(device_id == None or not device_id.isdigit()): raise Conflict('Device Id is empty or not integer!') device_id_new = device.get('id_new') if(device_id_new != None and not device_id_new.isdigit()): raise Conflict('New Device Id is not integer!') device_type = device.get('type') if(device_type != None and device_type != ''): device_type = device_type.lower() if(device_type not in Device_types): raise Conflict('Device Type not found!') device_name = device.get('name') if(device_name != None and device_name != device_list[device_id].get('name')): flag = 0 for i in device_list: if(device_list[i].get('name') == device_name): flag = 1 break if(flag): raise Conflict('Device Name already exist!') device_mac = device.get('mac') device_sample_rate = device.get('sample_rate') if(device_sample_rate != None): try: device_sample_rate = float(device_sample_rate) except: raise Conflict('Device Sample Rate not integer or float!') if(device_id in device_list): if(device_type == None or device_type == ''): device_type = device_list[device_id].get('type').lower() if(device_name == None): device_name = device_list[device_id].get('name') if(device_mac == None): device_mac = device_list[device_id].get('mac') if(device_sample_rate == None): device_sample_rate = device_list[device_id].get('sample_rate') instance_name = device_name self.driver_remove(instance_name) del device_list[device_id] if(device_id_new != None): device_id_new = int(device_id_new) while(str(device_id_new) in device_list): #if device_id_new exist -> device_id++ and try again device_id_new += 1 device_id = str(device_id_new) device_list[device_id] = {'type': device_type, 'name': device_name, 'mac': device_mac, 'sample_rate': device_sample_rate} instance_name = device_name driver_path = Driver_map[device_type].get('path') settings = {} if Driver_map[device_type].get('settings').find('xbee_device_manager') != -1: if(self.xbee_device_manager_name == None): self.xbee_device_manager_name = 'xbee_device_manager' self.driver_add('xbee_device_manager', 'devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager', settings={'worker_threads':2}) time.sleep(1) settings['xbee_device_manager'] = self.xbee_device_manager_name settings['extended_address'] = device_mac if Driver_map[device_type].get('settings').find('sample_rate_ms') != -1: if(device_sample_rate == None): device_sample_rate = 30 settings['sample_rate_ms'] = int(float(device_sample_rate) * 1000) elif Driver_map[device_type].get('settings').find('sample_rate_sec') != -1: if(device_sample_rate == None): device_sample_rate = 1.0 settings['sample_rate_sec'] = float(device_sample_rate) if Driver_map[device_type].get('settings').find('channel_settings') != -1: settings['channel_settings'] = Driver_map[device_type].get('channel_settings') self.driver_add(instance_name, driver_path, settings) try: self.device_list_update() except: traceback.print_exc() xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="202">\n' xml_response += '<device id="%s" message="Device Updated!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=202, content_type='text/xml') else: raise NotFound('Device not found!') @post(cndep_path + 'device/(?P<device_id>\w+)/(?P<channel_name>\w+)') def EditChannel(request, device_id=None, channel_name=None): if(device_id in device_list): xml_request = request.body root = ET.fromstring(xml_request) sensor = root.find('sensor') ch_value = sensor.get('value') ch_unit = sensor.get('unit') instance_name = device_list[device_id].get('name') data = self.driver_channel_get(instance_name, channel_name) value = data.value unit = data.unit if(ch_value == None): ch_value = value if(ch_unit == None): ch_unit = unit self.driver_channel_set(instance_name, channel_name, ch_value, ch_unit) xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="202">\n' xml_response += '<device id="%s" message="Channel Updated!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=202, content_type='text/xml') else: raise NotFound('device not found!') @delete(cndep_path + 'device/(?P<device_id>\w+)') def DeleteDevice(request, device_id=None): if(device_id in device_list): instance_name = device_list[device_id].get('name') self.driver_remove(instance_name) del device_list[device_id] try: self.device_list_update() except: traceback.print_exc() xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="200">\n' xml_response += '<device id="%s" message="Device Deleted!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=200, content_type='text/xml') else: raise NotFound('Device not found!') @delete(cndep_path + 'device/(?P<device_id>\w+)/(?P<channel_name>\w+)') def DeleteChannel(request, device_id=None, channel_name=None): if(device_id in device_list): instance_name = device_list[device_id].get('name') dm = self.__core.get_service("device_driver_manager") dm_self, asm = dm.get_ASM() new_device = asm.instance_get(dm_self, instance_name) if new_device.property_exists(channel_name): self.driver_channel_remove(instance_name, channel_name) xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="200">\n' xml_response += '<device id="%s" message="Device Channel Deleted!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=200, content_type='text/xml') else: raise NotFound('Channel not found!') else: raise NotFound('Device not found!')