-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.py
77 lines (64 loc) · 2.13 KB
/
miner.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
class Miner:
def __init__(self, name, ip, pin):
self.name = name
self.ip = ip
self.pin = pin
self.setPinOutMode()
def ping(self):
import os
import subprocess
from platform import system as system_name # Returns the system/OS name
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""
# Ping command count option as function of OS
param = '-n' if system_name().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '3', self.ip]
# Pnging
with open(os.devnull, 'w') as DEVNULL:
try:
subprocess.check_call(
command,
stdout=DEVNULL, # suppress output
stderr=DEVNULL
)
is_up = True
except subprocess.CalledProcessError:
is_up = False
return is_up
def shutDown(self):
import os
import time
os.system("gpio write " + str(self.pin) + " 0")
time.sleep(5)
os.system("gpio write " + str(self.pin) + " 1")
return True
def turnOn(self):
import os
import time
os.system("gpio write " + str(self.pin) + " 0")
time.sleep(0.1)
os.system("gpio write " + str(self.pin) + " 1")
return True
def restart(self):
import time
self.shutDown()
self.turnOn()
return True
def setPinOutMode(self):
import os
print self.getDate() + " GPIO: " + str(self.pin) + " mode out, def val: 1"
os.system("gpio mode " + str(self.pin) + " out")
os.system("gpio write " + str(self.pin) + " 1")
return True
def getStatus(self):
if self.ping() == True:
return "on"
else:
return "off"
@staticmethod
def getDate():
import datetime
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ")