Ejemplo n.º 1
0
    def delete(self, id):
        bus = Bus.get(id)
        try:
            db.session.delete(bus)
            db.session.commit()
            return Bus.get_bus_list()

        except Exception, e:
            db.session.rollback()
            raise e
Ejemplo n.º 2
0
    def delete(self, id):
        bus = Bus.get(id)
        try:
            db.session.delete(bus)
            db.session.commit()
            return Bus.get_bus_list()

        except Exception, e:
            db.session.rollback()
            raise e
Ejemplo n.º 3
0
    def put(self, id):
        bus = Bus.get(id)
        if not bus:
            res = jsonify(message='不存在该公交')
            res.status_code = 422
            return res
        route_id = request.json.get('route_id')
        plate_number = request.json.get('plate_number')
        light_number = request.json.get('light_number')
        eui = request.json.get('eui')

        bus.update_bus(route_id=route_id,
                       plate_number=plate_number,
                       light_number=light_number,
                       eui=eui)
        try:
            db.session.commit()
            time.sleep(2)
            return marshal(bus, bus_fields), 201
        except IntegrityError, e:
            params = e.params[:-1]
            err_str = u'参数错误'
            for i in xrange(len(params)):
                err_str = err_str + ',' + params[i]
            err_str += u'已经存在'
            response = jsonify({
                'code': '422',
                'errmsg': err_str,
                'data': None,
                'message': ''
            })
            response.status_code = 422
            return '', 422
Ejemplo n.º 4
0
    def get(self, id):
        buses_in_group = Bus.query.filter(and_(Bus.user_id == g.user.id, Bus.group_id == id)).all()
        buses_in_group_res = []
        for bus in buses_in_group:
            b = dict()
            b['route_id'] = bus.route_id
            b['bus_name'] = bus.plate_number
            b['bus_id'] = bus.id
            b['bus_eui'] = bus.eui
            buses_in_group_res.append(b)

        routes = Route.get_routes()
        buses_all = Bus.get_all()
        res = []
        for route in routes:
            data = {}
            data['route_id'] = route.id
            data['route_name'] = route.route_name
            buses = []
            for bus in buses_all:
                if bus.route_id == route.id:
                    in_group = bus.group_id == id
                    buses.append({'route_id': bus.route_id, 'bus_name': bus.plate_number, 'bus_id': bus.id, 'in_group': in_group,
                                  'bus_eui': bus.eui})
            data['buses'] = buses
            res.append(data)
        return jsonify({'buses_all': res, 'in_group': buses_in_group_res})
Ejemplo n.º 5
0
    def put(self, id):
        bus = Bus.get(id)
        if not bus:
            res = jsonify(message="不存在该公交")
            res.status_code = 422
            return res
        route_id = request.json.get("route_id")
        plate_number = request.json.get("plate_number")
        light_number = request.json.get("light_number")
        eui = request.json.get("eui")

        bus.update_bus(route_id=route_id, plate_number=plate_number, light_number=light_number, eui=eui)
        try:
            db.session.commit()
            time.sleep(2)
            return marshal(bus, bus_fields), 201
        except IntegrityError, e:
            params = e.params[:-1]
            err_str = u"参数错误"
            for i in xrange(len(params)):
                err_str = err_str + "," + params[i]
            err_str += u"已经存在"
            response = jsonify({"code": "422", "errmsg": err_str, "data": None, "message": ""})
            response.status_code = 422
            return "", 422
Ejemplo n.º 6
0
    def post(self):
        route_id = request.json.get('route_id')
        plate_number = request.json.get('plate_number')
        light_number = request.json.get('light_number')
        eui = request.json.get('eui')
        if not plate_number or not light_number:
            return jsonify({
                'code':
                '30',
                'msg':
                'plate_number and light_number should be exit'
            })
        bus = Bus(route_id, plate_number, light_number=light_number, eui=eui)

        try:
            db.session.add(bus)
            db.session.commit()
            return bus, 201
        except IntegrityError, e:
            db.session.rollback()
            raise e
Ejemplo n.º 7
0
 def get(self, id):
     bus = Bus.get(id)
     return bus, 201
Ejemplo n.º 8
0
 def get(self):
     buses = Bus.get_bus_list()
     return buses, 201
Ejemplo n.º 9
0
    def put(self, group_id):

        # socketio_cli = MSocketIO(LORA_HOST, LORA_PORT, EventNameSpace, params={'app_eui': APP_EUI, 'token': TOKEN})
        #
        # event_space = socketio_cli.define(EventNameSpace, path=NAMESPACE)

        group = Group.get(group_id)
        members = loads(request.data)
        delete = []
        add = []
        failed_rm_euis = []
        failed_add_euis = []

        def on_rm_dev_from_group(data):
            if data['success'] == 0:
                dev_eui = data['dev_eui']
                failed_rm_euis.append(dev_eui)

        def on_add_dev_into_group(data):
            if data['success'] == 0:
                dev_eui = data['dev_eui']
                failed_add_euis.append(dev_eui)

        event_space.on('rm_dev_from_group', on_rm_dev_from_group)
        event_space.on('add_dev_into_group', on_add_dev_into_group)

        if group.group_id:
            original_members = group.buses
            for bus in original_members:
                if bus.id not in [mem['bus_id'] for mem in members]:
                    delete.append(bus.id)

            for m in members:
                if m['bus_id'] not in [ori.id for ori in original_members]:
                    add.append(m['bus_id'])

            # delete members
            delete_buses = []
            for d in delete:
                bus = Bus.get(d)
                if bus:
                    delete_buses.append(bus)
             # add members
            add_buses = []
            for a in add:
                bus = Bus.get(a)
                if bus:
                    add_buses.append(bus)

            for bus in delete_buses:
                event_space.emit('rm_dev_from_group', {'group_id': group.group_id, 'cmd': 'rm_dev_from_group',
                                                        'dev_eui': bus.eui})
            for bus in add_buses:
                event_space.emit('add_dev_into_group', {'group_id': group.group_id, 'cmd': 'add_dev_into_group',
                                                        'dev_eui': bus.eui})
            # 等待socketio的操作结果
            socketio_cli.wait(3)

            # 写入数据库
            for bus in delete_buses:
                if bus.eui not in failed_rm_euis:
                    bus.group_id = None
                    db.session.add(bus)
            for bus in add_buses:
                if bus.eui.upper() not in failed_add_euis:
                    bus.group_id = group.id
                    db.session.add(bus)
            db.session.commit()

            error_message = ''
            if failed_add_euis + failed_rm_euis:
                error_message = ', '.join(failed_add_euis + failed_rm_euis) + ' 操作失败'
            socketio_cli.disconnect()
            return {'error_message': error_message}, 201

        socketio_cli.disconnect()
        return {'error_message': '组不存在'}, 422
Ejemplo n.º 10
0
 def get(self, id):
     bus = Bus.get(id)
     return bus, 201
Ejemplo n.º 11
0
 def get(self):
     buses = Bus.get_bus_list()
     return buses, 201