def serialize_neighbour_to_json(node_id): json_result = {} if node_id in globals.network.nodes: my_node = globals.network.nodes[node_id] json_result['data'] = {} json_result['data']['product_name'] = {'value': my_node.product_name} json_result['data']['location'] = {'value': my_node.location} node_name = my_node.name if globals.network.controller.node_id == node_id: node_name = my_node.product_name if utils.is_none_or_empty(node_name): node_name = my_node.product_name if utils.is_none_or_empty(node_name): node_name = 'Unknown' json_result['data']['name'] = {'value': node_name} json_result['data']['isPrimaryController'] = {'value': globals.network.controller.node_id == node_id} neighbour_is_enabled = my_node.generic != 1 # not a remote control if my_node.generic == 8 and not my_node.is_listening_device: neighbour_is_enabled = len(my_node.neighbors) > 0 json_result['data']['neighbours'] = {'value': list(my_node.neighbors), 'enabled': neighbour_is_enabled} json_result['data']['isDead'] = {'value': my_node.is_failed} json_result['data']['type'] = {'basic': my_node.basic, 'generic': my_node.generic, 'specific': my_node.specific} json_result['data']['state'] = {'value': utils.convert_query_stage_to_int(my_node.query_stage)} json_result['data']['isListening'] = {'value': my_node.is_listening_device} json_result['data']['isRouting'] = {'value': my_node.is_routing_device} else: logging.warning('This network does not contain any node with the id %s' % (node_id,)) return json_result
def get(self): request, response = self.request, self.response def get_request_params(): return (request.get('from'), request.get('to'), request.get('q'), request.get('callback')) from_currency, to_currency, qty, callback = get_request_params() if not utils.is_none_or_empty(from_currency) and not utils.is_none_or_empty(to_currency): req = GoogleCurrencyRateRequest() rate = req.get_rate(from_currency, to_currency) if (rate is None): result = {"err": 'error occurred'} else: result = {"from": from_currency, "to": to_currency, "rate": rate } if (qty is not None and len(qty) > 0): qty = float(qty) converted_qty = qty * rate result["v"] = converted_qty else: result = {"err": "invalid request"} utils.write_jsonp_output(response, result, callback)
def get(self): request, response = self.request, self.response def get_request_params(): def strip(v): if v is not None: v = v.strip() return v return (strip(request.get(u'from')), strip(request.get(u'to')), strip(request.get(u'q')), strip(request.get(u'callback'))) def get_rate(from_currency, to_currency): from_currency, to_currency = from_currency.upper( ), to_currency.upper() cache_key = u'{0}-{1}'.format(from_currency, to_currency) rate, err = memcache.get(cache_key), None if rate is None: req = XeCurrencyRateRequest() rate, err = req.get_rate(from_currency, to_currency) #logging.debug(u'rate fetched, key is {0}'.format(cache_key)) if rate is not None and memcache.add(cache_key, rate, 1200): pass #logging.debug(u'rate cached, key is {0}'.format(cache_key)) else: pass #logging.debug(u'rate fetched form cache, key is {0}'.format(cache_key)) return rate, err from_currency, to_currency, qty, callback = get_request_params() if not utils.is_none_or_empty( from_currency) and not utils.is_none_or_empty(to_currency): rate, err = get_rate(from_currency, to_currency) if rate is None: result = {u'err': err} elif rate == NOT_SUPPORTED_RATE: result = {u'err': u'not supported rate conversion.'} else: result = { u'from': from_currency, u'to': to_currency, u'rate': rate } if (qty is not None and len(qty) > 0): try: qty = float(qty) converted_qty = qty * rate result[u'v'] = converted_qty except: result[u'warning'] = u'invalid quantity, ignored.' else: result = {u'err': u'invalid request'} utils.write_jsonp_output(response, result, callback)
def get(self): request, response = self.request, self.response def get_request_params(): def strip(v): if v is not None: v = v.strip() return v return (strip(request.get(u'from')), strip(request.get(u'to')), strip(request.get(u'q')), strip(request.get(u'callback'))) def get_rate(from_currency, to_currency): from_currency, to_currency = from_currency.upper(), to_currency.upper() cache_key = u'{0}-{1}'.format(from_currency, to_currency) rate, err = memcache.get(cache_key), None if rate is None: req = XeCurrencyRateRequest() rate, err = req.get_rate(from_currency, to_currency) #logging.debug(u'rate fetched, key is {0}'.format(cache_key)) if rate is not None and memcache.add(cache_key, rate, 1200): pass #logging.debug(u'rate cached, key is {0}'.format(cache_key)) else: pass #logging.debug(u'rate fetched form cache, key is {0}'.format(cache_key)) return rate, err from_currency, to_currency, qty, callback = get_request_params() if not utils.is_none_or_empty(from_currency) and not utils.is_none_or_empty(to_currency): rate, err = get_rate(from_currency, to_currency) if rate is None: result = {u'err': err} elif rate == NOT_SUPPORTED_RATE: result = {u'err': u'not supported rate conversion.'} else: result = {u'from': from_currency, u'to': to_currency, u'rate': str(rate) } if (qty is not None and len(qty) > 0): try: qty = Decimal(qty) converted_qty = qty * rate result[u'v'] = str(converted_qty) except InvalidOperation: result[u'warning'] = u'invalid quantity, ignored.' else: result = {u'err': u'invalid request'} utils.write_jsonp_output(response, result, callback)
def ab_decrypt_msg(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg) + 16 - n else: n = len(msg) o = create_string_buffer(n + 40) m = so.AES_Data(DECRYPT, key, msg, o, n, AES_ENCMODE_128CBC) return o.raw[:m] if m > len(msg) else o.raw[:len(msg)]
def convert_temperature(mode, qty): if utils.is_none_or_empty(mode): return None mode_upper = mode.upper() if mode_upper == 'C2F': # Celsius to Fahrenheit v = qty * 9.0 / 5.0 + 32 elif mode_upper == 'F2C': v = (qty - 32) * 5.0 / 9.0 return v
def ab_decrypt_msg(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg)+16-n else: n = len(msg) o = create_string_buffer(n+40) m = so.AES_Data(DECRYPT, key, msg, o, n, AES_ENCMODE_128CBC) return o.raw[:m] if m > len(msg) else o.raw[:len(msg)]
def ab_decrypt_password(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg)+16-n else: n = len(msg) # n += 10 o = create_string_buffer(n) m = so.AES_PasswordByKey(DECRYPT, msg, o, key) return o.raw[:n/2]
def ab_decrypt_password(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg) + 16 - n else: n = len(msg) # n += 10 o = create_string_buffer(n) m = so.AES_PasswordByKey(DECRYPT, msg, o, key) return o.raw[:n / 2]
def ab_encrypt_password(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg) + 16 - n else: n = len(msg) # n = len(msg) n *= 2 o = create_string_buffer(n) m = so.AES_PasswordByKey(ENCRYPT, msg[:], o, key) s = o.raw[:n] return s
def ab_encrypt_password(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg)+16-n else: n = len(msg) # n = len(msg) n *= 2 o = create_string_buffer(n) m=so.AES_PasswordByKey(ENCRYPT, msg[:], o, key) s = o.raw[:n] return s
def get_nodes_list(): nodes_list = {'updateTime': int(time.time())} nodes_data = {} for node_id in list(globals.network.nodes): my_node = globals.network.nodes[node_id] json_node = {} try: manufacturer_id = int(my_node.manufacturer_id, 16) except ValueError: manufacturer_id = None try: product_id = int(my_node.product_id, 16) except ValueError: product_id = None try: product_type = int(my_node.product_type, 16) except ValueError: product_type = None node_name = my_node.name node_location = my_node.location if utils.is_none_or_empty(node_name): node_name = 'Unknown' if globals.network.controller.node_id == node_id: node_name = my_node.product_name node_location = 'Jeedom' json_node['description'] = {'name': node_name, 'location': node_location,'product_name': my_node.product_name,'is_static_controller': my_node.basic == 2,'is_enable': int(node_id) not in globals.disabled_nodes} json_node['product'] = {'manufacturer_id': manufacturer_id,'product_type': product_type,'product_id': product_id,'is_valid': manufacturer_id is not None and product_id is not None and product_type is not None} instances = [] for value_id in my_node.get_values(genre='User'): if my_node.values[value_id].instance in instances: continue instances.append(my_node.values[value_id].instance) json_node['multi_instance'] = {'support': globals.COMMAND_CLASS_MULTI_CHANNEL in my_node.command_classes,'instances': len(instances)} json_node['capabilities'] = {'isListening': my_node.is_listening_device,'isRouting': my_node.is_routing_device,'isBeaming': my_node.is_beaming_device,'isFlirs': my_node.is_frequent_listening_device} nodes_data[node_id] = json_node nodes_list['devices'] = nodes_data return utils.format_json_result(data=nodes_list)
def ab_encrypt_msg(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg) + 16 - n else: n = len(msg) o = create_string_buffer(n + 40) m = so.AES_Data(ENCRYPT, key, msg, o, len(msg), AES_ENCMODE_128CBC) i = m if m > len(msg) else len(msg) with open('r_c_msg.txt', 'wb') as w: w.write(o.raw[:i]) # o_hex = hexlify(o.value) # o2 = create_string_buffer(i+100) # # n=len(o.value) # a = o.raw[:i] # so.AES_Data(DECRYPT, key, a, o2, len(a), AES_ENCMODE_128CBC) # assert(o2.value==msg) return o.raw[:i]
def ab_encrypt_msg(msg, key): if is_none_or_empty(msg): return msg n = len(msg) % 16 if n != 0: n = len(msg)+16-n else: n = len(msg) o = create_string_buffer(n+40) m = so.AES_Data(ENCRYPT, key, msg, o, len(msg), AES_ENCMODE_128CBC) i = m if m > len(msg) else len(msg) with open('r_c_msg.txt', 'wb') as w: w.write(o.raw[:i]) # o_hex = hexlify(o.value) # o2 = create_string_buffer(i+100) # # n=len(o.value) # a = o.raw[:i] # so.AES_Data(DECRYPT, key, a, o2, len(a), AES_ENCMODE_128CBC) # assert(o2.value==msg) return o.raw[:i]
def get_nodes_list(): nodes_list = {'updateTime': int(time.time())} nodes_data = {} for node_id in list(globals.network.nodes): my_node = globals.network.nodes[node_id] json_node = {} try: manufacturer_id = int(my_node.manufacturer_id, 16) except ValueError: manufacturer_id = None try: product_id = int(my_node.product_id, 16) except ValueError: product_id = None try: product_type = int(my_node.product_type, 16) except ValueError: product_type = None node_name = my_node.name node_location = my_node.location if utils.is_none_or_empty(node_name): node_name = 'Unknown' if globals.network.controller.node_id == node_id: node_name = my_node.product_name node_location = 'Jeedom' json_node['description'] = { 'name': node_name, 'location': node_location, 'product_name': my_node.product_name, 'is_static_controller': my_node.basic == 2, 'is_enable': int(node_id) not in globals.disabled_nodes } json_node['product'] = { 'manufacturer_id': manufacturer_id, 'product_type': product_type, 'product_id': product_id, 'is_valid': manufacturer_id is not None and product_id is not None and product_type is not None } instances = [] for value_id in my_node.get_values(genre='User'): if my_node.values[value_id].instance in instances: continue instances.append(my_node.values[value_id].instance) json_node['multi_instance'] = { 'support': COMMAND_CLASS_MULTI_CHANNEL in my_node.command_classes, 'instances': len(instances) } json_node['capabilities'] = { 'isListening': my_node.is_listening_device, 'isRouting': my_node.is_routing_device, 'isBeaming': my_node.is_beaming_device, 'isFlirs': my_node.is_frequent_listening_device } nodes_data[node_id] = json_node nodes_list['devices'] = nodes_data return utils.format_json_result(data=nodes_list)
def serialize_node_to_json(node_id): json_result = {} if node_id not in globals.network.nodes or node_id in globals.not_supported_nodes: return json_result my_node = globals.network.nodes[node_id] try: timestamp = int(my_node.last_update) except TypeError: timestamp = int(1) try: manufacturer_id = int(my_node.manufacturer_id, 16) except ValueError: manufacturer_id = None try: product_id = int(my_node.product_id, 16) except ValueError: product_id = None try: product_type = int(my_node.product_type, 16) except ValueError: product_type = None json_result['data'] = {} node_name = my_node.name if globals.network.controller.node_id == node_id: node_name = my_node.product_name if utils.is_none_or_empty(node_name): node_name = 'Unknown' json_result['data']['description'] = {'name': node_name, 'location': my_node.location,'product_name': my_node.product_name} json_result['data']['manufacturerId'] = {'value': manufacturer_id, 'hex': my_node.manufacturer_id} json_result['data']['vendorString'] = {'value': my_node.manufacturer_name} json_result['data']['manufacturerProductId'] = {'value': product_id, 'hex': my_node.product_id} json_result['data']['product_name'] = {'value': my_node.product_name} json_result['data']['location'] = {'value': my_node.location} json_result['data']['name'] = {'value': my_node.name} json_result['data']['version'] = {'value': my_node.version} json_result['data']['manufacturerProductType'] = {'value': product_type, 'hex': my_node.product_type} json_result['data']['neighbours'] = {'value': list(my_node.neighbors)} json_result['data']['isVirtual'] = {'value': ''} if globals.network.controller.node_id == node_id and my_node.basic == 1: json_result['data']['basicType'] = {'value': 2} else: json_result['data']['basicType'] = {'value': my_node.basic} json_result['data']['genericType'] = {'value': my_node.generic} json_result['data']['specificType'] = {'value': my_node.specific} json_result['data']['type'] = {'value': my_node.type} json_result['data']['state'] = {'value': str(my_node.query_stage)} json_result['data']['isAwake'] = {'value': my_node.is_awake, "updateTime": timestamp} json_result['data']['isReady'] = {'value': my_node.is_ready, "updateTime": timestamp} json_result['data']['isEnable'] = {'value': int(node_id) not in globals.disabled_nodes} json_result['data']['isInfoReceived'] = {'value': my_node.is_info_received} try: can_wake_up = my_node.can_wake_up() except RuntimeError: can_wake_up = False json_result['data']['can_wake_up'] = {'value': can_wake_up} json_result['data']['battery_level'] = {'value': my_node.get_battery_level()} json_result['last_notification'] = {} next_wake_up = None if node_id in globals.node_notifications: notification = globals.node_notifications[node_id] next_wake_up = notification.next_wake_up json_result['last_notification'] = {"receiveTime": notification.receive_time,"description": notification.description,"help": notification.help} json_result['data']['wakeup_interval'] = {'value': node_utils.get_wake_up_interval(node_id), 'next_wakeup': next_wake_up} json_result['data']['isFailed'] = {'value': my_node.is_failed} json_result['data']['isListening'] = {'value': my_node.is_listening_device} json_result['data']['isRouting'] = {'value': my_node.is_routing_device} json_result['data']['isSecurity'] = {'value': my_node.is_security_device} json_result['data']['isBeaming'] = {'value': my_node.is_beaming_device} json_result['data']['isFrequentListening'] = {'value': my_node.is_frequent_listening_device} json_result['data']['security'] = {'value': my_node.security} json_result['data']['lastReceived'] = {'updateTime': timestamp} json_result['data']['maxBaudRate'] = {'value': my_node.max_baud_rate} json_result['data']['is_enable'] = {'value': int(node_id) not in globals.disabled_nodes} json_result['data']['isZwavePlus'] = {'value': my_node.is_zwave_plus} statistics = globals.network.manager.getNodeStatistics(globals.network.home_id, node_id) send_total = statistics['sentCnt'] + statistics['sentFailed'] percent_delivered = 0 if send_total > 0: percent_delivered = (statistics['sentCnt'] * 100) / send_total average_request_rtt = statistics['averageRequestRTT'] json_result['data']['statistics'] = {'total': send_total, 'delivered': percent_delivered,'deliveryTime': average_request_rtt} have_group = False query_stage_index = utils.convert_query_stage_to_int(my_node.query_stage) if my_node.groups and query_stage_index >= 12 and my_node.generic != 2: check_for_group = len(my_node.groups) > 0 if check_for_group : have_group = node_utils.check_primary_controller(my_node) else: check_for_group = False json_result['data']['is_groups_ok'] = {'value': have_group, 'enabled': check_for_group} is_neighbours_ok = query_stage_index > 13 if my_node.generic == 1: is_neighbours_ok = False if my_node.generic == 8 and not my_node.is_listening_device: is_neighbours_ok = False json_result['data']['is_neighbours_ok'] = {'value': len(my_node.neighbors) > 0,'neighbors': len(my_node.neighbors), 'enabled': is_neighbours_ok} json_result['data']['is_manufacturer_specific_ok'] = {'value': my_node.product_name != '','enabled': query_stage_index >= 7} is_secured = value_utils.get_value_by_label(node_id, globals.COMMAND_CLASS_SECURITY, 1, 'Secured') json_result['data']['isSecured'] = {'value': is_secured is not None and is_secured.data, 'enabled' : is_secured is not None} pending_changes = 0 json_result['instances'] = {"updateTime": timestamp} json_result['groups'] = {"updateTime": timestamp} for groupIndex in list(my_node.groups): group = my_node.groups[groupIndex] pending_state = 1 if my_node.node_id in globals.pending_associations: pending_associations = globals.pending_associations[my_node.node_id] if groupIndex in pending_associations: pending_association = pending_associations[groupIndex] if pending_association.state is not None: pending_state = pending_association.state if pending_state is not None and pending_state > 1: pending_changes += 1 json_result['groups'][groupIndex] = {"label": group.label, "maximumAssociations": group.max_associations,"associations": list(group.associations_instances),"pending": pending_state} json_result['associations'] = serialize_associations(node_id) if node_id in globals.node_notifications: notification = globals.node_notifications[node_id] json_result['last_notification'] = {"receiveTime": notification.receive_time,"code": notification.code,"description": notification.description,"help": notification.help,"next_wakeup": notification.next_wake_up} else: json_result['last_notification'] = {} json_result['command_classes'] = {} for command_class in my_node.command_classes: json_result['command_classes'][command_class] = {'name': my_node.get_command_class_as_string(command_class),'hex': '0x' + utils.convert_user_code_to_hex(command_class)} instances = [] for value_id in my_node.get_values(): my_value = my_node.values[value_id] if my_value.command_class is None or (my_value.instance > 1 and my_value.command_class in [globals.COMMAND_CLASS_ZWAVEPLUS_INFO,globals.COMMAND_CLASS_VERSION]): continue try: label = my_value.label except Exception, exception: label = exception.message logging.error('Value label contains unsupported text: %s' % (str(exception),)) try: value_help = my_value.help except Exception, exception: value_help = exception.message logging.error('Value help contains unsupported text: %s' % (str(exception),))