diff --git a/.env b/.env index e713735..bb5cf08 100644 --- a/.env +++ b/.env @@ -8,5 +8,5 @@ POSTGRES_PASSWORD=docker PGDATA=/var/lib/postgresql/data LC_ALL=C.UTF-8 TZ=America/Lima -DOCKER_DATA=/Volumes/dockers/radarsys/ +DOCKER_DATA=/data/dockers/radarsys/ LOCAL_IP=192.168.1.128 diff --git a/.gitignore b/.gitignore index bb32362..a430461 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ migrations/ +*.sqlite .vscode/ *.pyc .env diff --git a/apps/jars/models.py b/apps/jars/models.py index 17f22af..6c9f075 100644 --- a/apps/jars/models.py +++ b/apps/jars/models.py @@ -256,24 +256,16 @@ class JARSConfiguration(Configuration): if data['configurations']['byId'][key]['device_type'] in ('dds', 'cgs'): data['configurations']['allIds'].remove(key) data['configurations']['byId'].pop(key) - elif data['configurations']['byId'][key]['device_type'] == 'jars': - data['configurations']['byId'][key] = self.parms_to_dict()['configurations']['byId'][str(self.pk)] elif data['configurations']['byId'][key]['device_type'] == 'rc': data['configurations']['byId'][key]['pulses'] = '' data['configurations']['byId'][key]['delays'] = '' rc_ids = [pk for pk in data['configurations']['allIds'] if data['configurations']['byId'][pk]['device_type']=='rc'] - mix_ids = [pk for pk in rc_ids if data['configurations']['byId'][pk]['mix']] - if mix_ids: - params = data['configurations']['byId'][mix_ids[0]]['parameters'] - rc = data['configurations']['byId'][params.split('-')[0].split('|')[0]] - rc['mix'] = True - data['configurations']['byId'][rc['id']] = rc - elif len(rc_ids)==0: + if len(rc_ids)==0: self.message = 'Missing RC configuration' return False json_data = json.dumps(data) - + try: payload = self.request('write', 'post', json=json_data) self.device.status = payload['status'] diff --git a/apps/main/forms.py b/apps/main/forms.py index b1346a8..63e75e1 100644 --- a/apps/main/forms.py +++ b/apps/main/forms.py @@ -93,7 +93,7 @@ class ExperimentForm(forms.ModelForm): class Meta: model = Experiment - exclude = ['status'] + exclude = ['task', 'status'] class LocationForm(forms.ModelForm): class Meta: diff --git a/apps/main/models.py b/apps/main/models.py index 0c52593..8db2c4f 100644 --- a/apps/main/models.py +++ b/apps/main/models.py @@ -248,7 +248,7 @@ class Campaign(models.Model): def parms_to_dict(self): - params = Params() + params = Params({}) params.add(self.jsonify(), 'campaigns') for exp in Experiment.objects.filter(campaign = self): @@ -334,6 +334,7 @@ class Experiment(models.Model): freq = models.FloatField(verbose_name='Operating Freq. (MHz)', validators=[MinValueValidator(1), MaxValueValidator(10000)], default=49.9200) start_time = models.TimeField(default='00:00:00') end_time = models.TimeField(default='23:59:59') + task = models.CharField(max_length=36, default='', blank=True, null=True) status = models.PositiveSmallIntegerField(default=4, choices=EXP_STATES) class Meta: @@ -373,7 +374,7 @@ class Experiment(models.Model): confs = Configuration.objects.filter(experiment=self, type=0) self.pk = None - self.name = '{} [{:%Y/%m/%d}]'.format(self.name, datetime.now()) + self.name = '{} [{:%Y-%m-%d}]'.format(self.name, datetime.now()) for attr, value in kwargs.items(): setattr(self, attr, value) @@ -391,8 +392,16 @@ class Experiment(models.Model): ''' result = 2 - - confs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence') + confs = [] + allconfs = Configuration.objects.filter(experiment=self, type = 0).order_by('-device__device_type__sequence') + rc_mix = [conf for conf in allconfs if conf.device.device_type.name=='rc' and conf.mix] + if rc_mix: + for conf in allconfs: + if conf.device.device_type.name == 'rc' and not conf.mix: + continue + confs.append(conf) + else: + confs = allconfs #Only Configured Devices. for conf in confs: if conf.device.status in (0, 4): @@ -400,9 +409,9 @@ class Experiment(models.Model): return result for conf in confs: conf.stop_device() - #conf.write_device() + conf.write_device() conf.start_device() - print conf.device.name+' has started...' + time.sleep(1) return result @@ -422,7 +431,6 @@ class Experiment(models.Model): result = 0 continue conf.stop_device() - print conf.device.name+' has stopped...' return result @@ -464,7 +472,7 @@ class Experiment(models.Model): def parms_to_dict(self): - params = Params() + params = Params({}) params.add(self.jsonify(), 'experiments') configurations = Configuration.objects.filter(experiment=self, type=0) @@ -532,19 +540,13 @@ class Experiment(models.Model): class Configuration(PolymorphicModel): template = models.BooleanField(default=False) - name = models.CharField(verbose_name="Configuration Name", max_length=40, default='') - experiment = models.ForeignKey('Experiment', verbose_name='Experiment', null=True, blank=True, on_delete=models.CASCADE) device = models.ForeignKey('Device', verbose_name='Device', null=True, on_delete=models.CASCADE) - type = models.PositiveSmallIntegerField(default=0, choices=CONF_TYPES) - created_date = models.DateTimeField(auto_now_add=True) programmed_date = models.DateTimeField(auto_now=True) - parameters = models.TextField(default='{}') - message = "" class Meta: @@ -607,7 +609,7 @@ class Configuration(PolymorphicModel): def parms_to_dict(self): - params = Params() + params = Params({}) params.add(self.jsonify(), 'configurations') if self.device.device_type.name=='rc': diff --git a/apps/main/tasks.py b/apps/main/tasks.py index f2b470f..5a9a071 100644 --- a/apps/main/tasks.py +++ b/apps/main/tasks.py @@ -1,5 +1,6 @@ from __future__ import absolute_import +from radarsys.celery import app from celery import task from datetime import timedelta, datetime @@ -7,25 +8,47 @@ from .models import Experiment @task def task_start(id_exp): - exp = Experiment.objects.get(pk=id_exp) - - return exp.start() + status = exp.status + if exp.status == 2: + print('Experiment {} already running start task not executed'.format(exp)) + return 2 + if status == 3: + now = datetime.now() + start = datetime.combine(now.date(), exp.start_time) + end = datetime.combine(now.date(), exp.end_time) + if end < start: + end += timedelta(1) + try: + print('Starting exp:{}'.format(exp)) + exp.status = exp.start() + except: + print('Error') + exp.status = 0 + if exp.status == 2: + task = task_stop.apply_async((id_exp,), eta=end+timedelta(hours=5)) + exp.task = task.id + exp.save() + return exp.status @task def task_stop(id_exp): - exp = Experiment.objects.get(pk=id_exp) + if exp.status == 2: + try: + print('Stopping exp:{}'.format(exp)) + exp.status = exp.stop() + except: + print('Error') + exp.status = 0 - return exp.stop() - -def kill_tasks(): - - i = task.control.inspect() - tasks = i.scheduled() - print tasks - #if tasks: - # print dir(tasks[0]) + now = datetime.now() + start = datetime.combine(now.date()+timedelta(1), exp.start_time) + task = task_start.apply_async((id_exp, ), eta=start+timedelta(hours=5)) + exp.task = task.id + exp.status = 3 + exp.save() + return exp.status #Task to get status @task diff --git a/apps/main/templates/experiment.html b/apps/main/templates/experiment.html index 03dbc75..a584553 100644 --- a/apps/main/templates/experiment.html +++ b/apps/main/templates/experiment.html @@ -70,8 +70,12 @@ {%endif%}

{{item}}
{{item.device.ip_address}} - {{item.device.get_status_display}} -

+ {%if experiment.status == 3 %} + Configured + {%else%} + {{item.device.get_status_display}} + {%endif%} + {% endfor %} diff --git a/apps/main/templates/operation.html b/apps/main/templates/operation.html index e090a0b..677835a 100644 --- a/apps/main/templates/operation.html +++ b/apps/main/templates/operation.html @@ -18,30 +18,28 @@

Current Campaigns


- -
-
- -
+
+ +
{% endif %} diff --git a/apps/main/views.py b/apps/main/views.py index f792dcc..c719914 100644 --- a/apps/main/views.py +++ b/apps/main/views.py @@ -19,7 +19,7 @@ except ImportError: from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm from .forms import OperationSearchForm, FilterForm, ChangeIpForm -from .tasks import task_start, task_stop, task_status, kill_tasks +from .tasks import task_start from apps.rc.forms import RCConfigurationForm, RCLineCode, RCMixConfigurationForm from apps.dds.forms import DDSConfigurationForm @@ -37,6 +37,8 @@ from apps.abs.models import ABSConfiguration from apps.rc.models import RCConfiguration, RCLine, RCLineType from apps.dds.models import DDSConfiguration +from radarsys.celery import app + from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import user_passes_test from django.contrib.admin.views.decorators import staff_member_required @@ -836,7 +838,7 @@ def experiment_mix(request, id_exp): @user_passes_test(lambda u:u.is_staff) def experiment_mix_delete(request, id_exp): - conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) + conf = RCConfiguration.objects.get(experiment=id_exp, mix=True, type=0) values = conf.parameters.split('-') conf.parameters = '-'.join(values[:-1]) conf.save() @@ -1653,7 +1655,7 @@ def radar_start(request, id_camp, id_radar): now = datetime.now() for exp in experiments: start = datetime.combine(datetime.now().date(), exp.start_time) - end = datetime.combine(datetime.now().date(), exp.start_time) + end = datetime.combine(datetime.now().date(), exp.end_time) if end < start: end += timedelta(1) @@ -1670,16 +1672,17 @@ def radar_start(request, id_camp, id_radar): continue if now > start and now <= end: - task = task_start.delay(exp.pk) + exp.status = 3 + exp.save() + task = task_start.delay(exp.id) exp.status = task.wait() if exp.status==0: messages.error(request, 'Experiment {} not start'.format(exp)) if exp.status==2: - task = task_stop.apply_async((exp.pk,), eta=end+timedelta(hours=5)) messages.success(request, 'Experiment {} started'.format(exp)) else: - task = task_start.apply_async((exp.pk,), eta=start+timedelta(hours=5)) - task = task_stop.apply_async((exp.pk,), eta=end+timedelta(hours=5)) + task = task_start.apply_async((exp.pk, ), eta=start+timedelta(hours=5)) + exp.task = task.id exp.status = 3 messages.success(request, 'Experiment {} programmed to start at {}'.format(exp, start)) @@ -1696,14 +1699,13 @@ def radar_stop(request, id_camp, id_radar): for exp in experiments: + if exp.task: + app.control.revoke(exp.task) if exp.status == 2: - task = task_stop.delay(exp.pk) - exp.status = task.wait() + exp.stop() messages.warning(request, 'Experiment {} stopped'.format(exp)) - exp.save() - else: - messages.error(request, 'Experiment {} not running'.format(exp)) - kill_tasks() + exp.status = 1 + exp.save() return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) @@ -1731,4 +1733,4 @@ def real_time(request): kwargs['graphic_path'] = graphic_path kwargs['graphic1_path'] = 'http://www.bluemaize.net/im/girls-accessories/shark-beanie-11.jpg' - return render(request, 'real_time.html', kwargs) \ No newline at end of file + return render(request, 'real_time.html', kwargs) diff --git a/radarsys/celery.py b/radarsys/celery.py index 6024fa8..a2a5b78 100644 --- a/radarsys/celery.py +++ b/radarsys/celery.py @@ -10,6 +10,7 @@ app = Celery('radarsys') # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') +app.conf.broker_transport_options = {'visibility_timeout': 86400} app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)