forked from netik/rpc3control
-
Notifications
You must be signed in to change notification settings - Fork 4
/
off.py
65 lines (57 loc) · 2.1 KB
/
off.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
#!/usr/bin/env python
import sys
import re
import syslog
import time
import os
import errno
import datetime
import fcntl
import shutil
from rpc3Control import *
RPC=None
#RPCUSER= str(sys.argv[2])
RPCUSER= None
RPCPASS=None
OUTLET= int(sys.argv[1])
def file_exists(path, filename):
for file_or_folder in os.listdir(path):
if file_or_folder == filename:
return True
return False
(RPC, RPCUSER, RPCPASS, WHITELIST) = load_credentials("/var/homebridge/rpc3control/.credentials")
RPCUSER= None
#RPCUSER= str(sys.argv[2])
#telnet into unit and turn off outlet
r = rpc3Control(RPC, RPCUSER)
r.outlet(OUTLET, 'off')
lock_filename = 'telnetrunning.txt'
fileexists = file_exists("/var/homebridge/rpc3control/", lock_filename)
#if status file exists, read it and update timestamp and outlet status for the turned off outlet. Also mark the file with updated status flag.
if (fileexists):
my_file = open(lock_filename,'r')
statearray = [line.split(',') for line in my_file]
sttime = str(int(round(time.time() * 1000)))
statearray[0][0] = sttime
statearray[0][1] = "updated"
statearray[OUTLET][1] = "False"
my_file.close()
with open(lock_filename,'w') as my_file:
while True:
try:
fcntl.flock(my_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except IOError as e:
# raise on unrelated IOErrors
if e.errno != errno.EAGAIN:
raise
else:
time.sleep(0.05)
my_file.write(statearray[0][0] + "," + statearray[0][1] + ",\r\n")
i=1
while i<=8:
my_file.write(statearray[i][0] + "," + statearray[i][1] + "," + statearray[i][2])
i=i+1
fcntl.flock(my_file, fcntl.LOCK_UN)
my_file.close()
sys.exit()