当前位置: 首页>>代码示例>>Python>>正文


Python Server.get_host方法代码示例

本文整理汇总了Python中models.Server.get_host方法的典型用法代码示例。如果您正苦于以下问题:Python Server.get_host方法的具体用法?Python Server.get_host怎么用?Python Server.get_host使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Server的用法示例。


在下文中一共展示了Server.get_host方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: communication

# 需要导入模块: from models import Server [as 别名]
# 或者: from models.Server import get_host [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
开发者ID:2013211450,项目名称:air_conditioner,代码行数:58,代码来源:views.py

示例2: update_room_info

# 需要导入模块: from models import Server [as 别名]
# 或者: from models.Server import get_host [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})
开发者ID:2013211450,项目名称:air_conditioner,代码行数:50,代码来源:views.py


注:本文中的models.Server.get_host方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。