Skip to content

Commit

Permalink
Merge pull request #3 from lrqq/main
Browse files Browse the repository at this point in the history
update
  • Loading branch information
NewHkk committed Aug 15, 2022
1 parent 9eeea5e commit 89cbd75
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
Binary file modified db.sqlite3
Binary file not shown.
65 changes: 43 additions & 22 deletions index/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,28 @@ def publishMsg(request):

def get_info(request):
res = {}
if(global_var.get_value("status") == "weight_ok"):
global_var.set_value("status","not")
# 如果检测到地磅信号,则进行检测
if global_var.get_value("status") == "weight_ok":
global_var.set_value("status", "not")
src = snap_detect()
if(src!=False):
if src:
res["src"] = src
if(global_var.get_value("detect_status")=="ok"):
global client;
global_var.set_value("detect_status","not")
client = mqtt.Client(client_id=f'client-{random.randint(0, 1000)}', clean_session=False)
client.connect(mqttBroker)
client.publish("control_msg", "1")
time.sleep(1)
client.publish("control_msg", "3")
if global_var.get_value("detect_status") == "ok":
global_var.set_value("detect_status", "not")
client = mqtt.Client(client_id=f'client-{random.randint(0, 1000)}', clean_session=False)
client.connect(mqttBroker)
client.publish("control_msg", "3")
time.sleep(1)
client.publish("control_msg", "1")
res["state"] = "ok"
res["car_id"] = global_var.get_value("car_id")
res["sand_type"] = global_var.get_value("sand_type")
res["total_weight"] = global_var.get_value("total_weight")
return JsonResponse(res)
else:
return JsonResponse({"src":"null","state":"not","car_id": "null","sand_type":"null","total_weight":"null"})
return JsonResponse(
{"src": "null", "state": "not", "car_id": "null", "sand_type": "null", "total_weight": "null"})


def manage_info(request):
if request.method == 'POST':
Expand Down Expand Up @@ -83,46 +85,65 @@ def getImage(request, path):
return HttpResponse(image_data, content_type="image/png")
return JsonResponse({"code": 404, "msg": "failed"})

#客户端的浏览器请求了才能执行

# 客户端的浏览器请求了才能执行
def snapImage(request):
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
for i in range(5):
time.sleep(0.01)
ret, frame = cap.read()
if ret:
print("snapping...")
else:
print("can't snap, check index/views.py/snap_detect()")
return JsonResponse({"code": 200, "msg": "success", "src": './'})
date1 = time.time()
src = 'media/' + str(date1) + '.jpg'
res = cv2.imwrite(src, frame)
print("snap...")
print(res)
cap.release()
if operator.eq(sys.platform, "linux"):
cmd = "./myNcnnNet " + str(BASE_DIR) + '/' + src
print(cmd)
res = subprocess.getoutput(cmd)
print(res)
print("res=", res[0])
global_var.set_value("sand_type",res[0])
global_var.set_value("sand_type", res[0])
return JsonResponse({"code": 200, "msg": "success", "label": res[0], "src": src})
else:
print("非linux尚未实现检测!")
return JsonResponse({"code": 200, "msg": "success", "src": src})

#直接进行拍照和检测

# 直接进行拍照和检测
def snap_detect():
print("prepare to snap...")
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
for i in range(5):
time.sleep(0.01)
ret, frame = cap.read()
if ret:
print("snapping...")
else:
print("can't snap, check index/views.py/snap_detect()")
return False
date1 = time.time()
src = 'media/' + str(date1) + '.jpg'
res = cv2.imwrite(src, frame)
print("snap...")
if res:
print("snap done.")
cap.release()
if operator.eq(sys.platform, "linux"):
cmd = "./myNcnnNet " + str(BASE_DIR) + '/' + src
print(cmd)
res = subprocess.getoutput(cmd)
print(res)
print("res=", res[0])
global_var.set_value("sand_type",res[0])
global_var.set_value("detect_status","ok")
global_var.set_value("sand_type", res[0])
global_var.set_value("detect_status", "ok")
return src
else:
print("非linux尚未实现检测!")
global_var.set_value("detect_status","not")
return False
global_var.set_value("detect_status", "not")
return False
1 change: 1 addition & 0 deletions manage_system/gloabl_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ def get_value(key):
try:
return _global_dict[key]
except:
return 'NoneValue'
print('读取' + key + '失败\r\n')
6 changes: 3 additions & 3 deletions manage_system/mqtt_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from threading import Thread
import manage_system.gloabl_var as global_var

mqttBroker = "mqtt.eclipseprojects.io"

# 第一个参数固定,第二个参数是工程名称.settings
os.environ.setdefault('DJANGO_SETTING_MODULE', 'my_django.settings')
Expand Down Expand Up @@ -47,12 +46,13 @@ def mqttfunction():

# 启动函数
def mqtt_run():
# client = mqtt.Client(client_id=f'client-{random.randint(0, 1000)}', clean_session=False)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
# 绑定 MQTT 服务器地址
# broker = '192.168.31.228'
broker = "mqtt.eclipseprojects.io"
# mqttBroker = '192.168.31.228'
mqttBroker = "mqtt.eclipseprojects.io"
# MQTT服务器的端口号
client.connect(mqttBroker)
# 启动
Expand Down
1 change: 0 additions & 1 deletion manage_system/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

mqtt_functions.mqtt_run()
global_var._init()
# global_var end

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manage_system.settings')

Expand Down

0 comments on commit 89cbd75

Please sign in to comment.