Miroca_Server
This commit is contained in:
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
3
api/admin.py
Normal file
3
api/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
api/apps.py
Normal file
6
api/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api'
|
||||
5
api/mikrotik/api.py
Normal file
5
api/mikrotik/api.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import ros_api
|
||||
|
||||
router = ros_api.Api('192.168.10.1', user='server', password='Cbvgcjy0V', port='8728')
|
||||
r = router.talk('/interface/wireless/cap/print')
|
||||
print(r[0]['enabled'])
|
||||
15
api/mikrotik/total.py
Normal file
15
api/mikrotik/total.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import netmiko
|
||||
from netmiko import ConnectHandler
|
||||
|
||||
mikrotik_router_1 = {
|
||||
'device_type': 'mikrotik_routeros',
|
||||
'host': '192.168.10.1',
|
||||
'port': '2202',
|
||||
'username': 'server',
|
||||
'password': 'Cbvgcjy0V'
|
||||
}
|
||||
|
||||
sshCli = ConnectHandler(**mikrotik_router_1)
|
||||
print(sshCli.find_prompt())
|
||||
output = sshCli.send_command("/interface ethernet print")
|
||||
print(output)
|
||||
3
api/models.py
Normal file
3
api/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
15
api/serializers.py
Normal file
15
api/serializers.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth import get_user_model
|
||||
User = get_user_model()
|
||||
from upanel.models import Device,Hostname
|
||||
|
||||
class DeviceSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Device
|
||||
fields = ('id','name', 'device_id','device_ip','local_key','device_version','customuser_id')
|
||||
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id','username','email','ipaddress')
|
||||
3
api/tests.py
Normal file
3
api/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
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()
|
||||
12
api/urls.py
Normal file
12
api/urls.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.urls import path,include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from api import views
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'devices', views.DeviceViewSet)
|
||||
router.register(r'user', views.UserViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
path('', include((router.urls, 'app_name'), namespace='api/device')),
|
||||
path('', include((router.urls, 'app_name'), namespace='api/user')),
|
||||
]
|
||||
24
api/views.py
Normal file
24
api/views.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from django.shortcuts import render
|
||||
from django.contrib.auth import get_user_model
|
||||
User = get_user_model()
|
||||
from rest_framework import viewsets
|
||||
from api.serializers import DeviceSerializer,UserSerializer
|
||||
from upanel.models import *
|
||||
|
||||
from rest_framework.authentication import BasicAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
|
||||
|
||||
class DeviceViewSet(viewsets.ModelViewSet):
|
||||
authentication_classes = (BasicAuthentication,)
|
||||
permission_classes = (IsAuthenticated,)
|
||||
queryset = Device.objects.values().order_by('id')
|
||||
serializer_class = DeviceSerializer
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
authentication_classes = (BasicAuthentication,)
|
||||
permission_classes = (IsAuthenticated,)
|
||||
queryset = User.objects.values().order_by('id')
|
||||
serializer_class = UserSerializer
|
||||
Reference in New Issue
Block a user