Best Python code snippet using fMBT_python
actions.py
Source:actions.py
1from gpiozero import LED2from time import sleep3import json4from card_reader.monitor import *5from app.models import Log, Status, Setting, Simulation6import random7from django.db.models import Count8import requests910greenLED = LED(22)11redLED = LED(23)1213def sendswipe(cardID):14 response = requests.post('http://192.168.2.179:8000/cardhandler/swipe/', data = {"card":cardID})1516def red(state, time):17 if state=="on":18 redLED.on()19 if state=="off":20 redLED.off()21 22def green(state, time):23 if state=="on":24 greenLED.on()25 if state=="off":26 greenLED.off()2728def updatestatus(state):29 record = Status.objects.first()30 if record:31 scriptjson = json.loads(record.current)32 record.current = json.dumps({"state": state, "script": scriptjson["script"]})33 else:34 scriptstring = json.dumps({"state": state, "script": "stop"})35 record = Status(current=scriptstring)36 record.save()3738def readstatus():39 record = Status.objects.first()40 return record.current4142def updatesettings(state):43 record = Setting.objects.first()44 if record:45 record.current = state46 else:47 record = Setting(current=state)48 record.save()4950def readsettings():51 record = Setting.objects.first()52 return record.current5354def updatescript(state):55 record = Simulation.objects.first()56 if record:57 record.script = state58 else:59 record = Simulation(script=state)60 record.save()6162def readscript():63 record = Simulation.objects.first()64 return record.script6566def updaterun(run):67 record = Status.objects.first()68 if record:69 statejson = json.loads(record.current)70 record.current = json.dumps({"state": statejson["state"], "script": run})71 else:72 statusstring = json.dumps({"state": "close", "script": run})73 print(statusstring)74 record = Status(current=statusstring)75 record.save()76 if run == "start":77 scriptrun()7879def scriptrun():80 print("Run script")81 record = Simulation.objects.first()82 script = record.script83 scriptjson = json.loads(script)84 repeat = scriptjson["repeat"]85 print(repeat)86 print(scriptjson["script"])87 while repeat > 0:88 for command in scriptjson["script"]:89 if command["command"] == "swipe":90 sendswipe(command["card"])91 if command["wait"] == "rand1":92 sleep(random.randint(5, 30))93 elif command["wait"] == "rand2":94 sleep(random.randint(1, 10))95 elif command["wait"] == "rand3":96 sleep(random.randint(1, 3))97 repeat -= 198 print(repeat)99100
...
views.py
Source:views.py
1from django.shortcuts import render2from django.template import loader3from django.http import HttpResponse4from django.core.serializers import serialize5from app.models import Log6import json7from card_reader.actions import *8from card_reader.monitor import *9from datetime import datetime, timedelta 10from datetime import date11from django.http import JsonResponse12from django.utils import timezone13import time14def index(request):15 return HttpResponse("basic device index")16def command(request):17 action = json.loads(request.body)18 if action["command"]=="swipe":19 sendswipe(action["card"])20 actionlog(action["command"] +' '+ action["card"])21 return HttpResponse("command sent %s." % action)22def status(request):23 currentstatus = readstatus()24 return HttpResponse("Status %s." % currentstatus)25def devsettings(request):26 if request.method == 'GET':27 currentsettings = readsettings()28 elif request.method == 'POST':29 sentsettings = json.loads(request.body)30 updatesettings(sentsettings)31 currentsettings = readsettings()32 return HttpResponse("settings %s." % currentsettings)33def monitor(request):34 if request.body:35 range = json.loads(request.body)36 starttime = range["start"]37 endtime = range["end"]38 monitorlog = Log.objects.filter(event_date__gte=starttime, event_date__lte=endtime)39 logoutput = serialize('json', monitorlog)40 else:41 monitorlog = Log.objects.filter(event_date__gte=datetime.now(tz=timezone.utc)+ timedelta(hours=-1))42 logoutput = serialize('json', monitorlog)43 return HttpResponse("monitor log %s." % logoutput)44def simulation(request):45 sentscript = json.loads(request.body)46 updatescript(json.dumps(sentscript))47 currentscript = readscript()48 return HttpResponse("Script %s." % currentscript)49def start(request):50 currentstatus = updaterun("start")51 return HttpResponse("Status %s." % currentstatus)52def stop(request):53 currentstatus = updaterun("stop")54 return HttpResponse("Status %s." % currentstatus)55def stats(request):56 starttime=date.today()57 print(starttime)58 stats = list(Log.objects.filter(event_date__gte=starttime).extra({'hour': 'strftime("%%H", event_date )'}).order_by().values('hour').annotate(count=Count('id')))...
urls.py
Source:urls.py
1from django.contrib import admin2from django.urls import path, include3from django.conf.urls import url4from manager.views import *5from django.conf.urls.static import static6from django.conf import settings7urlpatterns = [8 path('showadmin/',admin.site.urls),9 #path('auth/', include('Accounts.urls')),10 url(r'^rest-auth/', include('rest_auth.urls')),11 # url(r'^rest-auth/', include('rest_auth.urls')),12 # url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),13 url('showPost/',PostsView.as_view(),name="postview"),14 url('showComments/',CommentView.as_view(),name="comment"),15 # url(r'^rest-auth/facebook/', FacebookLogin.as_view(), name='fb_login'),16 # path('rest-auth/google/', GoogleLogin.as_view(), name='google_login'),17 url('details/',DetailView.as_view(),name="hello"),18 # url('setPref/', SetPrefView.as_view(),name="Pref"),19 # url('getSwipe/',GetSwipe.as_view(),name="swipe"),20 # url('sendSwipe/', SwipeView.as_view(), name="SwipeView"),21 # url('getFeed/',NewsPost.as_view(),name='news'),22 # url('mySchedule/',ScheduleView.as_view(),name='sv')23 24]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!