#! /usr/bin/python3
#coding: utf-8
import serial
import MySQLdb
import string
import time
import requests
import cv2
import os
import pysftp
from time import gmtime, strftime
s = serial.Serial('com5', 9600, bytesize=8, parity='N', stopbits=1, timeout=1)
def photo(door):
if door == 2:
stream_url = 'http://192.168.2.74/action/snapshot/'
elif door == 1:
stream_url = 'http://192.168.2.74/action/snapshot/'
absolute_script_dir = os.path.dirname(os.path.realpath(__file__))
save_dir = absolute_script_dir + '/snapshots/'
save_path = save_dir + '/snapshot.jpg'
cap = cv2.VideoCapture(stream_url)
ret, frame = cap.read()
cv2.imwrite(save_path, frame)
try:
with sftp.Connection('http://museum.ru/telegram2.php', username='123', password='3123') as sftp:
remote = 'httpdocs/css/font/snapshot.jpg'
local = 'D:\\www\\htdocs\\checkout\\snapshots\\snapshot.jpg'
sftp.put(local,remote)
sftp.close()
except Exception:
print("Ошибка ftp")
while True:
last = ''
for byte in s.read(s.inWaiting()): last += chr(byte)
if (len(last) == 14):
conn = MySQLdb.connect('localhost', 'user', '123', 'sotr')
cursor = conn.cursor()
# Do whatever you want with last
print (last)
id = str(last)[4:]
#print(id)
try:
in_out = int(str(last)[:-11])
except Exception:
print("Ошибка в пропуске")
else:
zapros = ("SELECT COUNT(*) FROM fio_table where (propusk=" + id +")")
zapros_korr = ("SELECT COUNT(*) FROM `prohod` WHERE propusk = " + id +" AND status = '00FF00' ")
try:
cursor.execute(zapros)
except Exception:
print("Ошибка в пропуске")
else:
row = cursor.fetchone()
row = row[0]
print(in_out)
if row < 1:
print("Доступ ЗАПРЕЩЕН")
conn.close()
if in_out == 931:
photo(1)
teleg = "ОХРАННИК РАЗБЛОКИРОВАЛ ТУРНИКЕТ!!!"
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
print("ОХРАННИК РАЗБЛОКИРОВАЛ ТУРНИКЕТ!!!")
elif in_out == 962:
photo(2)
time.sleep(1)
teleg = "ОХРАННИК ВПУСТИЛ НЕИЗВЕСТНОГО!!!"
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
print("ОХРАННИК ВПУСТИЛ НЕИЗВЕСТНОГО!!!")
elif in_out == 972:
photo(1)
time.sleep(1)
teleg = "ОХРАННИК ВЫПУСТИЛ НЕИЗВЕСТНОГО!!!"
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
print("ОХРАННИК ВЫПУСТИЛ НЕИЗВЕСТНОГО!!!")
else:
print("Доступ РАЗРЕШЕН")
if in_out == 220:
conn.close()
time.sleep(1)
print("Подключение к базе")
conn = MySQLdb.connect(host='localhost', user='user', password='123', database='sotr', charset='utf8')
cursor = conn.cursor()
datey = strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datev = strftime("%H:%M:%S", time.localtime())
print("ОТправка запроса")
cursor.execute("INSERT INTO prohod (propusk, status, data, time_vh) VALUES ('"+ id +"', '00FF00', '" + datey + "', '" + datev + "')")
cursor.execute("SELECT fio FROM fio_table where propusk='"+ id +"'")
rs = cursor.fetchall()
ss = ""
for o in range(len(rs)):
ss+=str(rs)
teleg = ""
ss = ss[3:]
ss = ss[:-5]
print(ss)
teleg = "Вошёл" + " "+ ss
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
conn.commit()
conn.close()
print("Запрос в базу на вход")
if in_out == 200:
print("Входит")
cursor.execute(zapros_korr)
row = cursor.fetchone()
conn.close()
photo(2)
row = row[0]
#if row > 0:
# print("ИД уже на базе") тут нельзя войти 2 раза
#else:
print("Пропустить на вход")
sss = str("2200" + id)
ss = str.encode(sss)
type(ss) # ensure it is byte representation
print(ss)
time.sleep(.5)
s.write(ss)
ss = " "
elif in_out == 120:
print("Запрос в базу на выход")
conn.close()
time.sleep(1)
conn = MySQLdb.connect(host='localhost', user='user', password='123', database='sotr', charset='utf8')
cursor = conn.cursor()
datey = strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datev = strftime("%H:%M:%S", time.localtime())
cursor.execute("UPDATE prohod as pr, (SELECT id FROM prohod WHERE propusk='"+ id +"' ORDER BY id DESC LIMIT 1) as src SET pr.status = 'DC143C', pr.time_vih='" + datev + "', pr.date_vih = '" + datey + "', pr.time_ar=TIMEDIFF('" + datey + "', pr.data) WHERE pr.id = src.id;")
cursor.execute("SELECT fio FROM fio_table where propusk='"+ id +"'")
rs = cursor.fetchall()
ss = ""
for o in range(len(rs)):
ss+=str(rs)
teleg = ""
ss = ss[3:]
ss = ss[:-5]
print(ss)
teleg = "Вышел" + " "+ ss
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
conn.commit()
conn.close()
elif in_out == 100:
print("Выходит")
cursor.execute(zapros_korr)
row = cursor.fetchone()
conn.close()
photo(1)
row = row[0]
#if row < 1:
# print("Еще не вошел") тут нельзя выйти 2 раза
#else:
print("Пропустить на выход")
sss = str("1100" + id)
ss = str.encode(sss)
type(ss) # ensure it is byte representation
print(ss)
time.sleep(.5)
s.write(ss)
ss = " "
elif in_out == 900:
conn.close()
time.sleep(1)
conn = MySQLdb.connect(host='localhost', user='user', password='TWkuDB7jE84TLiqx', database='sotr', charset='utf8')
cursor = conn.cursor()
datey = strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datev = strftime("%H:%M:%S", time.localtime())
cursor.execute("INSERT INTO prohod (propusk, status, data, time_vh) VALUES ('"+ id +"', '800080', '" + datey + "', '" + datev + "')")
cursor.execute("SELECT fio FROM fio_table where propusk='"+ id +"'")
rs = cursor.fetchall()
#iterate through rows
ss = ""
for o in range(len(rs)):
ss+=str(rs)
teleg = ""
ss = ss[3:]
ss = ss[:-5]
print(ss)
teleg = "ВНИМАНИЕ "+ ss + " НЕ ТРЕЗВЫЙ!!!"
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
conn.commit()
conn.close()
print("Бухой Ахтунг!!!")
elif in_out == 910:
conn.close()
time.sleep(1)
conn = MySQLdb.connect(host='localhost', user='user', password='TWkuDB7jE84TLiqx', database='sotr', charset='utf8')
cursor = conn.cursor()
datey = strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datev = strftime("%H:%M:%S", time.localtime())
cursor.execute("INSERT INTO prohod (propusk, status, data, time_vh) VALUES ('"+ id +"', '800080', '" + datey + "', '" + datev + "')")
cursor.execute("SELECT fio FROM fio_table where propusk='"+ id +"'")
rs = cursor.fetchall()
#iterate through rows
ss = ""
for o in range(len(rs)):
ss+=str(rs)
teleg = ""
ss = ss[3:]
ss = ss[:-5]
print(ss)
teleg = ss + " отказывается пройти тест!!!"
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
conn.commit()
conn.close()
print("Отказался проходить тест!!!")
elif in_out == 130:
conn.close()
time.sleep(1)
conn = MySQLdb.connect(host='localhost', user='user', password='TWkuDB7jE84TLiqx', database='sotr', charset='utf8')
cursor = conn.cursor()
datey = strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datev = strftime("%H:%M:%S", time.localtime())
cursor.execute("INSERT INTO prohod (propusk, status, data, time_vh) VALUES ('"+ id +"', 'FFFF00', '" + datey + "', '" + datev + "')")
cursor.execute("SELECT fio FROM fio_table where propusk='"+ id +"'")
rs = cursor.fetchall()
#iterate through rows
ss = ""
for o in range(len(rs)):
ss+=str(rs)
teleg = ""
ss = ss[3:]
ss = ss[:-5]
print(ss)
teleg = ss + " ПРОПУСТИЛ ПОСТОРОННЕГО!!!"
response = requests.get(http://museum.ru/telegram2.php',params={'text': teleg})
conn.commit()
conn.close()
print("Пропустил шпиона!!!")
elif in_out == 230:
conn.close()
time.sleep(1)
conn = MySQLdb.connect(host='localhost', user='user', password='TWkuDB7jE84TLiqx', database='sotr', charset='utf8')
cursor = conn.cursor()
datey = strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datev = strftime("%H:%M:%S", time.localtime())
cursor.execute("INSERT INTO prohod (propusk, status, data, time_vh) VALUES ('"+ id +"', 'FFFF00', '" + datey + "', '" + datev + "')")
cursor.execute("SELECT fio FROM fio_table where propusk='"+ id +"'")
rs = cursor.fetchall()
#iterate through rows
ss = ""
for o in range(len(rs)):
ss+=str(rs)
teleg = ""
ss = ss[3:]
ss = ss[:-5]
print(ss)
teleg = ss + " ВЫПУСТИЛ ПОСТОРОННЕГО!!!"
#requests.get("http://museum.ru/telegram2.php?text='+ teleg'")
response = requests.get('http://museum.ru/telegram2.php',params={'text': teleg})
conn.commit()
conn.close()
print("Выпустил дисидента!!!")