Exemple #1
0
def get_compressed(request):
    if request.method == 'POST':
        json_data = json.loads(request.body)
    else:
        return HttpResponseBadRequest('Bad Request')

    if 'mac' in json_data:
        _mac = json_data['mac']
    else:
        return HttpResponseMessage(1, 'no mac in request')

    if 'start_time' in json_data:
        _starttime = json_data['start_time']
        try:
            start = datetime.strptime(_starttime, dateFormat)
        except:
            return HttpResponseMessage(4, 'start time not correct')
    else:
        return HttpResponseMessage(2, 'no start time in request')

    if 'end_time' in json_data:
        _endtime = json_data['end_time']
        try:
            end = datetime.strptime(_endtime, dateFormat)
        except:
            return HttpResponseMessage(5, 'end time not correct')
    else:
        return HttpResponseMessage(3, 'no end time in request')

    if 'compressed' in json_data:
        _compressed = True
    else:
        _compressed = False

    _compressed = True
    #IotTemp.objects.get(time.c)
    #temperatures = IotTemp.objects.filter(mac = _mac, time=end, time__gte=start)
    time1 = datetime.utcnow()
    temperatures = IotTemp.objects.filter(mac=_mac,
                                          time__lte=end,
                                          time__gte=start).order_by('seq_no')
    time2 = datetime.utcnow()
    if len(temperatures) > 0:
        print "find temperature count " + str(len(temperatures))
        temp_dict = []
        for t in temperatures:
            temp_dict.append(t_to_dict(t, _compressed))
        time3 = datetime.utcnow()
        logger.debug("time for getting temperature:" + str(time3 - time1))
        #str1 = zlib.compress(json.dumps(dict1), zlib.Z_BEST_COMPRESSION)
        return HttpResponseMessageWithCompressedData(0, 'success',
                                                     'temperatures', temp_dict)
        #return HttpResponse(str1, content_type = 'application/json')
    else:
        #result = {"start_str": _starttime, "start": datetime.strftime(start, dateFormat), "end_str": _endtime, "end": datetime.strftime(end, dateFormat)}
        #return HttpResponseMessageWithData(3, 'no temperature data', 'time', json.dumps(result))
        return HttpResponseMessage(6, 'no temperature data')
Exemple #2
0
def get(request):
    if request.method == 'POST':
        json_data = json.loads(request.body)
    else:
        return HttpResponseBadRequest('Bad Request')

    if 'mac' in json_data:
        _mac = json_data['mac']
    else:
        return HttpResponseMessage(1, 'no mac in request')

    if 'start_time' in json_data:
        _starttime = json_data['start_time']
        try:
            start = datetime.strptime(_starttime, dateFormat)
        except:
            return HttpResponseMessage(4, 'start time not correct')
    else:
        return HttpResponseMessage(2, 'no start time in request')

    if 'end_time' in json_data:
        _endtime = json_data['end_time']
        try:
            end = datetime.strptime(_endtime, dateFormat)
        except:
            return HttpResponseMessage(5, 'end time not correct')
    else:
        return HttpResponseMessage(3, 'no end time in request')

    time1 = datetime.utcnow()
    temperatures = IotTemp.objects.filter(mac=_mac,
                                          time__lte=end,
                                          time__gte=start).order_by('seq_no')
    time2 = datetime.utcnow()
    if len(temperatures) > 0:
        print "find temperature count " + str(len(temperatures))
        temp_dict = []
        for t in temperatures:
            temp_dict.append(t_to_dict(t, False))
        time3 = datetime.utcnow()
        logger.debug("time for getting temperature:" + str(time3 - time1))
        print "temp_dict size:" + str(len(json.dumps(temp_dict)))
        return HttpResponseMessageWithData(0, 'success', 'temperatures',
                                           temp_dict)
    else:
        #result = {"start_str": _starttime, "start": datetime.strftime(start, dateFormat), "end_str": _endtime, "end": datetime.strftime(end, dateFormat)}
        #return HttpResponseMessageWithData(3, 'no temperature data', 'time', json.dumps(result))
        return HttpResponseMessage(6, 'no temperature data')
Exemple #3
0
def gethighlow(_mac, _start, _end):
    _high = -200
    _low = 200
    temperatures = IotTemp.objects.filter(mac=_mac,
                                          time__lte=_end,
                                          time__gte=_start)
    if len(temperatures) > 0:
        logger.debug("find temperatures:" + str(len(temperatures)))
        for t in temperatures:
            if _high < t.temperature:
                _high = t.temperature
            if _low > t.temperature:
                _low = t.temperature
    else:
        logger.debug("no temperature found")
        _low = NOT_VALID_TEMPERATURE
    logger.debug("highest=" + str(_high))
    logger.debug("lowest=" + str(_low))
    data = {"highest": _high, "lowest": _low}
    return data
Exemple #4
0
def register(request):
    if request.method == 'POST':
        json_data = json.loads(request.body)
    else:
        return HttpResponseBadRequest('Bad Request')

    # print "%s" %(json_data)

    if 'mac' in json_data:
        _mac = json_data['mac']
        try:
            _device = IotDevice.objects.get(mac_addr=_mac)
            # filter
            # _device = IotDevice.objects.filter(mac_addr=_mac).first()
        except:
            _device = None
            # print "the device is exists----> "
            # return HttpResponseMessage(8, 'mac is exists')
    else:
        return HttpResponseMessage(1, 'no mac in request')

    if 'number' in json_data:
        _number = json_data['number']
        try:
            _express = IotExpress.objects.get(number=_number)
            # _express = IotExpress.objects.filter(number=_number).first()
        except:
            _express = None
            # print "the number is exists----> "
            # return HttpResponseMessage(9, 'express is exists')
    else:
        return HttpResponseMessage(2, 'no express info in request')

    # start_time is set in register, can not be updated later
    if 'start_time' in json_data:
        _starttime = json_data['start_time']
        # return HttpResponseMessage(3, _starttime)
        try:
            start = datetime.strptime(_starttime, dateFormat)
        except:
            return HttpResponseMessage(4, 'start time not correct')
    else:
        return HttpResponseMessage(3, 'no start time in request')
    # express = IotExpress.objects.get(number=_number)

    if 'end_time' in json_data:
        _endtime = json_data['end_time']
        try:
            end = datetime.strptime(_endtime, dateFormat)
        except:
            return HttpResponseMessage(5, 'end time not correct')
    else:
        end = None

    if 'high_temp' in json_data:
        _hightemp = json_data['high_temp']
    else:
        return HttpResponseMessage(6, 'no high temperature set')

    if 'low_temp' in json_data:
        _lowtemp = json_data['low_temp']
    else:
        return HttpResponseMessage(7, "no low temperature set")

    if 'coordinate' in json_data:
        _coordinate = json_data['coordinate']
    else:
        _coordinate = STATUS_INIT

    if 'status' in json_data:
        _status = json_data['status']
    else:
        _status = STATUS_INIT

    _type = 2
    if _status == STATUS_DONE:
        data = gethighlow(_mac, start, end)
        _highest = data['highest']
        _lowest = data['lowest']
        # EVENT_TYPE_CHOICES
        _type = 3
    else:
        _highest = NOT_VALID_TEMPERATURE
        _lowest = NOT_VALID_TEMPERATURE
        _type = 2

    # get_or_create

    _get_order = IotOrder.objects.filter(
        mac=_mac,
        number=_number,
    )

    # check order in db
    if _get_order.count() is not 0:
        print "already created order info about %s , we will update order" % (
            _mac)
        _updatedb = IotOrder.objects.filter(
            mac=_mac,
            number=_number,
        ).update(
            end_time=end,
            high_temp=_hightemp,
            low_temp=_lowtemp,
            highest_temp=_highest,
            lowest_temp=_lowest,
            status=_status,
            coordinate=_coordinate,
        )
        if _updatedb:
            _order = IotOrder.objects.filter(
                mac=_mac,
                number=_number,
            ).first()
        else:
            return HttpResponseMessage(8, "fatal error occurs in update db")
    else:
        print "does not find order info about %s , we will create order" % (
            _mac)
        _order = IotOrder.objects.create(
            mac=_mac,
            number=_number,
            device=_device,
            express=_express,
            start_time=start,
            end_time=end,
            high_temp=_hightemp,
            low_temp=_lowtemp,
            highest_temp=_highest,
            lowest_temp=_lowest,
            status=_status,
            coordinate=_coordinate,
        )

    # _order = IotOrder.objects.create(
    #     mac=_mac,
    #     number=_number,
    #     device=_device,
    #     express=_express,
    #     start_time=start,
    #     end_time=end,
    #     high_temp=_hightemp,
    #     low_temp=_lowtemp,
    #     highest_temp=_highest,
    #     lowest_temp=_lowest,
    #     status=_status,
    #     coordinate=_coordinate,
    # )

    logger.debug("get ip addr")
    _ip_addr = get_client_ip(request)
    # > todo update ? create ?
    event = IotOrderEvent.objects.create(order=_order,
                                         type=_type,
                                         description=_ip_addr)
    # return HttpResponseMessageWithData(0, 'success', "order_id", _order.id)
    # 'status',_test_order
    return HttpResponseMessageWithData(0, 'success', "order", _order.id)
Exemple #5
0
def get(request):
    logger.debug("order/get")
    if request.method == 'POST':
        json_data = json.loads(request.body)
    else:
        return HttpResponseBadRequest('Bad Request')

    if 'order_id' in json_data:
        _order_id = json_data['order_id']
        try:
            t = IotOrder.objects.get(pk=_order_id)
            order_dict = []
            a_order = t_to_dict(t)
            order_dict.append(a_order)
            return HttpResponseMessageWithData(0, 'success', "orders",
                                               order_dict)
        except:
            return HttpResponseMessage(4, 'the order id not exists')

    if 'mac' in json_data:
        _mac = json_data['mac']
        logger.debug("mac=" + _mac)
        '''
        try:
            _device = IotDevice.objects.get(mac_addr=_mac)
            print "express exist"
        except:
            _device = None
        '''
    else:
        _mac = None

    if 'number' in json_data:
        _number = json_data['number']
        '''
        try:
            _express = IotExpress.objects.get(number=_number)
            print "express exist"
        except:
            _express = None
        '''
    else:
        _number = None

    if 'status' in json_data:
        _status = json_data['status']
    else:
        _status = STATUS_ALL

    _device = None
    _express = None

    if _mac is not None and _number is not None:
        if _status == STATUS_INIT or _status == STATUS_DONE:
            if _device is None and _express is None:
                logger.debug("search with mac, number and status")
                order = IotOrder.objects.filter(
                    number=_number, mac=_mac,
                    status=_status).order_by('-start_time')
            else:
                # _device or express is not none, both device and express is not none
                if _device is None:
                    # express is not none
                    print "search with mac, express and status"
                    order = IotOrder.objects.filter(
                        express=_express, mac=_mac,
                        status=_status).order_by('-start_time')
                else:
                    # device is not none
                    if _express is None:
                        print "search with device and status"
                        order = IotOrder.objects.filter(
                            device=_device,
                            status=_status).order_by('-start_time')
                    else:
                        print "search with device, express and status"
                        order = IotOrder.objects.filter(
                            device=_device, express=_express,
                            status=_status).order_by('-start_time')
        else:
            if _device is None and _express is None:
                logger.debug("search with mac, number")
                order = IotOrder.objects.filter(
                    number=_number, mac=_mac).order_by('-start_time')
            else:
                # _device or express is not none, both device and express is not none
                if _device is None:
                    # express is not none
                    logger.debug("search with mac, express")
                    order = IotOrder.objects.filter(
                        express=_express, mac=_mac).order_by('-start_time')
                else:
                    # device is not none
                    if _express is None:
                        logger.debug("search with device")
                        order = IotOrder.objects.filter(
                            device=_device).order_by('-start_time')
                    else:
                        print "search with device, express"
                        order = IotOrder.objects.filter(
                            device=_device,
                            express=_express).order_by('-start_time')
    else:
        if _mac is not None:
            if _status == STATUS_INIT or _status == STATUS_DONE:
                if _device is None:
                    logger.debug("search with mac and status")
                    order = IotOrder.objects.filter(
                        mac=_mac, status=_status).order_by('-start_time')
                else:
                    logger.debug("search with device and status")
                    order = IotOrder.objects.filter(
                        device=_device, status=_status).order_by('-start_time')
            else:
                if _device is None:
                    logger.debug("search with mac")
                    order = IotOrder.objects.filter(
                        mac=_mac).order_by('-start_time')
                    logger.debug("search with mac end")
                else:
                    logger.debug("search with device")
                    order = IotOrder.objects.filter(
                        device=_device).order_by('-start_time')
        else:
            if _number is not None:
                if _status == STATUS_INIT or _status == STATUS_DONE:
                    if _express is None:
                        print "search with number and status"
                        order = IotOrder.objects.filter(
                            number=_number,
                            status=_status).order_by('-start_time')
                    else:
                        print "search with express and status"
                        order = IotOrder.objects.filter(
                            express=_express,
                            status=_status).order_by('-start_time')
                else:
                    if _express is None:
                        print "search with number"
                        order = IotOrder.objects.filter(
                            number=_number).order_by('-start_time')
                    else:
                        print "search with express"
                        order = IotOrder.objects.filter(
                            express=_express).order_by('-start_time')
            else:
                return HttpResponseMessage(
                    2, 'no order id, mac or express info in request')

    if order is not None and len(order) > 0:
        order_dict = []
        for t in order:
            a_order = t_to_dict(t)
            order_dict.append(a_order)
        return HttpResponseMessageWithData(0, 'success', "orders", order_dict)
    else:
        return HttpResponseMessage(4, 'the mac or number has no matched order')
Exemple #6
0
def update(request):
    if request.method == 'POST':
        json_data = json.loads(request.body)
    else:
        return HttpResponseBadRequest('Bad Request')

    if 'order_id' in json_data:
        _order_id = json_data['order_id']
    else:
        return HttpResponseMessage(1, 'no order id in request')

    try:
        order = IotOrder.objects.get(pk=_order_id)
    except:
        return HttpResponseMessage(3, 'the order not exists')

    if 'mac' in json_data:
        _mac = json_data['mac']
        if order.mac != _mac:
            order.mac = _mac
            isChanged = True
            try:
                _device = IotDevice.objects.get(mac_addr=_mac)
            except:
                _device = None
            order.device = _device

    if 'number' in json_data:
        _number = json_data['number']
        if order.number != _number:
            order.number = _number
            isChanged = True
            try:
                _express = IotExpress.objects.get(number=_number)
            except:
                _express = None
            order.express = _express

    if 'start_time' in json_data:
        _starttime = json_data['start_time']
        try:
            print _starttime
            start = datetime.strptime(_starttime, dateFormat)
            order.start_time = start
            isChanged = True
        except:
            return HttpResponseMessage(4, 'start time not correct')

    if 'end_time' in json_data:
        _endtime = json_data['end_time']
        try:
            end = datetime.strptime(_endtime, dateFormat)
            order.end_time = end
            isChanged = True
        except:
            return HttpResponseMessage(5, 'end time not correct')

    if 'high_temp' in json_data:
        _hightemp = json_data['high_temp']
        order.high_temp = _hightemp
        isChanged = True

    if 'low_temp' in json_data:
        _lowtemp = json_data['low_temp']
        order.low_temp = _lowtemp
        isChanged = True
    '''
    if 'highest_temp' in json_data:
        _highest = json_data['highest_temp']
        order.highest_temp = _highest
        isChanged = True

    if 'lowest_temp' in json_data:
        _lowest = json_data['lowest_temp']
        order.lowest_temp = _lowest
        isChanged = True
    '''

    # set finished to done after data transfering done
    _type = 2
    if 'status' in json_data:
        _status = json_data['status']
        if order.status != _status:
            print _status
            order.status = _status
            isChanged = True
            if _status == STATUS_DONE:
                data = gethighlow(order.mac, order.start_time, order.end_time)
                _highest = data['highest']
                _lowest = data['lowest']
                order.highest_temp = _highest
                order.lowest_temp = _lowest
                _type = 3
            else:
                # STATUS_INIT, reset highest and lowest value
                _highest = NOT_VALID_TEMPERATURE
                _lowest = NOT_VALID_TEMPERATURE
                order.highest_temp = _highest
                order.lowest_temp = _lowest
                _type = 2
        if _status == STATUS_DONE:
            _type = 3
        else:
            _type = 2

    if isChanged:
        order.save()
        _ip_addr = get_client_ip(request)
        logger.debug(_ip_addr)
        event = IotOrderEvent.objects.create(order=order,
                                             type=_type,
                                             description=_ip_addr)

    return HttpResponseMessageWithData(0, 'success', "order_id", order.id)