-
Notifications
You must be signed in to change notification settings - Fork 26
/
hardware_plugin.py
189 lines (150 loc) · 7.27 KB
/
hardware_plugin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# ===============================================================================
# Copyright 2013 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
# ============= enthought library imports =======================
from traits.api import HasTraits, Bool, Instance, List, Dict
# from traitsui.api import View, Item
from pychron.core.helpers.strtools import to_bool
from pychron.envisage.tasks.base_task_plugin import BaseTaskPlugin
from envisage.extension_point import ExtensionPoint
# from pychron.managers.hardware_manager import HardwareManager
# from pychron.remote_hardware.remote_hardware_manager import RemoteHardwareManager
from pychron.hardware.flag_manager import FlagManager
# from apptools.preferences.preference_binding import bind_preference
from pychron.hardware.core.i_core_device import ICoreDevice
from envisage.ui.tasks.task_factory import TaskFactory
from pychron.hardware.tasks.hardware_task import HardwareTask
from envisage.ui.tasks.task_extension import TaskExtension
from pyface.action.action import Action
from pyface.tasks.action.schema_addition import SchemaAddition
from pychron.hardware.tasks.hardware_preferences import HardwarePreferencesPane
from apptools.preferences.preference_binding import bind_preference
# ============= standard library imports ========================
# ============= local library imports ==========================
class Preference(HasTraits):
pass
class SerialPreference(Preference):
auto_find_handle = Bool
auto_write_handle = Bool
class DevicePreferences(HasTraits):
serial_preference = Instance(SerialPreference)
def _serial_preference_default(self):
return SerialPreference()
class OpenFlagManagerAction(Action):
name = 'Flag Manager'
def perform(self, event):
app = event.task.window.application
man = app.get_service('pychron.hardware.flag_manager.FlagManager')
app.open_view(man)
class HardwarePlugin(BaseTaskPlugin):
id = 'pychron.hardware.plugin'
managers = ExtensionPoint(List(Dict),
id='pychron.hardware.managers')
# my_managers = List(contributes_to='pychron.hardware.managers')
sources = List(contributes_to='pychron.video.sources')
# def _my_managers_default(self):
# return [dict(name='hardware', manager=self._hardware_manager_factory())]
# def _system_lock_manager_factory(self):
# return SystemLockManager(application=self.application)
enable_hardware_server = Bool
_remote_hardware_manager = Instance('pychron.remote_hardware.remote_hardware_manager.RemoteHardwareManager')
# _hardware_manager = Instance('pychron.managers.hardware_manager.HardwareManager')
def start(self):
# if self.managers:
from pychron.envisage.initialization.initializer import Initializer
dp = DevicePreferences()
afh = self.application.preferences.get('pychron.hardware.auto_find_handle')
awh = self.application.preferences.get('pychron.hardware.auto_write_handle')
if afh is not None:
# toBool = lambda x: True if x == 'True' else False
dp.serial_preference.auto_find_handle = to_bool(afh)
dp.serial_preference.auto_write_handle = to_bool(awh)
ini = Initializer(device_prefs=dp)
for m in self.managers:
ini.add_initialization(m)
# any loaded managers will be registered as services
if not ini.run(application=self.application):
self.application.exit()
# self.application.starting
return
# create the hardware server
# rhm = self.application.get_service(RemoteHardwareManager)
# bind_preference(rhm, 'enable_hardware_server', 'pychron.hardware.enable_hardware_server')
# bind_preference(rhm, 'enable_directory_server', 'pychron.hardware.enable_directory_server')
# rhm.bootstrap()
bind_preference(self, 'enable_hardware_server', 'pychron.hardware.enable_hardware_server')
if self.enable_hardware_server:
from pychron.remote_hardware.remote_hardware_manager import RemoteHardwareManager
rhm = RemoteHardwareManager(application=self.application)
rhm.bootstrap()
self._remote_hardware_manager = rhm
def stop(self):
if self._remote_hardware_manager:
self._remote_hardware_manager.kill()
# if self._hardware_manager:
# self._hardware_manager.kill()
if self.managers:
for m in self.managers:
man = m['manager']
if man:
man.kill()
# man.close_ui()
for s in self.application.get_services(ICoreDevice):
if s.is_scanable:
s.stop_scan()
#if s._scanning and not s._auto_started:
# s.save_to_db()
def _factory(self):
# man = self.application.get_service(HardwareManager)
# man = HardwareManager(application=self.application)
task = HardwareTask()
# self._hardware_manager = man
return task
def _flag_manager_factory(self):
return FlagManager(application=self.application)
# def _hardware_manager_factory(self):
# return HardwareManager(application=self.application)
# def _remote_hardware_manager_factory(self):
# return RemoteHardwareManager(application=self.application)
def _service_offers_default(self):
# so_hm = self.service_offer_factory(
# protocol=HardwareManager,
# factory=self._hardware_manager_factory)
#
# so_rhm = self.service_offer_factory(
# protocol=RemoteHardwareManager,
# factory=self._remote_hardware_manager_factory)
so_fm = self.service_offer_factory(
protocol=FlagManager,
factory=self._flag_manager_factory)
# return [so, so1, so2]
# return [so_hm, so_rhm, so_fm]
# return [so_hm, so_fm]
return [so_fm]
def _preferences_panes_default(self):
return [HardwarePreferencesPane]
def _sources_default(self):
return [('pvs://localhost:1081', 'Hardware')]
def _task_extensions_default(self):
return [TaskExtension(actions=[SchemaAddition(id='Flag Manager',
factory=OpenFlagManagerAction,
path='MenuBar/tools.menu'), ])]
def _tasks_default(self):
return [TaskFactory(id='tasks.hardware',
name='Hardware',
factory=self._factory,
image='configure-2',
task_group='hardware')]
# ============= EOF =============================================