-
Notifications
You must be signed in to change notification settings - Fork 0
/
station_config.py
1156 lines (1087 loc) · 40.1 KB
/
station_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# Copyright (C) 2021 IST-SUPSI (www.supsi.ch/ist)
#
# Author: Daniele Strigaro
#
# This file is part of station_configurator.
#
# station_configurator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# station_configurator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with station_configurator. If not, see <http://www.gnu.org/licenses/>.
import configparser
import os
import time
import logging
import json
import sys
import argparse
import getpass
from lib import SaraR4
# external lib
import yaml
import requests
from crontab import CronTab
import logzero
# sensors libs
from drivers.ponsel import Ponsel
from drivers.lufft import WS_UMB
from drivers.unilux import Unilux
from drivers.trilux import Trilux
from drivers.ina219 import read_ina219
__author__ = "Daniele Strigaro"
__email__ = "[email protected]"
__license__ = "Apache License, Version 2.0"
__revision__ = "$Rev: 100 $"
__date__ = "$Date: 2020-02-19 17:22:30 $"
__abspath__ = os.path.abspath(os.getcwd())
__config__ = os.path.join(
__abspath__,
'default.cfg'
)
# create log folder and file
log_path = os.path.join(
__abspath__, 'log'
)
if not os.path.exists(log_path):
os.makedirs(log_path)
logzero.logfile(
os.path.join(
__abspath__,
'log',
"station.log"
),
maxBytes=1e6,
backupCount=3
)
"""
This function save the configuration into the config file
(e.g. default.cfg).
Attributes:
real (int): The real part of complex number.
imag (int): The imaginary part of complex number.
"""
class Station():
"""
This is a class for configure a monitoring station.
"""
def __init__(self):
self.logger = logzero.logger
# reading configuration file default.cfg
self.config = configparser.ConfigParser()
self.config.read(__config__)
self.sections = self.config.sections()
# istsos variables
self.istsos_url = self.config['DEFAULT']['istsos']
self.istsos_srv = self.config['DEFAULT']['service']
self.istsos_auth = (
self.config['DEFAULT']['user'],
self.config['DEFAULT']['password']
)
# remote istsos variables
self.remote_istsos_url = self.config['DEFAULT']['remote_istsos_url']
self.remote_istsos_srv = self.config['DEFAULT']['remote_istsos_service']
self.remote_istsos_auth_type = self.config['DEFAULT']['remote_istsos_auth']
if self.remote_istsos_auth_type == 'oauth':
self.remote_oauth_token_url = self.config['DEFAULT']['remote_oauth_token_url']
self.remote_oauth_client_id = self.config['DEFAULT']['remote_oauth_client_id']
body_data = {
"username": self.config['DEFAULT']['remote_istsos_user'],
"password": self.config['DEFAULT']['remote_istsos_password'],
"grant_type": "password",
"client_id": "istsos-istsos"
}
req = requests.post(
self.remote_oauth_token_url,
headers={
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
data=body_data
)
if req.status_code==200:
res = req.json()
self.remote_istsos_auth = "Bearer {}".format(
res['access_token']
)
else:
raise "Can't get authorization token from oauth service"
elif self.remote_istsos_auth == 'basic':
self.remote_istsos_auth = (
self.config['DEFAULT']['remote_istsos_user'],
self.config['DEFAULT']['remote_istsos_password']
)
else:
print(self.remote_istsos_auth)
raise "remote_istsos_auth: remote authorization type not recognized"
# checking configuration file
self.check_default_section()
self.mqtt_login = False
def save_config(self):
"""
This function save the configuration into the config file
(e.g. default.cfg).
"""
with open(__config__, 'w') as f:
self.config.write(f)
def connect(self, serial_path, radio_mode, apn, umnoprof=100, debug=False):
try:
# SaraR4.reset()
self.sara_module = SaraR4.SaraR4Module(
serial_path=serial_path,
DEBUG=debug
)
# sara_module.set_umnoprof(umnoprof)
self.sara_module.set_radio_mode(radio_mode)
self.sara_module.set_provider_name(apn)
# sara_module.set_operator(22801, TIMEOUT=120)
if not self.sara_module.init():
time.sleep(5)
if not self.sara_module.init():
raise Exception("Can't connect to operator")
return self.sara_module
except Exception as e:
SaraR4.power_off()
raise e
def set_mqtt_params(self, mqtt_server, mqtt_port, mqtt_ssl, mqtt_user, mqtt_password):
self.sara_module.set_mqtt_params(
mqtt_server, mqtt_port,
mqtt_ssl, mqtt_user,
mqtt_password
)
def mqtt_logout(self):
return self.sara_module.mqtt_logout()
def mqtt_publish(self, topic, data):
self.sara_module.mqtt_publish(topic, data)
def set_send_data(self):
"""
This function creates a job in the crontab
which refers to the python script send_data.py.
This command is performed according to the sending_time
value set in the config file
"""
python_path = sys.executable
if self.config['DEFAULT']['transmission'] == 'lora':
path_script = os.path.join(
__abspath__,
'send_data_lora.py'
)
elif self.config['DEFAULT']['transmission'] == 'fastinsert':
path_script = os.path.join(
__abspath__,
'send_data.py'
)
elif self.config['DEFAULT']['transmission'] == 'mqtt':
raise Exception("transmission: mqtt still to be implemented")
elif self.config['DEFAULT']['transmission'] == 'nbiot':
path_script = os.path.join(
__abspath__,
'send_data_nbiot.py'
)
else:
raise Exception("transmission: transmission type not recognized")
path_config = os.path.join(
__abspath__,
'default.cfg'
)
cron = CronTab(
user=getpass.getuser()
)
path_log = os.path.join(
__abspath__,
'log',
f'send_data.log'
)
command = (
f'/usr/bin/flock -w 0 /tmp/send.lock {python_path} {path_script}'
f' -c {path_config} >> {path_log} 2>&1'
)
add_cron = True
for job in cron:
if command in job.command:
add_cron = False
if add_cron:
if int(self.config['DEFAULT']['sending_time']) > 30:
job = cron.new(command=command)
job.minute.on(
str(int(self.config['DEFAULT']['sending_time']) - 30)
)
cron.write()
job = cron.new(command=command)
job.minute.on(
self.config['DEFAULT']['sending_time']
)
cron.write()
else:
job = cron.new(command=command)
job.minute.every(
self.config['DEFAULT']['sending_time']
)
cron.write()
def create_service(self):
"""
This function creates the main istSOS service
to archive the raw data coming from the sensors
and collected with the python file 'sensor_rw.py'.
The service name is set according to the service
parameter set in the config file.
"""
service_name = self.config['DEFAULT']['service']
epsg = '4326'
if 'epsg' in self.config['DEFAULT'].keys():
epsg = self.config['DEFAULT']['epsg']
data_post = {
"service": f"{service_name}",
"epsg": epsg
}
req = requests.post(
f'{self.istsos_url}/wa/istsos/services',
data=json.dumps(data_post),
auth=self.istsos_auth
)
self.logger.info(f'{self.istsos_url}/wa/istsos/services')
if req.status_code == 200:
check_idx = req.text.find("true")
if check_idx > 0:
return {'success': True}
else:
check_idx = req.text.find("already exist")
if check_idx > 0:
return {'success': True}
else:
return {
'success': False,
'msg': req.text
}
else:
return {
'success': False,
'msg': req.text
}
def create_service_agg(self, section):
"""
This function creates the aggregation istSOS service
to archive the aggregated data coming from
the raw istSOS service and processed by
the python file 'sensor_agg.py'.
The service name is set according to the service
parameter set in the config file plus adding the text 'agg'.
"""
service_name = self.config['DEFAULT']['service']
epsg = '4326'
if 'epsg' in self.config[section].keys():
epsg = self.config[section]['epsg']
data_post = {
"service": f"{service_name}agg",
"epsg": epsg
}
req = requests.post(
f'{self.istsos_url}/wa/istsos/services',
data=json.dumps(data_post),
auth=self.istsos_auth
)
if req.status_code == 200:
check_idx = req.text.find("true")
if check_idx > 0:
return {'success': True}
else:
check_idx = req.text.find("already exist")
if check_idx > 0:
return {'success': True}
else:
return {
'success': False,
'msg': req.text
}
else:
return {
'success': False,
'msg': req.text
}
def set_aggregation(self):
"""
This function creates the aggregation istSOS service
to archive the aggregated data coming from
the raw istSOS service and processed by
the python file 'sensor_agg.py'.
The service name is set according to the service
parameter set in the config file plus adding the text 'agg'.
"""
python_path = sys.executable
path_script = os.path.join(
__abspath__,
'sensor_agg.py'
)
path_config = os.path.join(
__abspath__,
'default.cfg'
)
cron = CronTab(
user=getpass.getuser()
)
for section in self.sections:
add_cron = True
if 'aggregation_time' in self.config[section].keys():
path_log = os.path.join(
__abspath__,
'log',
f'{section}_agg.log'
)
command = (
f'{python_path} {path_script} -s {section}'
f' -c {path_config} >> {path_log} 2>&1'
)
add_cron = True
for job in cron:
if command in job.command:
add_cron = False
if add_cron:
job = cron.new(command=command)
job.minute.every(
self.config[section]['aggregation_time']
)
cron.write()
def set_sampling(self):
"""
This function sets the sampling process for each sensor.
"""
python_path = sys.executable
path_script = os.path.join(
__abspath__,
'sensor_rw.py'
)
path_config = os.path.join(
__abspath__,
'default.cfg'
)
cron = CronTab(
user=getpass.getuser()
)
files_sh = []
for section in self.sections:
if self.config[section].get('assigned_id'):
path_log = os.path.join(
__abspath__,
'log',
f'{section}.log'
)
command = (
f'{python_path} {path_script} -s {section}'
f' -c {path_config} >> {path_log} 2>&1'
)
sampling_time = self.config[section]['sampling_time']
shell_path = os.path.join(
__abspath__,
f'read_{sampling_time}.sh'
)
with open(shell_path, 'a') as f:
f.write(f'{command}\n')
if shell_path not in files_sh:
files_sh.append(shell_path)
for sh in files_sh:
add_cron = True
for job in cron:
if sh in job.command:
add_cron = False
if add_cron:
job = cron.new(command=f"/usr/bin/flock -w 0 /tmp/sampling.lock sh {sh}")
every = sh.split('_')[-1].split('.')[0]
job.minute.every(int(every))
cron.write()
def insert_sensor(self, sensor, section, agg=False, remote=False):
"""
This function registers the sernsors into the istSOS services.
Attributes:
sensor (str): The section related to the sensor name
which is equal to the section
of the default.cfg file.
"""
section_obj = self.config[section]
sensor_type = section_obj['type']
with open(
os.path.join(
__abspath__,
'support',
section_obj['driver'],
f'{sensor_type}.yaml'
)
) as f:
rs = yaml.safe_load(f)
if section_obj['driver'] == 'ponsel':
rs['system_id'] = sensor.get_serial_number()
rs['description'] = sensor.get_pod_desc()
rs['classification'][1]['value'] = sensor.get_pod_desc()
elif section_obj['driver'] == 'lufft':
rs['system_id'] = section
elif section_obj['driver'] == 'unilux':
rs['system_id'] = section
elif section_obj['driver'] == 'trilux':
rs['system_id'] = section
elif section_obj['driver'] == 'ina219':
rs['system_id'] = section
rs['system'] = section
rs['identification'][0][
'value'] = rs['identification'][0]['value'] + section
rs['location']['geometry']['coordinates'] = [
section_obj['lon'],
section_obj['lat'],
section_obj['alt']
]
rs['location']['crs']['properties']['name'] = section_obj['epsg']
rs['location']['properties']['name'] = section_obj['foi']
if agg:
req = requests.post(
f'{self.istsos_url}/wa/istsos/services/'
f'{self.istsos_srv}agg/procedures',
data=json.dumps(rs),
auth=self.istsos_auth
)
elif remote:
if self.remote_istsos_auth_type == 'oauth':
req = requests.post(
f'{self.remote_istsos_url}/wa/istsos/services/' +
f'{self.remote_istsos_srv}/procedures',
headers={
"Authorization": self.remote_istsos_auth
},
data=json.dumps(rs)
)
# req = requests.post(
# f'{self.remote_istsos_url}/wa/istsos/services/'
# f'{self.remote_istsos_srv}/procedures',
# headers={'Authorization': 'Bearer '+ token['access_token']},
# data=json.dumps(rs)
# )
else:
req = requests.post(
f'{self.remote_istsos_url}/wa/istsos/services/'
f'{self.remote_istsos_srv}/procedures',
data=json.dumps(rs),
auth=self.remote_istsos_auth
)
else:
req = requests.post(
f'{self.istsos_url}/wa/istsos/services/'
f'{self.istsos_srv}/procedures',
data=json.dumps(rs),
auth=self.istsos_auth
)
if req.status_code == 200:
check_idx = req.text.find("AssignedSensorId")
if check_idx > 0:
if not remote:
assigned_id = req.text.split(':')[-2].split('<')[0]
if agg:
self.config[section]['assignedagg_id'] = assigned_id
else:
self.config[section]['assigned_id'] = assigned_id
self.save_config()
else:
assigned_id = req.text.split(':')[-2].split('<')[0]
self.config[section]['assignedremote_id'] = assigned_id
self.save_config()
else:
check_idx = req.text.find("already exist")
if check_idx > 0:
if agg:
self.logger.info(
f'--> {section} already exist in service'
f' {self.istsos_srv}agg'
)
else:
self.logger.info(
f'--> {section} already exist in service'
f' {self.istsos_srv}'
)
else:
self.logger.warning(req.text)
else:
self.logger.error(req.text)
def set_sensors(self):
"""
This function sets the sensors.
"""
for section in self.sections:
self.logger.info(f'--> Installing sensor {section}')
if self.config[section]['driver'] == 'ponsel':
try:
if 'addr' in self.config[section].keys():
sensor = Ponsel(
"{}".format(self.config[section]['port']),
int(self.config[section]['addr'])
)
else:
sensor = Ponsel(
"{}".format(self.config[section]['port']),
int(self.config[section]['type'])
)
sensor.set_run_measurement(0x001f)
time.sleep(1)
description = sensor.get_pod_desc()
except:
time.sleep(1)
raise Exception("Can\'t find sensor")
self.logger.info(description)
if description:
self.insert_sensor(sensor, section)
if 'aggregation_time' in self.config[section].keys():
crt_srv_agg = self.create_service_agg(section)
if crt_srv_agg['success']:
self.insert_sensor(
sensor, section,
agg=True
)
if self.remote:
self.insert_sensor(
sensor, section,
agg=False, remote=True
)
else:
self.logger.error(
'\t\t--> Aggregation service NOT created'
)
return crt_srv_agg
else:
return {
'success': False,
'msg': '\t\t--> Can\'t get Ponsel sensor description'
}
self.logger.info(
f'--> Sensor {section} INSTALLED'
)
elif self.config[section]['driver'] == 'unilux':
try:
s = Unilux("{}".format(self.config[section]['port']))
s.start()
data = s.get_values()
if data:
s.stop()
self.insert_sensor(None, section)
if 'aggregation_time' in self.config[section].keys():
crt_srv_agg = self.create_service_agg(section)
if crt_srv_agg['success']:
self.insert_sensor(
None, section,
agg=True
)
if self.remote:
self.insert_sensor(
None, section,
agg=False, remote=True
)
else:
self.logger.error(
'\t\t--> Aggregation service NOT created'
)
return crt_srv_agg
else:
s.stop()
raise Exception("Can\'t get data from Unilux")
except Exception as e:
raise Exception("Can\'t find sensor")
elif self.config[section]['driver'] == 'trilux':
try:
s = Trilux("{}".format(self.config[section]['port']))
s.start()
data = s.get_values()
if data:
s.stop()
self.insert_sensor(None, section)
if 'aggregation_time' in self.config[section].keys():
crt_srv_agg = self.create_service_agg(section)
if crt_srv_agg['success']:
self.insert_sensor(
None, section,
agg=True
)
if self.remote:
self.insert_sensor(
None, section,
agg=False, remote=True
)
else:
self.logger.error(
'\t\t--> Aggregation service NOT created'
)
return crt_srv_agg
else:
s.stop()
raise Exception("Can\'t get data from Trlilux")
except Exception as e:
raise Exception("Can\'t find sensor")
elif self.config[section]['driver'] == 'lufft':
value_UMB = [200, 305, 500, 510, 900, 100, 110, 440, 400]
try:
values = []
try:
with WS_UMB("{}".format(self.config[section]['port'])) as umb:
for v in value_UMB:
value, st = umb.onlineDataQuery(v, int(self.config[section]['addr']))
values.append(
round(value, 2)
)
self.insert_sensor(umb, section)
if 'aggregation_time' in self.config[section].keys():
crt_srv_agg = self.create_service_agg(section)
if crt_srv_agg['success']:
self.insert_sensor(
umb, section,
agg=True
)
if self.remote:
self.insert_sensor(
umb, section,
agg=False, remote=True
)
else:
self.logger.error(
'\t\t--> Aggregation service NOT created'
)
return crt_srv_agg
except:
raise Exception("Can\'t find sensor")
# with WS_UMB(self.config[section]['port']) as umb:
# for v in value_UMB:
# value, st = umb.onlineDataQuery(v, int(self.config[section]['addr']))
# if st!=0:
# raise Exception('Not working')
except Exception as e:
return {
'success': False,
'msg': '\t\t--> Can\'t find lufft sensor'
}
elif self.config[section]['driver'] == 'ina219':
try:
values = []
try:
data = read_ina219()
self.insert_sensor(None, section)
if 'aggregation_time' in self.config[section].keys():
crt_srv_agg = self.create_service_agg(section)
if crt_srv_agg['success']:
self.insert_sensor(
None, section,
agg=True
)
if self.remote:
self.insert_sensor(
None, section,
agg=False, remote=True
)
else:
self.logger.error(
'\t\t--> Aggregation service NOT created'
)
return crt_srv_agg
except:
raise Exception("Can\'t find sensor")
except Exception as e:
return {
'success': False,
'msg': '\t\t--> Can\'t find lufft sensor'
}
else:
self.logger.error(
'Sensor type not supported.'
'Supported sensor list: ponsel'
)
return {
'success': False,
'msg': (
'Sensor type not supported.'
'Supported sensor list: ponsel'
)
}
return {'success': True}
def check_sensor_conf(self, name):
"""
Check the config file
TBD
"""
available_params = [
'sampling_time', 'aggregation_time',
'sending_time', 'driver', 'type',
'addr', 'port', 'baudrate',
'foi', 'lat', 'lon'
]
params = list(
self.config[name].keys()
)
for par in params:
if par not in available_params:
self.logger.error(
('ERROR --> ')
)
def check_default_section(self):
"""
Check the config file
TBD
"""
if self.config.defaults():
params = list(
self.config.defaults().keys()
)
if 'transmission' not in params:
raise Exception(
'ERROR --> transmission is not set.'
)
elif self.config.defaults()['transmission'] not in [
'lora', 'serial', 'nbiot', 'lte']:
self.logger.error(
'transmission is not set correctly.'
' Accepted values: lora, serial'
)
raise Exception(
(
'ERROR --> transmission is not set correctly.'
' Accepted values: lora, serial'
)
)
if 'istsos' not in params:
self.logger.error(
'istsos url is not set.'
)
raise Exception(
'ERROR --> istsos url is not set.'
)
# check remote istsos conf
if (self.remote_istsos_url and
self.remote_istsos_srv and
self.remote_istsos_auth):
if self.remote_istsos_auth_type == 'oauth':
req = requests.get(
f'{self.remote_istsos_url}/{self.remote_istsos_srv}?'
f'request=getCapabilities&service=SOS',
headers={
"Authorization": self.remote_istsos_auth
}
)
else:
req = requests.get(
f'{self.remote_istsos_url}/{self.remote_istsos_srv}?'
f'request=getCapabilities&service=SOS',
auth=self.remote_istsos_auth
)
if req.status_code == 200:
self.remote = True
else:
logging.error(req.text)
self.remote = False
self.logger.warning(
'The remote istSOS is not correctly set'
' or is not working'
)
else:
self.remote = False
self.logger.warning(
'The remote istSOS is not correctly set'
' or is not working'
)
else:
self.logger.error(
(
'DEFAULT section was'
' not found into the default.cfg file.'
)
)
def delete_service(self, agg=False, remote=False):
"""
This function delete the services configured
on the local istSOS.
Attributes:
agg (bool): If True the aggregation service is deleted.
If False the raw service is deleted.
"""
if agg:
self.logger.info(
'\t--> Aggregator service deleting...'
)
service_name = self.config['DEFAULT']['service'] + 'agg'
data_post = {
"service": f"{service_name}",
}
req = requests.delete(
f'{self.istsos_url}/wa/istsos/services/{service_name}',
data=json.dumps(data_post),
auth=self.istsos_auth
)
elif remote:
self.logger.info(
'\t--> Aggregator service deleting...'
)
service_name = self.config['DEFAULT']['remmote_istsos_service'] + 'agg'
data_post = {
"service": f"{service_name}",
}
if self.remote_istsos_auth_type == 'oauth':
req = requests.delete(
f'{self.remote_istsos_url}/wa/istsos/services/{service_name}',
headers={
"Authorization": self.remote_istsos_auth
},
data=json.dumps(data_post),
)
else:
req = requests.delete(
f'{self.remote_istsos_url}/wa/istsos/services/{service_name}',
data=json.dumps(data_post),
auth=self.remote_istsos_auth
)
else:
self.logger.info(
'\t--> Service deleting...'
)
service_name = self.config['DEFAULT']['service']
data_post = {
"service": f"{service_name}",
}
req = requests.delete(
f'{self.istsos_url}/wa/istsos/services/{service_name}',
data=json.dumps(data_post),
auth=self.istsos_auth
)
if req.status_code == 200:
check_idx = req.text.find("true")
if check_idx > 0:
self.logger.info(
f'\t\t--> Service {service_name} deleted'
)
return {'success': True}
else:
check_idx = req.text.find("already exist")
check_idx2 = req.text.find("does not exist")
if check_idx > 0:
self.logger.info(
f'\t\t--> Service {service_name} deleted'
)
return {'success': True}
elif check_idx2 > 0:
self.logger.info(
f'\t\t--> Service {service_name} does not exist'
)
return {'success': True}
else:
return {
'success': False,
'msg': req.text
}
else:
return {
'success': False,
'msg': 'Status code {}'.format(
req.status_code
)
}
def delete_cron_jobs(self):
"""
This function deletes all the jobs configured.
"""
cron = CronTab(
user=getpass.getuser()
)
removed = False
for job in cron:
if __abspath__ in job.command:
removed = True
cron.remove(job)
if '.sh' in job.command:
file_sh = job.command.split(' ')[-1]
# self.logger.info(job.command.split(' '))
os.remove(file_sh)
cron.write()
return {'success': True}
def reset_conf(self):
"""
This function restore the 'default.cfg' file.
"""
for section in self.sections:
self.config.remove_option(
section, 'assigned_id'
)
self.config.remove_option(
section, 'assignedagg_id'
)
self.save_config()
return {'success': True}
def clean_data(self):
"""
This function delete all the data older than the variable
'max_log_day' set in the 'default.cfg' file.
TBD
"""
self.logger.info(
'--> Clean old data'
)
log_days = int(
self.config['DEFAULT']['max_log_days']
)
# req = request.get(
# '',
# auth=self.istsos_auth
# )
self.logger.info(
'{}'.format(
self.config['DEFAULT']['max_log_days']
)
)
self.logger.info(
'The function is still not developed'
)
def install(self):
"""
This function configures all the processes
to run the monitoring station.
"""
crt = self.create_service()