-
Notifications
You must be signed in to change notification settings - Fork 0
/
devicesModule.py
124 lines (111 loc) · 3.19 KB
/
devicesModule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sqlite3
import subprocess
conn = sqlite3.connect('database')
c = conn.cursor()
def addDevice(ip,deviceName,deviceType,parent):
conn = sqlite3.connect('database')
c = conn.cursor()
try:
c.execute("INSERT INTO deviceList(deviceIp,deviceName,deviceType,parent) VALUES(?,?,?,?)",(ip,deviceName,deviceType,parent))
conn.commit()
return True
except:
return False
def deleteDevice(id):
conn = sqlite3.connect('database')
c = conn.cursor()
try:
c.execute("DELETE FROM deviceList WHERE ID=(?)",[id])
conn.commit()
return True
except:
return False
def fetchDeviceList():
conn = sqlite3.connect('database')
c = conn.cursor()
data = c.execute('SELECT * FROM deviceList')
res = []
for i in data:
res.append({
'id' : i[0],
'ip' : i[1],
'name' : i[2],
'type' : i[3],
'parent' : i[4]
})
return res
def addDeviceType(deviceType):
conn = sqlite3.connect('database')
c = conn.cursor()
try:
c.execute("INSERT INTO deviceType(deviceTypeName) VALUES(?)",[deviceType])
conn.commit()
return True
except:
return False
def fetchDeviceTypeList():
conn = sqlite3.connect('database')
c = conn.cursor()
output = c.execute("SELECT * FROM deviceType")
res = []
for i in output:
res.append({
'id' : i[0],
'deviceTypeName' : i[1]
})
return res
def deleteDeviceType(id):
conn = sqlite3.connect('database')
c = conn.cursor()
try:
c.execute("DELETE FROM deviceType WHERE ID=(?)",[id])
conn.commit()
return True
except:
return False
def getStatus(all_hosts):
status = []
color = []
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
for i in range(len(all_hosts)):
output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]
if "Destination host unreachable" in output.decode('utf-8'):
status.append("offline")
color.append('#f00')
elif "Request timed out" in output.decode('utf-8'):
status.append("offline")
color.append('#f00')
else:
status.append("online")
color.append('#0f0')
return status , color
def checkOnline():
conn = sqlite3.connect('database')
c = conn.cursor()
output = c.execute("SELECT * FROM deviceList")
Id = []
ip = []
name = []
Type = []
parent = []
for i in output:
Id.append(i[0])
ip.append(i[1])
name.append(i[2])
Type.append(i[3])
parent.append(i[4])
deviceStatus , color = getStatus(ip)
liveStatus = []
for i in range(len(ip)):
liveStatus.append({
'id' : Id[i],
'ip' : ip[i],
'name' : name[i],
'type' : Type[i],
'parent' : parent[i],
'status' : deviceStatus[i],
'color' : color[i]
})
return liveStatus