AttributeCombiner.cpp 41.2 KB
Newer Older
Jean-Luc Pons's avatar
Jean-Luc Pons committed
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
/*----- PROTECTED REGION ID(AttributeCombiner.cpp) ENABLED START -----*/
//=============================================================================
//
// file :        AttributeCombiner.cpp
//
// description : C++ source for the AttributeCombiner class and its commands.
//               The class is derived from Device. It represents the
//               CORBA servant object which will be accessed from the
//               network. All commands which can be executed on the
//               AttributeCombiner are implemented in this file.
//
// project :     AttributeCombiner
//
// This file is part of Tango device class.
// 
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Tango 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Tango.  If not, see <http://www.gnu.org/licenses/>.
// 
//
// Copyright (C): 2018
//                European Synchrotron Radiation Facility
//                BP 220, Grenoble 38043
//                France
//
//=============================================================================
//                This file is generated by POGO
//        (Program Obviously used to Generate tango Object)
//=============================================================================


#include <AttributeCombiner.h>
#include <AttributeCombinerClass.h>

#define SAFE_FREE(x) if(x) {free(x);x=NULL;}

/*----- PROTECTED REGION END -----*/	//	AttributeCombiner.cpp

/**
 *  AttributeCombiner class description:
 *    A class to combine attributes coming from several devices
 */

//================================================================
//  The following table gives the correspondence
//  between command and method names.
//
57
//  Command name       |  Method name
Jean-Luc Pons's avatar
Jean-Luc Pons committed
58
//================================================================
59
60
61
62
63
64
65
66
//  State              |  Inherited (no method)
//  Status             |  Inherited (no method)
//  ResetError         |  reset_error
//  UpdateSetpoint     |  update_setpoint
//  EnableDevice       |  enable_device
//  DisableDevice      |  disable_device
//  Indice2Devicename  |  indice2_devicename
//  Devicename2Indice  |  devicename2_indice
67
68
//  DisableAll         |  disable_all
//  EnableAll          |  enable_all
Jean-Luc Pons's avatar
Jean-Luc Pons committed
69
70
71
//================================================================

//================================================================
72
//  Attributes managed are:
Jean-Luc Pons's avatar
Jean-Luc Pons committed
73
//================================================================
74
75
//  Errors      |  Tango::DevString	Spectrum  ( max = 2048)
//  DeviceList  |  Tango::DevString	Spectrum  ( max = 4096)
Jean-Luc Pons's avatar
Jean-Luc Pons committed
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
//================================================================

namespace AttributeCombiner_ns
{
/*----- PROTECTED REGION ID(AttributeCombiner::namespace_starting) ENABLED START -----*/

//	static initializations

/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::namespace_starting

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::AttributeCombiner()
 *	Description : Constructors for a Tango device
 *                implementing the classAttributeCombiner
 */
//--------------------------------------------------------
AttributeCombiner::AttributeCombiner(Tango::DeviceClass *cl, string &s)
 : TANGO_BASE_CLASS(cl, s.c_str())
{
	/*----- PROTECTED REGION ID(AttributeCombiner::constructor_1) ENABLED START -----*/
	init_device();
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::constructor_1
}
//--------------------------------------------------------
AttributeCombiner::AttributeCombiner(Tango::DeviceClass *cl, const char *s)
 : TANGO_BASE_CLASS(cl, s)
{
	/*----- PROTECTED REGION ID(AttributeCombiner::constructor_2) ENABLED START -----*/
	init_device();
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::constructor_2
}
//--------------------------------------------------------
AttributeCombiner::AttributeCombiner(Tango::DeviceClass *cl, const char *s, const char *d)
 : TANGO_BASE_CLASS(cl, s, d)
{
	/*----- PROTECTED REGION ID(AttributeCombiner::constructor_3) ENABLED START -----*/
	init_device();
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::constructor_3
}

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::delete_device()
 *	Description : will be called at device destruction or at init command
 */
//--------------------------------------------------------
void AttributeCombiner::delete_device()
{
	DEBUG_STREAM << "AttributeCombiner::delete_device() " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::delete_device) ENABLED START -----*/
  delete deviceGroup;

	if (Tango::Util::instance()->is_svr_shutting_down() ||
			Tango::Util::instance()->is_device_restarting(get_name())) {
		attMap.clear();
	}

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::delete_device
138
	delete[] attr_DeviceList_read;
Jean-Luc Pons's avatar
Jean-Luc Pons committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
}

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::init_device()
 *	Description : will be called at device initialization.
 */
//--------------------------------------------------------
void AttributeCombiner::init_device()
{
	DEBUG_STREAM << "AttributeCombiner::init_device() create device " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::init_device_before) ENABLED START -----*/
	
	//	Initialization before get_device_property() call
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::init_device_before
	

	//	Get the device properties from database
	get_device_property();
	
160
	attr_DeviceList_read = new Tango::DevString[4096];
Jean-Luc Pons's avatar
Jean-Luc Pons committed
161
162
163
164
165
166
167
	/*----- PROTECTED REGION ID(AttributeCombiner::init_device) ENABLED START -----*/
	
	set_state(Tango::ON);
	set_status("ON");

	// Create BLM group
	deviceGroup = new Tango::Group("DEVGROUP");
Jean-Luc Pons's avatar
Jean-Luc Pons committed
168
	
Jean-Luc Pons's avatar
Jean-Luc Pons committed
169
170
171
172
173
174
175
	errorStack.clear();
	for (int i = 0; i < (int)deviceList.size(); i++) {
		// Populate error vector
		ERRITEM it;
		errorStack.push_back(it);
	}
	deviceGroup->add(deviceList);
176
	deviceGroup->set_timeout_millis(timeout);
Jean-Luc Pons's avatar
Jean-Luc Pons committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

	//TangoSys_OMemStream ss;
	//blmGroup->dump(ss);
	//cout << ss.str() << endl;

	Tango::GroupReply::enable_exception(false);
	self = new Tango::DeviceProxy(device_name);

	// Read disabled BLM
	Tango::DbData dbd;
	dbd.push_back(Tango::DbDatum("DisabledDevices"));
	get_db_device()->get_property(dbd);
	vector<string> values;
	dbd[0] >> values;
  for(int i=0;i<(int)values.size();i++) {
192
    cout << "Disable: " << values[i] << endl;
Jean-Luc Pons's avatar
Jean-Luc Pons committed
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
    deviceGroup->disable(values[i]);
  }

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::init_device
}

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::get_device_property()
 *	Description : Read database to initialize property data members.
 */
//--------------------------------------------------------
void AttributeCombiner::get_device_property()
{
	/*----- PROTECTED REGION ID(AttributeCombiner::get_device_property_before) ENABLED START -----*/
	
	//	Initialize property data members
	deviceList.clear();
	attributeList.clear();
	attributeRefList.clear();

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::get_device_property_before


	//	Read device properties from database.
	Tango::DbData	dev_prop;
	dev_prop.push_back(Tango::DbDatum("AttributeList"));
	dev_prop.push_back(Tango::DbDatum("AttributeRefList"));
	dev_prop.push_back(Tango::DbDatum("DeviceList"));
222
	dev_prop.push_back(Tango::DbDatum("CommandList"));
223
	dev_prop.push_back(Tango::DbDatum("Timeout"));
Jean-Luc Pons's avatar
Jean-Luc Pons committed
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

	//	is there at least one property to be read ?
	if (dev_prop.size()>0)
	{
		//	Call database and extract values
		if (Tango::Util::instance()->_UseDb==true)
			get_db_device()->get_property(dev_prop);
	
		//	get instance on AttributeCombinerClass to get class property
		Tango::DbDatum	def_prop, cl_prop;
		AttributeCombinerClass	*ds_class =
			(static_cast<AttributeCombinerClass *>(get_device_class()));
		int	i = -1;

		//	Try to initialize AttributeList from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  attributeList;
		else {
			//	Try to initialize AttributeList from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  attributeList;
		}
		//	And try to extract AttributeList value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  attributeList;

		//	Try to initialize AttributeRefList from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  attributeRefList;
		else {
			//	Try to initialize AttributeRefList from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  attributeRefList;
		}
		//	And try to extract AttributeRefList value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  attributeRefList;

		//	Try to initialize DeviceList from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  deviceList;
		else {
			//	Try to initialize DeviceList from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  deviceList;
		}
		//	And try to extract DeviceList value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  deviceList;

271
272
273
274
275
276
277
278
279
280
281
		//	Try to initialize CommandList from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  commandList;
		else {
			//	Try to initialize CommandList from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  commandList;
		}
		//	And try to extract CommandList value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  commandList;

282
283
284
285
286
287
288
289
290
291
292
		//	Try to initialize Timeout from class property
		cl_prop = ds_class->get_class_property(dev_prop[++i].name);
		if (cl_prop.is_empty()==false)	cl_prop  >>  timeout;
		else {
			//	Try to initialize Timeout from default device value
			def_prop = ds_class->get_default_device_property(dev_prop[i].name);
			if (def_prop.is_empty()==false)	def_prop  >>  timeout;
		}
		//	And try to extract Timeout value from database
		if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  timeout;

Jean-Luc Pons's avatar
Jean-Luc Pons committed
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
	}

	/*----- PROTECTED REGION ID(AttributeCombiner::get_device_property_after) ENABLED START -----*/
	
	//	Check device property data members init
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::get_device_property_after
}

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::always_executed_hook()
 *	Description : method always executed before any command is executed
 */
//--------------------------------------------------------
void AttributeCombiner::always_executed_hook()
{
	DEBUG_STREAM << "AttributeCombiner::always_executed_hook()  " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::always_executed_hook) ENABLED START -----*/
	
	//	code always executed before all requests
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::always_executed_hook
}

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::read_attr_hardware()
 *	Description : Hardware acquisition for attributes
 */
//--------------------------------------------------------
void AttributeCombiner::read_attr_hardware(TANGO_UNUSED(vector<long> &attr_list))
{
	DEBUG_STREAM << "AttributeCombiner::read_attr_hardware(vector<long> &attr_list) entering... " << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::read_attr_hardware) ENABLED START -----*/
	
	//	Add your own code
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::read_attr_hardware
}

//--------------------------------------------------------
/**
 *	Read attribute Errors related method
 *	Description: 
 *
 *	Data type:	Tango::DevString
 *	Attr type:	Spectrum max = 2048
 */
//--------------------------------------------------------
void AttributeCombiner::read_Errors(Tango::Attribute &attr)
{
	DEBUG_STREAM << "AttributeCombiner::read_Errors(Tango::Attribute &attr) entering... " << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::read_Errors) ENABLED START -----*/

	int nbError = 0;
	attr_Errors_read = new Tango::DevString[MAXERROR * deviceList.size()];
	for (int i = 0; i < (int) errorStack.size(); i++) {
		for (int j = 0; j < (int) errorStack[i].errList.size(); j++)
			attr_Errors_read[nbError++] = Tango::string_dup(errorStack[i].errList[j].c_str());
	}
	attr.set_value(attr_Errors_read, nbError, 0, true);

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::read_Errors
}
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//--------------------------------------------------------
/**
 *	Read attribute DeviceList related method
 *	Description: 
 *
 *	Data type:	Tango::DevString
 *	Attr type:	Spectrum max = 4096
 */
//--------------------------------------------------------
void AttributeCombiner::read_DeviceList(Tango::Attribute &attr)
{
	DEBUG_STREAM << "AttributeCombiner::read_DeviceList(Tango::Attribute &attr) entering... " << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::read_DeviceList) ENABLED START -----*/

  for (int i = 0; i < (int)deviceList.size(); i++) {
		attr_DeviceList_read[i] = CORBA::string_dup(deviceList[i].c_str());
  }

	attr.set_value(attr_DeviceList_read, deviceList.size());
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::read_DeviceList
}
Jean-Luc Pons's avatar
Jean-Luc Pons committed
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

//--------------------------------------------------------
/**
 *	Method      : AttributeCombiner::add_dynamic_attributes()
 *	Description : Create the dynamic attributes if any
 *                for specified device.
 */
//--------------------------------------------------------
void AttributeCombiner::add_dynamic_attributes()
{
	/*----- PROTECTED REGION ID(AttributeCombiner::add_dynamic_attributes) ENABLED START -----*/

	// Attribute List
	string usage = "9 fields expected name;attName list;type;readwrite;spectrum;grouped;lengthattname;expert;alarm";

	for (int i = 0; i < (int) attributeList.size(); i++) {

		vector<string> fields;
		split(fields, attributeList[i], ';');
		if (fields.size() != 9) {
			cerr << "ERROR: Wrong attribute configuration property for " << attributeList[i] << endl;
			cerr << "ERROR: " << usage << endl;
			exit(0);
		}

		string name = fields[0];
		vector<string> attNames;
		split(attNames, fields[1], ',');

		if (attNames.size() == 0) {
			cerr << "ERROR: Empty attribute list for " << attributeList[i] << endl;
			cerr << "ERROR: " << usage << endl;
			exit(0);
		}

		if (attNames.size() > MAXATTRIBUTE) {
			cerr << "ERROR: Attribute list too long for " << attributeList[i] << endl;
			exit(0);
		}

		int type = ToTangoType(name, fields[2]);

		tolower(fields[3]);
		Tango::AttrWriteType rwType;
		if (fields[3].compare("true") == 0) {
			rwType = Tango::READ_WRITE;
		} else {
			rwType = Tango::READ;
		}

		tolower(fields[4]);
		bool isSpectrum = fields[4].compare("true") == 0;

		tolower(fields[5]);
		bool grouped = fields[5].compare("true") == 0;

		string lengthAttName = fields[6];

		tolower(fields[7]);
		bool expert = fields[7].compare("true") == 0;

		tolower(fields[8]);
		bool alarm = fields[8].compare("true") == 0;

		if (grouped) {

			if (isSpectrum) {
				cerr << "ERROR: Wrong attribute configuration property for " << attributeList[i] << endl;
				cerr << "ERROR: Cannot group spectrum attribute" << endl;
				exit(0);
			}

			// We group all blm attribute in only one (for scalar only)
			DynAttribute *att = new DynAttribute(name.c_str(), type, rwType,expert);
			add_attribute(att);

		} else {

			DynSpectrumAttribute *att;

			if (!isSpectrum) {

				// Map scalar attritbute to spectrum (convert all type to double except state)
				if (type != Tango::DEV_STATE)
					att = new DynSpectrumAttribute(name.c_str(), Tango::DEV_DOUBLE, deviceList.size() * attNames.size(), rwType, expert);
				else
					att = new DynSpectrumAttribute(name.c_str(), Tango::DEV_STATE, deviceList.size() * attNames.size(), rwType, expert);

			} else {

				// Map spectrum attritbute to mulitple spectrum
				if (rwType == Tango::READ_WRITE) {
					cerr << "ERROR: Wrong attribute configuration property for " << attributeList[i] << endl;
					cerr << "ERROR: ReadWrite mode not supported for multiple spectrum" << endl;
					exit(0);
				}

				att = new DynSpectrumAttribute(name.c_str(), Tango::DEV_DOUBLE, deviceList.size() * attNames.size() * MAXVALUELENGTH, rwType, expert);

			}

			add_attribute(att);

		}

		attMap.add(name, attNames, grouped, type, rwType, isSpectrum, lengthAttName, alarm);


	}

	// Attribute ref list
	for (int i = 0; i < (int) attributeRefList.size(); i++) {

		DynSpectrumAttribute *att;
		att = new DynSpectrumAttribute(attributeRefList[i].c_str(), Tango::DEV_DOUBLE, MAXVALUELENGTH, Tango::READ_WRITE, true);
		add_attribute(att);
		attMap.addRef(attributeRefList[i].c_str());

	}

	// States attribute
Jean-Luc Pons's avatar
Jean-Luc Pons committed
501
502
503
504
505
506
507
	if( deviceList.size()>0 ) {
		vector<string> stateName;
		stateName.push_back("State");
		DynSpectrumAttribute *stateAtt = new DynSpectrumAttribute("States", Tango::DEV_STATE, deviceList.size(),Tango::READ, false);
		add_attribute(stateAtt);
		attMap.add("States", stateName, false, Tango::DEV_STATE, Tango::READ, false, "None", false);
	}
Jean-Luc Pons's avatar
Jean-Luc Pons committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::add_dynamic_attributes
}

//--------------------------------------------------------
/**
 *	Command ResetError related method
 *	Description: Reset Error stack
 *
 */
//--------------------------------------------------------
void AttributeCombiner::reset_error()
{
	DEBUG_STREAM << "AttributeCombiner::ResetError()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::reset_error) ENABLED START -----*/
	
	for (int i = 0; i < (int) errorStack.size(); i++)
		errorStack[i].errList.clear();

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::reset_error
}
//--------------------------------------------------------
/**
 *	Command UpdateSetpoint related method
 *	Description: Update local setpoints from device setpoints
 *
 */
//--------------------------------------------------------
void AttributeCombiner::update_setpoint()
{
	DEBUG_STREAM << "AttributeCombiner::UpdateSetpoint()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::update_setpoint) ENABLED START -----*/
	
	// Force update of setpoints at the next reading
	for(int i=0;i<(int)attMap.size();i++) {
		ATTITEM *item = attMap.get(i);
		item->setInit = false;
	}

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::update_setpoint
}
//--------------------------------------------------------
/**
 *	Command EnableDevice related method
 *	Description: Enable device
 *
 *	@param argin Enable device #i
 */
//--------------------------------------------------------
void AttributeCombiner::enable_device(Tango::DevShort argin)
{
	DEBUG_STREAM << "AttributeCombiner::EnableDevice()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::enable_device) ENABLED START -----*/

	if(argin<0 || argin>=(short)deviceList.size()) {
		Tango::Except::throw_exception(
						(const char *) "AttributeCombiner::error_write",
565
						(const char *) "Invalid device index",
Jean-Luc Pons's avatar
Jean-Luc Pons committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
						(const char *) "AttributeCombiner::disable_device");
	}

	deviceGroup->enable(deviceList[argin]);
	saveDisabledBLM();

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::enable_device
}
//--------------------------------------------------------
/**
 *	Command DisableDevice related method
 *	Description: Disable device
 *
 *	@param argin Disable device #i
 */
//--------------------------------------------------------
void AttributeCombiner::disable_device(Tango::DevShort argin)
{
	DEBUG_STREAM << "AttributeCombiner::DisableDevice()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::disable_device) ENABLED START -----*/

	if(argin<0 || argin>=(short)deviceList.size()) {
		Tango::Except::throw_exception(
						(const char *) "AttributeCombiner::error_write",
590
						(const char *) "Invalid device index",
Jean-Luc Pons's avatar
Jean-Luc Pons committed
591
592
593
594
595
596
597
598
599
						(const char *) "AttributeCombiner::disable_device");
	}

	deviceGroup->disable(deviceList[argin]);
	saveDisabledBLM();

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::disable_device
}
//--------------------------------------------------------
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
 *	Command Indice2Devicename related method
 *	Description: Return the devicename associated to the input indice
 *
 *	@param argin indice of device (from 0)
 *	@returns 
 */
//--------------------------------------------------------
Tango::DevString AttributeCombiner::indice2_devicename(Tango::DevShort argin)
{
	Tango::DevString argout;
	DEBUG_STREAM << "AttributeCombiner::Indice2Devicename()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::indice2_devicename) ENABLED START -----*/

	if(argin<0 || argin>=(short)deviceList.size()) {
		Tango::Except::throw_exception(
						(const char *) "AttributeCombiner::error_write",
						(const char *) "Invalid device index",
						(const char *) "AttributeCombiner::indice2_devicename");
	}

	argout = CORBA::string_dup(deviceList[argin].c_str());
	

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::indice2_devicename
	return argout;
}
//--------------------------------------------------------
/**
 *	Command Devicename2Indice related method
 *	Description: 
 *
 *	@param argin Devicename
 *	@returns indice of device (from 0) if the devicename is found. -1 otherwise
 */
//--------------------------------------------------------
Tango::DevShort AttributeCombiner::devicename2_indice(Tango::DevString argin)
{
	Tango::DevShort argout;
	DEBUG_STREAM << "AttributeCombiner::Devicename2Indice()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::devicename2_indice) ENABLED START -----*/

	string devicename = argin;
	for (argout=0; argout <(short)deviceList.size(); argout++)  {
		if (deviceList[argout] == devicename) return argout;
	}

	argout = -1;
	Tango::Except::throw_exception(
					(const char *) "AttributeCombiner::error_write",
					(const char *) "devicename not found",
					(const char *) "AttributeCombiner::disable_device");
	

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::devicename2_indice
	return argout;
}
//--------------------------------------------------------
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
 *	Command DisableAll related method
 *	Description: 
 *
 */
//--------------------------------------------------------
void AttributeCombiner::disable_all()
{
	DEBUG_STREAM << "AttributeCombiner::DisableAll()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::disable_all) ENABLED START -----*/


  for(int i=0;i<(int)deviceList.size();i++)
    deviceGroup->disable(deviceList[i]);
  saveDisabledBLM();


  /*----- PROTECTED REGION END -----*/	//	AttributeCombiner::disable_all
}
//--------------------------------------------------------
/**
 *	Command EnableAll related method
 *	Description: 
 *
 */
//--------------------------------------------------------
void AttributeCombiner::enable_all()
{
	DEBUG_STREAM << "AttributeCombiner::EnableAll()  - " << device_name << endl;
	/*----- PROTECTED REGION ID(AttributeCombiner::enable_all) ENABLED START -----*/
	
  for(int i=0;i<(int)deviceList.size();i++)
    deviceGroup->enable(deviceList[i]);
  saveDisabledBLM();

	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::enable_all
}
//--------------------------------------------------------
Jean-Luc Pons's avatar
Jean-Luc Pons committed
696
697
698
699
700
701
702
703
704
/**
 *	Method      : AttributeCombiner::add_dynamic_commands()
 *	Description : Create the dynamic commands if any
 *                for specified device.
 */
//--------------------------------------------------------
void AttributeCombiner::add_dynamic_commands()
{
	/*----- PROTECTED REGION ID(AttributeCombiner::add_dynamic_commands) ENABLED START -----*/
705
706
707
708
709
710
711
712

  for(int i=0;i<(int)commandList.size();i++) {

    // Create command
    DynCommandClass *cmd = new DynCommandClass(commandList[i].c_str(),Tango::DEV_VOID,Tango::DEV_VOID);
    add_command(cmd);

  }
Jean-Luc Pons's avatar
Jean-Luc Pons committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
	
	/*----- PROTECTED REGION END -----*/	//	AttributeCombiner::add_dynamic_commands
}

/*----- PROTECTED REGION ID(AttributeCombiner::namespace_ending) ENABLED START -----*/

template<typename inType>
void AttributeCombiner::READG(Tango::Attribute &attr,inType &value,ATTITEM *item,Tango::GroupAttrReplyList arl[]) {

	// READ grouped attribute
	// Set to alarm if not all value are equal

	string &attName = attr.get_name();
	bool allEqual = true;
	bool firstValue = true;
	int nbAtt = (int)item->attNames.size();
	int nbDev = (int)deviceGroup->get_size();

	for (int i = 0; i < nbDev; i++) {
		for (int j = 0; j < nbAtt; j++) {
			if (!arl[j][i].has_failed() && arl[j][i].group_element_enabled()) {
				inType val;
				arl[j][i] >> val;
				if (firstValue) {
					value = val;
					firstValue = false;
				} else {
					allEqual = allEqual && (val == value);
				}
			} else {
        if(arl[j][i].group_element_enabled()) {
          addError(i, arl[j][i]);
          allEqual = false;
        }
			}
		}
	}

	if (firstValue) {
		/* All reading failed */
		attr.set_quality(Tango::ATTR_INVALID);
	} else {
		attr.set_value(&value);
		if (!allEqual && item->manageAlarm)
			attr.set_quality(Tango::ATTR_ALARM);

		if (!item->setInit &&
				item->rwType == Tango::READ_WRITE) {
			Tango::WAttribute &att = getAtt(attName);
			att.set_write_value(value);
			item->setInit = true;
		}
	}

}

/* ------------------------------------------------------------------------------- */

void AttributeCombiner::read_dyn_attributes(Tango::Attribute &attr, DynAttribute *src) {

	string &attName = attr.get_name();
	ATTITEM *item = attMap.get(attName);
	if (item == NULL) {
		Tango::Except::throw_exception(
						(const char *) "BLMAll::error_read",
						(const char *) "Unknown attribute",
						(const char *) "BLMAll::read_dyn_attributes");
	}

	// Read all values

	Tango::GroupAttrReplyList arl[MAXATTRIBUTE];
	int nbAtt = (int)item->attNames.size();
	for(int i=0;i<nbAtt;i++)
		arl[i] = deviceGroup->read_attribute(item->attNames[i]);

	switch (item->type) {
		case Tango::DEV_BOOLEAN:
			READG(attr,item->bVal,item,arl);
			break;
793
794
795
		case Tango::DEV_USHORT:
			READG(attr,item->usVal,item,arl);
			break;
Jean-Luc Pons's avatar
Jean-Luc Pons committed
796
797
798
799
800
801
802
803
804
805
806
807
		case Tango::DEV_SHORT:
			READG(attr,item->sVal,item,arl);
			break;
		case Tango::DEV_ULONG:
			READG(attr,item->ulVal,item,arl);
			break;
		case Tango::DEV_LONG:
			READG(attr,item->lVal,item,arl);
			break;
		case Tango::DEV_LONG64:
			READG(attr,item->lVal64,item,arl);
			break;
Jean-Luc Pons's avatar
Jean-Luc Pons committed
808
809
810
		case Tango::DEV_ULONG64:
			READG(attr,item->ulVal64,item,arl);
			break;
Jean-Luc Pons's avatar
Jean-Luc Pons committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
		case Tango::DEV_DOUBLE:
			READG(attr,item->dVal,item,arl);
			break;

	}

}

/* ------------------------------------------------------------------------------- */

template <typename T>
void AttributeCombiner::WRITEG(Tango::WAttribute &attr,ATTITEM *item,Tango::GroupReplyList arl[]) {

	// WRITE grouped attribute
	T w_val;

	attr.get_write_value(w_val);

	int nbAtt = (int)item->attNames.size();
	for(int i=0;i<nbAtt;i++) {
		Tango::DeviceAttribute da(item->attNames[i], w_val);
		arl[i] = deviceGroup->write_attribute(da);
	}

}

void AttributeCombiner::write_dyn_attributes(Tango::WAttribute &attr, DynAttribute *src) {


	ATTITEM *item = attMap.get(attr.get_name());
	if (item == NULL) {
		Tango::Except::throw_exception(
						(const char *) "AttributeCombiner::error_write",
						(const char *) "Unknown attribute",
						(const char *) "AttributeCombiner::write_dyn_attributes");
	}

	Tango::GroupReplyList arl[MAXATTRIBUTE];
	int nbAtt = (int)item->attNames.size();
	int nbDev = (int)deviceGroup->get_size();

	switch (item->type) {

		case Tango::DEV_BOOLEAN:
			WRITEG<Tango::DevBoolean>(attr,item,arl);
			break;

858
859
860
861
		case Tango::DEV_USHORT:
			WRITEG<Tango::DevUShort>(attr,item,arl);
			break;

Jean-Luc Pons's avatar
Jean-Luc Pons committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
		case Tango::DEV_SHORT:
			WRITEG<Tango::DevShort>(attr,item,arl);
			break;

		case Tango::DEV_LONG:
			WRITEG<Tango::DevLong>(attr,item,arl);
			break;

		case Tango::DEV_LONG64:
			WRITEG<Tango::DevLong64>(attr,item,arl);
			break;

		case Tango::DEV_ULONG:
			WRITEG<Tango::DevULong>(attr,item,arl);
Jean-Luc Pons's avatar
Jean-Luc Pons committed
876
877
878
879
			break;

		case Tango::DEV_ULONG64:
			WRITEG<Tango::DevULong64>(attr,item,arl);
Jean-Luc Pons's avatar
Jean-Luc Pons committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
			break;

		case Tango::DEV_DOUBLE:
			WRITEG<Tango::DevDouble>(attr,item,arl);
			break;

	}

	// Check results
	for(int j=0;j<nbAtt;j++) {
		for (int i = 0; i < nbDev; i++) {
			if (arl[j][i].has_failed() && arl[j][i].group_element_enabled()) {
				char errStr[512];
				sprintf(errStr, "Failed to write %s/%s\n%s",
								arl[j][i].dev_name().c_str(), item->attNames[0].c_str(),
								(const char *) arl[j][i].get_err_stack().get_buffer()[0].desc);
				Tango::Except::throw_exception(
								(const char *) "AttributeCombiner::error_write",
								(const char *) errStr,
								(const char *) "AttributeCombiner::write_dyn_attributes");
			}
		}
	}

}

/* ------------------------------------------------------------------------------- */

template <typename inType,typename outType>
void AttributeCombiner::READSCALAR(Tango::Attribute &attr,ATTITEM *item,Tango::GroupAttrReplyList arl[],outType invalid,outType disableValue) {

	string& attName = attr.get_name();
	int nbAtt = (int) item->attNames.size();
	int nbDev = deviceGroup->get_size();
	int size = nbDev * nbAtt;

	// Read scalar attribute and combime them to spectrum
	outType *result = new outType[size];

	for (int i = 0; i < nbDev; i++) {

		for (int j = 0; j < nbAtt; j++) {
			if (arl[j][i].has_failed()) {
				result[i * nbAtt + j] = invalid;
				addError(i, arl[j][i]);
			} else {
        if(arl[j][i].group_element_enabled()) {
				  inType v;
				  arl[j][i] >> v;
				  result[i * nbAtt + j] = (outType) v;
        } else {
          result[i * nbAtt + j] = disableValue;
        }
			}
		}

	}

	if (!item->setInit &&
			item->rwType == Tango::READ_WRITE) {
		Tango::WAttribute &att = getAtt(attName);
		att.set_write_value(result, size);
		item->setInit = true;
	}
	attr.set_value(result, size, 0, true);

}

/* ------------------------------------------------------------------------------- */

template <typename inType>
void AttributeCombiner::READSPECTRUM(Tango::Attribute &attr,ATTITEM *item,Tango::GroupAttrReplyList arl[]) {

// Read spectrum attribute and combime them to multiple spectrum
	Tango::DevULong length;

	int nbAtt = (int) item->attNames.size();
	int nbDev = deviceGroup->get_size();
	int size = nbDev * nbAtt;


	if(isNumber(item->lengthAttName))
		length = atol(item->lengthAttName.c_str());
	else
		self->read_attribute(item->lengthAttName) >> length;

	int blockSize = length * nbAtt;
	Tango::DevDouble *result = new Tango::DevDouble[length * size];
	for (int i = 0; i < nbDev; i++) {

		for (int j = 0; j < nbAtt; j++) {
			if (arl[j][i].has_failed()) {
				for (int k = 0; k < (int) length; k++)
					result[i * blockSize + j * length + k] = NAN;
 				addError(i, arl[j][i]);
			} else {
        if(arl[j][i].group_element_enabled()) {
          int k = 0;
          vector<inType> v;
          arl[j][i] >> v;
          for (k = 0; k < (int) length && k < (int) v.size(); k++)
            result[i * blockSize + j * length + k] = (Tango::DevDouble) v[k];
          for (; k < (int) length; k++)
            result[i * length * nbAtt + j * length + k] = NAN;
        } else {
          // Disabled device
          for (int k = 0; k < (int) length; k++)
            result[i * blockSize + j * length + k] = NAN;
        }
			}
		}

	}
	attr.set_value(result, length * size, 0, true);

}

/* ------------------------------------------------------------------------------- */

void AttributeCombiner::read_dynspec_attributes(Tango::Attribute &attr,DynSpectrumAttribute *src) {