本文整理汇总了Python中models.Server.get_attr方法的典型用法代码示例。如果您正苦于以下问题:Python Server.get_attr方法的具体用法?Python Server.get_attr怎么用?Python Server.get_attr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Server
的用法示例。
在下文中一共展示了Server.get_attr方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: profile
# 需要导入模块: from models import Server [as 别名]
# 或者: from models.Server import get_attr [as 别名]
def profile(request):
host = get_server_host()
port = request.get_port()
user = request.user
if not user.is_superuser:
print "ERROR"
return HttpResponseRedirect('/account/logout/')
page_num = request.GET.get('page_num', 1)
page_num = int(page_num)
page_size = 6
offset = page_size * (page_num - 1)
server = Server.objects.filter(user_id=user.id).first()
host = host + ':' + port
server.host = host
if not server.work:
server_init()
server.work = 1
server.save()
count = Room.objects.filter(host=server.host).count()
page_count = (count + page_size - 1)/ page_size
if page_count < 1:
page_count = 1
if offset > count:
offset = count
if offset + page_size > count:
page_size = count - offset
rooms = Room.objects.filter(host=server.host)[offset:(offset+page_size)]
data = []
for room in rooms:
is_service = u'服务中'
is_link = u'已连接'
if not room.service:
is_service = u'未服务'
if not room.link:
is_link = u'连接已断开'
room_mode = MODE_DICT[MODE[Server.get_attr('mode')]]
room_speed = SPEED_DICT[SPEED[room.speed]]
data.append({
'id': room.id,
'is_link': is_link,
'numbers':room.numbers,
'ip_address': room.ip_address,
'service': is_service,
'mode': room_mode,
'speed': room_speed,
'power': CostPerDay.get_power(room.id, back=Server.get_report_days()),
'room_temperature': room.room_temperature,
'setting_temperature': room.setting_temperature,
'total_cost': CostPerDay.get_cost(room.id, back=Server.get_report_days()),
'rest_cost': room.total_cost,
})
return render(request, 'center.html', {'list': data, 'page_num':page_num, 'page_count':
page_count, 'user':user, 'host': host, 'report': Server.get_report_name(), 'mode':
MODE_DICT[MODE[server.mode]]})
示例2: update_room_info
# 需要导入模块: from models import Server [as 别名]
# 或者: from models.Server import get_attr [as 别名]
def update_room_info():
mode = Server.get_attr('mode')
query = Room.objects.select_for_update().filter(host=Server.get_host(), link=1)
for room in query.all():
resp = query_room_temperature(room.ip_address, room.numbers)
if not room.link:
continue
if resp['code'] == 0:
room.setting_temperature = resp['setting_temperature']
room.room_temperature = resp['room_temperature']
else:
room.link = 0
room.service = 0
print "break link!"
speed = room.speed
room.save()
if not room.service:
continue
if (room.setting_temperature >= room.room_temperature + 0.1 and mode == 0) or (room.setting_temperature + 0.1 <= room.room_temperature and mode == 2):
room.service = 0
if room.service == 0:
old_cost = RoomRequest.objects.filter(room_id=room.id).order_by("-id").first()
old_cost.end_time = datetime.now()
old_cost.end_temperature = room.room_temperature
old_cost.save()
resp = post_to_client(room.ip_address, {'type':'stop', 'source': 'host'})
if room.service == 1:
update_cost(room.id, POWER_PER_MIN[room.speed], room.price)
room.power += POWER_PER_MIN[room.speed]
room.total_cost = room.power * room.price
room.save()
service_count = query.filter(service=1).count()
if service_count < 3:
rooms = query.filter(service=0, speed__gt=0).all()
for room in rooms:
speed = room.speed
if (room.setting_temperature >= room.room_temperature + 0.1 and mode == 0) or (room.setting_temperature + 0.1 <= room.room_temperature and mode == 2):
continue
resp = post_to_client(room.ip_address, {'type':'send', 'source':'host'})
if resp['code'] == 0:
room.service = 1
room_request = RoomRequest.objects.create(room_id=room.id, start_time=datetime.now(), power = 0, cost = 0, start_temperature=room.room_temperature, speed=speed)
print "start service"
room.start_service_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
room.save()
break
return JsonResponse({'code': 0})
示例3: communication
# 需要导入模块: from models import Server [as 别名]
# 或者: from models.Server import get_attr [as 别名]
def communication(request):
import pdb
# pdb.set_trace()
print "start=====com"
if request.method != 'POST':
resp = JsonResponse({'type': 'login', 'source': 'host', 'ack_nak': 'NAK'})
resp['Access-Control-Allow-Origin'] = '*'
return resp
source = request.POST.get('source', '')
op = request.POST.get('type', 'login')
room = Room.objects.select_for_update().filter(numbers=source).first()
print "operator======, ", op
if op == 'login':
if not room:
user = User.objects.create(username=get_rand_name(), password='xxxx')
room = Room.objects.create(user_id=user.id, numbers=source)
ip_port = request.POST.get('ip_port', None)
print "ip___port:_______ ", ip_port
room.ip_address = ip_port
room.host = Server.get_host()
room.link = 1
room.save()
print "========================="
resp = JsonResponse({'type':'login', 'source':'host', 'ack_nak': 'ACK'})
resp['Access-Control-Allow-Origin'] = '*'
return resp
if not room:
resp = JsonResponse({'type': op, 'source': 'host', 'ack_nak': 'NAK'})
resp['Access-Control-Allow-Origin'] = '*'
return resp
if op == 'logout':
room.link = 0
room.save()
resp = JsonResponse({'type':'logout', 'source':'host', 'ack_nak': 'ACK'})
resp['Access-Control-Allow-Origin'] = '*'
return resp
elif op == 'require':
speed = request.POST.get('speed', 'low')
print "start stop"
resp = post_to_client(room.ip_address, {'type':'stop', 'source': 'host'})
stop_cost(room.id, room.room_temperature)
room.speed = RESPEED[speed]
room.service = 0
room.save()
resp = JsonResponse({'type':'require', 'source':'host', 'ack_nak': 'ACK'})
resp['Access-Control-Allow-Origin'] = '*'
return resp
elif op == 'query_cost':
resp = JsonResponse({'type': 'query_cost', 'source': 'host', 'ack_nak': 'ACK', 'power_consumption': room.power,
'price': room.price, 'total_cost': room.total_cost})
resp['Access-Control-Allow-Origin'] = '*'
return resp
elif op == 'query_mode':
resp = JsonResponse({'type': 'query_mode', 'source': 'host', 'ack_nak': 'ACK', 'mode': MODE[Server.get_attr('mode')]})
resp['Access-Control-Allow-Origin'] = '*'
return resp