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