Skip to content

Commit

Permalink
Create db.sqlite3
Browse files Browse the repository at this point in the history
  • Loading branch information
NewHkk committed Jul 14, 2022
1 parent e2fc7a3 commit 586ecca
Show file tree
Hide file tree
Showing 59 changed files with 501 additions and 0 deletions.
Binary file added db.sqlite3
Binary file not shown.
Empty file added index/__init__.py
Empty file.
Binary file added index/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added index/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file added index/__pycache__/apps.cpython-36.pyc
Binary file not shown.
Binary file added index/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file added index/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added index/__pycache__/views.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions index/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions index/apps.py
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 added index/migrations/__init__.py
Empty file.
Binary file added index/migrations/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions index/models.py
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)

2 changes: 2 additions & 0 deletions index/static/js/jquery-3.6.0.min.js

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions index/templates/index.html
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>
3 changes: 3 additions & 0 deletions index/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions index/urls.py
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 added login/__init__.py
Empty file.
Binary file added login/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added login/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file added login/__pycache__/apps.cpython-36.pyc
Binary file not shown.
Binary file added login/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file added login/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added login/__pycache__/views.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions login/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions login/apps.py
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 added login/migrations/__init__.py
Empty file.
Binary file added login/migrations/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions login/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
14 changes: 14 additions & 0 deletions login/templates/detail.html
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>
17 changes: 17 additions & 0 deletions login/templates/login.html
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>
3 changes: 3 additions & 0 deletions login/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions login/urls.py
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'),
]
21 changes: 21 additions & 0 deletions login/views.py
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 added logout/__init__.py
Empty file.
Binary file added logout/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added logout/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added logout/__pycache__/views.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions logout/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions logout/apps.py
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 added logout/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions logout/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions logout/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions logout/urls.py
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'),
]
10 changes: 10 additions & 0 deletions logout/views.py
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/")
22 changes: 22 additions & 0 deletions manage.py
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 added manage_system/__init__.py
Empty file.
Binary file added manage_system/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added manage_system/__pycache__/gloabl_var.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file added manage_system/__pycache__/settings.cpython-36.pyc
Binary file not shown.
Binary file added manage_system/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added manage_system/__pycache__/wsgi.cpython-36.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions manage_system/asgi.py
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()
18 changes: 18 additions & 0 deletions manage_system/gloabl_var.py
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')
59 changes: 59 additions & 0 deletions manage_system/mqtt_functions.py
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()
Loading

0 comments on commit 586ecca

Please sign in to comment.