Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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