Miroca_Server
This commit is contained in:
0
api/tuya/__init__.py
Normal file
0
api/tuya/__init__.py
Normal file
32
api/tuya/connect.py
Normal file
32
api/tuya/connect.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import psycopg2
|
||||
import tinytuya
|
||||
import json
|
||||
from tuya_connector import TuyaOpenAPI
|
||||
|
||||
tuya_file='tuya_con.json'
|
||||
|
||||
def connect_base():
|
||||
conn = psycopg2.connect(host='172.17.0.1',
|
||||
port='54322',
|
||||
user='miroca',
|
||||
password='cbvgcjy0',
|
||||
database='miroca')
|
||||
return conn
|
||||
|
||||
|
||||
def connect_local():
|
||||
device=tinytuya.deviceScan()
|
||||
return device
|
||||
|
||||
def connect_tuya():
|
||||
ACCESS_ID = "pardyqsmdjetwdxjcaxa"
|
||||
ACCESS_KEY = "4f81a40a36f349dc9ad11d53d74cb34b"
|
||||
API_ENDPOINT = "https://openapi.tuyaeu.com"
|
||||
|
||||
try:
|
||||
openapi = TuyaOpenAPI(API_ENDPOINT, ACCESS_ID, ACCESS_KEY)
|
||||
openapi.connect()
|
||||
return openapi
|
||||
except Exception as e:
|
||||
with open(tuya_file, 'w', encoding='utf-8') as f:
|
||||
json.dump([{'error': str(e)}], f,ensure_ascii=False, indent=4)
|
||||
85
api/tuya/device.py
Normal file
85
api/tuya/device.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from itertools import chain
|
||||
from psycopg2.extras import execute_values
|
||||
from connect import *
|
||||
import json
|
||||
|
||||
file_psycopd = 'psycopd_con.json'
|
||||
file_local = 'local_device.json'
|
||||
file_tuya = 'tuya_con.json'
|
||||
|
||||
|
||||
class DeviceError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def update_base():
|
||||
conn = connect_base()
|
||||
cursor = conn.cursor()
|
||||
count = 0
|
||||
try:
|
||||
with open(file_tuya, 'r', encoding='utf-8') as f:
|
||||
tuya_devices = json.load(f)
|
||||
if tuya_devices['error']:
|
||||
raise DeviceError(tuya_devices['error'])
|
||||
except Exception as e:
|
||||
print(f'Ошибка update_base: {e}')
|
||||
with open(file_local, 'r', encoding='utf-8') as f:
|
||||
local_devices = json.load(f)
|
||||
for device in tuya_devices:
|
||||
if device['result']['id'] == [v for v in local_devices.values()][count]['gwId']:
|
||||
cursor.execute("UPDATE upanel_device SET local_key = %s, name = %s "
|
||||
"WHERE device_id= %s",
|
||||
(device['result']['local_key'],
|
||||
device['result']['name'],
|
||||
device['result']['id']))
|
||||
conn.commit()
|
||||
count += 1
|
||||
cursor.close()
|
||||
|
||||
|
||||
def add_base():
|
||||
conn = connect_base()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
with open(file_local, 'r', encoding='utf-8') as f:
|
||||
local_devices_dict = json.load(f)
|
||||
local_devices = {i + 1: v for i, v in enumerate(local_devices_dict.values())}
|
||||
cursor.execute("SELECT * FROM upanel_device")
|
||||
devices = list(chain.from_iterable(cursor.fetchall()))
|
||||
execute_values(cursor, "INSERT INTO upanel_device "
|
||||
"(device_ip, device_id,device_version) "
|
||||
"VALUES %s",
|
||||
[(local_devices_dict[local_devices[device]['ip']]['ip'],
|
||||
local_devices_dict[local_devices[device]['ip']]['gwId'],
|
||||
local_devices_dict[local_devices[device]['ip']]['version'],)
|
||||
for device in local_devices if local_devices[device]['ip'] not in devices])
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
except Exception as e:
|
||||
print(f'ошибка add_base: {e}')
|
||||
with open('error_add.json', 'w', encoding='utf-8') as f:
|
||||
json.dump({'Error_add':e}, f)
|
||||
|
||||
def delete_base():
|
||||
conn = connect_base()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
with open(file_local, 'r', encoding='utf-8') as f:
|
||||
local_devices_dict = json.load(f)
|
||||
ip_file=sorted([v for k,v in enumerate(local_devices_dict)])
|
||||
cursor.execute('SELECT * FROM upanel_device')
|
||||
cursor.executemany("DELETE FROM upanel_device WHERE device_ip=%s",
|
||||
[tuple(str(item) for item in ip[2].split())
|
||||
for ip in cursor.fetchall()
|
||||
if ip[2] not in ip_file])
|
||||
conn.commit()
|
||||
|
||||
except Exception as e:
|
||||
print(f'Ошибка delete_base: {e}')
|
||||
with open('error_delete.json', 'w', encoding='utf-8') as f:
|
||||
json.dump({'Error_delete':e}, f)
|
||||
|
||||
if __name__ == '__main__':
|
||||
add_base()
|
||||
update_base()
|
||||
delete_base()
|
||||
44
api/tuya/input_file.py
Normal file
44
api/tuya/input_file.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from connect import *
|
||||
|
||||
psycopd_file = 'psycopd_con.json'
|
||||
local_file = 'local_device.json'
|
||||
tuya_file='tuya_con.json'
|
||||
|
||||
def file_base():
|
||||
try:
|
||||
cursor=connect_base().cursor()
|
||||
cursor.execute('SELECT * FROM upanel_device')
|
||||
except Exception as e:
|
||||
with open(psycopd_file, 'w', encoding='utf-8') as f:
|
||||
json.dump({'error': str(e)}, f)
|
||||
else:
|
||||
with open(psycopd_file, 'w', encoding='utf-8') as f:
|
||||
json.dump({v[0]:v[1:] for k,v in enumerate(cursor.fetchall())}, f,
|
||||
ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
|
||||
def file_local():
|
||||
try:
|
||||
with open(local_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(connect_local(), f, ensure_ascii=False, indent=4)
|
||||
except Exception as e:
|
||||
with open(local_file, 'w', encoding='utf-8') as f:
|
||||
json.dump({'error': str(e)}, f)
|
||||
|
||||
|
||||
def file_tuya():
|
||||
try:
|
||||
with open(local_file, 'r', encoding='utf-8') as f:
|
||||
local_device = json.load(f)
|
||||
with open(tuya_file, 'w', encoding='utf-8') as f:
|
||||
json.dump([connect_tuya().get(f"/v1.0/iot-03/devices/{local_device[dev]['id']}")
|
||||
for dev in local_device],f,ensure_ascii=False, indent=4)
|
||||
except Exception as e:
|
||||
with open(tuya_file, 'w', encoding='utf-8') as f:
|
||||
json.dump({'error': str(e)}, f)
|
||||
|
||||
if __name__ == '__main__':
|
||||
file_base()
|
||||
file_local()
|
||||
file_tuya()
|
||||
Reference in New Issue
Block a user