def home(request): if request.method == 'POST': device_id = request.POST['device_id'] red = set_light(request, 'red') blue = set_light(request, 'blue') green = set_light(request, 'green') tape = set_light(request, 'tape') created = Devices.objects.filter(device_id=device_id).count() if created != 0: Devices.objects.filter(device_id=device_id).update( device_id=device_id, red=red, green=green, blue=blue, tape=tape ) else: device_objects = Devices( device_id=device_id, red=red, blue=blue, green=green, tape=tape ) device_objects.save() data = {"url": "register"} return render(request, 'app/index.html', data)
def post(self): """Register device""" content = request.json if g.status == 200: if 'token' in content and 'device_model' in content and 'os_version' in content: try: userId = g.response['user']['userId'] user = UserNotifications.query.filter_by( username=userId).first() db.session.flush() if user is not None: user.devices.append( Devices(token=content['token'], device_model=content['device_model'], os_version=content['os_version'])) db.session.commit() else: user = UserNotifications(username=userId) user.devices.append( Devices(token=content['token'], device_model=content['device_model'], os_version=content['os_version'])) db.session.add(user) db.session.commit() return {"status": "OK"}, 200 except exc.IntegrityError: db.session.rollback() print("Device già registrato") return { 'errMsgTitle': "Attenzione", 'errMsg': "Device già registrato" }, 500 except: db.session.rollback() print("Unexpected error:") print("Title: " + sys.exc_info()[0].__name__) print("Description: " + traceback.format_exc()) return { 'errMsgTitle': sys.exc_info()[0].__name__, 'errMsg': traceback.format_exc() }, 500 else: return {'errMsg': 'Wrong body!'}, 500 else: return {'errMsg': 'Wrong username/pass'}, g.status
def devices(request, format=None): if request.method == 'POST': req = request.data try: newdev = Devices(devicename=req['devicename'], password=make_password(req['password']), nickname=req['nickname']) newdev.save() except Exception as e: return Response('require devicename, password, nickname', status=status.HTTP_400_BAD_REQUEST) ACLs.objects.create(device=newdev, clientname=newdev.devicename, topic='/' + str(newdev.id) + '/#', rw=2).save() # 更新用户的设备列表 ACLs.objects.create(user=request.user, device=newdev, clientname=request.user.username, topic='/' + str(newdev.id) + '/#', rw=2).save() return Response(status=status.HTTP_201_CREATED)
def getNewDevice(self): device = Devices() device.device_name = self.__device_name device.device_type_id = self.__device_type_id device.device_status = self.__device_status device.school_id = self.__school_id device.lend_log_id = self.__lend_log_id db.session.add(device) db.session.commit() return device
def home(request): if request.method == 'POST': device_id = request.POST['device_id'] red = set_light(request, 'red') blue = set_light(request, 'blue') green = set_light(request, 'green') tape = set_light(request, 'tape') created = Devices.objects.filter(device_id=device_id).count() if created != 0: Devices.objects.filter(device_id=device_id).update( device_id=device_id, red=red, green=green, blue=blue, tape=tape) else: device_objects = Devices(device_id=device_id, red=red, blue=blue, green=green, tape=tape) device_objects.save() data = {"url": "register"} return render(request, 'app/index.html', data)
def save_device_form(form): try: session = db.session.session_factory() session.autocommit = False #neleis daryti auto commit, po kiekvieno objekto pakeitimo bus daromas flush. session.autoflush = True id = int(form.id.data ) if form.id.data is not None and form.id.data != '' else None if (id is None): deviceObj = Devices(mac=form.mac.data) device = session.query(Devices).filter( Devices.mac == form.mac.data).first() if (device is not None): raise Exception( "Prietaisas su tokiu MAC adresu jau egzistuoja!") service = DevicesService() response = service.generate_device_keys(mac=deviceObj.mac, uuid=deviceObj.uuid) if (not response.success): raise Exception(response.message) session.add(deviceObj) flash('Prietaisas MAC: ' + deviceObj.mac + ' sukurtas!', 'success') else: device = session.query(Devices).filter(Devices.id == id).first() device.mac = form.mac.data session.commit() except Exception as ex: session.rollback() Logger.log_error(ex.args[0]) session.close() flash(ex.args[0], 'danger') return False finally: session.close() return True
def esp8266(dev_id, state, dev_type, name): # Эта функция раьотает с устройством dev_state = 0 device = Devices.query.filter_by(dev_id=dev_id).first() if state == "0": state = False if state == "1": state = True if device is None: dev = Devices(dev_id=dev_id, type=dev_type, prev_state=state, dev_name=name) db.session.add(dev) db.session.commit() return "hi" else: device.prev_state = state if device.state: dev_state = 1 if not device.state: dev_state = 0 db.session.commit() return str(dev_state)
from app import db from app.models import User, Devices, Log, Sensors pitu = User(username='******', admin=True) pitu.set_password('pitu') gordo = User(username='******', admin=False) gordo.set_password('gordo') db.session.add(pitu) db.session.add(gordo) device1 = Devices(user_perm=False, str_id='Luz Puerta', location='Cocina', dev_type=True, tactil_switch=False, handles='[]', state=True, set_point=None, mac_address='08:00:27:60:03:76') device2 = Devices(user_perm=False, str_id='Luz Mesada', location='Cocina', dev_type=True, tactil_switch=False, handles='[]', state=True, set_point=50, mac_address='08:00:27:60:03:78') device4 = Devices(user_perm=False, str_id='Luz Parrilla', location='Patio',