def add_assoc(node_id, group, target_id, instance, action): utils.can_execute_command(0) utils.check_node_exist(node_id) if action == 'add': utils.check_node_exist(target_id) logging.info(action + ' assoc to nodeId: ' + str(node_id) + ' in group ' + str(group) + ' with nodeId: ' + str(node_id) + ' on instance ' + str(instance)) if node_id not in globals.pending_associations: globals.pending_associations[node_id] = dict() if action == 'remove': globals.pending_associations[node_id][group] = PendingAssociation( pending_added=None, pending_removed=target_id, timeout=0) if instance < 1: globals.network.manager.removeAssociation(globals.network.home_id, node_id, group, target_id) else: globals.network.manager.removeAssociation(globals.network.home_id, node_id, group, target_id, instance) if action == 'add': globals.pending_associations[node_id][group] = PendingAssociation( pending_added=target_id, pending_removed=None, timeout=0) if instance < 1: globals.network.manager.addAssociation(globals.network.home_id, node_id, group, target_id) else: globals.network.manager.addAssociation(globals.network.home_id, node_id, group, target_id, instance) return utils.format_json_result()
def set_config(_node_id, _index_id, _value, _size): utils.can_execute_command(0) utils.check_node_exist(_node_id) if _size > 4: _size = 4 if _size not in [0, 1, 2, 4]: _size = 1 logging.info( 'set_config for nodeId : %s index : %s, value : %s, size : %s' % (_node_id, _index_id, _value, _size)) wake_up_time = node_utils.get_wake_up_interval(_node_id) if wake_up_time is None: wake_up_time = 0 wake_up_time += 10 for value_id in globals.network.nodes[_node_id].get_values( class_id=globals.COMMAND_CLASS_CONFIGURATION, genre='All', type='All', readonly=False, writeonly='All'): if globals.network.nodes[_node_id].values[value_id].index == _index_id: value = _value.replace("@", "/") my_value = globals.network.nodes[_node_id].values[value_id] if my_value.type == 'Button': if value.lower() == 'true': result = globals.network.manager.pressButton( my_value.value_id) else: result = globals.network.manager.releaseButton( my_value.value_id) elif my_value.type == 'List' and _size == 0: # list item must be a string, the size must be set to 0 result = globals.network.manager.setValue(value_id, value) mark_pending_change(my_value, value, wake_up_time) elif my_value.type == 'Bool': value = my_value.check_data(value) result = globals.network.manager.setValue(value_id, value) mark_pending_change(my_value, value, wake_up_time) else: # recommend parameters size is set to 0, for list item value handling if _size == 0: _size = 1 # set a parameter list item with value or not configured device result = globals.network.nodes[_node_id].set_config_param( _index_id, int(value), _size) # don't mark list item if set with value if my_value is not None and my_value.type != 'List': value = my_value.check_data(value) mark_pending_change(my_value, value, wake_up_time) logging.debug('set_configuration result: %s' % (result, )) return if _value.isdigit() and globals.network.nodes[_node_id].set_config_param( _index_id, int(_value), _size): logging.debug('set_configuration device without defined parameters') else: raise Exception('Configuration index : ' + str(_index_id) + ' not found')
def test_network(): utils.can_execute_command() logging.info("Sends a series of messages to a network node for testing network reliability") for node_id in list(globals.network.nodes): if node_id in globals.not_supported_nodes: logging.debug("skip not supported (nodeId: %s)" % (node_id,)) continue if node_id in globals.disabled_nodes: continue globals.network.manager.testNetworkNode(globals.network.home_id, node_id, 3) return utils.format_json_result()
def test_network(): utils.can_execute_command() logging.info( "Sends a series of messages to a network node for testing network reliability" ) for node_id in list(globals.network.nodes): if node_id in globals.not_supported_nodes: logging.debug("skip not supported (nodeId: %s)" % (node_id, )) continue if node_id in globals.disabled_nodes: continue globals.network.manager.testNetworkNode(globals.network.home_id, node_id, 3) return utils.format_json_result()
def set_config(_node_id, _index_id, _value, _size): utils.can_execute_command(0) utils.check_node_exist(_node_id) if _size > 4: _size = 4 if _size not in [0, 1, 2, 4]: _size = 1 logging.info('set_config for nodeId : %s index : %s, value : %s, size : %s' % (_node_id, _index_id, _value, _size)) wake_up_time = node_utils.get_wake_up_interval(_node_id) if wake_up_time is None : wake_up_time = 0 wake_up_time += 10 for value_id in globals.network.nodes[_node_id].get_values(class_id=globals.COMMAND_CLASS_CONFIGURATION, genre='All', type='All', readonly=False, writeonly='All'): if globals.network.nodes[_node_id].values[value_id].index == _index_id: value = _value.replace("@", "/") my_value = globals.network.nodes[_node_id].values[value_id] if my_value.type == 'Button': if value.lower() == 'true': result = globals.network.manager.pressButton(my_value.value_id) else: result = globals.network.manager.releaseButton(my_value.value_id) elif my_value.type == 'List' and _size == 0: # list item must be a string, the size must be set to 0 result = globals.network.manager.setValue(value_id, value) mark_pending_change(my_value, value,wake_up_time) elif my_value.type == 'Bool': value = my_value.check_data(value) result = globals.network.manager.setValue(value_id, value) mark_pending_change(my_value, value,wake_up_time) else: # recommend parameters size is set to 0, for list item value handling if _size == 0: _size = 1 # set a parameter list item with value or not configured device result = globals.network.nodes[_node_id].set_config_param(_index_id, int(value), _size) # don't mark list item if set with value if my_value is not None and my_value.type != 'List': value = my_value.check_data(value) mark_pending_change(my_value, value, wake_up_time) logging.debug('set_configuration result: %s' % (result,)) return if _value.isdigit() and globals.network.nodes[_node_id].set_config_param(_index_id, int(_value), _size): logging.debug('set_configuration device without defined parameters') else: raise Exception('Configuration index : '+str(_index_id)+' not found')
def heal_network(): utils.can_execute_command(0) logging.info("Heal network by requesting node's rediscover their neighbors") for node_id in list(globals.network.nodes): if node_id in globals.not_supported_nodes: logging.debug("skip not supported (nodeId: "+str(node_id)+")") continue if globals.network.nodes[node_id].is_failed: logging.debug("skip presume dead (nodeId: "+str(node_id)+")") continue if globals.network.nodes[node_id].query_stage != "Complete": logging.debug("skip query stage not complete (nodeId:"+str(node_id)+")") continue if globals.network.nodes[node_id].generic == 1: logging.debug("skip Remote controller (nodeId: "+str(node_id)+") (they don't have neighbors)") continue if node_id in globals.disabled_nodes: continue globals.network.manager.healNetworkNode(globals.network.home_id, node_id, False) return utils.format_json_result()
def add_assoc(node_id, group, target_id,instance,action): utils.can_execute_command(0) utils.check_node_exist(node_id) if action == 'add': utils.check_node_exist(target_id) logging.info(action + ' assoc to nodeId: ' + str(node_id) + ' in group ' + str(group) + ' with nodeId: ' + str( node_id) + ' on instance ' + str(instance)) if node_id not in globals.pending_associations: globals.pending_associations[node_id] = dict() if action == 'remove': globals.pending_associations[node_id][group] = PendingAssociation(pending_added=None, pending_removed=target_id,timeout=0) if instance < 1: globals.network.manager.removeAssociation(globals.network.home_id, node_id, group, target_id) else: globals.network.manager.removeAssociation(globals.network.home_id, node_id, group, target_id, instance) if action == 'add': globals.pending_associations[node_id][group] = PendingAssociation(pending_added=target_id, pending_removed=None,timeout=0) if instance < 1: globals.network.manager.addAssociation(globals.network.home_id, node_id, group, target_id) else: globals.network.manager.addAssociation(globals.network.home_id, node_id, group, target_id, instance) return utils.format_json_result()
def get(self): try: utils.check_apikey(self.get_argument('apikey', '')) type = self.get_argument('type', '') node_id = int(self.get_argument('node_id', '0')) info = self.get_argument('info', '') action = self.get_argument('action', '') do_security = int(self.get_argument('security', '0')) if type == 'replicationSend': utils.can_execute_command(0) utils.check_node_exist(node_id) logging.info('Send information from primary to secondary %s' % (node_id, )) self.write( utils.format_json_result( data=globals.network.manager.replicationSend( globals.network.home_id, node_id))) elif type == 'info': logging.info("Controller info " + str(info)) self.write(utils.format_json_result()) elif type == 'action': logging.info("Controller action " + str(action)) if action in globals.CONTROLLER_REST_MAPPING: self.write(globals.CONTROLLER_REST_MAPPING[action]()) else: self.write(utils.format_json_result()) elif type == 'addNode': utils.can_execute_command(0) if do_security == 1: do_security = True logging.info( "Start the Inclusion Process to add a Node to the Network with Security CC if the node is supports it" ) else: do_security = False logging.info( "Start the Inclusion Process to add a Node to the Network" ) execution_result = globals.network.manager.addNode( globals.network.home_id, do_security) if execution_result: globals.network_information.actual_mode = ControllerMode.AddDevice self.write(utils.format_json_result(data=execution_result)) elif type == 'removeNode': utils.can_execute_command(0) logging.info( "Remove a Device from the Z-Wave Network (Started)") execution_result = globals.network.manager.removeNode( globals.network.home_id) if execution_result: globals.network_information.actual_mode = ControllerMode.RemoveDevice self.write(utils.format_json_result(data=execution_result)) else: self.write(utils.format_json_result()) except Exception, e: self.write(utils.format_json_result(success="error", data=str(e)))
def heal_network(): utils.can_execute_command(0) logging.info( "Heal network by requesting node's rediscover their neighbors") for node_id in list(globals.network.nodes): if node_id in globals.not_supported_nodes: logging.debug("skip not supported (nodeId: " + str(node_id) + ")") continue if globals.network.nodes[node_id].is_failed: logging.debug("skip presume dead (nodeId: " + str(node_id) + ")") continue if globals.network.nodes[node_id].query_stage != "Complete": logging.debug("skip query stage not complete (nodeId:" + str(node_id) + ")") continue if globals.network.nodes[node_id].generic == 1: logging.debug("skip Remote controller (nodeId: " + str(node_id) + ") (they don't have neighbors)") continue if node_id in globals.disabled_nodes: continue globals.network.manager.healNetworkNode(globals.network.home_id, node_id, False) return utils.format_json_result()
def get(self): try: utils.check_apikey(self.get_argument('apikey','')) type = self.get_argument('type','') node_id = int(self.get_argument('node_id','0')) info = self.get_argument('info','') action = self.get_argument('action','') do_security = int(self.get_argument('security','0')) if type == 'replicationSend': utils.can_execute_command(0) utils.check_node_exist(node_id) logging.info('Send information from primary to secondary %s' % (node_id,)) self.write(utils.format_json_result(data=globals.network.manager.replicationSend(globals.network.home_id, node_id))) elif type == 'info': logging.info("Controller info "+str(info)) self.write(utils.format_json_result()) elif type == 'action': logging.info("Controller action "+str(action)) if action in globals.CONTROLLER_REST_MAPPING: self.write(globals.CONTROLLER_REST_MAPPING[action]()) else: self.write(utils.format_json_result()) elif type == 'addNode': utils.can_execute_command(0) if do_security == 1: do_security = True logging.info("Start the Inclusion Process to add a Node to the Network with Security CC if the node is supports it") else: do_security = False logging.info("Start the Inclusion Process to add a Node to the Network") execution_result = globals.network.manager.addNode(globals.network.home_id, do_security) if execution_result: globals.network_information.actual_mode = ControllerMode.AddDevice self.write(utils.format_json_result(data=execution_result)) elif type == 'removeNode': utils.can_execute_command(0) logging.info("Remove a Device from the Z-Wave Network (Started)") execution_result = globals.network.manager.removeNode(globals.network.home_id) if execution_result: globals.network_information.actual_mode = ControllerMode.RemoveDevice self.write(utils.format_json_result(data=execution_result)) else: self.write(utils.format_json_result()) except Exception,e: self.write(utils.format_json_result(success="error",data=str(e)))
def test_node(node_id, count=3): utils.can_execute_command() utils.check_node_exist(node_id, True) globals.network.manager.testNetworkNode(globals.network.home_id, node_id, count) return utils.format_json_result()
def transfer_primary_role(): utils.can_execute_command(0) logging.info("Transfer Primary Role") return utils.format_json_result(data=globals.network.manager.transferPrimaryRole(globals.network.home_id))
def receive_configuration(): utils.can_execute_command(0) logging.info("Receive Configuration") return utils.format_json_result(data=globals.network.manager.receiveConfiguration(globals.network.home_id))
def test_node(node_id, count=3): utils.can_execute_command() utils.check_node_exist(node_id,True) globals.network.manager.testNetworkNode(globals.network.home_id, node_id, count) return utils.format_json_result()
def get(self): try: utils.check_apikey(self.get_argument('apikey', '')) type = self.get_argument('type', '') node_id = int(self.get_argument('node_id', '0')) target_id = int(self.get_argument('target_id', '0')) cc_id = int(self.get_argument('cc_id', '0')) instance_id = int(self.get_argument('instance_id', '0')) index = int(self.get_argument('index', '0')) identical = int(self.get_argument('identical', '0')) frequency = int(self.get_argument('frequency', '0')) action = self.get_argument('action', '') info = self.get_argument('info', '') utils.check_node_exist(node_id) if type == 'action': utils.can_execute_command() logging.info("node action " + str(action)) if action in globals.NODE_REST_MAPPING: self.write(globals.NODE_REST_MAPPING[action](node_id)) else: self.write(utils.format_json_result()) elif type == 'info': logging.info("node info " + str(info)) if info in globals.NODE_REST_MAPPING: self.write(globals.NODE_REST_MAPPING[info](node_id)) else: self.write(utils.format_json_result()) elif type == 'refreshClass': logging.info('Request values refresh for ' + str(node_id) + ' on class ' + str(cc_id)) for value_id in globals.network.nodes[node_id].get_values( class_id=cc_id): if globals.network.nodes[node_id].values[ value_id].id_on_network in globals.pending_configurations: del globals.pending_configurations[ globals.network.nodes[node_id].values[value_id]. id_on_network] globals.network.manager.requestAllConfigParams( globals.network.home_id, node_id) self.write(utils.format_json_result()) elif type == 'removeDeviceZWConfig': my_node = globals.network.nodes[node_id] manufacturer_id = my_node.manufacturer_id product_id = my_node.product_id product_type = my_node.product_type list_to_remove = [node_id] if identical != 0: for child_id in list(globals.network.nodes): node = globals.network.nodes[child_id] if child_id != node_id and node.manufacturer_id == manufacturer_id and node.product_id == product_id and node.product_type == product_type: list_to_remove.append(child_id) globals.network_is_running = False globals.network.stop() logging.info('ZWave network is now stopped') time.sleep(5) filename = globals.data_folder + "/zwcfg_" + globals.network.home_id_str + ".xml" tree = etree.parse(filename) for child_id in list_to_remove: logging.info("Remove xml element for node %s" % (child_id, )) node = tree.find( "{http://code.google.com/p/open-zwave/}Node[@id='" + str(child_id) + "']") tree.getroot().remove(node) working_file = open(filename, "w") working_file.write('<?xml version="1.0" encoding="utf-8" ?>\n') working_file.writelines(etree.tostring(tree, pretty_print=True)) working_file.close() network_utils.start_network() self.write(utils.format_json_result()) elif type == 'copyConfigurations': utils.can_execute_command(0) logging.info( "copy_configuration from source_id:%s to target_id:%s" % ( node_id, target_id, )) items = 0 utils.check_node_exist(target_id) source = globals.network.nodes[node_id] target = globals.network.nodes[target_id] if source.manufacturer_id != target.manufacturer_id or source.product_type != target.product_type or source.product_id != target.product_id: raise Exception( 'The two nodes must be with same: manufacturer_id, product_type and product_id' ) for value_id in source.get_values(): configuration_value = source.values[value_id] if configuration_value.genre == 'Config': if configuration_value.type == 'Button': continue if configuration_value.is_write_only: continue target_value = value_utils.get_value_by_index( target_id, globals.COMMAND_CLASS_CONFIGURATION, 1, configuration_value.index) if target_value is not None: if configuration_value.type == 'List': globals.network.manager.setValue( target_value.value_id, configuration_value.data) accepted = True else: accepted = target.set_config_param( configuration_value.index, configuration_value.data) if accepted: items += 1 value_utils.mark_pending_change( target_value, configuration_value.data) my_result = items != 0 self.write(utils.format_json_result()) elif type == 'refreshData': for value_id in globals.network.nodes[node_id].get_values( class_id=cc_id): if globals.network.nodes[node_id].values[ value_id].instance == instance_id and globals.network.nodes[ node_id].values[value_id].index == index: globals.network.nodes[node_id].values[ value_id].refresh() self.write(utils.format_json_result()) return raise Exception( 'This device does not contain the specified value') elif type == 'data': logging.debug("get_config for nodeId:%s" % (node_id, )) config = {} for value_id in globals.network.nodes[node_id].values: list_values = [] my_value = globals.network.nodes[node_id].values[value_id] if my_value.command_class == cc_id: config[globals.network.nodes[node_id].values[value_id]. index] = {} if my_value.type == "List" and not my_value.is_read_only: result_data = globals.network.manager.getValueListSelectionNum( my_value.value_id) values = my_value.data_items for index_item, value_item in enumerate(values): list_values.append(value_item) if value_item == my_value.data_as_string: result_data = index_item elif my_value.type == "Bool" and not my_value.data: result_data = 0 elif my_value.type == "Bool" and my_value.data: result_data = 1 else: result_data = my_value.data config[my_value.index]['val'] = { 'value2': my_value.data, 'value': result_data, 'value3': my_value.label, 'value4': sorted(list_values), 'updateTime': int(time.time()), 'invalidateTime': 0 } self.write(utils.format_json_result(data=config)) elif type == 'setPolling': logging.info('set_polling_value for nodeId: ' + str(node_id) + ' instance: ' + str(instance_id) + ' cc : ' + str(cc_id) + ' index : ' + str(index) + ' at: ' + str(frequency)) for value_id in globals.network.nodes[node_id].get_values( class_id=cc_id): if globals.network.nodes[node_id].values[ value_id].instance == instance_id: my_value = globals.network.nodes[node_id].values[ value_id] if frequency == 0 & my_value.poll_intensity > 0: my_value.disable_poll() else: if globals.network.nodes[node_id].values[ value_id].index == index: value_utils.changes_value_polling( frequency, my_value) elif my_value.poll_intensity > 0: my_value.disable_poll() utils.write_config() self.write(utils.format_json_result()) elif type == 'buttonaction': logging.info('Button nodeId : ' + str(node_id) + ' instance: ' + str(instance_id) + ' cc : ' + str(cc_id) + ' index : ' + str(index) + ' : ' + str(action)) for value_id in globals.network.nodes[node_id].get_values( class_id=cc_id, genre='All', type='All', readonly=False, writeonly='All'): if globals.network.nodes[node_id].values[ value_id].instance == instance_id and globals.network.nodes[ node_id].values[value_id].index == index: if action == 'press': globals.network.manager.pressButton( globals.network.nodes[node_id]. values[value_id].value_id) elif action == 'release': globals.network.manager.releaseButton( globals.network.nodes[node_id]. values[value_id].value_id) self.write(utils.format_json_result()) return self.write( utils.format_json_result(success='error', data='Button not found')) elif type == 'setRaw': slot_id = int(self.get_argument('slot_id', '0')) value0 = self.get_argument('value0', '') logging.info("set_user_code2 nodeId:%s slot:%s user code:%s" % ( node_id, slot_id, value0, )) for value_id in globals.network.nodes[node_id].get_values( class_id=globals.COMMAND_CLASS_USER_CODE): if globals.network.nodes[node_id].values[ value_id].index == slot_id: globals.network.nodes[node_id].values[ value_id].data = binascii.a2b_hex(value0) self.write(utils.format_json_result()) return self.write( utils.format_json_result(success='error', data='Value not found')) elif type == 'setconfig': size = int(self.get_argument('size', '0')) value = self.get_argument('value', '') self.write( utils.format_json_result(data=value_utils.set_config( node_id, index, value, size))) elif type == 'setvalue': value = self.get_argument('value', '') self.write( utils.format_json_result(data=commands.send_command_zwave( node_id, cc_id, instance_id, index, value))) elif type == 'switchall': state = int(self.get_argument('state', '0')) if state == 0: logging.info("SwitchAll Off") globals.network.switch_all(False) else: logging.info("SwitchAll On") globals.network.switch_all(True) for node_id in globals.network.nodes: my_node = globals.network.nodes[node_id] if my_node.is_failed: continue value_ids = my_node.get_switches_all() if value_ids is not None and len(value_ids) > 0: for value_id in value_ids: if my_node.values[value_id].data == "Disabled": continue elif my_node.values[ value_id].data == "On and Off Enabled": pass if my_node.values[ value_id].data == "Off Enabled" and state != 0: continue if my_node.values[ value_id].data == "On Enabled" and state == 0: continue for switch in my_node.get_switches(): my_node.values[switch].refresh() for dimmer in my_node.get_dimmers(): my_node.values[dimmer].refresh() self.write(utils.format_json_result()) elif type == 'setDeviceName': location = self.get_argument('location', '') name = self.get_argument('name', '') is_enable = int(self.get_argument('is_enable', '0')) logging.info( "set_device_name node_id:%s new name ; '%s'. Is enable: %s" % ( node_id, name, is_enable, )) if node_id in globals.disabled_nodes and is_enable: globals.disabled_nodes.remove(node_id) elif node_id not in globals.disabled_nodes and not is_enable: globals.disabled_nodes.append(node_id) name = name.encode('utf8') name = name.replace('+', ' ') globals.network.nodes[node_id].set_field('name', name) location = location.encode('utf8') location = location.replace('+', ' ') globals.network.nodes[node_id].set_field('location', location) self.write(utils.format_json_result()) elif type == 'association': group = int(self.get_argument('group', '0')) self.write( node_utils.add_assoc(node_id, group, target_id, instance_id, action)) else: self.write(utils.format_json_result()) except Exception, e: logging.error('RequestHandler ' + e.message) self.write(utils.format_json_result(success="error", data=str(e)))
def create_new_primary(): utils.can_execute_command(0) logging.info("Add a new controller to the Z-Wave network") return utils.format_json_result(data=globals.network.manager.createNewPrimary(globals.network.home_id))
def receive_configuration(): utils.can_execute_command(0) logging.info("Receive Configuration") return utils.format_json_result( data=globals.network.manager.receiveConfiguration( globals.network.home_id))
def transfer_primary_role(): utils.can_execute_command(0) logging.info("Transfer Primary Role") return utils.format_json_result( data=globals.network.manager.transferPrimaryRole( globals.network.home_id))
def create_new_primary(): utils.can_execute_command(0) logging.info("Add a new controller to the Z-Wave network") return utils.format_json_result( data=globals.network.manager.createNewPrimary(globals.network.home_id))
def get(self): try: utils.check_apikey(self.get_argument('apikey','')) type = self.get_argument('type','') node_id = int(self.get_argument('node_id','0')) target_id = int(self.get_argument('target_id','0')) cc_id = int(self.get_argument('cc_id','0')) instance_id = int(self.get_argument('instance_id','0')) index = int(self.get_argument('index','0')) identical = int(self.get_argument('identical','0')) frequency = int(self.get_argument('frequency','0')) action = self.get_argument('action','') info = self.get_argument('info','') utils.check_node_exist(node_id) if type == 'action': utils.can_execute_command() logging.info("node action "+str(action)) if action in globals.NODE_REST_MAPPING: self.write(globals.NODE_REST_MAPPING[action](node_id)) else: self.write(utils.format_json_result()) elif type == 'info': logging.info("node info "+str(info)) if info in globals.NODE_REST_MAPPING: self.write(globals.NODE_REST_MAPPING[info](node_id)) else: self.write(utils.format_json_result()) elif type == 'refreshClass': logging.info('Request values refresh for '+str(node_id)+' on class '+str(cc_id)) for value_id in globals.network.nodes[node_id].get_values(class_id=cc_id): if globals.network.nodes[node_id].values[value_id].id_on_network in globals.pending_configurations: del globals.pending_configurations[globals.network.nodes[node_id].values[value_id].id_on_network] globals.network.manager.requestAllConfigParams(globals.network.home_id, node_id) self.write(utils.format_json_result()) elif type == 'removeDeviceZWConfig': my_node = globals.network.nodes[node_id] manufacturer_id = my_node.manufacturer_id product_id = my_node.product_id product_type = my_node.product_type list_to_remove = [node_id] if identical != 0: for child_id in list(globals.network.nodes): node = globals.network.nodes[child_id] if child_id != node_id and node.manufacturer_id == manufacturer_id and node.product_id == product_id and node.product_type == product_type: list_to_remove.append(child_id) globals.network_is_running = False globals.network.stop() logging.info('ZWave network is now stopped') time.sleep(5) filename = globals.data_folder + "/zwcfg_" + globals.network.home_id_str + ".xml" tree = etree.parse(filename) for child_id in list_to_remove: logging.info("Remove xml element for node %s" % (child_id,)) node = tree.find("{http://code.google.com/p/open-zwave/}Node[@id='" + str(child_id) + "']") tree.getroot().remove(node) working_file = open(filename, "w") working_file.write('<?xml version="1.0" encoding="utf-8" ?>\n') working_file.writelines(etree.tostring(tree, pretty_print=True)) working_file.close() network_utils.start_network() self.write(utils.format_json_result()) elif type == 'copyConfigurations': utils.can_execute_command(0) logging.info("copy_configuration from source_id:%s to target_id:%s" % (node_id, target_id,)) items = 0 utils.check_node_exist(target_id) source = globals.network.nodes[node_id] target = globals.network.nodes[target_id] if source.manufacturer_id != target.manufacturer_id or source.product_type != target.product_type or source.product_id != target.product_id: raise Exception('The two nodes must be with same: manufacturer_id, product_type and product_id') for value_id in source.get_values(): configuration_value = source.values[value_id] if configuration_value.genre == 'Config': if configuration_value.type == 'Button': continue if configuration_value.is_write_only: continue target_value = value_utils.get_value_by_index(target_id, globals.COMMAND_CLASS_CONFIGURATION, 1,configuration_value.index) if target_value is not None: if configuration_value.type == 'List': globals.network.manager.setValue(target_value.value_id, configuration_value.data) accepted = True else: accepted = target.set_config_param(configuration_value.index,configuration_value.data) if accepted: items += 1 value_utils.mark_pending_change(target_value, configuration_value.data) my_result = items != 0 self.write(utils.format_json_result()) elif type == 'refreshData': for value_id in globals.network.nodes[node_id].get_values(class_id=cc_id): if globals.network.nodes[node_id].values[value_id].instance == instance_id and globals.network.nodes[node_id].values[value_id].index == index: globals.network.nodes[node_id].values[value_id].refresh() self.write(utils.format_json_result()) return raise Exception('This device does not contain the specified value') elif type == 'data': logging.debug("get_config for nodeId:%s" % (node_id,)) config = {} for value_id in globals.network.nodes[node_id].values: list_values = [] my_value = globals.network.nodes[node_id].values[value_id] if my_value.command_class == cc_id: config[globals.network.nodes[node_id].values[value_id].index] = {} if my_value.type == "List" and not my_value.is_read_only: result_data = globals.network.manager.getValueListSelectionNum(my_value.value_id) values = my_value.data_items for index_item, value_item in enumerate(values): list_values.append(value_item) if value_item == my_value.data_as_string: result_data = index_item elif my_value.type == "Bool" and not my_value.data: result_data = 0 elif my_value.type == "Bool" and my_value.data: result_data = 1 else: result_data = my_value.data config[my_value.index]['val'] = {'value2': my_value.data, 'value': result_data,'value3': my_value.label, 'value4': sorted(list_values),'updateTime': int(time.time()), 'invalidateTime': 0} self.write(utils.format_json_result(data=config)) elif type == 'setPolling': logging.info('set_polling_value for nodeId: '+str(node_id)+' instance: '+str(instance_id)+' cc : '+str(cc_id)+' index : '+str(index)+' at: '+str(frequency)) for value_id in globals.network.nodes[node_id].get_values(class_id=cc_id): if globals.network.nodes[node_id].values[value_id].instance == instance_id: my_value = globals.network.nodes[node_id].values[value_id] if frequency == 0 & my_value.poll_intensity > 0: my_value.disable_poll() else: if globals.network.nodes[node_id].values[value_id].index == index: value_utils.changes_value_polling(frequency, my_value) elif my_value.poll_intensity > 0: my_value.disable_poll() utils.write_config() self.write(utils.format_json_result()) elif type == 'buttonaction': logging.info('Button nodeId : '+str(node_id)+' instance: '+str(instance_id)+' cc : '+str(cc_id)+' index : '+str(index)+' : ' +str(action)) for value_id in globals.network.nodes[node_id].get_values(class_id=cc_id, genre='All', type='All', readonly=False, writeonly='All'): if globals.network.nodes[node_id].values[value_id].instance == instance_id and globals.network.nodes[node_id].values[value_id].index == index: if action == 'press': globals.network.manager.pressButton(globals.network.nodes[node_id].values[value_id].value_id) elif action == 'release': globals.network.manager.releaseButton(globals.network.nodes[node_id].values[value_id].value_id) self.write(utils.format_json_result()) return self.write(utils.format_json_result(success='error', data='Button not found')) elif type == 'setRaw': slot_id = int(self.get_argument('slot_id','0')) value0 = self.get_argument('value0','') logging.info("set_user_code2 nodeId:%s slot:%s user code:%s" % (node_id, slot_id, value0,)) for value_id in globals.network.nodes[node_id].get_values(class_id=globals.COMMAND_CLASS_USER_CODE): if globals.network.nodes[node_id].values[value_id].index == slot_id: globals.network.nodes[node_id].values[value_id].data = binascii.a2b_hex(value0) self.write(utils.format_json_result()) return self.write(utils.format_json_result(success='error', data='Value not found')) elif type == 'setconfig': size = int(self.get_argument('size','0')) value = self.get_argument('value','') self.write(utils.format_json_result(data=value_utils.set_config(node_id, index, value, size))) elif type == 'setvalue': value = self.get_argument('value','') self.write(utils.format_json_result(data=commands.send_command_zwave(node_id, cc_id, instance_id, index, value))) elif type == 'switchall': state = int(self.get_argument('state','0')) if state == 0: logging.info("SwitchAll Off") globals.network.switch_all(False) else: logging.info("SwitchAll On") globals.network.switch_all(True) for node_id in globals.network.nodes: my_node = globals.network.nodes[node_id] if my_node.is_failed: continue value_ids = my_node.get_switches_all() if value_ids is not None and len(value_ids) > 0: for value_id in value_ids: if my_node.values[value_id].data == "Disabled": continue elif my_node.values[value_id].data == "On and Off Enabled": pass if my_node.values[value_id].data == "Off Enabled" and state != 0: continue if my_node.values[value_id].data == "On Enabled" and state == 0: continue for switch in my_node.get_switches(): my_node.values[switch].refresh() for dimmer in my_node.get_dimmers(): my_node.values[dimmer].refresh() self.write(utils.format_json_result()) elif type == 'setDeviceName': location = self.get_argument('location','') name = self.get_argument('name','') is_enable = int(self.get_argument('is_enable','0')) logging.info("set_device_name node_id:%s new name ; '%s'. Is enable: %s" % (node_id, name, is_enable,)) if node_id in globals.disabled_nodes and is_enable: globals.disabled_nodes.remove(node_id) elif node_id not in globals.disabled_nodes and not is_enable: globals.disabled_nodes.append(node_id) name = name.encode('utf8') name = name.replace('+', ' ') globals.network.nodes[node_id].set_field('name', name) location = location.encode('utf8') location = location.replace('+', ' ') globals.network.nodes[node_id].set_field('location', location) self.write(utils.format_json_result()) elif type == 'association': group = int(self.get_argument('group','0')) self.write(node_utils.add_assoc(node_id, group, target_id,instance_id,action)) else: self.write(utils.format_json_result()) except Exception,e: logging.error('RequestHandler ' + e.message) self.write(utils.format_json_result(success="error",data=str(e)))