Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

#!/usr/bin/env python 

 

""" 

@file ion/agents/instrumentagents/instrument_driver.py 

@author Steve Foley 

@author Edward Hunter 

@brief Instrument driver and client base classes. 

""" 

 

 

from twisted.internet import defer, reactor 

 

import ion.util.ionlog 

from ion.core.process.process import Process, ProcessClient 

 

 

 

log = ion.util.ionlog.getLogger(__name__) 

 

 

class InstrumentDriver(Process): 

    """ 

    Instrument driver base class for instrument specific driver subclasses. 

    """ 

 

    def op_execute(self, content, headers, msg): 

        """ 

        Execute a driver command. Commands may be 

        common or specific to the device, with specific commands known through 

        knowledge of the device or a previous get_capabilities query. 

        @param content A dict containing channels, command and optional timeout: 

            {'channels':[chan_arg,...,chan_arg], 

            'command':[command,arg,...,argN]), 

            'timeout':timeout} 

        @retval A reply message with a dict 

            {'success':success, 

            'result':{chan_arg:(success,command_specific_values),..., 

            chan_arg:(success,command_specific_values)}}.  

        """ 

 

 

    def op_get(self, content, headers, msg): 

        """ 

        Get configuration parameters from the device.  

        @param content A dict containing params and optional timeout: { 

            'params':[(chan_arg,param_arg),...,(chan_arg,param_arg)], 

            'timeout':timeout} 

        @retval A reply message with a dict 

            {'success':success,'result':{(chan_arg,param_arg):(success,val),... 

                ,(chan_arg,param_arg):(success,val)}}         

        """ 

 

 

    def op_set(self, content, headers, msg): 

        """ 

        Set parameters to the device. 

        @param content A dict containing param key-vals dict and optional 

            timeout: {'params':{(chan_arg,param_arg):val,..., 

            (chan_arg,param_arg):val},'timeout':timeout}. 

        @retval Reply message with a dict 

            {'success':success,'result': 

                {(chan_arg,param_arg):success,...,chan_arg,param_arg):success}}.         

        """ 

 

 

    def op_execute_direct(self, content, headers, msg): 

        """ 

        Execute untraslated commands on the device. 

        @param content A bytestring containing the raw device commands and 

            optional timeout: {'bytes':bytes,'timeout':timeout} 

        @retval Reply message with a dict containing success value and 

            raw bytes result. 

            {'success':success,'result':result}.         

        """ 

 

    def op_get_metadata(self, content, headers, msg): 

        """ 

        Retrieve metadata for the device, its transducers and parameters. 

        @param content A dict containing params list and optional timeout: 

            {'params':[(chan_arg,param_arg,meta_arg),..., 

            (chan_arg,param_arg,meta_arg)], 

            'timeout':timeout}. 

        @retval Reply message with a dict {'success':success,'result': 

                {(chan_arg,param_arg,meta_arg):(success,val),..., 

                chan_arg,param_arg,meta_arg):(success,val)}}.         

        """ 

 

 

    def op_get_status(self, content, headers, msg): 

        """ 

        Obtain the status of the device. This includes non-parameter 

        and non-lifecycle state of the instrument. 

        @param content A dict containing status params and optional timeout: 

            {'params':[(chan_arg,status_arg),...,(chan_arg,status_arg)], 

            'timeout':timeout}. 

        @retval A reply message with a dict 

            {'success':success,'result':{(chan_arg,status_arg):(success,val), 

                ...,chan_arg,status_arg):(success,val)}}. 

         """ 

 

    def op_get_capabilities(self, content, headers, msg): 

        """ 

        Obtain the capabilities of the device, including available commands, 

            parameters, channels and statuses, both common and device specific. 

        @param content A dict with capabilities params and optional timeout: 

            {'params':[cap_arg,...,cap_arg],'timeout':timeout}. 

        @retval A reply message with a dict 

            {'success':success,'result':{cap_arg:(success,val), 

                ...,cap_arg:(success,val)}}. 

         """ 

 

    def op_initialize(self, content, headers, msg): 

        """ 

        Restore driver to a default, unconfigured state. 

        @param content A dict with optional timeout {'timeout':timeout}. 

        @retval A reply message with a dict {'success':success,'result':None}. 

        """ 

 

 

    def op_configure(self, content, headers, msg): 

        """ 

        Configure the driver to establish communication with the device. 

        @param content a dict with configuration parameters and optional 

            timeout: {'params':{'cfg_arg':cfg_val,...,'cfg_arg':cfg_val}, 

            'timeout':timeout}. 

        @retval A reply message dict {'success':success,'result':content}. 

        """ 

 

 

    def op_connect(self, content, headers, msg): 

        """ 

        Establish connection to the device. 

        @param content A dict with optional timeout {'timeout':timeout}. 

        @retval A dict {'success':success,'result':None} giving the success 

            status of the connect operation. 

        """ 

 

 

    def op_disconnect(self, content, headers, msg): 

        """ 

        Close connection to the device. 

        @param content A dict with optional timeout {'timeout':timeout}. 

        @retval A dict {'success':success,'result':None} giving the success 

            status of the disconnect operation. 

        """ 

 

    def op_get_state(self,content,headers,msg): 

        """ 

        Retrive the current state of the driver. 

        @param content A dict with optional timeout {'timeout':timeout}. 

        @retval The current instrument state, from sbe37_state_list 

        (see ion.agents.instrumentagents.instrument_agent_constants. 

            device_state_list for the common states.) 

         """ 

 

 

class InstrumentDriverClient(ProcessClient): 

    """ 

    Driver client base class for instrument specific subclasses. 

    Provides RPC messaging to the driver service.     

    """ 

 

    @defer.inlineCallbacks 

    def execute(self,channels,command,timeout=None): 

        """ 

        Execute a driver command. Commands may be 

        common or specific to the device, with specific commands known through 

        knowledge of the device or a previous get_capabilities query. 

        @param channels a channel list [chan_arg,...,chan_arg] 

        @param command a command list[command,arg,...,argN] 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A reply message with a dict 

            {'success':success, 

            'result':{chan_arg:(success,command_specific_values),..., 

            chan_arg:(success,command_specific_values)}}.  

        """ 

 

        assert(isinstance(channels, (list,tuple))),'Expected list or tuple channels.' 

        assert(isinstance(command, (list,tuple))), 'Expected list or tuple command.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'channels':channels, 

                                'command':command,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('execute', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'channels':channels, 

                                'command':command,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('execute', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def get(self, params, timeout=None): 

        """ 

        Get configuration parameters from the device.  

        @param params a list [(chan_arg,param_arg),...,(chan_arg,param_arg)]. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A reply message with a dict 

            {'success':success,'result':{(chan_arg,param_arg):(success,val),... 

                ,(chan_arg,param_arg):(success,val)}}         

        """ 

 

        assert(isinstance(params, (list, tuple))), 'Expected a params list or tuple.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'params':params,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('get', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'params':params,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('get', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def set(self, params,timeout=None): 

        """ 

        Set parameters to the device. 

        @param params A dict {(chan_arg,param_arg):val,..., 

            (chan_arg,param_arg):val}. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval Reply message with a dict 

            {'success':success,'result': 

                {(chan_arg,param_arg):success,...,chan_arg,param_arg):success}}.         

        """ 

 

 

        assert(isinstance(params, dict)), 'Expected a params dict.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'params':params,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('set', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'params':params,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('set', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def execute_direct(self, bytes, timeout=None): 

        """ 

        Execute untraslated commands on the device. 

        @param bytes A bytestring containing the raw device commands. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval Reply message with a dict containing success value and 

            raw bytes result: {'success':success,'result':result}.         

        """ 

 

        assert(isinstance(bytes, str)), 'Expected a bytes string.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'bytes':bytes,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('execute_direct', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'bytes':bytes,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('execute_direct', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def get_metadata(self, params, timeout=None): 

        """ 

        Retrieve metadata for the device, its transducers and parameters. 

        @param params A list:[(chan_arg,param_arg,meta_arg),..., 

            (chan_arg,param_arg,meta_arg)] specifying the metadata to retrieve. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval Reply message with a dict {'success':success,'result': 

                {(chan_arg,param_arg,meta_arg):(success,val),..., 

                chan_arg,param_arg,meta_arg):(success,val)}}.         

        """ 

 

        assert(isinstance(params, (list, tuple))), 'Expected a params list or tuple.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'params':params,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('get_metadata', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'params':params,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('get_metadata', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def get_status(self, params, timeout=None): 

        """ 

        Obtain the status of the device. This includes non-parameter 

        and non-lifecycle state of the instrument. 

        @param params A list [(chan_arg,status_arg),..., 

            (chan_arg,status_arg)] specifying the status arguments to query. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A reply message with a dict 

            {'success':success,'result':{(chan_arg,status_arg):(success,val), 

                ...,chan_arg,status_arg):(success,val)}}. 

        """ 

 

        assert(isinstance(params, (list, tuple))), 'Expected a params list or tuple.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'params':params,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('get_status', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'params':params,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('get_status', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def get_capabilities(self, params, timeout=None): 

        """ 

        Obtain the capabilities of the device, including available commands, 

            parameters, channels and statuses, both common and device specific. 

        @param content A list [cap_arg,...,cap_arg] specifying the capability 

            arguments to query. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A reply message with a dict 

            {'success':success,'result':{cap_arg:(success,val), 

                ...,cap_arg:(success,val)}}. 

       """ 

 

        assert(isinstance(params, (list, tuple))), 'Expected a params list or tuple.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'params':params,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('get_capabilities', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'params':params,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('get_capabilities', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def initialize(self, timeout=None): 

        """ 

        Restore driver to a default, unconfigured state. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A reply message with a dict {'success':success,'result':None}. 

        """ 

 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('initialize', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'timeout':None} 

            (content, headers, message) = yield self.rpc_send('initialize', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def configure(self, params, timeout=None): 

        """ 

        Configure the driver to establish communication with the device. 

        @param params a dict containing required and optional 

            configuration parameters. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A reply message dict {'success':success,'result':content}. 

        """ 

        assert(isinstance(params, dict)), 'Expected a params dict.' 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'params':params,'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('configure', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'params':params,'timeout':None} 

            (content, headers, message) = yield self.rpc_send('configure', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def connect(self, timeout=None): 

        """ 

        Establish connection to the device. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A dict {'success':success,'result':None} giving the success 

            status of the connect operation. 

        """ 

 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('connect', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'timeout':None} 

            (content, headers, message) = yield self.rpc_send('connect', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def disconnect(self, timeout=None): 

        """ 

        Close connection to the device. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval A dict {'success':success,'result':None} giving the success 

            status of the disconnect operation. 

        """ 

 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('disconnect', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'timeout':None} 

            (content, headers, message) = yield self.rpc_send('disconnect', 

                                content_outgoing) 

 

        assert(isinstance(content, dict)), 'Expected a reply content dict.' 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def get_state(self, timeout=None): 

        """ 

        Retrive the current state of the driver. 

        @param timeout optional timeout to the driver causes the rpc timeout 

            to be set slightly longer. 

        @retval The current instrument state, from the instrument specific 

            state list. 

        (see ion.agents.instrumentagents.instrument_agent_constants. 

            device_state_list for the common states.) 

        """ 

 

        if timeout != None: 

            assert(isinstance(timeout, int)), 'Expected a timeout int.' 

            assert(timeout>0), 'Expected a positive timeout.' 

            rpc_timeout = timeout + 20 

            content_outgoing = {'timeout':timeout} 

            (content, headers, message) = yield self.rpc_send('get_state', 

                                content_outgoing,timeout=rpc_timeout) 

 

        else: 

            content_outgoing = {'timeout':None} 

            (content, headers, message) = yield self.rpc_send('get_state', 

                                content_outgoing) 

 

        assert(isinstance(content,str)), 'Expected a state string reply.' 

        defer.returnValue(content)