def add_network(): # First get the list of adapters response = nymea.send_command("Zigbee.GetAdapters", {}) if len(response['params']['adapters']) == 0: print "There are no adapters available." return None adapterList = []; for adapter in response['params']['adapters']: adapterList.append("%s (%s) - %s" % (adapter['description'], adapter['serialPort'], adapter['name'])) selection = nymea.get_selection("Please select a device descriptor", adapterList) selectedAdapter = {} if selection != None: selectedAdapter = response['params']['adapters'][selection] else: print "ERROR: invalid adapter selection." return None print("Selected adapter:") nymea.print_json_format(selectedAdapter) params = {} params["serialPort"] = selectedAdapter["serialPort"] params["baudRate"] = selectedAdapter["baudRate"] params["backend"] = selectedAdapter["backend"] response = nymea.send_command("Zigbee.AddNetwork", params) print("Add network returned %s" % response["params"]["zigbeeError"]) if response["params"]["zigbeeError"] == "ZigbeeErrorNoError": print("New network has the uuid %s" % response["params"]["networkUuid"])
def notification_sniffer(nymeaHost, nymeaPort): global commandId commandId = 0 print "Connecting notification handler..." try: tn = telnetlib.Telnet(nymeaHost, nymeaPort) except: print "ERROR: notification socket could not connect the to nymea-server. \n" return None print "...OK \n" #enable_notification(notificationSocket) enable_notification(tn.get_socket()) try: x = None while (x != ord('\n') and x != 27): socket_list = [sys.stdin, tn.get_socket()] read_sockets, write_sockets, error_sockets = select.select( socket_list, [], []) for sock in read_sockets: # notification messages: if sock == tn.get_socket(): packet = tn.read_until("}\n") packet = json.loads(packet) nymea.print_json_format(packet) elif sock == sys.stdin: x = sys.stdin.readline() return None finally: tn.close() print "Notification socket closed."
def list_rule_details(): ruleDescription = select_rule() if ruleDescription == None: print "\n No rules found" return None params = {} params['ruleId'] = ruleDescription['id'] rule = nymea.send_command("Rules.GetRuleDetails", params)['params']['rule'] print nymea.print_json_format(rule) nymea.debug_stop() print "========================================================" print "-> Details for rule \"%s\" (%s):\n" % (rule['name'], rule['id']) print "Rule is %s" % (print_rule_enabled_status(bool(rule['enabled']))) print "Rule is %s" % (print_rule_active_status(bool(rule['active']))) print "Rule is %s" % (print_rule_executable_status(bool( rule['executable']))) print "\nTimeDescriptor:\n" timedescriptor.printTimeDescriptor(rule['timeDescriptor']) if len(rule['eventDescriptors']) > 0: print "\nEvents:\n" events.print_eventDescriptors(rule['eventDescriptors']) if rule['stateEvaluator']: print "\nStates:\n", states.print_stateEvaluator(rule['stateEvaluator']) print "\nActions:\n" ruleactions.print_ruleActionList(rule['actions']) if len(rule['exitActions']) > 0: print "\nExit actions:\n" ruleactions.print_ruleActionList(rule['exitActions'])
def show_network_status(): params = {} response = nymea.send_command("NetworkManager.GetNetworkStatus", params) if 'status' in response['params']: nymea.print_json_format(response['params']['status']) else: nymea.print_networkmanager_error_code( response['params']['networkManagerError'])
def list_available_adapters(): params = {} response = nymea.send_command("Zigbee.GetAdapters", params) if len(response['params']['adapters']) == 0: print "There are no adapters available." return None nymea.print_json_format(response['params'])
def print_stateType(): deviceId = devices.select_configured_device() device = devices.get_device(deviceId) if device == None: return None stateType = select_stateType(device['deviceClassId']) if stateType == None: return None nymea.print_json_format(stateType)
def method_last_72_hours(): global nymeaHost global nymeaPort params = logs.create_last_time_logfilter(72 * 60) print "\nThe filter:\n" nymea.print_json_format(params) nymea.debug_stop() if params: logs.log_window(nymeaHost, nymeaPort, params)
def method_rule_logs(): global nymeaHost global nymeaPort params = logs.create_rule_logfilter() print "\nThe filter:\n" nymea.print_json_format(params) nymea.debug_stop() if params: logs.log_window(nymeaHost, nymeaPort, params)
def print_eventType(): deviceId = devices.select_configured_device() device = devices.get_device(deviceId) if not device: return None eventType = select_eventType(device['deviceClassId']) if not eventType: print "\n This device has no events" return None nymea.print_json_format(eventType)
def remove_network(): selectedNetwork = selectNetwork("Please select the network you want to remove") if selectedNetwork is None: return None print("Selected network:") nymea.print_json_format(selectedNetwork) params = {} params["networkUuid"] = selectedNetwork["networkUuid"] response = nymea.send_command("Zigbee.RemoveNetwork", params) print("Remove network returned %s" % response["params"]["zigbeeError"])
def set_language(): params = {} languages = get_languages() selection = nymea.get_selection( "Please select one of following allowed values:", languages) if selection == None: return None params['language'] = languages[selection] response = nymea.send_command("Configuration.SetLanguage", params) nymea.print_json_format(response['params'])
def set_debug_server_interface(): params = {} enabled = selector.getYesNoSelection( "Should the debug server interface be enabled?") if enabled == None: return None params['enabled'] = enabled response = nymea.send_command("Configuration.SetDebugServerEnabled", params) nymea.print_json_format(response['params'])
def enable_cloud_connection(): params = {} options = ["enable", "disable"] selection = nymea.get_selection( "Do you want to do with the cloud connection: ", options) if selection == 0: params['enable'] = True else: params['enable'] = False response = nymea.send_command("Cloud.Enable", params) nymea.print_json_format(response['params'])
def set_timezone(): params = {} timeZones = [] timeZones = get_timezones() selection = nymea.get_selection( "Please select one of following allowed values:", timeZones) if selection == None: return None params['timeZone'] = timeZones[selection] response = nymea.send_command("Configuration.SetTimeZone", params) nymea.print_json_format(response['params'])
def print_actionType(): deviceId = devices.select_configured_device() if deviceId == None: return None device = devices.get_device(deviceId) actionType = select_actionType(device['deviceClassId']) #print nymea.print_json_format(actionType) if actionType == None: print "\n This device has no actions" return None actionType = get_actionType(actionType['id']) nymea.print_json_format(actionType)
def add_rule(): params = {} params['name'] = raw_input("Please enter the name of the rule: ") print "\n========================================================" if selector.getYesNoSelection("Do you want to create a TimeDescriptor"): params['timeDescriptor'] = timedescriptor.createTimeDescriptor() if selector.getYesNoSelection( "Do you want to define \"Events\" for this rule?"): eventDescriptors = events.create_eventDescriptors() print nymea.print_json_format(eventDescriptors) params['eventDescriptors'] = eventDescriptors if selector.getYesNoSelection( "Do you want to add conditions (\"States\") for the events?"): print "\n========================================================" raw_input("-> Create a state descriptor! ") stateEvaluator = states.create_stateEvaluator() params['stateEvaluator'] = stateEvaluator params['actions'] = ruleactions.create_rule_actions(eventDescriptors) else: if selector.getYesNoSelection( "Do you want to define \"States\" for this rule?"): print "\n========================================================" raw_input("-> Press \"enter\" to create a state descriptor! ") params['stateEvaluator'] = states.create_stateEvaluator() params['actions'] = ruleactions.create_rule_actions() if selector.getYesNoSelection( "Do you want to add (\"ExitActions\") for this rule?"): params['exitActions'] = ruleactions.create_rule_actions() params['enabled'] = selector.getBoolSelection( "-> Should the rule initially be enabled?") params['executable'] = selector.getBoolSelection( "-> Should the rule be executable?") print "\n========================================================\n" print "Adding rule with params:\n", nymea.print_json_format(params) response = nymea.send_command("Rules.AddRule", params) nymea.print_rule_error_code(response['params']['ruleError'])
def list_device_states(): deviceId = select_configured_device() if deviceId == None: return None device = get_device(deviceId) deviceClass = get_deviceClass(device['deviceClassId']) nymea.print_json_format(device) print "-> States of device \"%s\" %s:\n" % (get_full_device_name(deviceId), device['id']) for i in range(len(deviceClass['stateTypes'])): params = {} params['deviceId'] = deviceId params['stateTypeId'] = deviceClass['stateTypes'][i]['id'] response = nymea.send_command("Devices.GetStateValue", params) print "%35s: %s" % (deviceClass['stateTypes'][i]['displayName'], response['params']['value'])
def selectNetwork(message): params = {} response = nymea.send_command("Zigbee.GetNetworks", params) nymea.print_json_format(response['params']) if len(response['params']['zigbeeNetworks']) == 0: print "ERROR: there are no networks configured." return None networkList = []; for network in response['params']['zigbeeNetworks']: networkList.append("%s %s (channel %s) - %s" % (network['backend'], network['macAddress'], network['channel'], network['serialPort'])) selection = nymea.get_selection(message, networkList) selectedNetwork = {} if selection != None: return response['params']['zigbeeNetworks'][selection] else: return None
def add_configured_device(deviceClassId): deviceClass = get_deviceClass(deviceClassId) params = {} params['deviceClassId'] = deviceClassId params['name'] = raw_input("\nEnter the \"name\" of the device: ") deviceParams = parameters.read_params(deviceClass['paramTypes']) if deviceParams: params['deviceParams'] = deviceParams print "\nAdding device with params:", nymea.print_json_format(params) response = nymea.send_command("Devices.AddConfiguredDevice", params) nymea.print_device_error_code(response['params']['deviceError'])
def edit_params(currentDeviceParams, paramTypes): params = [] for paramType in paramTypes: print "\nThe ParamType looks like this:\n", nymea.print_json_format( paramType) if 'readOnly' in paramType and paramType['readOnly']: print "--------------------------------------------------------" print "\nThe param \"%s\" is not writable! (current = \"%s\")\n" % ( paramType['displayName'], get_param_value(paramType['id'], currentDeviceParams)) raw_input("\nPress \"enter\" to continue...\n") print "--------------------------------------------------------" continue param = {} if any("allowedValues" in item for item in paramType) and len(paramType['allowedValues']) != 0: title = "Please select one of following allowed values: (current = \"%s\")" % ( get_param_value(paramType['id'], currentDeviceParams)) selection = nymea.get_selection(title, paramType['allowedValues']) if selection == None: return None paramValue = paramType['allowedValues'][selection] param['paramTypeId'] = paramType['id'] param['value'] = paramValue params.append(param) else: # make bool selectable to make shore they are "true" or "false" if paramType['type'] == "Bool": boolTypes = ["true", "false"] selectionString = "Please enter value (currently: \"%s\") for parameter \"%s\" (type: %s): " % ( get_param_value(paramType['id'], currentDeviceParams), paramType['displayName'], paramType['type']) selection = nymea.get_selection(selectionString, boolTypes) if selection == None: return None paramValue = boolTypes[selection] param['paramTypeId'] = paramType['id'] param['value'] = paramValue else: paramValue = raw_input( "Please enter value (currently: \"%s\") for parameter \"%s\" (type: %s): " % (get_param_value(paramType['id'], currentDeviceParams), paramType['displayName'], paramType['type'])) param['paramTypeId'] = paramType['id'] param['value'] = paramValue params.append(param) return params
def create_logfilter(): params = {} boolTypes = ["yes", "no"] # Devices selection = nymea.get_selection("Do you want to filter for \"Devices\"? ", boolTypes) if boolTypes[selection] == "yes": deviceIds = [] deviceId = devices.select_configured_device() deviceIds.append(deviceId) finished = False while not finished: selection = nymea.get_selection( "Do you want to add an other \"Device\"? ", boolTypes) if boolTypes[selection] == "no": finished = True break deviceId = devices.select_configured_device() if not deviceId: params['deviceIds'] = deviceIds return params deviceIds.append(deviceId) params['deviceIds'] = deviceIds # LoggingSources selection = nymea.get_selection( "Do you want to filter for \"LoggingSource\"? ", boolTypes) if boolTypes[selection] == "yes": sources = [] finished = False loggingSources = [ "LoggingSourceSystem", "LoggingSourceEvents", "LoggingSourceActions", "LoggingSourceStates", "LoggingSourceRules" ] selection = nymea.get_selection("Please select a \"LoggingSource\": ", loggingSources) if selection: sources.append(loggingSources[selection]) else: finished = True while not finished: selection = nymea.get_selection( "Do you want to add an other \"LoggingSource\"? ", boolTypes) if boolTypes[selection] == "no": finished = True break selection = get_selection("Please select a \"LoggingSource\": ", loggingSources) if selection: sources.append(loggingSources[selection]) else: finished = True break params['loggingSources'] = sources # LoggingLevel selection = nymea.get_selection( "Do you want to filter for \"LoggingLevel\"? ", boolTypes) if boolTypes[selection] == "yes": levels = [] loggingLevels = ["LoggingLevelInfo", "LoggingLevelAlert"] selection = nymea.get_selection("Please select a \"LoggingLevel\": ", loggingLevels) if selection: levels.append(loggingLevels[selection]) params['loggingLevels'] = levels # LoggingEventType selection = nymea.get_selection( "Do you want to filter for \"LoggingEventType\"? ", boolTypes) if boolTypes[selection] == "yes": types = [] loggingEventTypes = [ "LoggingEventTypeTrigger", "LoggingEventTypeActiveChange", "LoggingEventTypeEnabledChange", "LoggingEventTypeActionsExecuted", "LoggingEventTypeExitActionsExecuted" ] selection = nymea.get_selection( "Please select a \"LoggingEventType\": ", loggingEventTypes) if selection: types.append(loggingEventTypes[selection]) params['eventTypes'] = types # Value selection = nymea.get_selection( "Do you want to filter for certain log \"Values\"? ", boolTypes) if boolTypes[selection] == "yes": values = [] finished = False value = raw_input("Please enter value which should be filtered out: ") values.append(value) while not finished: selection = nymea.get_selection( "Do you want to add an other \"Value\"? ", boolTypes) if boolTypes[selection] == "no": finished = True break value = raw_input( "Please enter value which should be filtered out: ") values.append(value) params['values'] = values # Times selection = nymea.get_selection("Do you want to add a \"TimeFilter\"? ", boolTypes) if boolTypes[selection] == "yes": timeFilters = [] finished = False timeFilters.append(create_time_filter()) while not finished: selection = nymea.get_selection( "Do you want to add an other \"TimeFilter\"? ", boolTypes) if boolTypes[selection] == "no": finished = True break timeFilters.append(create_time_filter()) params['timeFilters'] = timeFilters nymea.print_json_format(params) nymea.debug_stop() return params
def list_network_devices(): params = {} response = nymea.send_command("NetworkManager.GetNetworkDevices", params) nymea.print_json_format(response['params'])
def print_cloud_status(): params = {} response = nymea.send_command("Cloud.GetConnectionStatus", params) nymea.print_json_format(response['params'])
def list_networks(): params = {} response = nymea.send_command("Zigbee.GetNetworks", params) nymea.print_json_format(response['params'])
def set_serverName(): params = {} params['serverName'] = raw_input("Please enter the server name:") response = nymea.send_command("Configuration.SetServerName", params) nymea.print_json_format(response['params'])
def list_backends(): params = {} response = nymea.send_command("Zigbee.GetAvailableBackends", params) nymea.print_json_format(response['params'])
def show_tcpServer_configuration(): response = nymea.send_command("Configuration.GetConfigurations") print "TCP server configuration\n" nymea.print_json_format(response['params']['tcpServerConfigurations'])
def create_stateDescriptor(): print "-> Creating a new stateDescriptor:\n" deviceId = devices.select_configured_device() device = devices.get_device(deviceId) if device == None: return None stateType = select_stateType(device['deviceClassId']) if stateType == None: return None valueOperator = nymea.select_valueOperator(stateType['displayName']) stateDescriptor = {} print "\nThe StateType looks like this:\n", nymea.print_json_format( stateType) print "Please enter the value for state \"%s\" (type: %s): " % ( stateType['displayName'], stateType['type']) # make bool selectable to make shore they are "true" or "false" if any("possibleValues" in item for item in stateType): # has to be a string (for sorting list) possibleValues = [] for value in stateType['possibleValues']: possibleValues.append(str(value)) selection = nymea.get_selection( "Please select one of following possible values:", possibleValues) if selection == None: return None stateValue = stateType['possibleValues'][selection] else: # make bool selectable to make shore they are "true" or "false" if stateType['type'] == "Bool": boolTypes = ["true", "false"] selectionString = "Please enter the value for state \"%s\" (type: %s): " % ( stateType['displayName'], stateType['type']) selection = nymea.get_selection(selectionString, boolTypes) if selection == None: return None stateValue = boolTypes[selection] elif stateType['type'] == "Int": stateValue = int( raw_input("%s %s " % (stateType['displayName'], nymea.get_valueOperator_string(valueOperator)))) elif stateType['type'] == "Double": stateValue = float( raw_input("%s %s " % (stateType['displayName'], nymea.get_valueOperator_string(valueOperator)))) elif stateType['type'] == "Uint": stateValue = int( raw_input("%s %s " % (stateType['displayName'], nymea.get_valueOperator_string(valueOperator)))) else: stateValue = raw_input( "%s %s " % (stateType['displayName'], nymea.get_valueOperator_string(valueOperator))) stateDescriptor['deviceId'] = deviceId stateDescriptor['stateTypeId'] = stateType['id'] stateDescriptor['value'] = stateValue stateDescriptor['operator'] = valueOperator print nymea.print_json_format(stateDescriptor) return stateDescriptor
def edit_rule(): ruleDescription = select_rule() if ruleDescription == None: print "\n No rules found" return None boolTypes = ["yes", "no"] params = {} params['ruleId'] = ruleDescription['id'] originalRule = nymea.send_command("Rules.GetRuleDetails", params)['params']['rule'] print "\n========================================================" print "Original rule JSON:\n" print nymea.print_json_format(originalRule) if selector.getYesNoSelection( "Do you want to chane the name (current = \"%s\"): " % (originalRule['name'])): print "\n========================================================" params['name'] = raw_input( "Please enter the new name of the rule (current = \"%s\"): " % (originalRule['name'])) else: params['name'] = originalRule['name'] if len(originalRule['eventDescriptors']) > 0: print "\nCurrent \"Events\":\n" events.print_eventDescriptors(originalRule['eventDescriptors']) print "\n========================================================\n" input = raw_input("Do you want change this \"Events\" (y/N): ") if input == "y": eventDescriptors = events.create_eventDescriptors() print nymea.print_json_format(eventDescriptors) params['eventDescriptors'] = eventDescriptors else: params['eventDescriptors'] = originalRule['eventDescriptors'] if originalRule['stateEvaluator']: print "\nStates:\n", states.print_stateEvaluator(originalRule['stateEvaluator']) if selector.getYesNoSelection( "Do you want to add conditions (\"States\") for the events?"): print "\n========================================================" raw_input("-> Create a state descriptor! ") stateEvaluator = states.create_stateEvaluator() params['stateEvaluator'] = stateEvaluator print "\ncurrent \"Actions\":\n" ruleactions.print_ruleActionList(originalRule['actions']) print "\n========================================================\n" input = raw_input("Do you want change this \"Actions\" (y/N): ") if input == "y": params['actions'] = ruleactions.create_rule_actions() else: params['actions'] = originalRule['actions'] else: if selector.getYesNoSelection( "Do you want to define \"States\" for this rule?"): print "\n========================================================" raw_input("-> Press \"enter\" to create a state descriptor! ") params['stateEvaluator'] = states.create_stateEvaluator() # there will always be at least one action print "\n========================================================\n" print "Current \"Actions\":\n" ruleactions.print_ruleActionList(originalRule['actions']) print "\n========================================================\n" input = raw_input("Do you want change this \"Actions\" (y/N): ") if input == "y": params['actions'] = ruleactions.create_rule_actions() else: params['actions'] = originalRule['actions'] if len(originalRule['exitActions']) > 0: print "\n========================================================\n" print "Current \"Actions\":\n" ruleactions.print_ruleActionList(originalRule['exitActions']) input = raw_input("Do you want change this \"Events\" (y/N): ") if input == "y": params['exitActions'] = ruleactions.create_rule_actions() else: params['exitActions'] = originalRule['exitActions'] else: if selector.getYesNoSelection( "Do you want to add (\"ExitActions\") for this rule?"): params['exitActions'] = ruleactions.create_rule_actions() params['enabled'] = selector.getBoolSelection( "-> Should the rule initially be enabled?") params['executable'] = selector.getBoolSelection( "-> Should the rule be executable?") print "\n========================================================\n" print "Edit rule with params:\n", nymea.print_json_format(params) response = nymea.send_command("Rules.EditRule", params) nymea.print_rule_error_code(response['params']['ruleError'])
def read_ruleActionParams(paramTypes, eventDescriptors=[]): params = [] for paramType in paramTypes: print nymea.print_json_format(paramType) if any("allowedValues" in item for item in paramType): selection = nymea.get_selection( "Please select one of following allowed values:", paramType['allowedValues']) if selection == None: return None paramValue = paramType['allowedValues'][selection] param = {} param['paramTypeId'] = paramType['id'] param['value'] = paramValue params.append(param) else: # check if we want normal action params or if we want to use an eventtypeid if eventDescriptors: selectionTypes = ["yes", "no"] selectionText = "-> Should the ruleActionParam \"%s\" depend on an event?" % ( paramType['displayName']) selection = nymea.get_selection(selectionText, selectionTypes) if selection == None: return None if selectionTypes[selection] == "yes": eventTypeIds = [] for i in eventDescriptors: eventTypeIds.append(i['eventTypeId']) selection = nymea.get_selection( "Please select an eventTypeId", eventTypeIds) eventTypeId = eventTypeIds[selection] eventType = events.get_eventType(eventTypeId) eventParamNames = [] eventParamTypeIds = [] for i in eventType['paramTypes']: eventParamNames.append(i['displayName']) eventParamTypeIds.append(i['id']) paramSelection = nymea.get_selection( "Please select the name of the eventParam for the action", eventParamNames) param = {} param['paramTypeId'] = paramType['id'] param['eventTypeId'] = eventTypeId param['eventParamTypeId'] = eventParamTypeIds[ paramSelection] params.append(param) else: if paramType['type'] == "Bool": boolTypes = ["true", "false"] selectionString = "Please enter value for parameter %s (type: %s): " % ( paramType['displayName'], paramType['type']) selection = nymea.get_selection( selectionString, boolTypes) if selection == None: return None paramValue = boolTypes[selection] elif paramType['type'] == "Int": paramValue = int( raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type']))) elif paramType['type'] == "Double": paramValue = double( raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type']))) elif paramType['type'] == "Uint": paramValue = uint( raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type']))) else: paramValue = raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type'])) param = {} param['paramTypeId'] = paramType['id'] param['value'] = paramValue params.append(param) else: # make bool selectable to make shore they are "true" or "false" if paramType['type'] == "Bool": boolTypes = ["true", "false"] selectionString = "Please enter value for parameter %s (type: %s): " % ( paramType['displayName'], paramType['type']) selection = nymea.get_selection(selectionString, boolTypes) if selection == None: return None paramValue = boolTypes[selection] elif paramType['type'] == "Int": paramValue = int( raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type']))) elif paramType['type'] == "Double": paramValue = float( raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type']))) elif paramType['type'] == "Uint": paramValue = int( raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type']))) else: paramValue = raw_input( "Please enter value for parameter \"%s\" (type: %s): " % (paramType['displayName'], paramType['type'])) param = {} param['paramTypeId'] = paramType['id'] param['value'] = paramValue params.append(param) return params