-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpyc_service.py
114 lines (102 loc) · 2.95 KB
/
rpyc_service.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
import os
import sys
import time
import rpyc
import thread
import platform
if '{} {}'.format(platform.system(), platform.release()) != 'Linux 3.16.0-4-amd64':
import RPi.GPIO as GPIO
from log import log
WINDOW_OPEN_PIN = 5
WINDOW_CLOSE_PIN = 6
WINDOW_STATUS_PIN = 26
# Close = False
# Open = True
status = False
WINDOW_STATUS = {False: 'Close', True: 'Open'}
device_sn = 'test'
connection = None
is_busy = False
def init_window():
log.info('Initializing the raspberry pi pins')
log.warning('Initializing the raspberry pi pins')
GPIO.setmode(GPIO.BCM)
GPIO.setup(WINDOW_OPEN_PIN, GPIO.OUT)
GPIO.setup(WINDOW_CLOSE_PIN, GPIO.OUT)
GPIO.output(WINDOW_OPEN_PIN, GPIO.LOW)
GPIO.output(WINDOW_CLOSE_PIN, GPIO.LOW)
#GPIO.setup(WINDOW_STATUS_PIN, GPIO.IN)
# taking input value from Pin 11
#input_value = GPIO.input(11)
#GPIO.output(13, GPIO.LOW)
def is_open():
"""
Check if the window is open or closed.
@return: True or False
"""
return WINDOW_STATUS[status]
def open_window(conn):
"""
Opens the window.
@return: True or False
"""
global status
log.warning('window opens')
print 'window started to open'
GPIO.output(WINDOW_CLOSE_PIN, GPIO.LOW)
GPIO.output(WINDOW_OPEN_PIN, GPIO.HIGH)
time.sleep(7)
status = True
GPIO.output(WINDOW_OPEN_PIN, GPIO.LOW)
global is_busy
is_busy = False
print 'window opened'
conn.root.action_finished(device_sn, 'open')
def close_window(conn):
"""
Closes the window.
@return: True or False
"""
global status
log.warning('window closes')
print 'window started to close'
GPIO.output(WINDOW_OPEN_PIN, GPIO.LOW)
GPIO.output(WINDOW_CLOSE_PIN, GPIO.HIGH)
time.sleep(7)
status = False
GPIO.output(WINDOW_CLOSE_PIN, GPIO.LOW)
global is_busy
is_busy = False
print 'window closed'
conn.root.action_finished(device_sn, 'close')
class RTUService(rpyc.Service):
def exposed_get_status(self):
return is_open()
def exposed_open_window(self):
log.warning('open_window from server')
global is_busy
is_busy = True
# log.warning('~~ is_busy: %s', is_busy)
try:
thread.start_new_thread(open_window, (connection,))
except Exception as err:
log.error('thread did not started: %s', err)
return True
def exposed_close_window(self):
log.warning('close window from server')
global is_busy
is_busy = True
# log.warning('~~ is_busy: %s', is_busy)
try:
thread.start_new_thread(close_window, (connection,))
except Exception as err:
log.error('thread did not started: %s', err)
return True
def exposed_get_uuid(self):
return device_sn
def exposed_is_busy(self):
return is_busy
def connect():
global connection
connection = rpyc.connect('gen8.doraz.ro', 8010, service=RTUService)
return connection