-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentModel.py
More file actions
132 lines (100 loc) · 3.34 KB
/
Copy pathagentModel.py
File metadata and controls
132 lines (100 loc) · 3.34 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#源代码3-2
class InteractiveObject:
pass
class InteractiveObjectImplA:
pass
class InteractiveObjectImplB:
pass
class Meditor:
def __init__(self):
self.__interactiveObjA = InteractiveObjectImplA()
self.__interactiveObjB = InteractiveObjectImplB()
def interactive(self):
print("interactive with A and B")
pass
#源代码3-3
from abc import ABCMeta, abstractmethod
from enum import Enum
class DeviceType(Enum):
TypeSpeaker = 1
TypeMicrophone = 2
TypeCamera = 3
class DeviceItem:
def __init__(self, id, name, type, isDefault=False):
self.__id = id
self.__name = name
self.__type = type
self.__isDefault = isDefault
def __str__(self):
return " type: " + str(self.__type) + "id: " + str(self.__id) + " name: " + self.__name + " isDefault: " + str(self.__isDefault)
def getId(self):
return self.__id
def getName(self):
return self.__name
def getType(self):
return self.__type
def isDefault(self):
return self.__isDefault
class DeviceList:
def __init__(self):
self.__devices = []
def add(self, deviceItem):
self.__devices.append(deviceItem)
def getCount(self):
return len(self.__devices)
def getByIndex(self, index):
if index < 0 or index >= len(self.__devices):
return None
return self.__devices[index]
def getById(self, id):
for d in self.__devices:
if d.getId() == id:
return d
return None
class DeviceMgr(metaclass=ABCMeta):
@abstractmethod
def enumerate(self):
pass
@abstractmethod
def active(self, deviceId):
pass
@abstractmethod
def getCurDeviceId(self):
pass
class SpeakerMgr(DeviceMgr):
def __init__(self):
self.__curDeviceId = None
def enumerate(self):
devices = DeviceList()
devices.add(DeviceItem("id 369-2-2-2-3","Realtek High Definition Audio", DeviceType.TypeSpeaker))
devices.add(DeviceItem("id 593-23-23-23-41","NVIDIA High Definition Audio", DeviceType.TypeSpeaker, True))
return devices
def active(self, deviceId):
self.__curDeviceId = deviceId
def getCurDeviceId(self):
return self.__curDeviceId
class DeviceUtil:
def __init__(self):
self.__mgrs = {}
self.__mgrs[DeviceType.TypeSpeaker] = SpeakerMgr()
# self.__microphoneMgr = MicrophoneMgr()
def __getDeviceMgr(self, type):
return self.__mgrs[type]
def getDeviceList(self, type):
mgr = self.__getDeviceMgr(type)
return mgr.enumerate()
def active(self, type, deviceId):
mgr = self.__getDeviceMgr(type)
mgr.active(deviceId)
def getCurDeviceId(self, type):
mgr = self.__getDeviceMgr(type)
return mgr.getCurDeviceId()
def testDevices():
deviceUtil = DeviceUtil()
deviceList = deviceUtil.getDeviceList(DeviceType.TypeSpeaker)
print("Speaker设备列表:")
if deviceList.getCount() > 0:
deviceUtil.active(DeviceType.TypeSpeaker, deviceList.getByIndex(0).getId())
for idx in range(deviceList.getCount()):
print(deviceList.getByIndex(idx))
print("当前使用的Speaker设备id: " + str(deviceUtil.getCurDeviceId(DeviceType.TypeSpeaker)))