1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,113 | |||||
|
1 | ''' | |||
|
2 | Created on Nov 25, 2015 | |||
|
3 | ||||
|
4 | @author: Miguel Urco | |||
|
5 | ||||
|
6 | eth_device decorator is used to implement an api to ethernet devices. | |||
|
7 | When eth_device decorator is used it adds two parameters to any function (ip and port) | |||
|
8 | ||||
|
9 | #Definition of a function using eth_device decorator | |||
|
10 | ||||
|
11 | @eth_device(ID_CLASS) | |||
|
12 | def enable_acq(message) | |||
|
13 | cmd = "xxxxx" | |||
|
14 | payload = message | |||
|
15 | ||||
|
16 | return cmd, payload | |||
|
17 | ||||
|
18 | #How to call this function: | |||
|
19 | answer = enable_acq(ip, port, message) | |||
|
20 | ||||
|
21 | ''' | |||
|
22 | import sys | |||
|
23 | import struct | |||
|
24 | import json | |||
|
25 | ||||
|
26 | from devices.jro_device import eth_device, IdClass | |||
|
27 | ||||
|
28 | ID_CLASS = IdClass["jars"] | |||
|
29 | ||||
|
30 | CMD_RESET = 0X01 | |||
|
31 | CMD_CHANGEIP = 0X03 | |||
|
32 | #Add other commands | |||
|
33 | CMD_CONFIGURE = 0X10 | |||
|
34 | CMD_STATUS = 0X11 | |||
|
35 | CMD_SET_EXEPATH = 0X12 | |||
|
36 | CMD_ECHO = 0XFE | |||
|
37 | CMD_READ = 0X08 | |||
|
38 | CMD_STOP = 0X09 | |||
|
39 | ||||
|
40 | @eth_device(ID_CLASS) | |||
|
41 | def reset(): | |||
|
42 | ||||
|
43 | cmd = CMD_RESET | |||
|
44 | payload = '' | |||
|
45 | ||||
|
46 | return cmd, payload | |||
|
47 | ||||
|
48 | @eth_device(ID_CLASS) | |||
|
49 | def stop(): | |||
|
50 | ||||
|
51 | cmd = CMD_STOP | |||
|
52 | payload = '' | |||
|
53 | ||||
|
54 | return cmd, payload | |||
|
55 | ||||
|
56 | @eth_device(ID_CLASS) | |||
|
57 | def echo(message): | |||
|
58 | ||||
|
59 | cmd = CMD_ECHO | |||
|
60 | payload = message | |||
|
61 | ||||
|
62 | return cmd, payload | |||
|
63 | ||||
|
64 | @eth_device(ID_CLASS) | |||
|
65 | def configure(conf): | |||
|
66 | ||||
|
67 | cmd = CMD_CONFIGURE | |||
|
68 | payload = conf | |||
|
69 | ||||
|
70 | return cmd, payload | |||
|
71 | ||||
|
72 | @eth_device(ID_CLASS) | |||
|
73 | def status(): | |||
|
74 | ||||
|
75 | cmd = CMD_STATUS | |||
|
76 | payload = '' | |||
|
77 | ||||
|
78 | return cmd, payload | |||
|
79 | ||||
|
80 | @eth_device(ID_CLASS) | |||
|
81 | def read(): | |||
|
82 | ||||
|
83 | cmd = CMD_READ | |||
|
84 | payload = '' | |||
|
85 | ||||
|
86 | return cmd, payload | |||
|
87 | ||||
|
88 | @eth_device(ID_CLASS) | |||
|
89 | def set_exepath(path): | |||
|
90 | ||||
|
91 | cmd = CMD_SET_EXEPATH | |||
|
92 | payload = path | |||
|
93 | ||||
|
94 | return cmd, payload | |||
|
95 | ||||
|
96 | #--To take .json file from computer: | |||
|
97 | #with open('/home/fquino/Downloads/Experiment.json') as data_file: | |||
|
98 | # data = json.load(data_file) | |||
|
99 | ||||
|
100 | # data['configurations']['dds']='' | |||
|
101 | # data['configurations']['rc']['pulses']='' | |||
|
102 | # data['configurations']['rc']['delays']='' | |||
|
103 | ||||
|
104 | #data = json.dumps(data) | |||
|
105 | #----------------------------------- | |||
|
106 | ||||
|
107 | #print reset('10.10.10.100', 10000) | |||
|
108 | #print echo('10.10.10.95', 10000, 'Hola JARS :)') | |||
|
109 | ||||
|
110 | #json_data = json.dumps({'name':'archivo1','variable':9}) | |||
|
111 | #print configure('10.10.10.95', 10000, data) | |||
|
112 | #print configure('10.10.10.100', 10000, '') | |||
|
113 | #print status('10.10.10.100', 10000) |
@@ -1,14 +1,14 | |||||
1 | ## DJANGO |
|
1 | ## DJANGO | |
2 |
|
2 | |||
3 | WSGIScriptAlias / /usr/local/radarsys/radarsys/wsgi.py |
|
3 | WSGIScriptAlias / /usr/local/radarsys/radarsys/wsgi.py | |
4 | WSGIPythonPath /usr/local/radarsys/:/usr/local/virtuals/radarsys/lib/python2.7/site-packages |
|
4 | WSGIPythonPath /usr/local/radarsys/:/usr/local/virtuals/radarsys/lib/python2.7/site-packages | |
5 |
|
5 | |||
6 | Alias /static/ /var/www/html/static/ |
|
6 | Alias /static/ /var/www/html/static/ | |
7 |
|
7 | |||
8 | <Directory /usr/local/radarsys/> |
|
8 | <Directory /usr/local/radarsys/> | |
9 | <Files wsgi.py> |
|
9 | <Files wsgi.py> | |
10 | #Order deny,allow |
|
10 | #Order deny,allow | |
11 | Require all granted |
|
11 | Require all granted | |
12 | #Allow from all |
|
12 | #Allow from all | |
13 | </Files> |
|
13 | </Files> | |
14 | </Directory> No newline at end of file |
|
14 | </Directory> |
@@ -1,10 +1,6 | |||||
1 | from django.conf.urls import url |
|
1 | from django.conf.urls import url | |
2 |
|
2 | |||
3 | urlpatterns = ( |
|
3 | urlpatterns = ( | |
4 | url(r'^(?P<id_conf>-?\d+)/$', 'apps.main.views.dev_conf', name='url_abs_conf'), |
|
4 | url(r'^(?P<id_conf>-?\d+)/$', 'apps.main.views.dev_conf', name='url_abs_conf'), | |
5 | url(r'^(?P<id_conf>-?\d+)/edit/$', 'apps.main.views.dev_conf_edit', name='url_edit_abs_conf'), |
|
5 | url(r'^(?P<id_conf>-?\d+)/edit/$', 'apps.main.views.dev_conf_edit', name='url_edit_abs_conf'), | |
6 | url(r'^(?P<id_conf>-?\d+)/write/$', 'apps.main.views.dev_conf_write', name='url_write_abs_conf'), |
|
6 | ) | |
7 | url(r'^(?P<id_conf>-?\d+)/read/$', 'apps.main.views.dev_conf_read', name='url_read_abs_conf'), |
|
|||
8 | url(r'^(?P<id_conf>-?\d+)/import/$', 'apps.main.views.dev_conf_import', name='url_import_abs_conf'), |
|
|||
9 | url(r'^(?P<id_conf>-?\d+)/export/$', 'apps.main.views.dev_conf_export', name='url_export_abs_conf'), |
|
|||
10 | ) No newline at end of file |
|
@@ -1,34 +1,34 | |||||
1 | from django import forms |
|
1 | from django import forms | |
2 | from apps.main.models import Device |
|
2 | from apps.main.models import Device | |
3 | from .models import CGSConfiguration |
|
3 | from .models import CGSConfiguration | |
4 |
|
4 | |||
5 | class CGSConfigurationForm(forms.ModelForm): |
|
5 | class CGSConfigurationForm(forms.ModelForm): | |
6 |
|
6 | |||
7 | def __init__(self, *args, **kwargs): |
|
7 | def __init__(self, *args, **kwargs): | |
8 | #request = kwargs.pop('request') |
|
8 | #request = kwargs.pop('request') | |
9 | super(CGSConfigurationForm, self).__init__(*args, **kwargs) |
|
9 | super(CGSConfigurationForm, self).__init__(*args, **kwargs) | |
10 |
|
10 | |||
11 | instance = getattr(self, 'instance', None) |
|
11 | instance = getattr(self, 'instance', None) | |
12 |
|
12 | |||
13 | if instance and instance.pk: |
|
13 | if instance and instance.pk: | |
14 |
|
14 | |||
15 | devices = Device.objects.filter(device_type__name='cgs') |
|
15 | devices = Device.objects.filter(device_type__name='cgs') | |
16 |
|
16 | |||
17 | if instance.experiment: |
|
17 | if instance.experiment: | |
18 | self.fields['experiment'].widget.attrs['disabled'] = 'disabled' |
|
18 | self.fields['experiment'].widget.attrs['disabled'] = 'disabled' | |
19 |
|
19 | |||
20 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
20 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] | |
21 |
|
21 | |||
22 | def clean(self): |
|
22 | def clean(self): | |
23 | return |
|
23 | return | |
24 | # |
|
24 | # | |
25 |
|
25 | |||
26 | class Meta: |
|
26 | class Meta: | |
27 | model = CGSConfiguration |
|
27 | model = CGSConfiguration | |
28 | exclude = ('type', 'parameters', 'status') |
|
28 | exclude = ('type', 'parameters', 'status') | |
29 | #fields = ('experiment', 'device', 'freq0', 'freq1', 'freq2', 'freq3') |
|
29 | #fields = ('experiment', 'device', 'freq0', 'freq1', 'freq2', 'freq3') | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | class UploadFileForm(forms.Form): |
|
32 | class UploadFileForm(forms.Form): | |
33 | title = forms.CharField(label='Extension Type', widget=forms.TextInput(attrs={'readonly':'readonly'})) |
|
33 | title = forms.CharField(label='Extension Type', widget=forms.TextInput(attrs={'readonly':'readonly'})) | |
34 | file = forms.FileField() No newline at end of file |
|
34 | file = forms.FileField() |
@@ -1,268 +1,273 | |||||
1 | from django.db import models |
|
1 | from django.db import models | |
2 | from apps.main.models import Configuration |
|
2 | from apps.main.models import Configuration | |
3 | from django.core.validators import MinValueValidator, MaxValueValidator |
|
3 | from django.core.validators import MinValueValidator, MaxValueValidator | |
4 | from django.core.urlresolvers import reverse |
|
4 | from django.core.urlresolvers import reverse | |
5 | from devices.jars import api |
|
5 | from devices.jars import api | |
6 |
|
6 | |||
7 | from apps.rc.models import RCConfiguration |
|
7 | from apps.rc.models import RCConfiguration | |
8 |
|
8 | |||
9 | import json |
|
9 | import json | |
10 | # Create your models here. |
|
10 | # Create your models here. | |
11 |
|
11 | |||
12 | EXPERIMENT_TYPE = ( |
|
12 | EXPERIMENT_TYPE = ( | |
13 | (0, 'RAW_DATA'), |
|
13 | (0, 'RAW_DATA'), | |
14 | (1, 'PDATA'), |
|
14 | (1, 'PDATA'), | |
15 | ) |
|
15 | ) | |
16 |
|
16 | |||
17 | DATA_TYPE = ( |
|
17 | DATA_TYPE = ( | |
18 | (0, 'SHORT'), |
|
18 | (0, 'SHORT'), | |
19 | (1, 'FLOAT'), |
|
19 | (1, 'FLOAT'), | |
20 | ) |
|
20 | ) | |
21 |
|
21 | |||
22 | class JARSfilter(models.Model): |
|
22 | class JARSfilter(models.Model): | |
23 |
|
23 | |||
24 | JARS_NBITS = 32 |
|
24 | JARS_NBITS = 32 | |
25 |
|
25 | |||
26 | name = models.CharField(max_length=60, unique=True, default='') |
|
26 | name = models.CharField(max_length=60, unique=True, default='') | |
27 | clock = models.FloatField(verbose_name='Clock In (MHz)',validators=[MinValueValidator(5), MaxValueValidator(75)], null=True, default=60) |
|
27 | clock = models.FloatField(verbose_name='Clock In (MHz)',validators=[MinValueValidator(5), MaxValueValidator(75)], null=True, default=60) | |
28 | mult = models.PositiveIntegerField(verbose_name='Multiplier',validators=[MinValueValidator(1), MaxValueValidator(20)], default=5) |
|
28 | mult = models.PositiveIntegerField(verbose_name='Multiplier',validators=[MinValueValidator(1), MaxValueValidator(20)], default=5) | |
29 | fch = models.DecimalField(verbose_name='Frequency (MHz)', validators=[MinValueValidator(0), MaxValueValidator(150)], max_digits=19, decimal_places=16, null=True, default=49.9200) |
|
29 | fch = models.DecimalField(verbose_name='Frequency (MHz)', validators=[MinValueValidator(0), MaxValueValidator(150)], max_digits=19, decimal_places=16, null=True, default=49.9200) | |
30 | fch_decimal = models.BigIntegerField(verbose_name='Frequency (Decimal)',validators=[MinValueValidator(0), MaxValueValidator(2**JARS_NBITS-1)], null=True, default=721554505) |
|
30 | fch_decimal = models.BigIntegerField(verbose_name='Frequency (Decimal)',validators=[MinValueValidator(0), MaxValueValidator(2**JARS_NBITS-1)], null=True, default=721554505) | |
31 | filter_fir = models.PositiveIntegerField(verbose_name='FIR Filter',validators=[MinValueValidator(1), MaxValueValidator(20)], default = 6) |
|
31 | filter_fir = models.PositiveIntegerField(verbose_name='FIR Filter',validators=[MinValueValidator(1), MaxValueValidator(20)], default = 6) | |
32 | filter_2 = models.PositiveIntegerField(verbose_name='Filter 2',validators=[MinValueValidator(1), MaxValueValidator(20)], default = 10) |
|
32 | filter_2 = models.PositiveIntegerField(verbose_name='Filter 2',validators=[MinValueValidator(1), MaxValueValidator(20)], default = 10) | |
33 | filter_5 = models.PositiveIntegerField(verbose_name='Filter 5',validators=[MinValueValidator(1), MaxValueValidator(20)], default = 1) |
|
33 | filter_5 = models.PositiveIntegerField(verbose_name='Filter 5',validators=[MinValueValidator(1), MaxValueValidator(20)], default = 1) | |
34 | speed = models.PositiveIntegerField(verbose_name='Speed',validators=[MinValueValidator(0), MaxValueValidator(100000)], default = 0) |
|
34 | speed = models.PositiveIntegerField(verbose_name='Speed',validators=[MinValueValidator(0), MaxValueValidator(100000)], default = 0) | |
35 |
|
35 | |||
36 | class Meta: |
|
36 | class Meta: | |
37 | db_table = 'jars_filters' |
|
37 | db_table = 'jars_filters' | |
38 |
|
38 | |||
39 | def __unicode__(self): |
|
39 | def __unicode__(self): | |
40 | return u'%s' % (self.name) |
|
40 | return u'%s' % (self.name) | |
41 |
|
41 | |||
42 | def parms_to_dict(self): |
|
42 | def parms_to_dict(self): | |
43 |
|
43 | |||
44 | parameters = {} |
|
44 | parameters = {} | |
45 |
|
45 | |||
46 | parameters['name'] = self.name |
|
46 | parameters['name'] = self.name | |
47 | parameters['clock'] = float(self.clock) |
|
47 | parameters['clock'] = float(self.clock) | |
48 | parameters['mult'] = int(self.mult) |
|
48 | parameters['mult'] = int(self.mult) | |
49 | parameters['fch'] = float(self.fch) |
|
49 | parameters['fch'] = float(self.fch) | |
50 | parameters['fch_decimal'] = int(self.fch) |
|
50 | parameters['fch_decimal'] = int(self.fch) | |
51 | parameters['filter_fir'] = int(self.filter_fir) |
|
51 | parameters['filter_fir'] = int(self.filter_fir) | |
52 | parameters['filter_2'] = int(self.filter_2) |
|
52 | parameters['filter_2'] = int(self.filter_2) | |
53 | parameters['filter_5'] = int(self.filter_5) |
|
53 | parameters['filter_5'] = int(self.filter_5) | |
54 | parameters['speed'] = int(self.speed) |
|
54 | parameters['speed'] = int(self.speed) | |
55 |
|
55 | |||
56 | return parameters |
|
56 | return parameters | |
57 |
|
57 | |||
58 | def dict_to_parms(self, parameters): |
|
58 | def dict_to_parms(self, parameters): | |
59 |
|
59 | |||
60 | self.name = parameters['name'] |
|
60 | self.name = parameters['name'] | |
61 | self.clock = parameters['clock'] |
|
61 | self.clock = parameters['clock'] | |
62 | self.mult = parameters['mult'] |
|
62 | self.mult = parameters['mult'] | |
63 | self.fch = parameters['fch'] |
|
63 | self.fch = parameters['fch'] | |
64 | self.fch_decimal = parameters['fch_decimal'] |
|
64 | self.fch_decimal = parameters['fch_decimal'] | |
65 | self.filter_fir = parameters['filter_fir'] |
|
65 | self.filter_fir = parameters['filter_fir'] | |
66 | self.filter_2 = parameters['filter_2'] |
|
66 | self.filter_2 = parameters['filter_2'] | |
67 | self.filter_5 = parameters['filter_5'] |
|
67 | self.filter_5 = parameters['filter_5'] | |
68 | self.speed = parameters['speed'] |
|
68 | self.speed = parameters['speed'] | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | class JARSConfiguration(Configuration): |
|
71 | class JARSConfiguration(Configuration): | |
72 |
|
72 | |||
73 | ADC_RESOLUTION = 8 |
|
73 | ADC_RESOLUTION = 8 | |
74 | PCI_DIO_BUSWIDTH = 32 |
|
74 | PCI_DIO_BUSWIDTH = 32 | |
75 | HEADER_VERSION = 1103 |
|
75 | HEADER_VERSION = 1103 | |
76 | BEGIN_ON_START = True |
|
76 | BEGIN_ON_START = True | |
77 | REFRESH_RATE = 1 |
|
77 | REFRESH_RATE = 1 | |
78 |
|
78 | |||
79 | #rc = models.ForeignKey(RCConfiguration, on_delete=models.CASCADE, null=True) |
|
79 | #rc = models.ForeignKey(RCConfiguration, on_delete=models.CASCADE, null=True) | |
80 | exp_type = models.PositiveIntegerField(verbose_name='Experiment Type', choices=EXPERIMENT_TYPE, default=0) |
|
80 | exp_type = models.PositiveIntegerField(verbose_name='Experiment Type', choices=EXPERIMENT_TYPE, default=0) | |
81 | cards_number = models.PositiveIntegerField(verbose_name='Number of Cards', validators=[MinValueValidator(1), MaxValueValidator(4)], default = 1) |
|
81 | cards_number = models.PositiveIntegerField(verbose_name='Number of Cards', validators=[MinValueValidator(1), MaxValueValidator(4)], default = 1) | |
82 | channels_number = models.PositiveIntegerField(verbose_name='Number of Channels', validators=[MinValueValidator(1), MaxValueValidator(8)], default = 5) |
|
82 | channels_number = models.PositiveIntegerField(verbose_name='Number of Channels', validators=[MinValueValidator(1), MaxValueValidator(8)], default = 5) | |
83 | channels = models.CharField(verbose_name='Channels', max_length=15, default = '1,2,3,4,5') |
|
83 | channels = models.CharField(verbose_name='Channels', max_length=15, default = '1,2,3,4,5') | |
84 | rd_directory = models.CharField(verbose_name='Raw Data Directory', max_length=200, default='', blank=True, null=True) |
|
84 | rd_directory = models.CharField(verbose_name='Raw Data Directory', max_length=200, default='', blank=True, null=True) | |
85 | pd_directory = models.CharField(verbose_name='Process Data Directory', max_length=200, default='', blank=True, null=True) |
|
85 | pd_directory = models.CharField(verbose_name='Process Data Directory', max_length=200, default='', blank=True, null=True) | |
86 | #raw_data_blocks = models.PositiveIntegerField(verbose_name='Raw Data Blocks', validators=[MaxValueValidator(5000)], default=120) |
|
86 | #raw_data_blocks = models.PositiveIntegerField(verbose_name='Raw Data Blocks', validators=[MaxValueValidator(5000)], default=120) | |
87 | data_type = models.PositiveIntegerField(verbose_name='Data Type', choices=DATA_TYPE, default=0) |
|
87 | data_type = models.PositiveIntegerField(verbose_name='Data Type', choices=DATA_TYPE, default=0) | |
88 | acq_profiles = models.PositiveIntegerField(verbose_name='Acquired Profiles', validators=[MaxValueValidator(5000)], default=400) |
|
88 | acq_profiles = models.PositiveIntegerField(verbose_name='Acquired Profiles', validators=[MaxValueValidator(5000)], default=400) | |
89 | profiles_block = models.PositiveIntegerField(verbose_name='Profiles Per Block', validators=[MaxValueValidator(5000)], default=400) |
|
89 | profiles_block = models.PositiveIntegerField(verbose_name='Profiles Per Block', validators=[MaxValueValidator(5000)], default=400) | |
90 | ftp_interval = models.PositiveIntegerField(verbose_name='FTP Interval', default=60) |
|
90 | ftp_interval = models.PositiveIntegerField(verbose_name='FTP Interval', default=60) | |
91 | fftpoints = models.PositiveIntegerField(verbose_name='FFT Points',default=16) |
|
91 | fftpoints = models.PositiveIntegerField(verbose_name='FFT Points',default=16) | |
92 | cohe_integr_str = models.PositiveIntegerField(verbose_name='Coh. Int. Stride',validators=[MinValueValidator(1)], default=30) |
|
92 | cohe_integr_str = models.PositiveIntegerField(verbose_name='Coh. Int. Stride',validators=[MinValueValidator(1)], default=30) | |
93 | cohe_integr = models.PositiveIntegerField(verbose_name='Coherent Integrations',validators=[MinValueValidator(1)], default=30) |
|
93 | cohe_integr = models.PositiveIntegerField(verbose_name='Coherent Integrations',validators=[MinValueValidator(1)], default=30) | |
94 | incohe_integr = models.PositiveIntegerField(verbose_name='Incoherent Integrations',validators=[MinValueValidator(1)], default=30) |
|
94 | incohe_integr = models.PositiveIntegerField(verbose_name='Incoherent Integrations',validators=[MinValueValidator(1)], default=30) | |
95 | filter = models.ForeignKey(JARSfilter, on_delete=models.CASCADE, null=True) |
|
95 | filter = models.ForeignKey(JARSfilter, on_delete=models.CASCADE, null=True) | |
96 | spectral_number = models.PositiveIntegerField(verbose_name='# Spectral Combinations',validators=[MinValueValidator(1)], default=1) |
|
96 | spectral_number = models.PositiveIntegerField(verbose_name='# Spectral Combinations',validators=[MinValueValidator(1)], default=1) | |
97 | spectral = models.CharField(verbose_name='Combinations', max_length=5000, default = '[0, 0],') |
|
97 | spectral = models.CharField(verbose_name='Combinations', max_length=5000, default = '[0, 0],') | |
98 | create_directory = models.BooleanField(verbose_name='Create Directory Per Day', default=True) |
|
98 | create_directory = models.BooleanField(verbose_name='Create Directory Per Day', default=True) | |
99 | include_expname = models.BooleanField(verbose_name='Experiment Name in Directory', default=True) |
|
99 | include_expname = models.BooleanField(verbose_name='Experiment Name in Directory', default=True) | |
100 | #acq_link = models.BooleanField(verbose_name='Acquisition Link', default=True) |
|
100 | #acq_link = models.BooleanField(verbose_name='Acquisition Link', default=True) | |
101 | #view_raw_data = models.BooleanField(verbose_name='View Raw Data', default=True) |
|
101 | #view_raw_data = models.BooleanField(verbose_name='View Raw Data', default=True) | |
102 | save_ch_dc = models.BooleanField(verbose_name='Save Channels DC', default=True) |
|
102 | save_ch_dc = models.BooleanField(verbose_name='Save Channels DC', default=True) | |
103 | save_data = models.BooleanField(verbose_name='Save Data', default=True) |
|
103 | save_data = models.BooleanField(verbose_name='Save Data', default=True) | |
104 | filter_parms = models.CharField(max_length=10000, default='{}') |
|
104 | filter_parms = models.CharField(max_length=10000, default='{}') | |
105 |
|
105 | |||
106 | class Meta: |
|
106 | class Meta: | |
107 | db_table = 'jars_configurations' |
|
107 | db_table = 'jars_configurations' | |
108 |
|
108 | |||
109 | def parms_to_dict(self): |
|
109 | def parms_to_dict(self): | |
110 |
|
110 | |||
111 | parameters = {} |
|
111 | parameters = {} | |
112 |
|
112 | |||
113 | parameters['device_id'] = self.device.id |
|
113 | parameters['device_id'] = self.device.id | |
114 | parameters['name'] = self.name |
|
114 | parameters['name'] = self.name | |
115 | #parameters['rc'] = self.rc.name |
|
115 | #parameters['rc'] = self.rc.name | |
116 | parameters['exp_type'] = self.exp_type |
|
116 | parameters['exp_type'] = self.exp_type | |
117 | parameters['exptype'] = EXPERIMENT_TYPE[self.exp_type][1] |
|
117 | parameters['exptype'] = EXPERIMENT_TYPE[self.exp_type][1] | |
118 | parameters['cards_number'] = self.cards_number |
|
118 | parameters['cards_number'] = self.cards_number | |
119 | parameters['channels_number'] = self.channels_number |
|
119 | parameters['channels_number'] = self.channels_number | |
120 | parameters['channels'] = self.channels |
|
120 | parameters['channels'] = self.channels | |
121 | parameters['rd_directory'] = self.rd_directory |
|
121 | parameters['rd_directory'] = self.rd_directory | |
122 | #parameters['raw_data_blocks'] = self.raw_data_blocks |
|
122 | #parameters['raw_data_blocks'] = self.raw_data_blocks | |
123 | parameters['data_type'] = self.data_type |
|
123 | parameters['data_type'] = self.data_type | |
124 | parameters['cohe_integr_str'] = self.cohe_integr_str |
|
124 | parameters['cohe_integr_str'] = self.cohe_integr_str | |
125 | parameters['acq_profiles'] = self.acq_profiles |
|
125 | parameters['acq_profiles'] = self.acq_profiles | |
126 | parameters['profiles_block'] = self.profiles_block |
|
126 | parameters['profiles_block'] = self.profiles_block | |
127 | parameters['ftp_interval'] = self.ftp_interval |
|
127 | parameters['ftp_interval'] = self.ftp_interval | |
128 | parameters['fftpoints'] = self.fftpoints |
|
128 | parameters['fftpoints'] = self.fftpoints | |
129 | parameters['cohe_integr'] = self.cohe_integr |
|
129 | parameters['cohe_integr'] = self.cohe_integr | |
130 | #parameters['incohe_integr'] = self.incohe_integr |
|
130 | #parameters['incohe_integr'] = self.incohe_integr | |
131 | parameters['filter'] = self.filter.name |
|
131 | parameters['filter'] = self.filter.name | |
132 | parameters['filter_parms'] = self.filter_parms |
|
132 | parameters['filter_parms'] = self.filter_parms | |
133 | #parameters['spectral_number'] = self.spectral_number |
|
133 | #parameters['spectral_number'] = self.spectral_number | |
134 | #parameters['spectral'] = self.spectral |
|
134 | #parameters['spectral'] = self.spectral | |
135 | parameters['create_directory'] = bool(self.create_directory) |
|
135 | parameters['create_directory'] = bool(self.create_directory) | |
136 | parameters['include_expname'] = bool(self.include_expname) |
|
136 | parameters['include_expname'] = bool(self.include_expname) | |
137 | #parameters['acq_link'] = bool(self.acq_link) |
|
137 | #parameters['acq_link'] = bool(self.acq_link) | |
138 | #parameters['view_raw_data'] = bool(self.view_raw_data) |
|
138 | #parameters['view_raw_data'] = bool(self.view_raw_data) | |
139 | parameters['save_ch_dc'] = bool(self.save_ch_dc) |
|
139 | parameters['save_ch_dc'] = bool(self.save_ch_dc) | |
140 | parameters['save_data'] = bool(self.save_data) |
|
140 | parameters['save_data'] = bool(self.save_data) | |
141 |
|
141 | |||
142 | if parameters['exptype'] == 'PDATA': |
|
142 | if parameters['exptype'] == 'PDATA': | |
143 | parameters['incohe_integr'] = self.incohe_integr |
|
143 | parameters['incohe_integr'] = self.incohe_integr | |
144 | parameters['spectral_number'] = self.spectral_number |
|
144 | parameters['spectral_number'] = self.spectral_number | |
145 | parameters['spectral'] = self.spectral |
|
145 | parameters['spectral'] = self.spectral | |
146 | parameters['pd_directory'] = self.pd_directory |
|
146 | parameters['pd_directory'] = self.pd_directory | |
147 |
|
147 | |||
148 | return parameters |
|
148 | return parameters | |
149 |
|
149 | |||
150 | def add_parms_to_filter(self): |
|
150 | def add_parms_to_filter(self): | |
151 | self.filter_parms = self.filter.parms_to_dict() |
|
151 | self.filter_parms = self.filter.parms_to_dict() | |
152 | self.save() |
|
152 | self.save() | |
153 |
|
153 | |||
154 | def dict_to_parms(self, parameters): |
|
154 | def dict_to_parms(self, parameters): | |
155 |
|
155 | |||
156 | self.name = parameters['name'] |
|
156 | self.name = parameters['name'] | |
157 | self.device.id = int(parameters['device_id']) |
|
157 | self.device.id = int(parameters['device_id']) | |
158 |
|
158 | |||
159 | self.exp_type = int(parameters['exp_type']) |
|
159 | self.exp_type = int(parameters['exp_type']) | |
160 | if parameters['exptype'] == 'PDATA': |
|
160 | if parameters['exptype'] == 'PDATA': | |
161 | self.incohe_integr = parameters['incohe_integr'] |
|
161 | self.incohe_integr = parameters['incohe_integr'] | |
162 | self.spectral_number = parameters['spectral_number'] |
|
162 | self.spectral_number = parameters['spectral_number'] | |
163 | self.spectral = parameters['spectral'] |
|
163 | self.spectral = parameters['spectral'] | |
164 | self.pd_directory = parameters['pd_directory'] |
|
164 | self.pd_directory = parameters['pd_directory'] | |
165 |
|
165 | |||
166 | self.cards_number = int(parameters['cards_number']) |
|
166 | self.cards_number = int(parameters['cards_number']) | |
167 | self.channels_number = int(parameters['channels_number']) |
|
167 | self.channels_number = int(parameters['channels_number']) | |
168 | self.channels = parameters['channels'] |
|
168 | self.channels = parameters['channels'] | |
169 | self.rd_directory = parameters['rd_directory'] |
|
169 | self.rd_directory = parameters['rd_directory'] | |
170 | #self.raw_data_blocks = parameters['raw_data_blocks'] |
|
170 | #self.raw_data_blocks = parameters['raw_data_blocks'] | |
171 | self.data_type = parameters['data_type'] |
|
171 | self.data_type = parameters['data_type'] | |
172 | self.cohe_integr_str = parameters['cohe_integr_str'] |
|
172 | self.cohe_integr_str = parameters['cohe_integr_str'] | |
173 | self.acq_profiles = parameters['acq_profiles'] |
|
173 | self.acq_profiles = parameters['acq_profiles'] | |
174 | self.profiles_block = parameters['profiles_block'] |
|
174 | self.profiles_block = parameters['profiles_block'] | |
175 | self.ftp_interval = parameters['ftp_interval'] |
|
175 | self.ftp_interval = parameters['ftp_interval'] | |
176 | self.fftpoints = parameters['fftpoints'] |
|
176 | self.fftpoints = parameters['fftpoints'] | |
177 | self.cohe_integr = parameters['cohe_integr'] |
|
177 | self.cohe_integr = parameters['cohe_integr'] | |
178 |
|
178 | |||
179 | filter_name = parameters['filter'] |
|
179 | filter_name = parameters['filter'] | |
180 | self.filter = JARSfilter.objects.get(name=filter_name) |
|
180 | self.filter = JARSfilter.objects.get(name=filter_name) | |
181 | self.add_parms_to_filter() |
|
181 | self.add_parms_to_filter() | |
182 | self.filter_parms = parameters['filter_parms'] |
|
182 | self.filter_parms = parameters['filter_parms'] | |
183 |
|
183 | |||
184 | self.create_directory = bool(parameters['create_directory']) |
|
184 | self.create_directory = bool(parameters['create_directory']) | |
185 | self.include_expname = bool(parameters['include_expname']) |
|
185 | self.include_expname = bool(parameters['include_expname']) | |
186 | #self.acq_link = bool(parameters['acq_link']) |
|
186 | #self.acq_link = bool(parameters['acq_link']) | |
187 | #self.view_raw_data = bool(parameters['view_raw_data']) |
|
187 | #self.view_raw_data = bool(parameters['view_raw_data']) | |
188 | self.save_ch_dc = bool(parameters['save_ch_dc']) |
|
188 | self.save_ch_dc = bool(parameters['save_ch_dc']) | |
189 | self.save_data = bool(parameters['save_data']) |
|
189 | self.save_data = bool(parameters['save_data']) | |
190 |
|
190 | |||
191 | def status_device(self): |
|
191 | def status_device(self): | |
192 |
|
192 | |||
193 | answer = api.status(self.device.ip_address,self.device.port_address) |
|
193 | answer = api.status(self.device.ip_address,self.device.port_address) | |
194 | self.device.status = int(answer[0]) |
|
194 | self.device.status = int(answer[0]) | |
195 | self.message = answer[2:] |
|
195 | self.message = answer[2:] | |
196 | self.device.save() |
|
196 | self.device.save() | |
197 |
|
197 | |||
198 | return self.device.status |
|
198 | return self.device.status | |
199 |
|
199 | |||
200 | def stop_device(self): |
|
200 | def stop_device(self): | |
201 |
|
201 | |||
202 | answer = api.stop(self.device.ip_address,self.device.port_address) |
|
202 | answer = api.stop(self.device.ip_address,self.device.port_address) | |
203 | self.device.status = int(answer[0]) |
|
203 | self.device.status = int(answer[0]) | |
204 | self.message = answer[2:] |
|
204 | self.message = answer[2:] | |
205 | self.device.save() |
|
205 | self.device.save() | |
206 |
|
206 | |||
207 | return self.device.status |
|
207 | return self.device.status | |
208 |
|
208 | |||
209 | def read_device(self): |
|
209 | def read_device(self): | |
210 |
|
210 | |||
211 | answer = api.read(self.device.ip_address,self.device.port_address) |
|
211 | answer = api.read(self.device.ip_address,self.device.port_address) | |
212 | self.device.status = int(answer[0]) |
|
212 | self.device.status = int(answer[0]) | |
213 | try: |
|
213 | try: | |
214 | data = json.loads(answer[2:]) |
|
214 | data = json.loads(answer[2:]) | |
215 | parms = data['configurations']['jars'] |
|
215 | parms = data['configurations']['jars'] | |
216 | except: |
|
216 | except: | |
217 | self.device.status = 0 |
|
217 | self.device.status = 0 | |
218 | self.device.save() |
|
218 | self.device.save() | |
219 | self.message = 'Could not read JARS configuration.' |
|
219 | self.message = 'Could not read JARS configuration.' | |
220 | return '' |
|
220 | return '' | |
221 |
|
221 | |||
222 | #self.dict_to_parms(parms) |
|
222 | #self.dict_to_parms(parms) | |
223 | self.message = 'Current JARS configuration was read successfully.' |
|
223 | self.message = 'Current JARS configuration was read successfully.' | |
224 | self.device.save() |
|
224 | self.device.save() | |
225 | return parms |
|
225 | return parms | |
226 |
|
226 | |||
227 |
|
227 | |||
228 | def write_device(self): |
|
228 | def write_device(self): | |
229 |
|
229 | |||
230 | data = self.experiment.parms_to_dict() |
|
230 | data = self.experiment.parms_to_dict() | |
231 | data = json.loads(data) |
|
231 | data = json.loads(data) | |
232 | data['configurations']['dds'] ='' |
|
232 | data['configurations']['dds'] ='' | |
233 | data['configurations']['cgs'] ='' |
|
233 | data['configurations']['cgs'] ='' | |
234 | data['configurations']['rc']['pulses']='' |
|
234 | data['configurations']['rc']['pulses']='' | |
235 | data['configurations']['rc']['delays']='' |
|
235 | data['configurations']['rc']['delays']='' | |
236 | json_data = json.dumps(data) |
|
236 | json_data = json.dumps(data) | |
237 |
|
237 | |||
238 | answer = api.configure(self.device.ip_address,self.device.port_address,json_data) |
|
238 | answer = api.configure(self.device.ip_address,self.device.port_address,json_data) | |
239 | #print answer |
|
239 | #print answer | |
240 | self.device.status = int(answer[0]) |
|
240 | self.device.status = int(answer[0]) | |
241 | self.message = answer[2:] |
|
241 | self.message = answer[2:] | |
242 |
|
242 | |||
243 | self.device.save() |
|
243 | self.device.save() | |
244 |
|
244 | |||
245 | return self.device.status |
|
245 | return self.device.status | |
246 |
|
246 | |||
247 |
|
247 | |||
|
248 | def start_device(self): | |||
|
249 | ||||
|
250 | self.write_device() | |||
|
251 | ||||
|
252 | ||||
248 | def echo(self): |
|
253 | def echo(self): | |
249 |
|
254 | |||
250 | answer = api.echo(self.device.ip_address,self.device.port_address,'(=') |
|
255 | answer = api.echo(self.device.ip_address,self.device.port_address,'(=') | |
251 | #print answer |
|
256 | #print answer | |
252 | self.device.status = int(answer[0]) |
|
257 | self.device.status = int(answer[0]) | |
253 | self.message = answer[2:] |
|
258 | self.message = answer[2:] | |
254 |
|
259 | |||
255 | self.device.save() |
|
260 | self.device.save() | |
256 |
|
261 | |||
257 | return #self.device.status |
|
262 | return #self.device.status | |
258 |
|
263 | |||
259 | def update_from_file(self, parameters): |
|
264 | def update_from_file(self, parameters): | |
260 |
|
265 | |||
261 | self.dict_to_parms(parameters) |
|
266 | self.dict_to_parms(parameters) | |
262 | self.save() |
|
267 | self.save() | |
263 |
|
268 | |||
264 | def get_absolute_url_import(self): |
|
269 | def get_absolute_url_import(self): | |
265 | return reverse('url_import_jars_conf', args=[str(self.id)]) |
|
270 | return reverse('url_import_jars_conf', args=[str(self.id)]) | |
266 |
|
271 | |||
267 | def get_absolute_url_read(self): |
|
272 | def get_absolute_url_read(self): | |
268 | return reverse('url_read_jars_conf', args=[str(self.id)]) No newline at end of file |
|
273 | return reverse('url_read_jars_conf', args=[str(self.id)]) |
@@ -1,386 +1,384 | |||||
1 | import os |
|
1 | import os | |
2 | import ast |
|
|||
3 | import json |
|
2 | import json | |
4 |
|
3 | |||
5 | from django import forms |
|
4 | from django import forms | |
6 | from django.utils.safestring import mark_safe |
|
5 | from django.utils.safestring import mark_safe | |
7 | from apps.main.models import Device |
|
6 | from apps.main.models import Device | |
8 | from apps.main.forms import add_empty_choice |
|
7 | from apps.main.forms import add_empty_choice | |
9 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode |
|
8 | from .models import RCConfiguration, RCLine, RCLineType, RCLineCode | |
10 | from .widgets import KmUnitWidget, KmUnitHzWidget, KmUnitDcWidget, UnitKmWidget, DefaultWidget, CodesWidget, HiddenWidget, HCheckboxSelectMultiple |
|
9 | from .widgets import KmUnitWidget, KmUnitHzWidget, KmUnitDcWidget, UnitKmWidget, DefaultWidget, CodesWidget, HiddenWidget, HCheckboxSelectMultiple | |
11 |
|
10 | |||
12 | def create_choices_from_model(model, conf_id, all_choice=False): |
|
11 | def create_choices_from_model(model, conf_id, all_choice=False): | |
13 |
|
12 | |||
14 | if model=='RCLine': |
|
13 | if model=='RCLine': | |
15 | instance = RCConfiguration.objects.get(pk=conf_id) |
|
14 | instance = RCConfiguration.objects.get(pk=conf_id) | |
16 | choices = [(line.pk, line.get_name()) for line in instance.get_lines(line_type__name='tx')] |
|
15 | choices = [(line.pk, line.get_name()) for line in instance.get_lines(line_type__name='tx')] | |
17 | if all_choice: |
|
16 | if all_choice: | |
18 | choices = add_empty_choice(choices, label='All') |
|
17 | choices = add_empty_choice(choices, label='All') | |
19 | else: |
|
18 | else: | |
20 | instance = globals()[model] |
|
19 | instance = globals()[model] | |
21 | choices = instance.objects.all().values_list('pk', 'name') |
|
20 | choices = instance.objects.all().values_list('pk', 'name') | |
22 |
|
21 | |||
23 | return choices |
|
22 | return choices | |
24 |
|
23 | |||
25 |
|
24 | |||
26 | class ExtFileField(forms.FileField): |
|
25 | class ExtFileField(forms.FileField): | |
27 | """ |
|
26 | """ | |
28 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
27 | Same as forms.FileField, but you can specify a file extension whitelist. | |
29 |
|
28 | |||
30 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
29 | >>> from django.core.files.uploadedfile import SimpleUploadedFile | |
31 | >>> |
|
30 | >>> | |
32 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
31 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) | |
33 | >>> |
|
32 | >>> | |
34 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
33 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) | |
35 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
34 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) | |
36 | >>> |
|
35 | >>> | |
37 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
36 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) | |
38 | Traceback (most recent call last): |
|
37 | Traceback (most recent call last): | |
39 | ... |
|
38 | ... | |
40 | ValidationError: [u'Not allowed filetype!'] |
|
39 | ValidationError: [u'Not allowed filetype!'] | |
41 | """ |
|
40 | """ | |
42 | def __init__(self, *args, **kwargs): |
|
41 | def __init__(self, *args, **kwargs): | |
43 | extensions = kwargs.pop("extensions") |
|
42 | extensions = kwargs.pop("extensions") | |
44 | self.extensions = [i.lower() for i in extensions] |
|
43 | self.extensions = [i.lower() for i in extensions] | |
45 |
|
44 | |||
46 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
45 | super(ExtFileField, self).__init__(*args, **kwargs) | |
47 |
|
46 | |||
48 | def clean(self, *args, **kwargs): |
|
47 | def clean(self, *args, **kwargs): | |
49 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
48 | data = super(ExtFileField, self).clean(*args, **kwargs) | |
50 | filename = data.name |
|
49 | filename = data.name | |
51 | ext = os.path.splitext(filename)[1] |
|
50 | ext = os.path.splitext(filename)[1] | |
52 | ext = ext.lower() |
|
51 | ext = ext.lower() | |
53 | if ext not in self.extensions: |
|
52 | if ext not in self.extensions: | |
54 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
53 | raise forms.ValidationError('Not allowed file type: %s' % ext) | |
55 |
|
54 | |||
56 |
|
55 | |||
57 | class RCConfigurationForm(forms.ModelForm): |
|
56 | class RCConfigurationForm(forms.ModelForm): | |
58 |
|
57 | |||
59 | def __init__(self, *args, **kwargs): |
|
58 | def __init__(self, *args, **kwargs): | |
60 | super(RCConfigurationForm, self).__init__(*args, **kwargs) |
|
59 | super(RCConfigurationForm, self).__init__(*args, **kwargs) | |
61 |
|
60 | |||
62 | instance = getattr(self, 'instance', None) |
|
61 | instance = getattr(self, 'instance', None) | |
63 |
|
62 | |||
64 | if instance and instance.pk: |
|
63 | if instance and instance.pk: | |
65 |
|
64 | |||
66 | devices = Device.objects.filter(device_type__name='rc') |
|
65 | devices = Device.objects.filter(device_type__name='rc') | |
67 | if instance.experiment: |
|
66 | if instance.experiment: | |
68 | self.fields['experiment'].widget.attrs['read_only'] = True |
|
67 | self.fields['experiment'].widget.attrs['read_only'] = True | |
69 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] |
|
68 | #self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)] | |
70 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] |
|
69 | self.fields['device'].widget.choices = [(device.id, device) for device in devices] | |
71 | self.fields['ipp'].widget = KmUnitHzWidget(attrs={'km2unit':instance.km2unit}) |
|
70 | self.fields['ipp'].widget = KmUnitHzWidget(attrs={'km2unit':instance.km2unit}) | |
72 | self.fields['clock'].widget.attrs['readonly'] = True |
|
71 | self.fields['clock'].widget.attrs['readonly'] = True | |
73 |
|
72 | |||
74 | self.fields['time_before'].label = mark_safe(self.fields['time_before'].label) |
|
73 | self.fields['time_before'].label = mark_safe(self.fields['time_before'].label) | |
75 | self.fields['time_after'].label = mark_safe(self.fields['time_after'].label) |
|
74 | self.fields['time_after'].label = mark_safe(self.fields['time_after'].label) | |
76 |
|
75 | |||
77 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): |
|
76 | if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'): | |
78 | self.fields['experiment'].widget.attrs['readonly'] = True |
|
77 | self.fields['experiment'].widget.attrs['readonly'] = True | |
79 |
|
78 | |||
80 | class Meta: |
|
79 | class Meta: | |
81 | model = RCConfiguration |
|
80 | model = RCConfiguration | |
82 | exclude = ('type', 'parameters', 'status', 'total_units', 'mix') |
|
81 | exclude = ('type', 'parameters', 'status', 'total_units', 'mix') | |
83 |
|
82 | |||
84 | def clean(self): |
|
83 | def clean(self): | |
85 | form_data = super(RCConfigurationForm, self).clean() |
|
84 | form_data = super(RCConfigurationForm, self).clean() | |
86 |
|
85 | |||
87 | if 'clock_divider' in form_data: |
|
86 | if 'clock_divider' in form_data: | |
88 | if form_data['clock_divider']<1: |
|
87 | if form_data['clock_divider']<1: | |
89 | self.add_error('clock_divider', 'Invalid Value') |
|
88 | self.add_error('clock_divider', 'Invalid Value') | |
90 | else: |
|
89 | else: | |
91 | if form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider']))%10<>0: |
|
90 | if form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider']))%10<>0: | |
92 | self.add_error('ipp', 'Invalid IPP units={}'.format(form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider'])))) |
|
91 | self.add_error('ipp', 'Invalid IPP units={}'.format(form_data['ipp']*(20./3*(form_data['clock_in']/form_data['clock_divider'])))) | |
93 |
|
92 | |||
94 | return form_data |
|
93 | return form_data | |
95 |
|
94 | |||
96 | def save(self): |
|
95 | def save(self): | |
97 | conf = super(RCConfigurationForm, self).save() |
|
96 | conf = super(RCConfigurationForm, self).save() | |
98 | conf.total_units = conf.ipp*conf.ntx*conf.km2unit |
|
97 | conf.total_units = conf.ipp*conf.ntx*conf.km2unit | |
99 | conf.save() |
|
98 | conf.save() | |
100 | return conf |
|
99 | return conf | |
101 |
|
100 | |||
102 |
|
101 | |||
103 | class RCMixConfigurationForm(forms.Form): |
|
102 | class RCMixConfigurationForm(forms.Form): | |
104 |
|
103 | |||
105 | clock_in = forms.CharField(widget=forms.HiddenInput()) |
|
104 | clock_in = forms.CharField(widget=forms.HiddenInput()) | |
106 | clock_divider = forms.CharField(widget=forms.HiddenInput()) |
|
105 | clock_divider = forms.CharField(widget=forms.HiddenInput()) | |
107 | name = forms.CharField() |
|
106 | name = forms.CharField() | |
108 | experiment = forms.ChoiceField() |
|
107 | experiment = forms.ChoiceField() | |
109 | mode = forms.ChoiceField(widget=forms.RadioSelect(), |
|
108 | mode = forms.ChoiceField(widget=forms.RadioSelect(), | |
110 | choices=[(0, 'Parallel'), (1, 'Sequence')], |
|
109 | choices=[(0, 'Parallel'), (1, 'Sequence')], | |
111 | initial=0) |
|
110 | initial=0) | |
112 | operation = forms.ChoiceField(widget=forms.RadioSelect(), |
|
111 | operation = forms.ChoiceField(widget=forms.RadioSelect(), | |
113 | choices=[(0, 'OR'), (1, 'XOR'), (2, 'AND'), (3, 'NAND')], |
|
112 | choices=[(0, 'OR'), (1, 'XOR'), (2, 'AND'), (3, 'NAND')], | |
114 | initial=1) |
|
113 | initial=1) | |
115 | delay = forms.CharField() |
|
114 | delay = forms.CharField() | |
116 | mask = forms.MultipleChoiceField(choices=[(0, 'L1'),(1, 'L2'),(2, 'L3'),(3, 'L4'),(4, 'L5'),(5, 'L6'),(6, 'L7'),(7, 'L8')], |
|
115 | mask = forms.MultipleChoiceField(choices=[(0, 'L1'),(1, 'L2'),(2, 'L3'),(3, 'L4'),(4, 'L5'),(5, 'L6'),(6, 'L7'),(7, 'L8')], | |
117 | widget=HCheckboxSelectMultiple()) |
|
116 | widget=HCheckboxSelectMultiple()) | |
118 | result = forms.CharField(required=False, |
|
117 | result = forms.CharField(required=False, | |
119 | widget=forms.Textarea(attrs={'readonly':True, 'rows':5, 'class':'tabuled'})) |
|
118 | widget=forms.Textarea(attrs={'readonly':True, 'rows':5, 'class':'tabuled'})) | |
120 |
|
119 | |||
121 | def __init__(self, *args, **kwargs): |
|
120 | def __init__(self, *args, **kwargs): | |
122 | confs = kwargs.pop('confs', []) |
|
121 | confs = kwargs.pop('confs', []) | |
123 | if confs: |
|
122 | if confs: | |
124 | km2unit = confs[0].km2unit |
|
123 | km2unit = confs[0].km2unit | |
125 | clock_in = confs[0].clock_in |
|
124 | clock_in = confs[0].clock_in | |
126 | clock_divider = confs[0].clock_divider |
|
125 | clock_divider = confs[0].clock_divider | |
127 | else: |
|
126 | else: | |
128 | km2unit = clock_in = clock_divider = 0 |
|
127 | km2unit = clock_in = clock_divider = 0 | |
129 | super(RCMixConfigurationForm, self).__init__(*args, **kwargs) |
|
128 | super(RCMixConfigurationForm, self).__init__(*args, **kwargs) | |
130 | self.fields['experiment'].choices = [(conf.pk, '{} | {}'.format(conf.pk, conf.name)) for conf in confs] |
|
129 | self.fields['experiment'].choices = [(conf.pk, '{} | {}'.format(conf.pk, conf.name)) for conf in confs] | |
131 | self.fields['delay'].widget = KmUnitWidget(attrs = {'km2unit':km2unit}) |
|
130 | self.fields['delay'].widget = KmUnitWidget(attrs = {'km2unit':km2unit}) | |
132 | self.fields['clock_in'].initial = clock_in |
|
131 | self.fields['clock_in'].initial = clock_in | |
133 | self.fields['clock_divider'].initial = clock_divider |
|
132 | self.fields['clock_divider'].initial = clock_divider | |
134 |
|
133 | |||
135 |
|
134 | |||
136 | class RCLineForm(forms.ModelForm): |
|
135 | class RCLineForm(forms.ModelForm): | |
137 |
|
136 | |||
138 | def __init__(self, *args, **kwargs): |
|
137 | def __init__(self, *args, **kwargs): | |
139 | self.extra_fields = kwargs.pop('extra_fields', []) |
|
138 | self.extra_fields = kwargs.pop('extra_fields', []) | |
140 | super(RCLineForm, self).__init__(*args, **kwargs) |
|
139 | super(RCLineForm, self).__init__(*args, **kwargs) | |
141 |
|
140 | |||
142 | if 'initial' in kwargs and 'line_type' in kwargs['initial']: |
|
141 | if 'initial' in kwargs and 'line_type' in kwargs['initial']: | |
143 | line_type = RCLineType.objects.get(pk=kwargs['initial']['line_type']) |
|
142 | line_type = RCLineType.objects.get(pk=kwargs['initial']['line_type']) | |
144 |
|
143 | |||
145 | if 'code_id' in kwargs['initial']: |
|
144 | if 'code_id' in kwargs['initial']: | |
146 | model_initial = kwargs['initial']['code_id'] |
|
145 | model_initial = kwargs['initial']['code_id'] | |
147 | else: |
|
146 | else: | |
148 | model_initial = 0 |
|
147 | model_initial = 0 | |
149 |
|
148 | |||
150 | params = json.loads(line_type.params) |
|
149 | params = json.loads(line_type.params) | |
151 |
|
150 | |||
152 | for label, value in self.extra_fields.items(): |
|
151 | for label, value in self.extra_fields.items(): | |
153 | if label=='params': |
|
152 | if label=='params': | |
154 | continue |
|
153 | continue | |
155 |
|
154 | |||
156 | if 'model' in params[label]: |
|
155 | if 'model' in params[label]: | |
157 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], |
|
156 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], | |
158 | kwargs['initial']['rc_configuration']), |
|
157 | kwargs['initial']['rc_configuration']), | |
159 | initial=model_initial) |
|
158 | initial=model_initial) | |
160 |
|
159 | |||
161 |
|
160 | |||
162 | else: |
|
161 | else: | |
163 | if label=='codes' and 'code_id' in kwargs['initial']: |
|
162 | if label=='codes' and 'code_id' in kwargs['initial']: | |
164 | self.fields[label] = forms.CharField(initial=RCLineCode.objects.get(pk=kwargs['initial']['code_id']).codes) |
|
163 | self.fields[label] = forms.CharField(initial=RCLineCode.objects.get(pk=kwargs['initial']['code_id']).codes) | |
165 | else: |
|
164 | else: | |
166 | self.fields[label] = forms.CharField(initial=value['value']) |
|
165 | self.fields[label] = forms.CharField(initial=value['value']) | |
167 |
|
166 | |||
168 | if label=='codes': |
|
167 | if label=='codes': | |
169 | self.fields[label].widget = CodesWidget() |
|
168 | self.fields[label].widget = CodesWidget() | |
170 |
|
169 | |||
171 | if self.data: |
|
170 | if self.data: | |
172 | line_type = RCLineType.objects.get(pk=self.data['line_type']) |
|
171 | line_type = RCLineType.objects.get(pk=self.data['line_type']) | |
173 |
|
172 | |||
174 | if 'code_id' in self.data: |
|
173 | if 'code_id' in self.data: | |
175 | model_initial = self.data['code_id'] |
|
174 | model_initial = self.data['code_id'] | |
176 | else: |
|
175 | else: | |
177 | model_initial = 0 |
|
176 | model_initial = 0 | |
178 |
|
177 | |||
179 | params = json.loads(line_type.params) |
|
178 | params = json.loads(line_type.params) | |
180 |
|
179 | |||
181 | for label, value in self.extra_fields.items(): |
|
180 | for label, value in self.extra_fields.items(): | |
182 | if label=='params': |
|
181 | if label=='params': | |
183 | continue |
|
182 | continue | |
184 |
|
183 | |||
185 | if 'model' in params[label]: |
|
184 | if 'model' in params[label]: | |
186 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], |
|
185 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], | |
187 | self.data['rc_configuration']), |
|
186 | self.data['rc_configuration']), | |
188 | initial=model_initial) |
|
187 | initial=model_initial) | |
189 |
|
188 | |||
190 |
|
189 | |||
191 | else: |
|
190 | else: | |
192 | if label=='codes' and 'code' in self.data: |
|
191 | if label=='codes' and 'code' in self.data: | |
193 | self.fields[label] = forms.CharField(initial=self.data['codes']) |
|
192 | self.fields[label] = forms.CharField(initial=self.data['codes']) | |
194 | else: |
|
193 | else: | |
195 | self.fields[label] = forms.CharField(initial=self.data[label]) |
|
194 | self.fields[label] = forms.CharField(initial=self.data[label]) | |
196 |
|
195 | |||
197 | if label=='codes': |
|
196 | if label=='codes': | |
198 | self.fields[label].widget = CodesWidget() |
|
197 | self.fields[label].widget = CodesWidget() | |
199 |
|
198 | |||
200 |
|
199 | |||
201 | class Meta: |
|
200 | class Meta: | |
202 | model = RCLine |
|
201 | model = RCLine | |
203 | fields = ('rc_configuration', 'line_type', 'channel') |
|
202 | fields = ('rc_configuration', 'line_type', 'channel') | |
204 | widgets = { |
|
203 | widgets = { | |
205 | 'channel': forms.HiddenInput(), |
|
204 | 'channel': forms.HiddenInput(), | |
206 | } |
|
205 | } | |
207 |
|
206 | |||
208 |
|
207 | |||
209 | def clean(self): |
|
208 | def clean(self): | |
210 |
|
209 | |||
211 | form_data = self.cleaned_data |
|
210 | form_data = self.cleaned_data | |
212 | if 'code' in self.data and self.data['TX_ref']=="0": |
|
211 | if 'code' in self.data and self.data['TX_ref']=="0": | |
213 | self.add_error('TX_ref', 'Choose a valid TX reference') |
|
212 | self.add_error('TX_ref', 'Choose a valid TX reference') | |
214 |
|
213 | |||
215 | if RCLineType.objects.get(pk=self.data['line_type']).name=='mix': |
|
214 | if RCLineType.objects.get(pk=self.data['line_type']).name=='mix': | |
216 | self.add_error('line_type', 'Invalid Line type') |
|
215 | self.add_error('line_type', 'Invalid Line type') | |
217 |
|
216 | |||
218 | return form_data |
|
217 | return form_data | |
219 |
|
218 | |||
220 |
|
219 | |||
221 | def save(self): |
|
220 | def save(self): | |
222 | line = super(RCLineForm, self).save() |
|
221 | line = super(RCLineForm, self).save() | |
223 |
|
222 | |||
224 | #auto add channel |
|
223 | #auto add channel | |
225 | line.channel = RCLine.objects.filter(rc_configuration=line.rc_configuration).count()-1 |
|
224 | line.channel = RCLine.objects.filter(rc_configuration=line.rc_configuration).count()-1 | |
226 |
|
225 | |||
227 | #auto add position for TX, TR & CODE |
|
226 | #auto add position for TX, TR & CODE | |
228 | if line.line_type.name in ('tx', ): |
|
227 | if line.line_type.name in ('tx', ): | |
229 | line.position = RCLine.objects.filter(rc_configuration=line.rc_configuration, line_type=line.line_type).count()-1 |
|
228 | line.position = RCLine.objects.filter(rc_configuration=line.rc_configuration, line_type=line.line_type).count()-1 | |
230 |
|
229 | |||
231 | #save extra fields in params |
|
230 | #save extra fields in params | |
232 | params = {} |
|
231 | params = {} | |
233 | for label, value in self.extra_fields.items(): |
|
232 | for label, value in self.extra_fields.items(): | |
234 | if label=='params': |
|
233 | if label=='params': | |
235 | params['params'] = [] |
|
234 | params['params'] = [] | |
236 | elif label=='codes': |
|
235 | elif label=='codes': | |
237 | params[label] = [s for s in self.data[label].split('\r\n') if s] |
|
236 | params[label] = [s for s in self.data[label].split('\r\n') if s] | |
238 | else: |
|
237 | else: | |
239 | params[label] = self.data[label] |
|
238 | params[label] = self.data[label] | |
240 | line.params = json.dumps(params) |
|
239 | line.params = json.dumps(params) | |
241 | line.save() |
|
240 | line.save() | |
242 | return |
|
241 | return | |
243 |
|
242 | |||
244 |
|
243 | |||
245 | class RCLineViewForm(forms.Form): |
|
244 | class RCLineViewForm(forms.Form): | |
246 |
|
245 | |||
247 | def __init__(self, *args, **kwargs): |
|
246 | def __init__(self, *args, **kwargs): | |
248 |
|
247 | |||
249 | extra_fields = kwargs.pop('extra_fields') |
|
248 | extra_fields = kwargs.pop('extra_fields') | |
250 | line = kwargs.pop('line') |
|
249 | line = kwargs.pop('line') | |
251 | subform = kwargs.pop('subform', False) |
|
250 | subform = kwargs.pop('subform', False) | |
252 | super(RCLineViewForm, self).__init__(*args, **kwargs) |
|
251 | super(RCLineViewForm, self).__init__(*args, **kwargs) | |
253 |
|
252 | |||
254 | if subform: |
|
253 | if subform: | |
255 | params = json.loads(line.line_type.params)['params'] |
|
254 | params = json.loads(line.line_type.params)['params'] | |
256 | else: |
|
255 | else: | |
257 | params = json.loads(line.line_type.params) |
|
256 | params = json.loads(line.line_type.params) | |
258 |
|
257 | |||
259 | for label, value in extra_fields.items(): |
|
258 | for label, value in extra_fields.items(): | |
260 |
|
259 | |||
261 | if label=='params': |
|
260 | if label=='params': | |
262 | continue |
|
261 | continue | |
263 | if 'ref' in label: |
|
262 | if 'ref' in label: | |
264 | if value in (0, '0'): |
|
263 | if value in (0, '0'): | |
265 | value = 'All' |
|
264 | value = 'All' | |
266 | else: |
|
265 | else: | |
267 | value = RCLine.objects.get(pk=value).get_name() |
|
266 | value = RCLine.objects.get(pk=value).get_name() | |
268 | elif label=='code': |
|
267 | elif label=='code': | |
269 | value = RCLineCode.objects.get(pk=value).name |
|
268 | value = RCLineCode.objects.get(pk=value).name | |
270 |
|
269 | |||
271 | self.fields[label] = forms.CharField(initial=value) |
|
270 | self.fields[label] = forms.CharField(initial=value) | |
272 |
|
271 | |||
273 | if 'widget' in params[label]: |
|
272 | if 'widget' in params[label]: | |
274 | km2unit = line.rc_configuration.km2unit |
|
273 | km2unit = line.rc_configuration.km2unit | |
275 | if params[label]['widget']=='km': |
|
274 | if params[label]['widget']=='km': | |
276 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
275 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
277 | elif params[label]['widget']=='unit': |
|
276 | elif params[label]['widget']=='unit': | |
278 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
277 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
279 | elif params[label]['widget']=='dc': |
|
278 | elif params[label]['widget']=='dc': | |
280 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
279 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
281 | elif params[label]['widget']=='codes': |
|
280 | elif params[label]['widget']=='codes': | |
282 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) |
|
281 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'disabled':True}) | |
283 | else: |
|
282 | else: | |
284 | self.fields[label].widget = DefaultWidget(attrs={'disabled':True}) |
|
283 | self.fields[label].widget = DefaultWidget(attrs={'disabled':True}) | |
285 |
|
284 | |||
286 |
|
285 | |||
287 | class RCLineEditForm(forms.ModelForm): |
|
286 | class RCLineEditForm(forms.ModelForm): | |
288 |
|
287 | |||
289 | def __init__(self, *args, **kwargs): |
|
288 | def __init__(self, *args, **kwargs): | |
290 |
|
289 | |||
291 | extra_fields = kwargs.pop('extra_fields', []) |
|
290 | extra_fields = kwargs.pop('extra_fields', []) | |
292 | conf = kwargs.pop('conf', False) |
|
291 | conf = kwargs.pop('conf', False) | |
293 | line = kwargs.pop('line') |
|
292 | line = kwargs.pop('line') | |
294 | subform = kwargs.pop('subform', False) |
|
293 | subform = kwargs.pop('subform', False) | |
295 |
|
294 | |||
296 | super(RCLineEditForm, self).__init__(*args, **kwargs) |
|
295 | super(RCLineEditForm, self).__init__(*args, **kwargs) | |
297 |
|
296 | |||
298 | if subform is not False: |
|
297 | if subform is not False: | |
299 | params = json.loads(line.line_type.params)['params'] |
|
298 | params = json.loads(line.line_type.params)['params'] | |
300 | count = subform |
|
299 | count = subform | |
301 | else: |
|
300 | else: | |
302 | params = json.loads(line.line_type.params) |
|
301 | params = json.loads(line.line_type.params) | |
303 | count = -1 |
|
302 | count = -1 | |
304 |
|
303 | |||
305 | for label, value in extra_fields.items(): |
|
304 | for label, value in extra_fields.items(): | |
306 |
|
305 | |||
307 | if label in ('params',): |
|
306 | if label in ('params',): | |
308 | continue |
|
307 | continue | |
309 | if 'help' in params[label]: |
|
308 | if 'help' in params[label]: | |
310 | help_text = params[label]['help'] |
|
309 | help_text = params[label]['help'] | |
311 | else: |
|
310 | else: | |
312 | help_text = '' |
|
311 | help_text = '' | |
313 |
|
312 | |||
314 | if 'model' in params[label]: |
|
313 | if 'model' in params[label]: | |
315 | if line.line_type.name=='tr': |
|
314 | if line.line_type.name=='tr': | |
316 | all_choice = True |
|
315 | all_choice = True | |
317 | else: |
|
316 | else: | |
318 | all_choice = False |
|
317 | all_choice = False | |
319 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], conf.id, all_choice=all_choice), |
|
318 | self.fields[label] = forms.ChoiceField(choices=create_choices_from_model(params[label]['model'], conf.id, all_choice=all_choice), | |
320 | initial=value, |
|
319 | initial=value, | |
321 | widget=forms.Select(attrs={'name':'%s|%s|%s' % (count, line.id, label)}), |
|
320 | widget=forms.Select(attrs={'name':'%s|%s|%s' % (count, line.id, label)}), | |
322 | help_text=help_text) |
|
321 | help_text=help_text) | |
323 |
|
322 | |||
324 | else: |
|
323 | else: | |
325 |
|
||||
326 | self.fields[label] = forms.CharField(initial=value, help_text=help_text) |
|
324 | self.fields[label] = forms.CharField(initial=value, help_text=help_text) | |
327 |
|
325 | |||
328 | if label in ('code', ): |
|
326 | if label in ('code', ): | |
329 | self.fields[label].widget = HiddenWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) |
|
327 | self.fields[label].widget = HiddenWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) | |
330 |
|
328 | |||
331 | elif 'widget' in params[label]: |
|
329 | elif 'widget' in params[label]: | |
332 | km2unit = line.rc_configuration.km2unit |
|
330 | km2unit = line.rc_configuration.km2unit | |
333 | if params[label]['widget']=='km': |
|
331 | if params[label]['widget']=='km': | |
334 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
332 | self.fields[label].widget = KmUnitWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
335 | elif params[label]['widget']=='unit': |
|
333 | elif params[label]['widget']=='unit': | |
336 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
334 | self.fields[label].widget = UnitKmWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
337 | elif params[label]['widget']=='dc': |
|
335 | elif params[label]['widget']=='dc': | |
338 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
336 | self.fields[label].widget = KmUnitDcWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
339 | elif params[label]['widget']=='codes': |
|
337 | elif params[label]['widget']=='codes': | |
340 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) |
|
338 | self.fields[label].widget = CodesWidget(attrs={'line':line, 'km2unit':km2unit, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
341 | else: |
|
339 | else: | |
342 | self.fields[label].widget = DefaultWidget(attrs={'name':'%s|%s|%s' % (count, line.id, label)}) |
|
340 | self.fields[label].widget = DefaultWidget(attrs={'line':line, 'name':'%s|%s|%s' % (count, line.id, label)}) | |
343 |
|
341 | |||
344 |
|
342 | |||
345 | class Meta: |
|
343 | class Meta: | |
346 | model = RCLine |
|
344 | model = RCLine | |
347 | exclude = ('rc_configuration', 'line_type', 'channel', 'position', 'params', 'pulses') |
|
345 | exclude = ('rc_configuration', 'line_type', 'channel', 'position', 'params', 'pulses') | |
348 |
|
346 | |||
349 |
|
347 | |||
350 | class RCSubLineEditForm(forms.Form): |
|
348 | class RCSubLineEditForm(forms.Form): | |
351 |
|
349 | |||
352 | def __init__(self, *args, **kwargs): |
|
350 | def __init__(self, *args, **kwargs): | |
353 | extra_fields = kwargs.pop('extra_fields') |
|
351 | extra_fields = kwargs.pop('extra_fields') | |
354 | count = kwargs.pop('count') |
|
352 | count = kwargs.pop('count') | |
355 | line = kwargs.pop('line') |
|
353 | line = kwargs.pop('line') | |
356 | super(RCSubLineEditForm, self).__init__(*args, **kwargs) |
|
354 | super(RCSubLineEditForm, self).__init__(*args, **kwargs) | |
357 | for label, value in extra_fields.items(): |
|
355 | for label, value in extra_fields.items(): | |
358 | self.fields[label] = forms.CharField(initial=value, |
|
356 | self.fields[label] = forms.CharField(initial=value, | |
359 | widget=forms.TextInput(attrs={'name':'%s|%s|%s' % (count, line, label)})) |
|
357 | widget=forms.TextInput(attrs={'name':'%s|%s|%s' % (count, line, label)})) | |
360 |
|
358 | |||
361 |
|
359 | |||
362 | class RCImportForm(forms.Form): |
|
360 | class RCImportForm(forms.Form): | |
363 |
|
361 | |||
364 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) |
|
362 | file_name = ExtFileField(extensions=['.racp', '.json', '.dat']) | |
365 |
|
363 | |||
366 |
|
364 | |||
367 | class RCLineCodesForm(forms.ModelForm): |
|
365 | class RCLineCodesForm(forms.ModelForm): | |
368 |
|
366 | |||
369 | def __init__(self, *args, **kwargs): |
|
367 | def __init__(self, *args, **kwargs): | |
370 | super(RCLineCodesForm, self).__init__(*args, **kwargs) |
|
368 | super(RCLineCodesForm, self).__init__(*args, **kwargs) | |
371 |
|
369 | |||
372 | if 'initial' in kwargs: |
|
370 | if 'initial' in kwargs: | |
373 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), |
|
371 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), | |
374 | initial=kwargs['initial']['code']) |
|
372 | initial=kwargs['initial']['code']) | |
375 | if 'instance' in kwargs: |
|
373 | if 'instance' in kwargs: | |
376 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), |
|
374 | self.fields['code'] = forms.ChoiceField(choices=RCLineCode.objects.all().values_list('pk', 'name'), | |
377 | initial=kwargs['instance'].pk) |
|
375 | initial=kwargs['instance'].pk) | |
378 |
|
376 | |||
379 | self.fields['codes'].widget = CodesWidget() |
|
377 | self.fields['codes'].widget = CodesWidget() | |
380 |
|
378 | |||
381 |
|
379 | |||
382 | class Meta: |
|
380 | class Meta: | |
383 | model = RCLineCode |
|
381 | model = RCLineCode | |
384 | exclude = ('name',) |
|
382 | exclude = ('name',) | |
385 |
|
383 | |||
386 | No newline at end of file |
|
384 |
@@ -1,396 +1,399 | |||||
1 | ''' |
|
1 | ''' | |
2 | Created on Dec 2, 2014 |
|
2 | Created on Dec 2, 2014 | |
3 |
|
3 | |||
4 | @author: Miguel Urco |
|
4 | @author: Miguel Urco | |
5 | ''' |
|
5 | ''' | |
6 | import time |
|
6 | import time | |
7 | import struct |
|
7 | import struct | |
8 | import socket |
|
8 | import socket | |
9 |
|
9 | |||
10 | DEBUG = False |
|
10 | DEBUG = False | |
11 | CMD_RESET =0X01 |
|
11 | CMD_RESET =0X01 | |
12 | CMD_ENABLE =0X02 |
|
12 | CMD_ENABLE =0X02 | |
13 | CMD_CHANGEIP =0X03 |
|
13 | CMD_CHANGEIP =0X03 | |
14 |
|
14 | |||
15 | IdClass={ |
|
15 | IdClass={ | |
16 | "rc" : 0x01, |
|
16 | "rc" : 0x01, | |
17 | "dds" : 0x02, |
|
17 | "dds" : 0x02, | |
18 | "jars" : 0x03, |
|
18 | "jars" : 0x03, | |
19 | "usrp" : 0x04, |
|
19 | "usrp" : 0x04, | |
20 | "echotek" : 0x05, |
|
20 | "echotek" : 0x05, | |
21 | "abs" : 0x06, |
|
21 | "abs" : 0x06, | |
22 | "clk_gen" : 0x07 |
|
22 | "clk_gen" : 0x07 | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | def ascii2hex(cadena): |
|
25 | def ascii2hex(cadena): | |
26 |
|
26 | |||
27 | hex_cad = '' |
|
27 | hex_cad = '' | |
28 | for c in cadena: |
|
28 | for c in cadena: | |
29 | hex_cad += hex(ord(c))[2:].rjust(2,'0') + ' ' |
|
29 | hex_cad += hex(ord(c))[2:].rjust(2,'0') + ' ' | |
30 |
|
30 | |||
31 | return hex_cad |
|
31 | return hex_cad | |
32 |
|
32 | |||
33 | def ping(host): |
|
33 | def ping(host): | |
34 | """ |
|
34 | """ | |
35 | Returns True if host responds to a ping request |
|
35 | Returns True if host responds to a ping request | |
36 | """ |
|
36 | """ | |
37 | import os, platform |
|
37 | import os, platform | |
38 |
|
38 | |||
39 | # Ping parameters as function of OS |
|
39 | # Ping parameters as function of OS | |
40 | ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1" |
|
40 | ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1" | |
41 |
|
41 | |||
42 | # Ping |
|
42 | # Ping | |
43 | return os.system("ping " + ping_str + " " + host) == 0 |
|
43 | return os.system("ping " + ping_str + " " + host) == 0 | |
44 |
|
44 | |||
45 | class IPData(object): |
|
45 | class IPData(object): | |
46 |
|
46 | |||
47 | ''' |
|
47 | ''' | |
48 | Clase para manejar la trama de datos provenientes del/hacia un dispositivo Ethernet. |
|
48 | Clase para manejar la trama de datos provenientes del/hacia un dispositivo Ethernet. | |
49 | La trama de datos es la siguiente: |
|
49 | La trama de datos es la siguiente: | |
50 |
|
50 | |||
51 | ********************************** |
|
51 | ********************************** | |
52 | ** FORMATO GENERAL DE UNA TRAMA ** |
|
52 | ** FORMATO GENERAL DE UNA TRAMA ** | |
53 | ********************************** |
|
53 | ********************************** | |
54 |
|
54 | |||
55 | 1. Cabecera (5 bytes): Secuencia Fija que deber ser "$JRO$" |
|
55 | 1. Cabecera (5 bytes): Secuencia Fija que deber ser "$JRO$" | |
56 | 2. Longitud (3 bytes): Cantidad de bytes de la Data, contados desde IdClass hasta el Xor |
|
56 | 2. Longitud (3 bytes): Cantidad de bytes de la Data, contados desde IdClass hasta el Xor | |
57 | 3. Id class (1 byte) : Clase de dispositivo a configurar. Por defecto 0 |
|
57 | 3. Id class (1 byte) : Clase de dispositivo a configurar. Por defecto 0 | |
58 | 4. Id device (1 byte): Identificar del dispositivo a configurar. Por defecto 0 |
|
58 | 4. Id device (1 byte): Identificar del dispositivo a configurar. Por defecto 0 | |
59 | 5. Cmd (2 bytes): Identificador del comando a ejecutarse. |
|
59 | 5. Cmd (2 bytes): Identificador del comando a ejecutarse. | |
60 | 3. Payload (n bytes): Carga Util conteniendo secuencia,comandos y parametros |
|
60 | 3. Payload (n bytes): Carga Util conteniendo secuencia,comandos y parametros | |
61 | 4. Xor (1 byte): Byte de revision de consistencia de la data al aplicar Xor a todos los bytes, |
|
61 | 4. Xor (1 byte): Byte de revision de consistencia de la data al aplicar Xor a todos los bytes, | |
62 | desde la longitud hasta el payload. |
|
62 | desde la longitud hasta el payload. | |
63 |
|
63 | |||
64 | ''' |
|
64 | ''' | |
65 | __HEADER = "$JRO$" |
|
65 | __HEADER = "$JRO$" | |
66 |
|
66 | |||
67 | def __init__(self, ip, port, id_class=0, id_dev=0): |
|
67 | def __init__(self, ip, port, id_class=0, id_dev=0): | |
68 | ''' |
|
68 | ''' | |
69 | ''' |
|
69 | ''' | |
70 | self.id_class = id_class |
|
70 | self.id_class = id_class | |
71 | self.id_dev = id_dev |
|
71 | self.id_dev = id_dev | |
72 |
|
72 | |||
73 | self.address = (str(ip), int(port)) |
|
73 | self.address = (str(ip), int(port)) | |
74 |
|
74 | |||
75 | self.__iniVariables() |
|
75 | self.__iniVariables() | |
76 |
|
76 | |||
77 | def __iniVariables(self): |
|
77 | def __iniVariables(self): | |
78 |
|
78 | |||
79 | self.tx_buffer = None |
|
79 | self.tx_buffer = None | |
80 | self.rx_buffer = None |
|
80 | self.rx_buffer = None | |
81 |
|
81 | |||
82 | #self.header = None |
|
82 | #self.header = None | |
83 | self.len = None |
|
83 | self.len = None | |
84 | self.cmd = None |
|
84 | self.cmd = None | |
85 | self.payload = None |
|
85 | self.payload = None | |
86 |
|
86 | |||
87 | self.invalid = True |
|
87 | self.invalid = True | |
88 | self.errormsg = '' |
|
88 | self.errormsg = '' | |
89 | self.hasPayload = False |
|
89 | self.hasPayload = False | |
90 |
|
90 | |||
91 | def __getXor(self, cadena): |
|
91 | def __getXor(self, cadena): | |
92 | ''' |
|
92 | ''' | |
93 | ''' |
|
93 | ''' | |
94 | #trama = '%03d' %lenght + ipPayload |
|
94 | #trama = '%03d' %lenght + ipPayload | |
95 | xor = 0 |
|
95 | xor = 0 | |
96 | for character in cadena: |
|
96 | for character in cadena: | |
97 | xor = xor ^ ord(character) |
|
97 | xor = xor ^ ord(character) | |
98 |
|
98 | |||
99 | # xor_hex = hex(xor) |
|
99 | # xor_hex = hex(xor) | |
100 | # xor_hex = xor_hex[2:] |
|
100 | # xor_hex = xor_hex[2:] | |
101 | # xor_hex = xor_hex.rjust(2,'0') |
|
101 | # xor_hex = xor_hex.rjust(2,'0') | |
102 |
|
102 | |||
103 | return xor |
|
103 | return xor | |
104 |
|
104 | |||
105 | def __verifyXor(self, cadena): |
|
105 | def __verifyXor(self, cadena): | |
106 |
|
106 | |||
107 | xor = self.__getXor(cadena) |
|
107 | xor = self.__getXor(cadena) | |
108 |
|
108 | |||
109 | if xor != 0: |
|
109 | if xor != 0: | |
110 | return 0 |
|
110 | return 0 | |
111 |
|
111 | |||
112 | return 1 |
|
112 | return 1 | |
113 |
|
113 | |||
114 | def __encoder(self, cmd, payload): |
|
114 | def __encoder(self, cmd, payload): | |
115 | ''' |
|
115 | ''' | |
116 | Inputs: |
|
116 | Inputs: | |
117 | cmd : Entero que indica el tipo de comando |
|
117 | cmd : Entero que indica el tipo de comando | |
118 | payload : Cadena de caracteres con informacion, depende del comando |
|
118 | payload : Cadena de caracteres con informacion, depende del comando | |
119 |
|
119 | |||
120 | ''' |
|
120 | ''' | |
121 |
|
121 | |||
122 | #seq = '%04d' %(sequence) |
|
122 | #seq = '%04d' %(sequence) | |
123 | #conf = '%04d' %(confcode) |
|
123 | #conf = '%04d' %(confcode) | |
124 |
|
124 | |||
125 | #Number to Cad: 2 Bytes <> H, 4 Bytes <> I |
|
125 | #Number to Cad: 2 Bytes <> H, 4 Bytes <> I | |
126 | cmd_cad = struct.pack(">H", cmd) |
|
126 | cmd_cad = struct.pack(">H", cmd) | |
127 |
|
127 | |||
128 | data = chr(self.id_class) + chr(self.id_dev) + cmd_cad + payload |
|
128 | data = chr(self.id_class) + chr(self.id_dev) + cmd_cad + payload | |
129 |
|
129 | |||
130 | len_data = len(data) + 1 # + xor |
|
130 | len_data = len(data) + 1 # + xor | |
131 | len_cad = struct.pack('>I', len_data) |
|
131 | len_cad = struct.pack('>I', len_data) | |
132 |
|
132 | |||
133 | lenAndData = len_cad + chr(self.id_class) + chr(self.id_dev) + cmd_cad + payload |
|
133 | lenAndData = len_cad + chr(self.id_class) + chr(self.id_dev) + cmd_cad + payload | |
134 |
|
134 | |||
135 | xor = self.__getXor(lenAndData) |
|
135 | xor = self.__getXor(lenAndData) | |
136 |
|
136 | |||
137 | trama = self.__HEADER + lenAndData + chr(xor) |
|
137 | trama = self.__HEADER + lenAndData + chr(xor) | |
138 |
|
138 | |||
139 | self.tx_buffer = trama |
|
139 | self.tx_buffer = trama | |
140 |
|
140 | |||
141 | return trama |
|
141 | return trama | |
142 |
|
142 | |||
143 | def __decoder(self, rx_buffer): |
|
143 | def __decoder(self, rx_buffer): | |
144 | ''' |
|
144 | ''' | |
145 | Evalua la trama y la separa en los campos correspondientes |
|
145 | Evalua la trama y la separa en los campos correspondientes | |
146 |
|
146 | |||
147 | 4Bytes | 4Bytes | 1Byte | 1 Byte | 2 Bytes | n Bytes | 1 Byte |
|
147 | 4Bytes | 4Bytes | 1Byte | 1 Byte | 2 Bytes | n Bytes | 1 Byte | |
148 | Header | Len | Id class | Id dev | Cmd | Payload | Xor |
|
148 | Header | Len | Id class | Id dev | Cmd | Payload | Xor | |
149 |
|
149 | |||
150 | ''' |
|
150 | ''' | |
151 | self.invalid = True |
|
151 | self.invalid = True | |
152 | self.hasPayload = False |
|
152 | self.hasPayload = False | |
153 | self.rx_buffer = rx_buffer |
|
153 | self.rx_buffer = rx_buffer | |
154 |
|
154 | |||
155 | try: |
|
155 | try: | |
156 | index = rx_buffer.find(self.__HEADER) |
|
156 | index = rx_buffer.find(self.__HEADER) | |
157 | except: |
|
157 | except: | |
158 | self.errormsg = "rx_buffer is not a string" |
|
158 | self.errormsg = "rx_buffer is not a string" | |
159 | return 0 |
|
159 | return 0 | |
160 |
|
160 | |||
161 | if index == -1: |
|
161 | if index == -1: | |
162 | self.errormsg = "No header found: %s" %ascii2hex(rx_buffer) |
|
162 | self.errormsg = "No header found: %s" %ascii2hex(rx_buffer) | |
163 | return 0 |
|
163 | return 0 | |
164 |
|
164 | |||
165 | rx_buffer = rx_buffer[index + len(self.__HEADER):] |
|
165 | rx_buffer = rx_buffer[index + len(self.__HEADER):] | |
166 |
|
166 | |||
167 | len_cad = rx_buffer[0:4] |
|
167 | len_cad = rx_buffer[0:4] | |
168 |
|
168 | |||
169 | len_data = struct.unpack('>I',len_cad)[0] |
|
169 | len_data = struct.unpack('>I',len_cad)[0] | |
170 |
|
170 | |||
171 | lenAndDataAndXor = rx_buffer[0:len_data + 4] #Incluye los 4 bytes de la longitud |
|
171 | lenAndDataAndXor = rx_buffer[0:len_data + 4] #Incluye los 4 bytes de la longitud | |
172 |
|
172 | |||
173 | dataAndXor = lenAndDataAndXor[4:] |
|
173 | dataAndXor = lenAndDataAndXor[4:] | |
174 |
|
174 | |||
175 | if len(dataAndXor) < len_data: |
|
175 | if len(dataAndXor) < len_data: | |
176 | self.errormsg = "Data length is lower than %s" %(len_data) |
|
176 | self.errormsg = "Data length is lower than %s" %(len_data) | |
177 | return 0 |
|
177 | return 0 | |
178 |
|
178 | |||
179 | # print self.header, ", ", ascii2hex(lenCad), ", ", ascii2hex(ipDataAndXor), ", ", hex(self.xor) |
|
179 | # print self.header, ", ", ascii2hex(lenCad), ", ", ascii2hex(ipDataAndXor), ", ", hex(self.xor) | |
180 |
|
180 | |||
181 | if not self.__verifyXor(lenAndDataAndXor): |
|
181 | if not self.__verifyXor(lenAndDataAndXor): | |
182 | self.errormsg = "Invalid xor: %s" %lenAndDataAndXor[-1] |
|
182 | self.errormsg = "Invalid xor: %s" %lenAndDataAndXor[-1] | |
183 | return 0 |
|
183 | return 0 | |
184 |
|
184 | |||
185 | self.invalid = False |
|
185 | self.invalid = False | |
186 |
|
186 | |||
187 | len_payload = len_data - 5 #Decrementar 1B (id_class), 1B (id_dev), 2B (cmd) y 1B (xor) |
|
187 | len_payload = len_data - 5 #Decrementar 1B (id_class), 1B (id_dev), 2B (cmd) y 1B (xor) | |
188 |
|
188 | |||
189 | id_class = ord(dataAndXor[0]) |
|
189 | id_class = ord(dataAndXor[0]) | |
190 | id_dev = ord(dataAndXor[1]) |
|
190 | id_dev = ord(dataAndXor[1]) | |
191 | cmd_cad = dataAndXor[2:4] |
|
191 | cmd_cad = dataAndXor[2:4] | |
192 | payload = dataAndXor[4:4+len_payload] |
|
192 | payload = dataAndXor[4:4+len_payload] | |
193 |
|
193 | |||
194 | cmd = struct.unpack('>H',cmd_cad)[0] |
|
194 | cmd = struct.unpack('>H',cmd_cad)[0] | |
195 |
|
195 | |||
196 | self.id_class = id_class |
|
196 | self.id_class = id_class | |
197 | self.id_dev = id_dev |
|
197 | self.id_dev = id_dev | |
198 | self.cmd = cmd |
|
198 | self.cmd = cmd | |
199 |
|
199 | |||
200 | if len(payload) < 1: |
|
200 | if len(payload) < 1: | |
201 | self.errormsg = "IP data is valid but it hasn't payload" |
|
201 | self.errormsg = "IP data is valid but it hasn't payload" | |
202 | return 1 |
|
202 | return 1 | |
203 |
|
203 | |||
204 | self.hasPayload = True |
|
204 | self.hasPayload = True | |
205 | self.payload = payload |
|
205 | self.payload = payload | |
206 |
|
206 | |||
207 | self.errormsg = "Successful" |
|
207 | self.errormsg = "Successful" | |
208 |
|
208 | |||
209 | return 1 |
|
209 | return 1 | |
210 |
|
210 | |||
211 | def __decoder_api(self, rx_buffer, debug = DEBUG): |
|
211 | def __decoder_api(self, rx_buffer, debug = DEBUG): | |
212 | """ |
|
212 | """ | |
213 | Input: |
|
213 | Input: | |
214 | rx_buffer : Trama recibida como respuesta a un comando enviada a un dispositivo. |
|
214 | rx_buffer : Trama recibida como respuesta a un comando enviada a un dispositivo. | |
215 |
|
215 | |||
216 | Return: |
|
216 | Return: | |
217 | 0 : Trama recibida incorrecta. La cadena "rx_buffer" no ha sido decodificada correctamente. |
|
217 | 0 : Trama recibida incorrecta. La cadena "rx_buffer" no ha sido decodificada correctamente. | |
218 | -1 : Dispositivo no inicializado. El dispositivo, dds o rc, no ha sido inicializado |
|
218 | -1 : Dispositivo no inicializado. El dispositivo, dds o rc, no ha sido inicializado | |
219 | correctamente. |
|
219 | correctamente. | |
220 | -2 : Trama enviada no reconocida. La cadena recibida es correcta y el dispositivo ha sido |
|
220 | -2 : Trama enviada no reconocida. La cadena recibida es correcta y el dispositivo ha sido | |
221 | inicializaado correctamente pero la trama enviada no ha sido reconocida por el |
|
221 | inicializaado correctamente pero la trama enviada no ha sido reconocida por el | |
222 | dispositivo o el comando enviado no ha sido implementado. |
|
222 | dispositivo o el comando enviado no ha sido implementado. | |
223 | >0 : Trama enviada y recibida correctamente |
|
223 | >0 : Trama enviada y recibida correctamente | |
224 | """ |
|
224 | """ | |
225 |
|
225 | |||
226 | if not self.__decoder(rx_buffer): |
|
226 | if not self.__decoder(rx_buffer): | |
227 | return "0:Error decoding eth data: " + ascii2hex(self.rx_buffer) |
|
227 | return "0:Error decoding eth data: " + ascii2hex(self.rx_buffer) | |
228 |
|
228 | |||
229 | # if self.getPayload() == "OK": |
|
229 | # if self.getPayload() == "OK": | |
230 | # return 1 |
|
230 | # return 1 | |
231 | # |
|
231 | # | |
232 | # if self.getPayload() == "NI": |
|
232 | # if self.getPayload() == "NI": | |
233 | # return -1 |
|
233 | # return -1 | |
234 | # |
|
234 | # | |
235 | # if self.getPayload() == "KO": |
|
235 | # if self.getPayload() == "KO": | |
236 | # return -2 |
|
236 | # return -2 | |
237 |
|
237 | |||
238 | if debug: |
|
238 | if debug: | |
239 | print ascii2hex(self.rx_buffer) |
|
239 | print ascii2hex(self.rx_buffer) | |
240 |
|
240 | |||
241 | return self.payload |
|
241 | return self.payload | |
242 |
|
242 | |||
243 | def getRxBufferHex(self): |
|
243 | def getRxBufferHex(self): | |
244 |
|
244 | |||
245 | if self.rx_buffer == None: |
|
245 | if self.rx_buffer == None: | |
246 | return '' |
|
246 | return '' | |
247 |
|
247 | |||
248 | cad = ascii2hex(self.rx_buffer) |
|
248 | cad = ascii2hex(self.rx_buffer) | |
249 |
|
249 | |||
250 | return cad |
|
250 | return cad | |
251 |
|
251 | |||
252 | def getTxBufferHex(self): |
|
252 | def getTxBufferHex(self): | |
253 |
|
253 | |||
254 | if self.tx_buffer == None: |
|
254 | if self.tx_buffer == None: | |
255 | return '' |
|
255 | return '' | |
256 |
|
256 | |||
257 | cad = ascii2hex(self.tx_buffer) |
|
257 | cad = ascii2hex(self.tx_buffer) | |
258 |
|
258 | |||
259 | return cad |
|
259 | return cad | |
260 |
|
260 | |||
261 | def isInvalid(self): |
|
261 | def isInvalid(self): | |
262 |
|
262 | |||
263 | return self.invalid |
|
263 | return self.invalid | |
264 |
|
264 | |||
265 | def getCmd(self): |
|
265 | def getCmd(self): | |
266 | return self.cmd |
|
266 | return self.cmd | |
267 |
|
267 | |||
268 | def getPayload(self): |
|
268 | def getPayload(self): | |
269 | return self.payload |
|
269 | return self.payload | |
270 |
|
270 | |||
271 | def getErrorMessage(self): |
|
271 | def getErrorMessage(self): | |
272 |
|
272 | |||
273 | return self.errormsg |
|
273 | return self.errormsg | |
274 |
|
274 | |||
275 | def getTxBuffer(self): |
|
275 | def getTxBuffer(self): | |
276 |
|
276 | |||
277 | return self.tx_buffer |
|
277 | return self.tx_buffer | |
278 |
|
278 | |||
279 | def getRxBuffer(self): |
|
279 | def getRxBuffer(self): | |
280 |
|
280 | |||
281 | return self.rx_buffer |
|
281 | return self.rx_buffer | |
282 |
|
282 | |||
283 | def __encodeIpCmd(self, ip, mask, gateway): |
|
283 | def __encodeIpCmd(self, ip, mask, gateway): | |
284 |
|
284 | |||
285 | payload = ip + '/' + mask + '/' + gateway |
|
285 | payload = ip + '/' + mask + '/' + gateway | |
286 | return self.__encoder(CMD_CHANGEIP, payload) |
|
286 | return self.__encoder(CMD_CHANGEIP, payload) | |
287 |
|
287 | |||
288 | def __encodeResetCmd(self): |
|
288 | def __encodeResetCmd(self): | |
289 |
|
289 | |||
290 | payload = "" |
|
290 | payload = "" | |
291 | return self.__encoder(CMD_RESET, payload) |
|
291 | return self.__encoder(CMD_RESET, payload) | |
292 |
|
292 | |||
293 | def __sendTCPData(self, cadena): |
|
293 | def __sendTCPData(self, cadena): | |
294 |
|
294 | |||
295 | sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
295 | sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
296 |
sck.settimeout( |
|
296 | sck.settimeout(3) | |
297 |
|
297 | |||
298 | try: |
|
298 | try: | |
299 | sck.connect(self.address) |
|
299 | sck.connect(self.address) | |
300 | except: |
|
300 | except: | |
301 | return None |
|
301 | return None | |
302 |
|
302 | |||
303 | # print "TX: ", ascii2hex(cadena) |
|
303 | # print "TX: ", ascii2hex(cadena) | |
304 |
|
304 | |||
305 | sck.send(cadena) |
|
305 | sck.send(cadena) | |
306 |
|
306 | |||
307 | rx_buffer = "" |
|
307 | rx_buffer = "" | |
308 |
|
308 | |||
309 | ini = time.time() |
|
309 | ini = time.time() | |
310 |
|
310 | |||
311 | try: |
|
311 | try: | |
312 | while True: |
|
312 | while True: | |
313 |
|
313 | |||
314 | if time.time() - ini > 0.5: |
|
314 | if time.time() - ini > 0.5: | |
315 | break |
|
315 | break | |
316 |
|
316 | |||
317 |
t |
|
317 | try: | |
|
318 | tmp = sck.recv(4096) | |||
|
319 | except: | |||
|
320 | break | |||
318 |
|
321 | |||
319 | if len(tmp) < 1: |
|
322 | if len(tmp) < 1: | |
320 | continue |
|
323 | continue | |
321 |
|
324 | |||
322 | ini = time.time() |
|
325 | ini = time.time() | |
323 | rx_buffer += tmp |
|
326 | rx_buffer += tmp | |
324 |
|
327 | |||
325 | finally: |
|
328 | finally: | |
326 | sck.close() |
|
329 | sck.close() | |
327 |
|
330 | |||
328 | # print "RX: ", ascii2hex(rx_buffer) |
|
331 | # print "RX: ", ascii2hex(rx_buffer) | |
329 |
|
332 | |||
330 | return rx_buffer |
|
333 | return rx_buffer | |
331 |
|
334 | |||
332 | def changeIP(self, ip, mask, gateway): |
|
335 | def changeIP(self, ip, mask, gateway): | |
333 |
|
336 | |||
334 | tx_buffer = self.__encodeIpCmd(ip, mask, gateway) |
|
337 | tx_buffer = self.__encodeIpCmd(ip, mask, gateway) | |
335 | rx_buffer = self.__sendTCPData(tx_buffer) |
|
338 | rx_buffer = self.__sendTCPData(tx_buffer) | |
336 |
|
339 | |||
337 | sts = self.__decoder_api(rx_buffer) |
|
340 | sts = self.__decoder_api(rx_buffer) | |
338 |
|
341 | |||
339 | if sts > 0: |
|
342 | if sts > 0: | |
340 | self.address = (ip, self.address[1]) |
|
343 | self.address = (ip, self.address[1]) | |
341 |
|
344 | |||
342 | return sts |
|
345 | return sts | |
343 |
|
346 | |||
344 | def reset(self): |
|
347 | def reset(self): | |
345 |
|
348 | |||
346 | tx_buffer = self.__encodeResetCmd() |
|
349 | tx_buffer = self.__encodeResetCmd() | |
347 | rx_buffer = self.__sendTCPData(tx_buffer) |
|
350 | rx_buffer = self.__sendTCPData(tx_buffer) | |
348 |
|
351 | |||
349 | return self.__decoder_api(rx_buffer) |
|
352 | return self.__decoder_api(rx_buffer) | |
350 |
|
353 | |||
351 | def sendData(self, cmd, payload, server=False): |
|
354 | def sendData(self, cmd, payload, server=False): | |
352 |
|
355 | |||
353 | if server: |
|
356 | if server: | |
354 | tx_buffer = self.__encoder(cmd, payload) |
|
357 | tx_buffer = self.__encoder(cmd, payload) | |
355 |
|
358 | |||
356 | print "TX: ", ascii2hex(tx_buffer) |
|
359 | print "TX: ", ascii2hex(tx_buffer) | |
357 |
|
360 | |||
358 | self.client_connection.sendall(tx_buffer) |
|
361 | self.client_connection.sendall(tx_buffer) | |
359 |
|
362 | |||
360 | else: |
|
363 | else: | |
361 |
|
364 | |||
362 | tx_buffer = self.__encoder(cmd, payload) |
|
365 | tx_buffer = self.__encoder(cmd, payload) | |
363 |
|
366 | |||
364 | print "TX: ", ascii2hex(tx_buffer) |
|
367 | print "TX: ", ascii2hex(tx_buffer) | |
365 |
|
368 | |||
366 | rx_buffer = self.__sendTCPData(tx_buffer) |
|
369 | rx_buffer = self.__sendTCPData(tx_buffer) | |
367 |
|
370 | |||
368 | if not rx_buffer: |
|
371 | if not rx_buffer: | |
369 | msg = "0:Could not connect to Device %s" %str(self.address) |
|
372 | msg = "0:Could not connect to Device %s" %str(self.address) | |
370 | return msg |
|
373 | return msg | |
371 |
|
374 | |||
372 | print "RX: ", ascii2hex(rx_buffer) |
|
375 | print "RX: ", ascii2hex(rx_buffer) | |
373 |
|
376 | |||
374 | return self.__decoder_api(rx_buffer) |
|
377 | return self.__decoder_api(rx_buffer) | |
375 |
|
378 | |||
376 | def receiveData(self, rx_buffer): |
|
379 | def receiveData(self, rx_buffer): | |
377 |
|
380 | |||
378 | print "RX: ", ascii2hex(rx_buffer) |
|
381 | print "RX: ", ascii2hex(rx_buffer) | |
379 |
|
382 | |||
380 | return self.__decoder(rx_buffer) |
|
383 | return self.__decoder(rx_buffer) | |
381 |
|
384 | |||
382 |
|
385 | |||
383 | def eth_device(id_class): |
|
386 | def eth_device(id_class): | |
384 | def inner_func(func): |
|
387 | def inner_func(func): | |
385 | def func_wrapper(ip, port, *args): |
|
388 | def func_wrapper(ip, port, *args): | |
386 |
|
389 | |||
387 | cmd, payload = func(*args) |
|
390 | cmd, payload = func(*args) | |
388 |
|
391 | |||
389 | ipObj = IPData(ip, port, id_class=id_class) |
|
392 | ipObj = IPData(ip, port, id_class=id_class) | |
390 |
|
393 | |||
391 | rx = ipObj.sendData(cmd, payload) |
|
394 | rx = ipObj.sendData(cmd, payload) | |
392 |
|
395 | |||
393 | return rx |
|
396 | return rx | |
394 |
|
397 | |||
395 | return func_wrapper |
|
398 | return func_wrapper | |
396 | return inner_func No newline at end of file |
|
399 | return inner_func |
@@ -1,153 +1,135 | |||||
1 | ''' |
|
1 | ''' | |
2 | Created on Dec 2, 2014 |
|
2 | Created on Dec 2, 2014 | |
3 |
|
3 | |||
4 | @author: Miguel Urco |
|
4 | @author: Miguel Urco | |
5 |
|
5 | |||
6 | eth_device decorator is used to implement an api to ethernet devices. |
|
6 | eth_device decorator is used to implement an api to ethernet devices. | |
7 | When eth_device decorator is used it adds two parameters to any function (ip and port) |
|
7 | When eth_device decorator is used it adds two parameters to any function (ip and port) | |
8 |
|
8 | |||
9 | #Definition |
|
9 | #Definition | |
10 |
|
10 | |||
11 | @eth_device |
|
11 | @eth_device | |
12 | def enable_rf() |
|
12 | def enable_rf() | |
13 | cmd = "xxxxx" |
|
13 | cmd = "xxxxx" | |
14 | payload = "xxxxxx" |
|
14 | payload = "xxxxxx" | |
15 |
|
15 | |||
16 | return cmd, payload |
|
16 | return cmd, payload | |
17 |
|
17 | |||
18 | #How to call this function: |
|
18 | #How to call this function: | |
19 | answer = enable_rf(ip, port) |
|
19 | answer = enable_rf(ip, port) | |
20 |
|
20 | |||
21 | ''' |
|
21 | ''' | |
22 | import data |
|
22 | import data | |
23 |
|
23 | |||
24 | from devices.jro_device import eth_device, IdClass |
|
24 | from devices.jro_device import eth_device, IdClass | |
25 |
|
25 | |||
26 | ID_CLASS = IdClass["rc"] |
|
26 | ID_CLASS = IdClass["rc"] | |
27 |
|
27 | |||
28 | CMD_RESET =0X01 |
|
28 | CMD_RESET =0X01 | |
29 | CMD_ENABLE =0X02 |
|
29 | CMD_ENABLE =0X02 | |
30 | CMD_CHANGEIP =0X03 |
|
30 | CMD_CHANGEIP =0X03 | |
31 | CMD_STATUS =0X04 |
|
31 | CMD_STATUS =0X04 | |
32 | CMD_DISABLE =0X02 |
|
32 | CMD_DISABLE =0X02 | |
33 | CMD_ECHO =0XFE |
|
33 | CMD_ECHO =0XFE | |
34 |
|
34 | |||
35 | RC_CMD_RESET =0X10 |
|
35 | RC_CMD_RESET =0X10 | |
36 | RC_CMD_WRITE =0x50 |
|
36 | RC_CMD_WRITE =0x50 | |
37 | RC_CMD_READ =0x8000 |
|
37 | RC_CMD_READ =0x8000 | |
38 | RC_CMD_ENABLE =0X24 |
|
|||
39 | RC_CMD_DISABLE =0X00 |
|
|||
40 |
|
38 | |||
41 | @eth_device(ID_CLASS) |
|
39 | @eth_device(ID_CLASS) | |
42 | def reset(): |
|
40 | def reset(): | |
43 |
|
41 | |||
44 | cmd = CMD_RESET |
|
42 | cmd = CMD_RESET | |
45 | payload = "" |
|
43 | payload = "" | |
46 |
|
44 | |||
47 | return cmd, payload |
|
45 | return cmd, payload | |
48 |
|
46 | |||
49 | @eth_device(ID_CLASS) |
|
47 | @eth_device(ID_CLASS) | |
50 | def change_ip(ip, mask="255.255.255.0", gateway="0.0.0.0"): |
|
48 | def change_ip(ip, mask="255.255.255.0", gateway="0.0.0.0"): | |
51 |
|
49 | |||
52 | cmd = CMD_CHANGEIP |
|
50 | cmd = CMD_CHANGEIP | |
53 | payload = ip + '/' + mask + '/' + gateway |
|
51 | payload = ip + '/' + mask + '/' + gateway | |
54 |
|
52 | |||
55 | return cmd, payload |
|
53 | return cmd, payload | |
56 |
|
54 | |||
57 | @eth_device(ID_CLASS) |
|
55 | @eth_device(ID_CLASS) | |
58 | def status(): |
|
56 | def status(): | |
59 |
|
57 | |||
60 | cmd = CMD_STATUS |
|
58 | cmd = CMD_STATUS | |
61 | payload = "" |
|
59 | payload = "" | |
62 |
|
60 | |||
63 | return cmd, payload |
|
61 | return cmd, payload | |
64 |
|
62 | |||
65 | @eth_device(ID_CLASS) |
|
63 | @eth_device(ID_CLASS) | |
66 | def echo(): |
|
64 | def echo(): | |
67 |
|
65 | |||
68 | cmd = CMD_ECHO |
|
66 | cmd = CMD_ECHO | |
69 | payload = "" |
|
67 | payload = "" | |
70 |
|
68 | |||
71 | return cmd, payload |
|
69 | return cmd, payload | |
72 |
|
70 | |||
73 | @eth_device(ID_CLASS) |
|
71 | @eth_device(ID_CLASS) | |
74 | def enable(): |
|
|||
75 |
|
||||
76 | cmd = RC_CMD_ENABLE |
|
|||
77 | payload = chr(0x01) |
|
|||
78 |
|
||||
79 | return cmd, payload |
|
|||
80 |
|
||||
81 | @eth_device(ID_CLASS) |
|
|||
82 | def disable(): |
|
|||
83 |
|
||||
84 | cmd = RC_CMD_DISABLE |
|
|||
85 | payload = chr(0x00) |
|
|||
86 |
|
||||
87 | return cmd, payload |
|
|||
88 |
|
||||
89 | @eth_device(ID_CLASS) |
|
|||
90 | def read_all_device(): |
|
72 | def read_all_device(): | |
91 |
|
73 | |||
92 | payload = "" |
|
74 | payload = "" | |
93 |
|
75 | |||
94 |
return |
|
76 | return CR_CMD_READ, payload | |
95 |
|
77 | |||
96 | @eth_device(ID_CLASS) |
|
78 | @eth_device(ID_CLASS) | |
97 | def write_all_device(payload): |
|
79 | def write_all_device(payload): | |
98 |
|
80 | |||
99 |
return |
|
81 | return CR_CMD_WRITE, payload | |
100 |
|
82 | |||
101 | def read_config(ip, port): |
|
83 | def read_config(ip, port): | |
102 | """ |
|
84 | """ | |
103 | Output: |
|
85 | Output: | |
104 | parms : Dictionary with keys |
|
86 | parms : Dictionary with keys | |
105 |
|
87 | |||
106 | """ |
|
88 | """ | |
107 | payload = read_all_device(ip, port) |
|
89 | payload = read_all_device(ip, port) | |
108 |
|
90 | |||
109 | return data.rc_str_to_dict(payload) |
|
91 | return data.rc_str_to_dict(payload) | |
110 |
|
92 | |||
111 | def write_config(ip, port, parms): |
|
93 | def write_config(ip, port, parms): | |
112 | """ |
|
94 | """ | |
113 | Input: |
|
95 | Input: | |
114 | ip : |
|
96 | ip : | |
115 | port : |
|
97 | port : | |
116 | parms : Dictionary with keys |
|
98 | parms : Dictionary with keys | |
117 |
|
99 | |||
118 | """ |
|
100 | """ | |
119 |
|
101 | |||
120 | payload = write_ram_memory(parms['pulses'], parms['delays']) |
|
102 | payload = data.dict_to_rc_str(parms) | |
121 |
|
103 | |||
122 | answer = write_all_device(ip, port, payload) |
|
104 | answer = write_all_device(ip, port, payload) | |
123 |
|
105 | |||
124 | return answer |
|
106 | return answer | |
125 |
|
107 | |||
126 | def __get_low_byte(valor): |
|
108 | def __get_low_byte(valor): | |
127 |
|
109 | |||
128 |
|
|
110 | return ord(valor & 0x00FF) | |
129 |
|
111 | |||
130 | def __get_high_byte(valor): |
|
112 | def __get_high_byte(valor): | |
131 |
|
113 | |||
132 |
|
|
114 | return ord((valor & 0xFF00) >> 8) | |
133 |
|
||||
134 |
|
115 | |||
|
116 | @eth_device(ID_CLASS) | |||
135 | def write_ram_memory(vector_valores, vector_tiempos): |
|
117 | def write_ram_memory(vector_valores, vector_tiempos): | |
136 |
|
118 | |||
137 | l1 = len(vector_valores) |
|
119 | l1 = len(vector_valores) | |
138 | l2 = len(vector_tiempos) |
|
120 | l2 = len(vector_tiempos) | |
139 |
|
121 | |||
140 | cad = "" |
|
122 | cad = "" | |
141 |
|
123 | |||
142 | for i in range(l1): |
|
124 | for i in range(l1): | |
143 |
|
|
125 | cad += ord(84) + __get_low_byte(vector_valores[i]) + ord(85) + __get_high_byte(vector_valores[i]) + \ | |
144 | ord(84) + __get_low_byte(vector_tiempos[i]) + ord(85) + __get_high_byte(vector_tiempos[i]) |
|
126 | ord(84) + __get_low_byte(vector_tiempos[i]) + ord(85) + __get_high_byte(vector_tiempos[i]) | |
145 |
|
127 | |||
146 | return cad |
|
128 | return RC_CMD_WRITE, cad | |
147 |
|
129 | |||
148 | if __name__ == '__main__': |
|
130 | if __name__ == '__main__': | |
149 | ip = "10.10.20.150" |
|
131 | ip = "10.10.20.150" | |
150 | port = 2000 |
|
132 | port = 2000 | |
151 |
|
133 | |||
152 | print status(ip, port) |
|
134 | print status(ip, port) | |
153 | print read_config(ip, port) No newline at end of file |
|
135 | print read_config(ip, port) |
@@ -1,31 +1,31 | |||||
1 | """radarsys URL Configuration |
|
1 | """radarsys URL Configuration | |
2 |
|
2 | |||
3 | The `urlpatterns` list routes URLs to views. For more information please see: |
|
3 | The `urlpatterns` list routes URLs to views. For more information please see: | |
4 | https://docs.djangoproject.com/en/1.8/topics/http/urls/ |
|
4 | https://docs.djangoproject.com/en/1.8/topics/http/urls/ | |
5 | Examples: |
|
5 | Examples: | |
6 | Function views |
|
6 | Function views | |
7 | 1. Add an import: from my_app import views |
|
7 | 1. Add an import: from my_app import views | |
8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') |
|
8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') | |
9 | Class-based views |
|
9 | Class-based views | |
10 | 1. Add an import: from other_app.views import Home |
|
10 | 1. Add an import: from other_app.views import Home | |
11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') |
|
11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') | |
12 | Including another URLconf |
|
12 | Including another URLconf | |
13 | 1. Add an import: from blog import urls as blog_urls |
|
13 | 1. Add an import: from blog import urls as blog_urls | |
14 | 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) |
|
14 | 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) | |
15 | """ |
|
15 | """ | |
16 | from django.conf.urls import include, url |
|
16 | from django.conf.urls import include, url | |
17 | from django.contrib import admin |
|
17 | from django.contrib import admin | |
18 |
|
18 | |||
19 | urlpatterns = [ |
|
19 | urlpatterns = [ | |
20 |
url(r'^$', 'apps.main.views.index', name='index'), |
|
20 | url(r'^$', 'apps.main.views.index', name='index'), | |
21 | url(r'^admin/', include(admin.site.urls)), |
|
21 | url(r'^admin/', include(admin.site.urls)), | |
22 | url(r'^accounts/', include('apps.accounts.urls')), |
|
22 | url(r'^accounts/', include('apps.accounts.urls')), | |
23 | url(r'^', include('apps.main.urls')), |
|
23 | url(r'^', include('apps.main.urls')), | |
24 | url(r'^rc/', include('apps.rc.urls')), |
|
24 | url(r'^rc/', include('apps.rc.urls')), | |
25 | url(r'^dds/', include('apps.dds.urls')), |
|
25 | url(r'^dds/', include('apps.dds.urls')), | |
26 | url(r'^cgs/', include('apps.cgs.urls')), |
|
26 | url(r'^cgs/', include('apps.cgs.urls')), | |
27 | url(r'^jars/', include('apps.jars.urls')), |
|
27 | url(r'^jars/', include('apps.jars.urls')), | |
28 | url(r'^usrp/', include('apps.usrp.urls')), |
|
28 | url(r'^usrp/', include('apps.usrp.urls')), | |
29 | url(r'^abs/', include('apps.abs.urls')), |
|
29 | url(r'^abs/', include('apps.abs.urls')), | |
30 | url(r'^misc/', include('apps.misc.urls')), |
|
30 | url(r'^misc/', include('apps.misc.urls')), | |
31 | ] |
|
31 | ] |
@@ -1,9 +1,8 | |||||
1 | Django==1.8.6 |
|
1 | Django==1.8.6 | |
2 | django-bootstrap3==6.2.2 |
|
2 | django-bootstrap3==6.2.2 | |
3 | MySQL-python==1.2.5 |
|
3 | MySQL-python==1.2.5 | |
4 | django-polymorphic==0.7.2 |
|
4 | django-polymorphic==0.7.2 | |
5 | requests==2.9.1 |
|
5 | requests==2.9.1 | |
6 | bokeh==0.11.0 |
|
6 | bokeh==0.11.0 | |
7 | numpy==1.10.4 |
|
7 | numpy==1.10.4 | |
8 | matplotlib==1.5.1 |
|
8 | matplotlib==1.5.1 | |
9 |
|
General Comments 0
You need to be logged in to leave comments.
Login now