rename cmdmgr.py -> guh-cli.py
This commit is contained in:
parent
24b7acf5f0
commit
d1b638017b
@ -7,48 +7,73 @@ HOST='localhost'
|
||||
PORT=1234
|
||||
commandId=0
|
||||
|
||||
methods = {'-> List supported vendors': 'list_vendors',
|
||||
'-> List supported devices': 'list_deviceClasses',
|
||||
'-> List configured devices': 'list_configured_devices',
|
||||
'-> List configured device params': 'list_configured_device_params',
|
||||
'-> Add device': 'add_device',
|
||||
'-> Remove a device': 'remove_device',
|
||||
'-> List supported devices by vendor': 'list_deviceClasses_by_vendor',
|
||||
'-> Execute an action': 'execute_action',
|
||||
'-> List device states': 'list_device_states',
|
||||
'-> Add a rule': 'add_rule',
|
||||
'-> List rules': 'list_rules',
|
||||
'-> List rule details': 'list_rule_detail',
|
||||
'-> List rules containing a certain device' : 'list_rules_containig_deviceId',
|
||||
'-> Remove a rule': 'remove_rule',
|
||||
'-> Enable/Disable a rule': 'enable_disable_rule'}
|
||||
methods = {'1': 'add_device',
|
||||
'2': 'remove_device',
|
||||
'3': 'list_configured_device_params',
|
||||
'4': 'list_device_states',
|
||||
'5': 'execute_action',
|
||||
'6': 'add_rule',
|
||||
'7': 'remove_rule',
|
||||
'8': 'list_rule_detail',
|
||||
'9': 'enable_disable_rule',
|
||||
'10': 'list_vendors',
|
||||
'11': 'list_configured_devices',
|
||||
'12': 'list_deviceClasses',
|
||||
'13': 'list_deviceClasses_by_vendor',
|
||||
'14': 'list_rules',
|
||||
'15': 'list_rules_containig_deviceId'}
|
||||
|
||||
list_methods = {'-> List supported vendors': 'list_vendors',
|
||||
'-> List supported devices': 'list_deviceClasses',
|
||||
'-> List supported devices by vendor': 'list_deviceClasses_by_vendor'}
|
||||
|
||||
device_methods = {'-> Add a device': 'add_device',
|
||||
'-> Remove a device': 'remove_device',
|
||||
'-> List configured devices': 'list_configured_devices',
|
||||
'-> Execute an action': 'execute_action',
|
||||
'-> List device states': 'list_device_states',
|
||||
'-> List device parameters': 'list_configured_device_params'}
|
||||
|
||||
rule_methods = {'-> Add a rule': 'add_rule',
|
||||
'-> Remove a rule': 'remove_rule',
|
||||
'-> List rules containing a certain device' : 'list_rules_containig_deviceId',
|
||||
'-> List rule details': 'list_rule_detail',
|
||||
'-> List rules': 'list_rules'}
|
||||
def get_menu_selection():
|
||||
print ""
|
||||
print "----------------------------------------"
|
||||
print "What do you want to do?"
|
||||
print "----------------------------------------"
|
||||
print ""
|
||||
print "Devices --------------------------------"
|
||||
print " 1 -> Add a new device"
|
||||
print " 2 -> Remove a device"
|
||||
print " 3 -> List device parameters"
|
||||
print " 4 -> List device states"
|
||||
print " 5 -> Execute an action"
|
||||
print ""
|
||||
print "Rules ----------------------------------"
|
||||
print " 6 -> Add a new rule"
|
||||
print " 7 -> Remove a rule"
|
||||
print " 8 -> Rule details"
|
||||
print " 9 -> Enable/Disable a rule"
|
||||
print ""
|
||||
print "Other ----------------------------------"
|
||||
print " 10 -> List supported vendors"
|
||||
print " 11 -> List configured devices"
|
||||
print " 12 -> List supported devices"
|
||||
print " 13 -> List supported devices by vendor"
|
||||
print " 14 -> List configured rules"
|
||||
print " 15 -> List rules containing a certain device"
|
||||
print "----------------------------------------"
|
||||
print ""
|
||||
selection = raw_input("Enter selection: ")
|
||||
n = 0
|
||||
for i in methods.keys():
|
||||
if i == selection:
|
||||
return methods.values()[int(n)]
|
||||
|
||||
n= n+1
|
||||
|
||||
print "\nError in selection!"
|
||||
return None
|
||||
|
||||
|
||||
def get_selection(title, options):
|
||||
print "\n\n", title
|
||||
for i in range(0,len(options)):
|
||||
print "%5i: %s" % (i, options[i])
|
||||
|
||||
selection = raw_input("Enter selection: ")
|
||||
if not selection:
|
||||
print "\n -> error in selection"
|
||||
return None
|
||||
|
||||
return int(selection)
|
||||
|
||||
|
||||
@ -66,6 +91,8 @@ def send_command(method, params = None):
|
||||
response = json.loads(tn.read_until("\n}\n"))
|
||||
if response['status'] != "success":
|
||||
print "JSON error happened: %s" % response
|
||||
return None
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@ -78,6 +105,7 @@ def list_vendors():
|
||||
print "=== Vendors ==="
|
||||
for vendor in response['params']['vendors']:
|
||||
print "%40s %s" % (vendor['name'], vendor['id'])
|
||||
|
||||
print "=== Vendors ==="
|
||||
|
||||
|
||||
@ -85,12 +113,14 @@ def select_vendor():
|
||||
vendors = get_vendors()['params']['vendors']
|
||||
if not vendors:
|
||||
print "\n No vendors found. Please install guh-plugins and restart guhd."
|
||||
return ""
|
||||
return None
|
||||
|
||||
vendorList = []
|
||||
vendorIdList = []
|
||||
for i in range(0,len(vendors)):
|
||||
vendorList.append(vendors[i]['name'])
|
||||
vendorIdList.append(vendors[i]['id'])
|
||||
|
||||
selection = get_selection("Please select vendor", vendorList)
|
||||
if selection != None:
|
||||
return vendorIdList[selection]
|
||||
@ -100,6 +130,7 @@ def get_deviceClasses(vendorId = None):
|
||||
params = {};
|
||||
if vendorId != None:
|
||||
params['vendorId'] = vendorId
|
||||
|
||||
return send_command("Devices.GetSupportedDevices", params)['params']['deviceClasses']
|
||||
|
||||
def list_configured_device_params():
|
||||
@ -110,6 +141,7 @@ def list_configured_device_params():
|
||||
print "=== Params ==="
|
||||
for i in range(len(deviceParams)):
|
||||
print "%20s: %s" % (deviceParams[i]['name'], deviceParams[i]['value'])
|
||||
|
||||
print "=== Params ==="
|
||||
|
||||
|
||||
@ -118,6 +150,7 @@ def list_deviceClasses(vendorId = None):
|
||||
print "=== DeviceClasses ==="
|
||||
for deviceClass in response:
|
||||
print "%40s %s" % (deviceClass['name'], deviceClass['id'])
|
||||
|
||||
print "=== DeviceClasses ==="
|
||||
|
||||
|
||||
@ -126,12 +159,14 @@ def select_deviceClass():
|
||||
deviceClasses = get_deviceClasses(vendorId)
|
||||
if len(deviceClasses) == 0:
|
||||
print " No supported devices for this vendor"
|
||||
return ""
|
||||
return None
|
||||
|
||||
deviceClassList = []
|
||||
deviceClassIdList = []
|
||||
for i in range(0,len(deviceClasses)):
|
||||
deviceClassList.append(deviceClasses[i]['name'])
|
||||
deviceClassIdList.append(deviceClasses[i]['id'])
|
||||
|
||||
selection = get_selection("Please select device class", deviceClassList)
|
||||
if selection != None:
|
||||
return deviceClassIdList[selection]
|
||||
@ -144,9 +179,11 @@ def select_configured_device():
|
||||
for device in devices:
|
||||
deviceList.append(device['name'])
|
||||
deviceIdList.append(device['id'])
|
||||
|
||||
selection = get_selection("Please select a device: ", deviceList)
|
||||
if selection != None:
|
||||
return deviceIdList[selection]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@ -176,6 +213,7 @@ def list_configured_devices():
|
||||
print "=== Configured Devices ==="
|
||||
for device in deviceList:
|
||||
print "Name: %40s, ID: %s, DeviceClassID: %s" % (device['name'], device['id'], device['deviceClassId'])
|
||||
|
||||
print "=== Configured Devices ==="
|
||||
|
||||
|
||||
@ -193,8 +231,9 @@ def read_params(paramTypes):
|
||||
param = {}
|
||||
param['name'] = paramType['name']
|
||||
param['value'] = paramValue
|
||||
|
||||
params.append(param)
|
||||
#print "got params:", params
|
||||
|
||||
return params
|
||||
|
||||
|
||||
@ -203,6 +242,7 @@ def select_valueOperator():
|
||||
selection = get_selection("Please select an operator to compare this parameter: ", valueOperators)
|
||||
if selection != None:
|
||||
return valueOperators[selection]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@ -211,6 +251,7 @@ def select_stateOperator():
|
||||
selection = get_selection("Please select an operator to compare this state: ", stateOperators)
|
||||
if selection != None:
|
||||
return stateOperators[selection]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@ -224,6 +265,7 @@ def read_paramDescriptors(paramTypes):
|
||||
param['value'] = paramValue
|
||||
param['operator'] = operator
|
||||
params.append(param)
|
||||
|
||||
print "got params:", params
|
||||
return params
|
||||
|
||||
@ -231,11 +273,10 @@ def read_paramDescriptors(paramTypes):
|
||||
def discover_device(deviceClassId = None):
|
||||
if deviceClassId == None:
|
||||
deviceClassId = select_deviceClass()
|
||||
|
||||
deviceClass = get_deviceClass(deviceClassId)
|
||||
|
||||
params = {}
|
||||
params['deviceClassId'] = deviceClassId
|
||||
|
||||
discoveryParams = read_params(deviceClass['discoveryParamTypes'])
|
||||
if len(discoveryParams) > 0:
|
||||
params['discoveryParams'] = discoveryParams
|
||||
@ -247,19 +288,23 @@ def discover_device(deviceClassId = None):
|
||||
for deviceDescriptor in response['params']['deviceDescriptors']:
|
||||
deviceDescriptorList.append("%s (%s)" % (deviceDescriptor['title'], deviceDescriptor['description']))
|
||||
deviceDescriptorIdList.append(deviceDescriptor['id'])
|
||||
|
||||
if not deviceDescriptorIdList:
|
||||
print "\n No device found"
|
||||
return -1
|
||||
print "\n Timeout: no device found"
|
||||
return None
|
||||
|
||||
selection = get_selection("Please select a device descriptor", deviceDescriptorList)
|
||||
if selection != None:
|
||||
return deviceDescriptorIdList[selection]
|
||||
|
||||
|
||||
|
||||
def get_deviceClass(deviceClassId):
|
||||
deviceClasses = get_deviceClasses()
|
||||
for deviceClass in deviceClasses:
|
||||
if deviceClass['id'] == deviceClassId:
|
||||
return deviceClass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@ -268,6 +313,7 @@ def get_device(deviceId):
|
||||
for device in devices:
|
||||
if device['id'] == deviceId:
|
||||
return device
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@ -339,14 +385,16 @@ def add_device():
|
||||
if deviceClassId == "":
|
||||
print " Empty deviceClass. Can't continue"
|
||||
return
|
||||
|
||||
deviceClass = get_deviceClass(deviceClassId)
|
||||
print "createmethods are", deviceClass['createMethods']
|
||||
if "CreateMethodUser" in deviceClass['createMethods']:
|
||||
add_configured_device(deviceClassId)
|
||||
elif "CreateMethodDiscovery" in deviceClass['createMethods']:
|
||||
deviceDescriptorId = discover_device(deviceClassId)
|
||||
if deviceDescriptorId == -1:
|
||||
if deviceDescriptorId == None:
|
||||
return
|
||||
|
||||
add_discovered_device(deviceClassId, deviceDescriptorId)
|
||||
elif "CreateMethodAuto" in deviceClass['createMethods']:
|
||||
print "Can't create this device manually. It'll be created automatically when hardware is discovered."
|
||||
@ -359,6 +407,7 @@ def select_device():
|
||||
for i in range(len(devices)):
|
||||
deviceList.append(devices[i]['name'])
|
||||
deviceIdList.append(devices[i]['id'])
|
||||
|
||||
selection = get_selection("Please select a device", deviceList)
|
||||
if selection != None:
|
||||
return deviceIdList[selection]
|
||||
@ -376,12 +425,14 @@ def remove_device():
|
||||
def select_actionType(deviceClassId):
|
||||
actions = get_action_types(deviceClassId)
|
||||
if not actions:
|
||||
return ""
|
||||
return None
|
||||
|
||||
actionList = []
|
||||
print "got actions", actions
|
||||
for i in range(len(actions)):
|
||||
print "got actiontype", actions[i]
|
||||
actionList.append(actions[i]['name'])
|
||||
|
||||
selection = get_selection("Please select an action type:", actionList)
|
||||
return actions[selection]
|
||||
|
||||
@ -389,10 +440,12 @@ def select_actionType(deviceClassId):
|
||||
def select_eventType(deviceClassId):
|
||||
eventTypes = get_eventTypes(deviceClassId)
|
||||
if not eventTypes:
|
||||
return ""
|
||||
return None
|
||||
|
||||
eventTypeList = []
|
||||
for i in range(len(eventTypes)):
|
||||
eventTypeList.append(eventTypes[i]['name'])
|
||||
|
||||
selection = get_selection("Please select an event type:", eventTypeList)
|
||||
return eventTypes[selection]
|
||||
|
||||
@ -404,6 +457,7 @@ def execute_action():
|
||||
if actionType == "":
|
||||
print "\n This device has no actions"
|
||||
return
|
||||
|
||||
actionTypeId = actionType['id']
|
||||
params = {}
|
||||
params['actionTypeId'] = actionTypeId
|
||||
@ -488,10 +542,9 @@ def list_device_states():
|
||||
params = {}
|
||||
params['deviceId'] = deviceId
|
||||
params['stateTypeId'] = deviceClass['stateTypes'][i]['id']
|
||||
|
||||
response = send_command("Devices.GetStateValue", params)
|
||||
#print_device_error_code(response['params']['deviceError'])
|
||||
print "%s: %s" % (deviceClass['stateTypes'][i]['name'], response['params']['value'])
|
||||
|
||||
print "=== States ==="
|
||||
|
||||
|
||||
@ -509,12 +562,12 @@ def create_eventDescriptors():
|
||||
eventDescriptor['eventTypeId'] = eventType['id']
|
||||
if len(params) > 0:
|
||||
eventDescriptor['paramDescriptors'] = params
|
||||
|
||||
|
||||
eventDescriptors.append(eventDescriptor)
|
||||
|
||||
input = raw_input("Do you want to add another EventDescriptor? (y/N): ")
|
||||
if not input == "y":
|
||||
enough = True
|
||||
|
||||
print "got eventDescriptors:", eventDescriptors
|
||||
return eventDescriptors
|
||||
|
||||
@ -549,45 +602,25 @@ def create_actions():
|
||||
action['actionTypeId'] = actionType['id']
|
||||
if len(params) > 0:
|
||||
action['params'] = params
|
||||
|
||||
|
||||
actions.append(action)
|
||||
|
||||
input = raw_input("Do you want to add another action? (y/N): ")
|
||||
if not input == "y":
|
||||
enough = True
|
||||
#print "got actions:", actions
|
||||
|
||||
return actions
|
||||
|
||||
|
||||
def add_rule():
|
||||
ruleType = select_rule_type()
|
||||
if ruleType == "EventBasedRule":
|
||||
params = {}
|
||||
#params['name'] = raw_input("Please enter the name of the rule: ")
|
||||
params['eventDescriptor'] = create_eventDescriptor()
|
||||
params['actions'] = create_actions()
|
||||
print "adding rule with params:", params
|
||||
response = send_command("Rules.AddRule", params)
|
||||
print_rule_error_code(response['params']['ruleError'])
|
||||
elif ruleType == "StateBasedRule":
|
||||
params = {}
|
||||
params['eventDescriptorList'] = create_eventDescriptors()
|
||||
if len(params['eventDescriptorList']) > 1:
|
||||
params['stateEvaluator'] = select_stateOperator()
|
||||
params['actions'] = create_actions()
|
||||
print "adding rule with params:", params
|
||||
response = send_command("Rules.AddRule", params)
|
||||
print_rule_error_code(response['params']['ruleError'])
|
||||
elif ruleType == "MixedRule":
|
||||
print "not implemented yet in this script...comming soon ;)"
|
||||
else:
|
||||
print " No rule added";
|
||||
|
||||
|
||||
def select_rule_type():
|
||||
ruleTypes = ["EventBasedRule", "StateBasedRule", "MixedRule"]
|
||||
selection = get_selection("Please select a rule type: ", ruleTypes)
|
||||
return ruleTypes[selection]
|
||||
params = {}
|
||||
params['eventDescriptorList'] = create_eventDescriptors()
|
||||
if len(params['eventDescriptorList']) > 1:
|
||||
params['stateEvaluator'] = select_stateOperator()
|
||||
|
||||
params['actions'] = create_actions()
|
||||
print "adding rule with params:", params
|
||||
response = send_command("Rules.AddRule", params)
|
||||
print_rule_error_code(response['params']['ruleError'])
|
||||
|
||||
|
||||
def enable_disable_rule():
|
||||
@ -595,17 +628,18 @@ def enable_disable_rule():
|
||||
if ruleId == "":
|
||||
print "\n No rules found"
|
||||
return
|
||||
|
||||
actionTypes = ["enable", "disable"]
|
||||
selection = get_selection("What do you want to do with this rule: ", actionTypes)
|
||||
if selection == 0:
|
||||
params = {}
|
||||
params['ruleId'] = ruleId
|
||||
answere = send_command("Rules.EnableRule", params)
|
||||
response = send_command("Rules.EnableRule", params)
|
||||
print_rule_error_code(response['params']['ruleError'])
|
||||
else:
|
||||
params = {}
|
||||
params['ruleId'] = ruleId
|
||||
answere = send_command("Rules.DisableRule", params)
|
||||
response = send_command("Rules.DisableRule", params)
|
||||
print_rule_error_code(response['params']['ruleError'])
|
||||
|
||||
|
||||
@ -622,7 +656,8 @@ def list_rules():
|
||||
response = send_command("Rules.GetRules", {})
|
||||
if not response['params']['ruleIds']:
|
||||
print "\n No rules found."
|
||||
return
|
||||
return None
|
||||
|
||||
print "\nRules found:"
|
||||
for i in range(len(response['params']['ruleIds'])):
|
||||
ruleId = response['params']['ruleIds'][i]
|
||||
@ -637,13 +672,14 @@ def list_rule_detail():
|
||||
ruleId = select_rule()
|
||||
if ruleId == "":
|
||||
print "\n No rules found"
|
||||
return
|
||||
return None
|
||||
|
||||
params = {}
|
||||
params['ruleId'] = ruleId
|
||||
response = send_command("Rules.GetRuleDetails", params)
|
||||
print response
|
||||
print "\n Details for rule", ruleId, "which currently is", get_rule_status(ruleId)
|
||||
print "\n\nEvents (", get_stateEvaluator_text(response['params']['rule']['stateEvaluator']['operator']), "):"
|
||||
print "\nDetails for rule", ruleId, "which currently is", get_rule_status(ruleId)
|
||||
print "\nEvents ->", get_stateEvaluator_text(response['params']['rule']['stateEvaluator']['operator']), ":"
|
||||
for i in range(len(response['params']['rule']['eventDescriptors'])):
|
||||
eventDescriptor = response['params']['rule']['eventDescriptors'][i]
|
||||
device = get_device(eventDescriptor['deviceId'])
|
||||
@ -652,7 +688,7 @@ def list_rule_detail():
|
||||
print "%5s. -> %40s -> eventTypeId: %10s: " %(i, device['name'], eventType['name'])
|
||||
for i in range(len(paramDescriptors)):
|
||||
print "%58s %s %s" %(paramDescriptors[i]['name'], get_valueOperator_symbol(paramDescriptors[i]['operator']), paramDescriptors[i]['value'])
|
||||
print ""
|
||||
|
||||
print "\nActions:"
|
||||
for i in range(len(response['params']['rule']['actions'])):
|
||||
action = response['params']['rule']['actions'][i]
|
||||
@ -662,7 +698,7 @@ def list_rule_detail():
|
||||
print "%5s. -> %40s -> action: %s" %(i, device['name'], actionType['name'])
|
||||
for i in range(len(actionParams)):
|
||||
print "%61s: %s" %(actionParams[i]['name'], actionParams[i]['value'])
|
||||
print ""
|
||||
|
||||
|
||||
def get_valueOperator_symbol(valueOperator):
|
||||
if valueOperator == "ValueOperatorEquals":
|
||||
@ -682,9 +718,9 @@ def get_valueOperator_symbol(valueOperator):
|
||||
|
||||
def get_stateEvaluator_text(stateEvaluator):
|
||||
if stateEvaluator == "StateOperatorAnd":
|
||||
return "ALL of the events/states has to be valid."
|
||||
return "(AND) | ALL of the events/states has to be true/emited."
|
||||
elif stateEvaluator == "StateOperatorOr":
|
||||
return "ONE of this events/states has to be valid."
|
||||
return "(OR) | ONE of the events/states has to be true/emited."
|
||||
else:
|
||||
return "<unknown state evaluator>"
|
||||
|
||||
@ -697,7 +733,8 @@ def list_rules_containig_deviceId():
|
||||
response = send_command("Rules.FindRules", params)
|
||||
if not response['params']['ruleIds']:
|
||||
print "\nThere is no rule containig this device."
|
||||
return
|
||||
return None
|
||||
|
||||
print "\nFollowing rules contain this device"
|
||||
for i in range(len(response['params']['ruleIds'])):
|
||||
print "Device ", deviceId, "found in rule", response['params']['ruleIds'][i]
|
||||
@ -706,7 +743,8 @@ def list_rules_containig_deviceId():
|
||||
def select_rule():
|
||||
ruleIds = send_command("Rules.GetRules", {})['params']['ruleIds']
|
||||
if not ruleIds:
|
||||
return ""
|
||||
return None
|
||||
|
||||
selection = get_selection("Please select rule:", ruleIds)
|
||||
if selection != None:
|
||||
return ruleIds[selection]
|
||||
@ -715,13 +753,17 @@ def select_rule():
|
||||
def remove_rule():
|
||||
ruleId = select_rule()
|
||||
if ruleId == "":
|
||||
print "\nNo rules found"
|
||||
return
|
||||
print "\nNo rule found"
|
||||
return None
|
||||
|
||||
params = {}
|
||||
params['ruleId'] = ruleId
|
||||
response = send_command("Rules.RemoveRule", params)
|
||||
print "removeRule response", response
|
||||
|
||||
|
||||
|
||||
############################################################################################
|
||||
import sys
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
@ -734,11 +776,10 @@ packet = json.loads(packet)
|
||||
print "connected to", packet["server"], "\nserver version:", packet["version"], "\nprotocol version:", packet["protocol version"], "\n"
|
||||
|
||||
while True:
|
||||
selection = get_selection("What do you want to do?", methods.keys())
|
||||
if selection != None:
|
||||
selectionKey = methods.keys()
|
||||
methodName = methods[methods.keys()[selection]]
|
||||
methodToCall = globals()[methods[methods.keys()[selection]]]
|
||||
methodToCall()
|
||||
method = get_menu_selection()
|
||||
if method != None:
|
||||
#print "call method: ", method
|
||||
methodCall = globals()[method]
|
||||
methodCall()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user