forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
155 lines (143 loc) · 3.87 KB
/
misc.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
#############################################################################
################### Miscellenous function for RPIEasy #######################
#############################################################################
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import platform
import rpieGlobals
from datetime import datetime
import Settings
import socket
supportedsys = ['Not supported', 'Linux-apt (partially supported)', 'Linux-pacman (experimental support)','OPI-Linux-armbian (experimental)','-Reserved','-Reserved','-Reserved','-Reserved','-Reserved','-Reserved','RPI-Linux-apt (supported)']
SystemLog = []
def getosname(lvl=0):
if lvl==0:
return platform.system().lower()
elif lvl==1:
return platform.release().lower()
elif lvl==2:
try:
return platform.linux_distribution()[0].lower()
except:
return ""
def getsupportlevel(of=0):
global supportedsys
if getosname(0)=="linux":
import linux_os
lvl = 0
if rpieGlobals.osinuse=="linux":
if (linux_os.is_command_found('dpkg')) and (linux_os.is_command_found('apt')):
lvl = 1
if linux_os.checkRPI():
lvl = 10
elif linux_os.checkOPI():
lvl = 3
elif (linux_os.is_command_found('pacman')):
lvl = 2
if of == 0:
return supportedsys[lvl]
else:
return lvl
def WebLog(lvl,logstamp, line):
global SystemLog
if len(SystemLog)>rpieGlobals.LOG_MAXLINES:
tvar = []
for i in range(0,10):
SystemLog[i]=SystemLog[rpieGlobals.LOG_MAXLINES-10+i]
SystemLog = tvar
SystemLog.append({"t":logstamp,"l":line,"lvl":lvl})
def addLog(logLevel, line):
lstamp = datetime.now().strftime('%H:%M:%S')
if int(logLevel)<=int(Settings.AdvSettings["webloglevel"]): # if weblogging enabled
WebLog(logLevel,lstamp,line)
if int(logLevel)<=int(Settings.AdvSettings["sysloglevel"]): # if syslogging enabled
lstamp2 = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z')
SysLog(logLevel,lstamp2,line)
if int(logLevel)<=int(Settings.AdvSettings["consoleloglevel"]): # if console logging enabled
if logLevel==rpieGlobals.LOG_LEVEL_ERROR:
lstamp += ": !"
else:
lstamp += ": "
print(lstamp+line)
def udpsender(destip,data,dport=514,retrynum=1):
if destip != "":
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if type(data) is bytes:
dsend = data
elif type(data) is str:
dsend = bytes(data,"utf-8")
else:
dsend = bytes(data)
for r in range(retrynum):
try:
s.sendto(dsend, (destip,int(dport)))
if r<retrynum-1:
time.sleep(0.1)
except:
print("UDP send failed with network error.")
def SysLog(lvl,logstamp,line):
slip = ""
try:
slip = Settings.AdvSettings["syslogip"]
except:
slip = ""
if slip != "":
facility = 0
prio = (facility*8)
if (lvl == rpieGlobals.LOG_LEVEL_ERROR):
prio += 3
elif (lvl == rpieGlobals.LOG_LEVEL_INFO):
prio += 5
else:
prio += 7
lstr = "<"+str(prio)+">RPIEasy "+str(Settings.Settings["Name"])+": "+str(line)
udpsender(slip,lstr)
else:
print("Syslog IP is empty!")
def str2num(data):
try:
data + ''
return float(data.replace(',','.'))
except TypeError:
return data
def str2num2(data):
try:
return round(str2num(data),2)
except:
return data
def formatnum(num,decimal):
res = ""
try:
decimal = int(decimal)
except:
decimal=0
try:
num=float(num)
except:
pass
if decimal<0:
res = str(num)
else:
nformat = "{0:."+str(decimal)+"f}"
try:
res = nformat.format(num)
except:
res = num
return res
def get_battery_value():
bval = 255
try:
if Settings.AdvSettings["battery"]["enabled"]:
bval = float(Settings.Tasks[int(Settings.AdvSettings["battery"]["tasknum"])].uservar[int(Settings.AdvSettings["battery"]["taskvaluenum"])])
else:
bval = 255
except:
bval = 255
if bval!=255:
if bval<0:
bval = 0
elif bval>100:
bval = 100
return bval