Пример #1
0
def checkOpt(dev_opt):
    dev_opt_tmp = dev_opt.lower()
    opt_name_list_tmp = map(lambda x: x.lower(), opt_name_list)
    if dev_opt_tmp not in opt_name_list_tmp:
        outputInfo("Error: Invalid operation[%s]" % dev_opt, 'error')
        return -1
    return opt_name_list_tmp.index(dev_opt_tmp)
Пример #2
0
def checkOpt(dev_opt):
	dev_opt_tmp = dev_opt.lower()
	opt_name_list_tmp = map(lambda x: x.lower(), opt_name_list)
	if dev_opt_tmp not in opt_name_list_tmp:
		outputInfo("Error: Invalid operation[%s]" % dev_opt, 'error')
		return -1
	return opt_name_list_tmp.index(dev_opt_tmp)
Пример #3
0
def checkDeviceid(dev_id):
	if re.match('^EP[\d]{6}$', dev_id):
		return dev_id
	if re.match('^[\d]{6}$', dev_id):
		dev_id = 'EP' + dev_id
		return dev_id
	else:
		outputInfo("Error: device's id[%s] invalid" % dev_id, 'error')
		return None
Пример #4
0
def checkDeviceid(dev_id):
    if re.match('^EP[\d]{6}$', dev_id):
        return dev_id
    if re.match('^[\d]{6}$', dev_id):
        dev_id = 'EP' + dev_id
        return dev_id
    else:
        outputInfo("Error: device's id[%s] invalid" % dev_id, 'error')
        return None
Пример #5
0
def optBase(addr,
            dev_type,
            dev_id,
            cmd,
            value=None,
            channel=None,
            status=None):
    if not addr[0]:
        closeOutput()
        dev_info = scanIP(dev_id)
        openOutput()
        if not dev_info or not dev_info.has_key(dev_type):
            outputInfo("Error: cannot found ip of %s" % dev_type, 'error')
            return None
        addr = (dev_info[dev_type], addr[1])

    socket_udp = getUDPSocket(host, port_udp)

    if status:
        send = cmdMix = '{"cmd":"%s","status":"%s","deviceID":"%s"}' % (
            cmd, status, dev_id)
    elif channel:
        send = cmdMix = '{"cmd":"%s", "channel":"%s","value":"%s","deviceID":"%s"}' % (
            cmd, channel, value, dev_id)
    elif value:
        send = cmdMix = '{"cmd":"%s","value":"%s","deviceID":"%s"}' % (
            cmd, value, dev_id)
    else:
        send = cmdMix = '{"cmd":"%s","deviceID":"%s"}' % (cmd, dev_id)
    outputInfo("Send [%s] to [%s]" % (send, str(addr)))

    try:
        socket_udp.sendto(send, addr)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        traceback.print_exc()

    try:
        socket_udp.settimeout(time_out)
        data, addr = socket_udp.recvfrom(buf_size)
        outputInfo("Receive [%s] from [%s]" % (data, addr))
    except socket.timeout:
        outputInfo('Error: Request timed out!', 'error')
        socket_udp.close()
        return None

    jsondata = json.loads(data)
    cmd = "res" + cmd[3:]
    if jsondata['cmd'] != cmd \
     or jsondata['deviceType'] != dev_type:
        outputInfo("Error: cmd or deviceType don't match", 'error')
        socket_udp.close()
        return None
    socket_udp.close()
    return jsondata
Пример #6
0
def optBase(addr, dev_type, dev_id, cmd, value=None, channel=None, status=None):
	if not addr[0]:
		closeOutput()
		dev_info = scanIP(dev_id)
		openOutput()
		if not dev_info or not dev_info.has_key(dev_type):
			outputInfo("Error: cannot found ip of %s" % dev_type, 'error')
			return None
		addr = (dev_info[dev_type], addr[1])

	socket_udp = getUDPSocket(host, port_udp)

	if status:
		send = cmdMix = '{"cmd":"%s","status":"%s","deviceID":"%s"}'%(cmd, status, dev_id)
	elif channel:
		send = cmdMix = '{"cmd":"%s", "channel":"%s","value":"%s","deviceID":"%s"}'%(cmd, channel, value, dev_id)
	elif value:
		send = cmdMix = '{"cmd":"%s","value":"%s","deviceID":"%s"}'%(cmd, value, dev_id)
	else:
		send = cmdMix = '{"cmd":"%s","deviceID":"%s"}'%(cmd, dev_id)
	outputInfo("Send [%s] to [%s]" % (send, str(addr)))

	try:
		socket_udp.sendto(send, addr)
	except (KeyboardInterrupt, SystemExit):
		raise
	except:
		traceback.print_exc()

	try:
		socket_udp.settimeout(time_out)
		data, addr = socket_udp.recvfrom(buf_size)
		outputInfo("Receive [%s] from [%s]" % (data, addr))
	except socket.timeout:
		outputInfo('Error: Request timed out!', 'error')
		socket_udp.close()
		return None

	jsondata = json.loads(data)
	cmd = "res" + cmd[3:]
	if jsondata['cmd'] != cmd \
		or jsondata['deviceType'] != dev_type:
		outputInfo("Error: cmd or deviceType don't match", 'error')
		socket_udp.close()
		return None
	socket_udp.close()
	return jsondata
Пример #7
0
def reboot(dev_id):
	closeOutput()
	dev_info = scanIP(dev_id)
	openOutput()

	ret = True
	res = []
	for dev_type in device_types:
		if not dev_info or not dev_info.has_key(dev_type):
			ret = False
			res.append((False, "not found ip"))
			continue
		ret_info = mytelnet.reboot(dev_info[dev_type])
		if ret_info == mytelnet.t_finish:
			res.append((True, 'ongoing...'))
		else:
			ret = False
			res.append((False, 'failure'))

	outputInfo('Reboot:', 'info')
	for item, dev_type in zip(res, device_types):
		string = '\t%-9s: ' % dev_type
		string += item[1]
		if item[0]:
			outputInfo(string, 'info')
		else:
			outputInfo(string, 'info_error')
	return ret
Пример #8
0
def printHelp():
	import inspect
	outputInfo("usage: %s device_id options args" % sys.argv[0], 'info_error')
	outputInfo("options:", 'info')
	for opt in opt_list:
		str_opt = "\t%s" % opt.__name__
		outputInfo(str_opt, 'info')
		args = inspect.getargspec(opt)[0]
		if len(args) == 1:
			continue
		defaults = inspect.getargspec(opt)[-1]
		len_ignore = 0
		str_args = "\t\targs: "
		if defaults:
			len_ignore = len(defaults)
		if args and len(args) > 1:
			if args[0] == 'dev_id':
				args.pop(0)
			for i in range(len(args)-len_ignore):
				str_args += '%s ' % args[i]
			for i in range(len(args)-len_ignore, len(args)):
				str_args += '[%s] ' % args[i]
		print str_args
	print "example:"
	print "\t%s EP123456 setReverbSW on" % sys.argv[0]
Пример #9
0
def printHelp():
    import inspect
    outputInfo("usage: %s device_id options args" % sys.argv[0], 'info_error')
    outputInfo("options:", 'info')
    for opt in opt_list:
        str_opt = "\t%s" % opt.__name__
        outputInfo(str_opt, 'info')
        args = inspect.getargspec(opt)[0]
        if len(args) == 1:
            continue
        defaults = inspect.getargspec(opt)[-1]
        len_ignore = 0
        str_args = "\t\targs: "
        if defaults:
            len_ignore = len(defaults)
        if args and len(args) > 1:
            if args[0] == 'dev_id':
                args.pop(0)
            for i in range(len(args) - len_ignore):
                str_args += '%s ' % args[i]
            for i in range(len(args) - len_ignore, len(args)):
                str_args += '[%s] ' % args[i]
        print str_args
    print "example:"
    print "\t%s EP123456 setReverbSW on" % sys.argv[0]
Пример #10
0
def reboot(dev_id):
    closeOutput()
    dev_info = scanIP(dev_id)
    openOutput()

    ret = True
    res = []
    for dev_type in device_types:
        if not dev_info or not dev_info.has_key(dev_type):
            ret = False
            res.append((False, "not found ip"))
            continue
        ret_info = mytelnet.reboot(dev_info[dev_type])
        if ret_info == mytelnet.t_finish:
            res.append((True, 'ongoing...'))
        else:
            ret = False
            res.append((False, 'failure'))

    outputInfo('Reboot:', 'info')
    for item, dev_type in zip(res, device_types):
        string = '\t%-9s: ' % dev_type
        string += item[1]
        if item[0]:
            outputInfo(string, 'info')
        else:
            outputInfo(string, 'info_error')
    return ret
Пример #11
0
def getStatus(dev_id):
    cmd = "reqStatus"
    ret = True

    closeOutput()
    dev_info = scanIP(dev_id)
    openOutput()

    res = []
    for dev_type in device_types:
        outputInfo("*** Get Status: %s" % dev_type)
        if not dev_info or not dev_info.has_key(dev_type):
            ret = False
            res.append((False, "not found ip"))
            continue
        data = optBase((dev_info[dev_type], port_udp_to), dev_type, dev_id,
                       cmd)
        if not data or not data.has_key('status') or data['status'] != 'nomal':
            ret = False
            res.append((False, data))
            continue
        res.append((True, data))

    outputInfo('Status:', 'info')
    for dev_type, status in zip(device_types, res):
        string = "\t%-9s: " % dev_type
        if status[0]:
            string += status[1]['status']
            outputInfo(string, 'info')
        elif isinstance(status[1], str):
            string += status[1]
            outputInfo(string, 'info_error')
        else:
            string += status[1]['status']
            outputInfo(string, 'info_error')

    return ret
Пример #12
0
def getStatus(dev_id):
	cmd = "reqStatus"
	ret = True

	closeOutput()
	dev_info = scanIP(dev_id)
	openOutput()

	res = []
	for dev_type in device_types:
		outputInfo("*** Get Status: %s" % dev_type)
		if not dev_info or not dev_info.has_key(dev_type):
			ret = False
			res.append((False, "not found ip"))
			continue
		data = optBase((dev_info[dev_type], port_udp_to), dev_type, dev_id, cmd)
		if not data or not data.has_key('status') or data['status'] != 'nomal':
			ret = False
			res.append((False, data))
			continue
		res.append((True, data))

	outputInfo('Status:', 'info')
	for dev_type, status in zip(device_types, res):
		string = "\t%-9s: " % dev_type
		if status[0]:
			string += status[1]['status']
			outputInfo(string, 'info')
		elif isinstance(status[1], str):
			string += status[1]
			outputInfo(string, 'info_error')
		else:
			string += status[1]['status']
			outputInfo(string, 'info_error')

	return ret
Пример #13
0
def setGain(dev_id, channel, value, judgement_ip=None):
	cmd = "reqGain"
	dev_type = 'judgement'
	outputInfo("*** Set Gain: {channel: %s,value: %s}" % (channel, value))

	data = optBase((judgement_ip, port_udp_to), dev_type, dev_id, cmd, value=value, channel=channel)
	if not data or not data.has_key('value') or not data.has_key('channel'):
		return False

	if data['value'] != value:
		outputInfo("Error: value don't match", 'error')
		return False
	if data['channel'] != channel:
		outputInfo("Error: channel don't match", 'error')
		return False

	outputInfo("Set the %s channel gain to %s" % (channel, value), 'info')

	return True
Пример #14
0
def getPowerMeter(dev_id, recorder_ip=None):
	cmd = "reqPowerMeter"
	dev_type = 'recorder'
	outputInfo("*** Get Power Meter")

	data = optBase((recorder_ip, port_udp_to), dev_type, dev_id, cmd)
	if not data:
		return False
	if not data.has_key("value_left") or not data.has_key("value_right"):
		outputInfo("Error: do not received the value of value_left or value_right", 'error')
		return False

	string = "Get PowerMeter: {value_left: %s, value_right: %s}" % (data['value_left'], data['value_right'])
	outputInfo(string, 'info')
	return True
Пример #15
0
def setMixture(dev_id, value, reverb_ip=None):
	cmd = "reqMix"
	dev_type = 'reverb'
	outputInfo("*** Set Mixture: {value: %s}" % value)

	data = optBase((reverb_ip, port_udp_to), dev_type, dev_id, cmd, value=value)
	if not data or not data.has_key('value'):
		return False

	if data['value'] != value:
		outputInfo("Error: value don't match", 'error')
		return False

	outputInfo("Set the value of mixture to %s" % value, 'info')

	return True
Пример #16
0
def setReverbSW(dev_id, status, reverb_ip=None):
	cmd = "reqReverbSW"
	dev_type = 'reverb'
	outputInfo("*** Set ReverbSW: {status: %s}" % status)

	data = optBase((reverb_ip, port_udp_to), dev_type, dev_id, cmd, status=status)
	if not data or not data.has_key('status'):
		return False

	if data['status'] != status:
		outputInfo("Error: status don't match", 'error')
		return False

	outputInfo("Turn %s the reverb" % status, 'info')

	return True
Пример #17
0
def do_telnet(host, username, password, finish, command):
	# 连接Telnet服务器
	try:
		tn = telnetlib.Telnet(host, timeout=t_timeout)
	except:
		now = time.strftime("%H:%M:%S", time.localtime())
		outputInfo('[%s] connet : Could not connect to host' % now)
		exit()
	tn.set_debuglevel(0)

	# 输入登录用户名
	log = tn.read_until('login: '******'[%s] login: user' % (now))
	tn.write(username + '\n')

	# 输入登录密码
	tn.read_until('Password: '******'[%s] login: passwd' % (now))
	tn.write(password + '\n')

	# 登录完毕后执行命令
	tn.read_until(finish)

	tn.write('%s\n' % command)
	time.sleep(0.5)
	log = tn.read_very_eager()  #.replace('\n', '    ')
	# log = tn.read_some()
	now = time.strftime("%H:%M:%S", time.localtime())
	outputInfo('[%s] cmd: %s' % (now, repr(log)))

	#执行完毕后,终止Telnet连接(或输入exit退出)
	tn.close() # tn.write('exit\n')

	return log.split('\n')[1]
Пример #18
0
def setGain(dev_id, channel, value, judgement_ip=None):
    cmd = "reqGain"
    dev_type = 'judgement'
    outputInfo("*** Set Gain: {channel: %s,value: %s}" % (channel, value))

    data = optBase((judgement_ip, port_udp_to),
                   dev_type,
                   dev_id,
                   cmd,
                   value=value,
                   channel=channel)
    if not data or not data.has_key('value') or not data.has_key('channel'):
        return False

    if data['value'] != value:
        outputInfo("Error: value don't match", 'error')
        return False
    if data['channel'] != channel:
        outputInfo("Error: channel don't match", 'error')
        return False

    outputInfo("Set the %s channel gain to %s" % (channel, value), 'info')

    return True
Пример #19
0
def getPowerMeter(dev_id, recorder_ip=None):
    cmd = "reqPowerMeter"
    dev_type = 'recorder'
    outputInfo("*** Get Power Meter")

    data = optBase((recorder_ip, port_udp_to), dev_type, dev_id, cmd)
    if not data:
        return False
    if not data.has_key("value_left") or not data.has_key("value_right"):
        outputInfo(
            "Error: do not received the value of value_left or value_right",
            'error')
        return False

    string = "Get PowerMeter: {value_left: %s, value_right: %s}" % (
        data['value_left'], data['value_right'])
    outputInfo(string, 'info')
    return True
Пример #20
0
def setReverbSW(dev_id, status, reverb_ip=None):
    cmd = "reqReverbSW"
    dev_type = 'reverb'
    outputInfo("*** Set ReverbSW: {status: %s}" % status)

    data = optBase((reverb_ip, port_udp_to),
                   dev_type,
                   dev_id,
                   cmd,
                   status=status)
    if not data or not data.has_key('status'):
        return False

    if data['status'] != status:
        outputInfo("Error: status don't match", 'error')
        return False

    outputInfo("Turn %s the reverb" % status, 'info')

    return True
Пример #21
0
def setMixture(dev_id, value, reverb_ip=None):
    cmd = "reqMix"
    dev_type = 'reverb'
    outputInfo("*** Set Mixture: {value: %s}" % value)

    data = optBase((reverb_ip, port_udp_to),
                   dev_type,
                   dev_id,
                   cmd,
                   value=value)
    if not data or not data.has_key('value'):
        return False

    if data['value'] != value:
        outputInfo("Error: value don't match", 'error')
        return False

    outputInfo("Set the value of mixture to %s" % value, 'info')

    return True
Пример #22
0
def scanIP(dev_id):
	outputInfo("*** Scan IP")
	socket_broadcast = getBroadcastSocket(host, port_brocast)
	dev_info = getDeviceInfo(socket_broadcast, dev_id, allow_repeat=True)
	info = copy.deepcopy(dev_info)

	status = True

	string = "%s: " % dev_id
	if not info:
		string += 'None'
		status = False
	else:
		string += '{'
		# keys = info.keys()
		for dev_type in device_types:
			string += '%s: ' % dev_type
			if not info.has_key(dev_type):
				string += '%s, ' % 'None'
				status = False
			else:
				dev_ip = info.pop(dev_type)
				string += '%s, ' % dev_ip
		for eachKey in info:
			string += '%s: %s, ' % (eachKey, info[eachKey])
		string = string[:-2]
		string += '}'
	# outputInfo('=' * len(string))
	if status:
		outputInfo(string, 'info')
	else:
		outputInfo(string, 'info_error')
	# outputInfo('=' * len(string))

	socket_broadcast.close()

	return dev_info
Пример #23
0
def scanIP(dev_id):
    outputInfo("*** Scan IP")
    socket_broadcast = getBroadcastSocket(host, port_brocast)
    dev_info = getDeviceInfo(socket_broadcast, dev_id, allow_repeat=True)
    info = copy.deepcopy(dev_info)

    status = True

    string = "%s: " % dev_id
    if not info:
        string += 'None'
        status = False
    else:
        string += '{'
        # keys = info.keys()
        for dev_type in device_types:
            string += '%s: ' % dev_type
            if not info.has_key(dev_type):
                string += '%s, ' % 'None'
                status = False
            else:
                dev_ip = info.pop(dev_type)
                string += '%s, ' % dev_ip
        for eachKey in info:
            string += '%s: %s, ' % (eachKey, info[eachKey])
        string = string[:-2]
        string += '}'
    # outputInfo('=' * len(string))
    if status:
        outputInfo(string, 'info')
    else:
        outputInfo(string, 'info_error')
    # outputInfo('=' * len(string))

    socket_broadcast.close()

    return dev_info
Пример #24
0
def getVersion(dev_id):
    closeOutput()
    dev_info = scanIP(dev_id)
    openOutput()

    ret = True
    res = []
    for dev_type in device_types:
        if not dev_info or not dev_info.has_key(dev_type):
            ret = False
            res.append((False, "not found ip"))
            continue
        version = mytelnet.getVersion(dev_type, dev_info[dev_type])
        res.append((True, version))

    outputInfo('Version:', 'info')
    for item, dev_type in zip(res, device_types):
        string = '\t%-9s: ' % dev_type
        string += item[1]
        if item[0]:
            outputInfo(string, 'info')
        else:
            outputInfo(string, 'info_error')
    return ret
Пример #25
0
def getVersion(dev_id):
	closeOutput()
	dev_info = scanIP(dev_id)
	openOutput()

	ret = True
	res = []
	for dev_type in device_types:
		if not dev_info or not dev_info.has_key(dev_type):
			ret = False
			res.append((False, "not found ip"))
			continue
		version = mytelnet.getVersion(dev_type, dev_info[dev_type])
		res.append((True, version))

	outputInfo('Version:', 'info')
	for item, dev_type in zip(res, device_types):
		string = '\t%-9s: ' % dev_type
		string += item[1]
		if item[0]:
			outputInfo(string, 'info')
		else:
			outputInfo(string, 'info_error')
	return ret
Пример #26
0
def getDeviceInfo(socket_broadcast, dev_id, allow_repeat=False):
	try:
		cmdSendScan = '{"cmd":"reqinfo","deviceID":"%s"}' % (dev_id)
		outputInfo("Broadcast: [%s] port[%s]" % (cmdSendScan, port_brocast_to))
		socket_broadcast.sendto(cmdSendScan,('<broadcast>', port_brocast_to))
	except (KeyboardInterrupt, SystemExit):
		raise
	except:
		traceback.print_exc()

	devInfo = {}
	for i in range(len(device_types)):
		outputInfo("getDeviceInfo: %d" % (i+1))
		data = ''
		try:
			socket_broadcast.settimeout(time_out)
			data, addr = socket_broadcast.recvfrom(buf_size)
			outputInfo("Receive [%s] from [%s]" % (repr(data), addr))
		except socket.timeout:
			outputInfo('Error: Request timed out!', 'error')
			return devInfo
		jsondata = json.loads(data)
		if jsondata['cmd'] == "resinfo" :
			devType = jsondata['deviceType']
			outputInfo("devType: %s" % devType)
			if devInfo.has_key(devType):
				outputInfo( "Error: Have the same device type!", 'error')
				if not allow_repeat:
					return None
				devInfo[devType+'repeat'] = jsondata['ip']
			else :
				devInfo[devType] = jsondata['ip']
	outputInfo("Recieve IP address: %s" % json.dumps(devInfo))
	return devInfo
Пример #27
0
def getDeviceInfo(socket_broadcast, dev_id, allow_repeat=False):
    try:
        cmdSendScan = '{"cmd":"reqinfo","deviceID":"%s"}' % (dev_id)
        outputInfo("Broadcast: [%s] port[%s]" % (cmdSendScan, port_brocast_to))
        socket_broadcast.sendto(cmdSendScan, ('<broadcast>', port_brocast_to))
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        traceback.print_exc()

    devInfo = {}
    for i in range(len(device_types)):
        outputInfo("getDeviceInfo: %d" % (i + 1))
        data = ''
        try:
            socket_broadcast.settimeout(time_out)
            data, addr = socket_broadcast.recvfrom(buf_size)
            outputInfo("Receive [%s] from [%s]" % (repr(data), addr))
        except socket.timeout:
            outputInfo('Error: Request timed out!', 'error')
            return devInfo
        jsondata = json.loads(data)
        if jsondata['cmd'] == "resinfo":
            devType = jsondata['deviceType']
            outputInfo("devType: %s" % devType)
            if devInfo.has_key(devType):
                outputInfo("Error: Have the same device type!", 'error')
                if not allow_repeat:
                    return None
                devInfo[devType + 'repeat'] = jsondata['ip']
            else:
                devInfo[devType] = jsondata['ip']
    outputInfo("Recieve IP address: %s" % json.dumps(devInfo))
    return devInfo