Ejemplo n.º 1
0
def data(did, sid):
    print(type(request.json), request.json)
    device = g.current_user.devices.filter_by(id=did).first()
    if device is None:
        return jsonify({'status': 404, 'info': '找不到你的设备'})

    sensor = device.sensors.filter_by(id=sid).first()
    if sensor is None:
        return jsonify({'status': 404, 'info': '找不到你的传感器'})
    print(request.json)
    if request.json is None:
        return jsonify({'status': 404, 'info': '找不到你的数据'})

    if 'data' not in request.json.keys():
        return jsonify({'status': 404, 'info': '找不到你的数据'})

    # 判断报警

    data = Data()
    data.data = request.json.get('data')
    data.sensor_id = sid
    db.session.add(data)
    db.session.commit()
    if data.data > sensor.max:
        alert = Alert()
        alert.sensor_id = sensor.id
        alert.reason = '超出最大值'
        alert.data_id = data.id
        alert.current_max = sensor.max
        alert.current_min = sensor.min
        db.session.add(alert)
        db.session.commit()
    if data.data < sensor.max:
        alert = Alert()
        alert.sensor_id = sensor.id
        alert.reason = '低于最小值'
        alert.data_id = data.id
        alert.current_max = sensor.max
        alert.current_min = sensor.min
        db.session.add(alert)
        db.session.commit()
    return jsonify({'status': 200})
Ejemplo n.º 2
0
def newdata(device_id, sensor_id):
    count = current_app.config['DATA_COUNT']
    interval = current_app.config['DATA_INTERCAL']
    d = g.current_user.devices.filter_by(id=device_id).first()
    if d is None:
        return jsonify({'status': 404})
    else:
        s = d.sensors.filter_by(id=sensor_id).first()
        if s is None:
            return jsonify({'status': 404})
        else:
            if request.method == 'GET':
                newdata = s.datas.order_by(Data.id.desc()).first()
                if newdata is None:
                    return jsonify({'status': 404})
                else:
                    return jsonify({'status': 200, 'data': newdata.to_json()})
            else:
                data_json = request.json
                if data_json is None:
                    return jsonify({'status': 404})
                else:
                    data = data_json.get('data')
                    if data is None:
                        return jsonify({'status': 404})
                    else:
                        old_time = time.mktime(
                            s.datas.order_by(
                                Data.id.desc()).first().time.timetuple())
                        new_time = time.mktime(datetime.utcnow().timetuple())
                        if new_time - old_time < interval:
                            return jsonify({
                                'status': 412,
                                'tip': '请求时间间隔不能低于20s'
                            })
                        else:
                            d = Data()
                            print(s.datas.count())
                            if s.datas.count() + 1 > count:
                                sd = s.datas.first()
                                db.session.delete(sd)
                            d.sensor_id = sensor_id
                            d.data = data
                            db.session.add(d)

                            alert = Waring()
                            alert.max = s.max
                            alert.min = s.min
                            alert.sensor_id = sensor_id
                            alert.device_id = device_id
                            alert.user_id = g.current_user.id
                            alert.data = d.data
                            if s.max > s.min:
                                if d.data > s.max:
                                    alert.about = '超过上限'
                                    db.session.add(alert)
                                if d.data < s.min:
                                    alert.about = '低于下限'
                                    db.session.add(alert)
                            db.session.commit()

                            return jsonify({'status': 200, 'success': True})