-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class IndexConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'index' |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.db import models | ||
|
||
|
||
class Temperature_Humidity(models.Model): | ||
temperature = models.CharField(max_length=10) | ||
humidity = models.CharField(max_length=10) | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{% load static %} | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
{% if request.session.is_login %} | ||
<h1>智慧砂场</h1> | ||
{% else %} | ||
<h1>你尚未登陆</h1> | ||
{% endif %} | ||
<h2>{{ subscribe_msg }}</h2> | ||
<div id="info"></div> | ||
<form method="post" action="msg/"> | ||
{% csrf_token %} | ||
<input type="hidden" name="publish_msg" value=1> | ||
<input type="submit", value="开车"> | ||
</form> | ||
<button id="button1", value="1", onclick="">开车</button> | ||
<button id="button2", value="0", onclick="stopCar()">停车</button> | ||
<script src="{% static 'js/jquery.js' %}"></script> | ||
<script> | ||
$(document).ready(function (){ | ||
setInterval("request_data()", 200000); | ||
$("#button1").click(function (){ | ||
let msg = document.getElementById('button1').value | ||
$.ajax({ | ||
{#调用Ajax指令#} | ||
url:"{% url "pubMsg" %}", | ||
method:'post', //使用post方式 | ||
headers:{'X-CSRFToken':'{{csrf_token}}'}, // 注意放到引号里面 | ||
data:{publish_msg:msg}, //传递参数 | ||
success: function (data) { | ||
console.log(data) | ||
} | ||
}) | ||
}); | ||
}); | ||
function stopCar(){ | ||
let msg = document.getElementById('button2').value | ||
$.ajax({ | ||
{#调用Ajax指令#} | ||
url:"{% url "pubMsg" %}", | ||
method:'post', //使用post方式 | ||
headers:{'X-CSRFToken':'{{csrf_token}}'}, // 注意放到引号里面 | ||
data:{publish_msg:msg}, //传递参数 | ||
success: function (data) { | ||
console.log('success') | ||
} | ||
}) | ||
} | ||
function request_data() { | ||
$.ajax({ | ||
'url': 'getInfo', //访问的url地址 | ||
'dateType': 'json', //想要获得的返回数据类型 | ||
success: function (data){ | ||
document.getElementById('info').innerHTML = data['msg'] | ||
} | ||
}) | ||
}; | ||
</script> | ||
<li><a href="/logout/">退出</a></li> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
|
||
urlpatterns = [ | ||
path('', views.index, name='index'), | ||
path('msg/', views.publishMsg, name='pubMsg'), | ||
path('getInfo/', views.get_info, name='getInfo'), | ||
] |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class LoginConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'login' |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>控制界面</title> | ||
</head> | ||
<body> | ||
{% if request.session.is_login %} | ||
<h1>智慧砂场</h1> | ||
{% else %} | ||
<h1>你尚未登陆</h1> | ||
{% endif %} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>登录界面</title> | ||
</head> | ||
<body> | ||
<h1>用户登录</h1> | ||
<form method="post" action="/login/"> | ||
{% csrf_token %} | ||
<input type="text", name="user", placeholder="用户名"> | ||
<input type="password", name="pwd", placeholder="密码"> | ||
<input type="submit", value="提交"> | ||
<span style="color:red">{{ error_msg }}</span> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
|
||
urlpatterns = [ | ||
path('', views.login, name='user_login'), | ||
path('detail/', views.detail, name='detail'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.shortcuts import render, redirect | ||
from django.http import HttpResponse | ||
|
||
|
||
# Create your views here. | ||
def login(request): | ||
if request.method == 'GET': | ||
return render(request, 'login.html') | ||
elif request.method == 'POST': | ||
print(request.POST) | ||
userName = request.POST.get('user') | ||
password = request.POST.get('pwd') | ||
if userName == 'gxu' and password == '2022': | ||
request.session['is_login'] = True | ||
return redirect('/index/') | ||
else: | ||
return render(request, 'login.html', {"error_msg": "登录失败,用户名或密码错误。"}) | ||
|
||
|
||
def detail(request): | ||
return render(request, 'detail.html') |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class LogoutConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'logout' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
|
||
urlpatterns = [ | ||
path('', views.logout, name='logout'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.shortcuts import render, redirect | ||
|
||
|
||
# Create your views here. | ||
def logout(request): | ||
if not request.session.get('is_login', None): | ||
# 如果本来就未登录,也就没有登出一说 | ||
return redirect("/index/") | ||
request.session.flush() | ||
return redirect("/index/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python | ||
"""Django's command-line utility for administrative tasks.""" | ||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_system.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
ASGI config for manage_system project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_system.settings') | ||
|
||
application = get_asgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
def _init(): # 初始化 | ||
global _global_dict | ||
_global_dict = {} | ||
|
||
|
||
def set_value(key, value): | ||
# 定义一个全局变量 | ||
_global_dict[key] = value | ||
|
||
|
||
def get_value(key): | ||
# 获得一个全局变量,不存在则提示读取对应变量失败 | ||
try: | ||
return _global_dict[key] | ||
except: | ||
print('读取' + key + '失败\r\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 为了能在外部脚本中调用Django ORM模型,必须配置脚本环境变量,将脚本注册到Django的环境变量中 | ||
from email import message | ||
import os, sys | ||
import django | ||
# 引入mqtt包 | ||
import paho.mqtt.client as mqtt | ||
# 使用独立线程运行 | ||
from threading import Thread | ||
import manage_system.gloabl_var as global_var | ||
|
||
# 第一个参数固定,第二个参数是工程名称.settings | ||
os.environ.setdefault('DJANGO_SETTING_MODULE', 'my_django.settings') | ||
django.setup() | ||
|
||
|
||
# 建立mqtt连接 | ||
def on_connect(client, userdata, flag, rc): | ||
print("Connect with the result code " + str(rc)) | ||
client.subscribe('temperature', qos=2) | ||
|
||
|
||
# 接收、处理mqtt消息 | ||
def on_message(client, userdata, msg): | ||
out = str(msg.payload.decode('utf-8')) | ||
global_var.set_value("msg", out) | ||
print(out) | ||
|
||
|
||
# mqtt客户端启动函数 | ||
def mqttfunction(): | ||
global client | ||
# 使用loop_start 可以避免阻塞Django进程,使用loop_forever()可能会阻塞系统进程 | ||
client.loop_start() | ||
# client.loop_forever() 有掉线重连功能 | ||
# client.loop_forever(retry_first_connection=True) | ||
|
||
|
||
client = mqtt.Client(client_id="test", clean_session=False) | ||
|
||
|
||
# 启动函数 | ||
def mqtt_run(): | ||
client.on_connect = on_connect | ||
client.on_message = on_message | ||
# 绑定 MQTT 服务器地址 | ||
# broker = '192.168.31.228' | ||
broker = "mqtt.eclipseprojects.io" | ||
# MQTT服务器的端口号 | ||
client.connect(broker) | ||
# 启动 | ||
mqttthread = Thread(target=mqttfunction) | ||
mqttthread.start() | ||
|
||
|
||
# 启动 MQTT | ||
# mqtt_run() | ||
|
||
if __name__ == "__main__": | ||
mqtt_run() |
Oops, something went wrong.