-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.py
102 lines (82 loc) · 2.87 KB
/
example.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
"""
Test example showing how to use the ihcsdk to connect to the ihc controller
To run the example create a file '.parameters' in this folder and add:
ihcurl username password resourceid
The resourceid is an ihc resource id of any boolean resource in you controller.
The resource will be toggled when the test starts, and after this you can set it
using '1' and '2'. 'q' to quit
"""
import logging
import sys
from datetime import datetime
from ihcsdk.ihccontroller import IHCController
_LOGGER = logging.getLogger(__name__)
def main():
"""Do the test"""
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# set log level below to debug for further troubleshooting
logging.getLogger("ihcsdk").setLevel( logging.INFO)
starttime = datetime.now()
def on_ihc_change(ihcid, value):
"""Callback when ihc resource changes"""
print(
"Resource change " + str(ihcid) + "->" + str(value) + " time: " + gettime()
)
def gettime():
dif = datetime.now() - starttime
return str(dif)
cmdline = open(".parameters", "rt").read()
args = cmdline.split(" ")
if len(args) < 4:
print(
"The '.parameters' file should contain: ihcurl username password resourceid"
)
exit()
url = args[0]
username = args[1]
password = args[2]
resid = int(args[3])
if not IHCController.is_ihc_controller(url):
print("The device in this url does not look like a IHC controller")
exit()
print("Url response like a IHC controller - now authenticating")
ihc = IHCController(url, username, password)
if not ihc.authenticate():
print("Authenticate failed")
exit()
print("Authenticate succeeded\r\n")
# read the ihc project
project = ihc.get_project()
if project is False:
print("Failed to read project")
else:
print("Project downloaded successfully")
log = ihc.client.get_user_log()
if log:
print("log: " + log)
info = ihc.client.get_system_info()
print(info)
runtimevalue = ihc.get_runtime_value(resid)
print("Runtime value: " + str(runtimevalue))
ihc.set_runtime_value_bool(resid, not runtimevalue)
runtimevalue = ihc.get_runtime_value(resid)
print("Runtime value: " + str(runtimevalue))
ihc.client.enable_runtime_notification(resid)
changes = ihc.client.wait_for_resource_value_changes(10)
print(repr(changes))
ihc.add_notify_event(resid, on_ihc_change, True)
while True:
i = input()
if i == "1":
starttime = datetime.now()
ihc.set_runtime_value_bool(resid, False)
continue
if i == "2":
starttime = datetime.now()
ihc.set_runtime_value_bool(resid, True)
continue
if i == "q":
break
ihc.disconnect()
ihc.client.connection.session.close()
main()