-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.py
162 lines (130 loc) · 5.75 KB
/
Device.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# MultimodalMiddlewareProtocol
# Copyright (C) 2008 Nilo Menezes - Multitel ASBL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import traceback
from MultimodalMiddlewareProtocol import Server, Fields, FieldFactory
from MultimodalMiddlewareProtocol.util.Hashes import HashLong
from MultimodalMiddlewareProtocol.util.Streamers import Reader, Writer
class MultimodalDevice (Server.IClient):
MMDevice_Init = 0
MMDevice_Registering = 1
MMDevice_Registered = 2
MMDevice_Disconnecting = 3
MMDevice_Disconnected = 4
# Creates a new instance of MultimodalDevice
def __init__(self, name, server, deviceID):
self.name = name
self.produces = []
self.consumes = []
self.clients = HashLong()
self.suppliers = HashLong()
self.server = server
self.deviceID= deviceID
self.eventProcessors = {}
server.setReceiver(self)
self.state = 0
self.UCID = 0
#self.eventProcessors.add()
self.eventProcessors[0xF0000001L] = self.confirmSubscription
def log(self, text):
print "LOG: %s" % (text)
def register(self):
self.state = MultimodalDevice.MMDevice_Registering
#MMArrayField fProduces, fConsumes;
record = FieldFactory.fieldFromCodeList("LLLAAs")
record[0].set(self.UCID)
record[1].set(self.deviceID)
record[2].set(0xF0000000L) # EventID
record[3].copyLongVector(self.produces)
record[4].copyLongVector(self.consumes)
record[5].set(self.name) # Device Name
self.server.sendFields(None, record)
##########################################################
#modified by ND
def disconnect(self):
self.state = MultimodalDevice.MMDevice_Disconnecting
record = FieldFactory.fieldFromCodeList("LLLI")
record[0].set(self.UCID)
record[1].set(self.deviceID)
record[2].set(0xF0000006L) # EventID for disconnection
record[3].set(0)
self.server.sendFields(None, record)
#modified by ND
##########################################################
def confirmSubscription(self, command):
record = FieldFactory.fieldFromCodeList("LLLLAAL")
fProduces = record[4]
fConsumes = record[5]
try:
FieldFactory.readFields(record, command.dataIn)
self.deviceId = command.mDeviceID.get()
self.UCID = record[3].get()
for x in fProduces.elements:
self.clients.addOrUpdate(x, self.UCID) # Add eventID - UCID to producers list
for x in fConsumes.elements:
self.suppliers.addOrUpdate(x, self.UCID) # Add eventID - UCID to consumers list
self.state = MultimodalDevice.MMDevice_Registered
except Exception:
raise
finally:
record = None
fProduces = None
fConsumes = None
def setName(self, name):
self.name = name
def getName(self):
return self.name
def receive(self, sender, data):
dataIn = Reader(data[:]) # Copies data to dataIn
mUCID = Fields.LongField()
mDeviceID= Fields.LongField()
mEventID= Fields.LongField()
try:
if(data==None):
self.log("Data received as null")
return
if(sender==None):
self.log("Sender received as null")
return
if(len(data)<9):
self.log("Datagram with incorrect size received: %d" % (len(data)))
return
mUCID.read(dataIn)
mDeviceID.read(dataIn)
mEventID.read(dataIn)
#print "UCID: %s DeviceID: %s EventID: %s" % (mUCID.get(), mDeviceID.get(), mEventID.get())
dataIn.reset(); # As 3 fields were read... rewind the stream
try:
#print self.eventProcessors
#print mEventID.get()
if self.eventProcessors.has_key(mEventID.get()):
command = self.eventProcessors[mEventID.get()]
command(Server.CommandHeader(mUCID, mDeviceID, mEventID, sender, dataIn))
else: # If a standard command processor is not available, try to find a forwarder
forwarder = self.eventProcessors[0]
forwarder(Server.CommandHeader(mUCID, mDeviceID, mEventID, sender, dataIn))
except:
traceback.print_exc()
print("Unhandled EventID: %s received from UCID: %s" % (mEventID.get(), mUCID.get()))
except:
self.log(sys.exc_info()[0])
raise
finally:
dataIn=None
mUCID=None
mDeviceID=None
mEventID=None