def didl_parse(xml, xmlpath, xslpath, delimiter, standardtags, tags = None, returnresult = None): "retrieving audio tags from response xml" didl_xml = OHcommons.xml_prepare(xml, xmlpath, 'string') didl_xsl = OHcommons.xsl_prepare('didl', xslpath) returnstring = None if tags == None: for tag in standardtags.split(','): tagvalue = OHcommons.xsl_transform(didl_xml, didl_xsl, tag) if str(tagvalue) == '': tagvalue = '-' if returnstring == None: returnstring = str(tagvalue) else: returnstring = returnstring + delimiter + str(tagvalue) else: for tag in tags: tagvalue = OHcommons.xsl_transform(didl_xml, didl_xsl, tag) if returnstring == None: returnstring = str(tagvalue) else: returnstring = returnstring + delimiter + str(tagvalue) if returnresult == None: print returnstring return else: return returnstring
def parse_event(text, action, state_var, path): xml = OHcommons.xml_prepare(text, '', 'string') xsl = OHcommons.xsl_prepare('OHevent', path) result = OHcommons.xsl_transform(xml, xsl, action, state_var) return str(result)
def subscription_handler(descriptions, available_devices, config, units_used, state_vars): 'initialize subscriptions for all available devices' timeouts = [] for device in available_devices: logging.info('Processing ' + device + ' ...') if device in units_used.keys(): for unit in units_used[device]: xsl = OHcommons.xsl_prepare('description', config['xslpath']) result = OHcommons.xsl_transform(descriptions[device]['xml'], xsl, 'event_url', unit[0]) if str(result) != '': unit.append(descriptions[device]['baseurl'] + str(result)) sid, timeout = subscribe('subscribe', device, descriptions[device]['baseurl'], str(result), \ config['callback'], config['port']) if timeout != None: timeouts.append(int(timeout)) if sid != None: unit.append(sid) logging.info('Processing ' + device + ' finished.') logging.debug('units: ' + str(units_used)) try: timeout = min(timeouts) / 2 except: timeout = 300 logging.debug('timeout: ' + str(timeout)) logging.info( 'Initial device search and subscription done. Entering serice loop ...' ) while True: for counter in xrange(1, 10): time.sleep(int(timeout / 10)) if config['message'] == 'newdevice': config['message'] = '' break while config['message'] != '': logging.info('Subscription handler blocked (' + config['message'] + '). Waiting ...') if config['message'] == 'subscription': logging.warning( 'Subscription is blocking itself. Removing block.') config['message'] = '' if config['message'] == 'newdevice': config['message'] = '' break time.sleep(10) config['message'] = 'subscription' for device in available_devices: logging.info('Processing ' + device + ' ...') if device in units_used.keys(): for unit in units_used[device]: if len(unit) > 1: if subscribe('renew', device, unit[1], '', '', config['port'], unit[2]): logging.info('Subscription for ' + device + ' event url ' + unit[1] + ' renewed.') else: del unit[1:] logging.info('Subscription for ' + device + ' failed. Deleting sid from list.') else: xsl = OHcommons.xsl_prepare('description', config['xslpath']) result = OHcommons.xsl_transform( descriptions[device]['xml'], xsl, 'event_url', unit[0]) if str(result) != '': unit.append(descriptions[device]['baseurl'] + str(result)) sid, timeout = subscribe('subscribe', device, descriptions[device]['baseurl'], str(result), \ config['callback'], config['port']) if timeout != None: timeouts.append(int(timeout)) if sid != None: unit.append(sid) logging.info('Processing ' + device + ' finished.') timeouts = list(set(timeouts)) try: timeout = min(timeouts) / 2 except: timeout = 300 logging.debug('timeout: ' + str(timeout)) logging.info('Subscription loop done. Timeout: ' + str(timeout) + '.') config['message'] = '' return