-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpylink.py
More file actions
executable file
·112 lines (92 loc) · 3.37 KB
/
pylink.py
File metadata and controls
executable file
·112 lines (92 loc) · 3.37 KB
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
#!/usr/bin/python
#
# sudo dpkg-reconfigure tzdata
# sudo apt-get install libyaml-cpp0.3
# sudo pip-3.2 install datetime
# sudo pip-3.2 install collections
# sudo pip-3.2 install pyaml
# sudo pip-3.2 install apscheduler
# 2:
# sudo easy_install apscheduler
#
# TODO:
# - Check config params are defined
# - Add config to update every second
# When run in directory containing downloaded PyIsy
import sys
sys.path.insert(0,".")
sys.path.insert(0,"../PyISY")
# Load our dependancies
import re
import requests
import logging
import yaml
from datetime import datetime
#from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
import PyISY
from asus import AsusDeviceScanner
from PylDevice import PylDevice
from PylREST import PylREST
# Load the config file.
config_file = open('config.yaml', 'r')
config = yaml.load(config_file)
config_file.close
# Start the log_file
print('pylink: Started: %s' % datetime.now())
logfile = config['log_file']
print('pylink: Writing to log: ' + config['log_file'])
logging.basicConfig(filename=config['log_file']);
logger = logging.getLogger()
logger.setLevel(0)
info = "Startinsg PyISY: host=" + config['isy_host'] + " PORT=" + str(config['isy_port']) + "log=" + logfile
logger.info(info)
isy = PyISY.ISY(config['isy_host'], config['isy_port'], config['isy_user'], config['isy_password'], False, "1.1", logger)
logger.info("Connected: " + str(isy.connected))
isy.auto_update = True
def second_function():
print('second_function: The time is: %s' % datetime.now())
def minute_function():
logger.debug('minute_function: The minute is: %s' % datetime.now().minute)
isy.variables[2]['s.PyISY.Minute'].val = datetime.now().minute
def router_check():
sc = AsusDeviceScanner(config['router_host'],config['router_user'],config['router_password'])
if sc.client_connected("android-8c4066f") is True:
isy.variables[2]['s.OC.JimConnected.Router'].val = 1
else:
isy.variables[2]['s.OC.JimConnected.Router'].val = 0
def hour_function():
logger.debug('hour_function: The hour is: %s' % datetime.now().hour)
isy.variables[2]['s.PyISY.Hour'].val = datetime.now().hour
def day_function():
dt = datetime.now()
print('day_function: It is a new day! The time is: %s' % dt)
isy.variables[2]['s.PyISY.Day'].val = dt.day
isy.variables[2]['s.PyISY.Month'].val = dt.month
isy.variables[2]['s.PyISY.Year'].val = dt.year
# Initialize all on startup
minute_function()
hour_function()
day_function()
#router_check()
sched = BackgroundScheduler()
# Schedules second_function to be run at the change of each second.
#sched.add_job(second_function, 'cron', second='0-59')
# Schedules minute_function to be run at the change of each minute.
sched.add_job(minute_function, 'cron', second='0')
# Schedules hour_function to be run at the change of each hour.
sched.add_job(hour_function, 'cron', minute='0', second='0')
# Schedules day_function to be run at the start of each day.
sched.add_job(day_function, 'cron', minute='0', second='0', hour='0')
#sched.add_job(router_check, 'cron', minute="10,20,30,40,50")
sched.start()
print("Configuring devices...")
#print config['devices']
# Setup the devices
devices = []
for device in config['devices']:
# dict by ip for lookup
devices.append(PylDevice(device,isy,sched))
print("Starting REST interface...")
rest = PylREST(devices)
rest.run()