Skip to content

Commit 0b26787

Browse files
committed
Refactoring and normalisation.
1 parent 0422e22 commit 0b26787

File tree

6 files changed

+89
-77
lines changed

6 files changed

+89
-77
lines changed

app/dmdbctl.f90

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program dmdbctl
1010
character(len=*), parameter :: APP_NAME = 'dmdbctl'
1111
integer, parameter :: APP_MAJOR = 0
1212
integer, parameter :: APP_MINOR = 9
13-
integer, parameter :: APP_PATCH = 4
13+
integer, parameter :: APP_PATCH = 5
1414

1515
! Database operations (CRUD).
1616
integer, parameter :: OP_NONE = 0
@@ -386,44 +386,62 @@ end function db_update
386386

387387
integer function read_args(app) result(rc)
388388
!! Reads command-line arguments.
389+
integer, parameter :: OPT_CREATE = 1
390+
integer, parameter :: OPT_READ = 2
391+
integer, parameter :: OPT_UPDATE = 3
392+
integer, parameter :: OPT_DELETE = 4
393+
integer, parameter :: OPT_DATABASE = 5
394+
integer, parameter :: OPT_ID = 6
395+
integer, parameter :: OPT_NAME = 7
396+
integer, parameter :: OPT_META = 8
397+
integer, parameter :: OPT_NODE = 9
398+
integer, parameter :: OPT_SN = 10
399+
integer, parameter :: OPT_TYPE = 11
400+
integer, parameter :: OPT_STATE = 12
401+
integer, parameter :: OPT_X = 13
402+
integer, parameter :: OPT_Y = 14
403+
integer, parameter :: OPT_Z = 15
404+
integer, parameter :: OPT_LON = 16
405+
integer, parameter :: OPT_LAT = 17
406+
integer, parameter :: OPT_ALT = 18
407+
integer, parameter :: OPT_VERBOSE = 19
408+
389409
type(app_type), intent(out) :: app !! App settings.
390410

391411
character(len=SENSOR_TYPE_NAME_LEN) :: sensor ! Sensor type name.
392412
character(len=TYPE_NAME_LEN) :: type ! DMPACK derived type name.
393413

394414
integer :: i, n
395415
logical :: mask(OP_LAST) ! CRUD operation mask.
396-
type(arg_type) :: args(19)
416+
type(arg_type) :: args(OPT_VERBOSE)
397417

398418
! Required and optional command-line arguments.
399-
args = [ &
400-
arg_type('create', short='C', type=ARG_TYPE_STRING), & ! -C, --create <type>
401-
arg_type('read', short='R', type=ARG_TYPE_STRING), & ! -R, --read <type>
402-
arg_type('update', short='U', type=ARG_TYPE_STRING), & ! -U, --update <type>
403-
arg_type('delete', short='D', type=ARG_TYPE_STRING), & ! -D, --delete <type>
404-
arg_type('database', short='d', type=ARG_TYPE_DATABASE, required=.true.), & ! -d, --database <path>
405-
arg_type('id', short='I', type=ARG_TYPE_ID, required=.true.), & ! -I, --id <id>
406-
arg_type('name', short='n', type=ARG_TYPE_STRING, max_len=NODE_NAME_LEN), & ! -n, --name <string>
407-
arg_type('meta', short='M', type=ARG_TYPE_STRING, max_len=NODE_META_LEN), & ! -M, --meta <string>
408-
arg_type('node', short='N', type=ARG_TYPE_ID), & ! -N, --node <id>
409-
arg_type('sn', short='Q', type=ARG_TYPE_STRING, max_len=SENSOR_SN_LEN), & ! -Q, --sn <string>
410-
arg_type('type', short='t', type=ARG_TYPE_STRING, max_len=SENSOR_TYPE_NAME_LEN), & ! -t, --type <type>
411-
arg_type('state', short='S', type=ARG_TYPE_INTEGER), & ! -S, --state <state>
412-
arg_type('x', short='X', type=ARG_TYPE_REAL), & ! -X, --x <x>
413-
arg_type('y', short='Y', type=ARG_TYPE_REAL), & ! -Y, --y <y>
414-
arg_type('z', short='Z', type=ARG_TYPE_REAL), & ! -Z, --z <z>
415-
arg_type('lon', short='G', type=ARG_TYPE_REAL), & ! -G, --lon <lng>
416-
arg_type('lat', short='L', type=ARG_TYPE_REAL), & ! -L, --lat <lat>
417-
arg_type('alt', short='A', type=ARG_TYPE_REAL), & ! -A, --alt <alt>
418-
arg_type('verbose', short='V', type=ARG_TYPE_LOGICAL) & ! -V, --verbose
419-
]
419+
args(OPT_CREATE) = arg_type('create', short='C', type=ARG_TYPE_STRING) ! -C, --create <type>
420+
args(OPT_READ) = arg_type('read', short='R', type=ARG_TYPE_STRING) ! -R, --read <type>
421+
args(OPT_UPDATE) = arg_type('update', short='U', type=ARG_TYPE_STRING) ! -U, --update <type>
422+
args(OPT_DELETE) = arg_type('delete', short='D', type=ARG_TYPE_STRING) ! -D, --delete <type>
423+
args(OPT_DATABASE) = arg_type('database', short='d', type=ARG_TYPE_DATABASE, required=.true.) ! -d, --database <path>
424+
args(OPT_ID) = arg_type('id', short='I', type=ARG_TYPE_ID, required=.true.) ! -I, --id <id>
425+
args(OPT_NAME) = arg_type('name', short='n', type=ARG_TYPE_STRING, max_len=NODE_NAME_LEN) ! -n, --name <string>
426+
args(OPT_META) = arg_type('meta', short='M', type=ARG_TYPE_STRING, max_len=NODE_META_LEN) ! -M, --meta <string>
427+
args(OPT_NODE) = arg_type('node', short='N', type=ARG_TYPE_ID) ! -N, --node <id>
428+
args(OPT_SN) = arg_type('sn', short='Q', type=ARG_TYPE_STRING, max_len=SENSOR_SN_LEN) ! -Q, --sn <string>
429+
args(OPT_TYPE) = arg_type('type', short='t', type=ARG_TYPE_STRING, max_len=SENSOR_TYPE_NAME_LEN) ! -t, --type <type>
430+
args(OPT_STATE) = arg_type('state', short='S', type=ARG_TYPE_INTEGER) ! -S, --state <state>
431+
args(OPT_X) = arg_type('x', short='X', type=ARG_TYPE_REAL) ! -X, --x <x>
432+
args(OPT_Y) = arg_type('y', short='Y', type=ARG_TYPE_REAL) ! -Y, --y <y>
433+
args(OPT_Z) = arg_type('z', short='Z', type=ARG_TYPE_REAL) ! -Z, --z <z>
434+
args(OPT_LON) = arg_type('lon', short='G', type=ARG_TYPE_REAL) ! -G, --lon <lng>
435+
args(OPT_LAT) = arg_type('lat', short='L', type=ARG_TYPE_REAL) ! -L, --lat <lat>
436+
args(OPT_ALT) = arg_type('alt', short='A', type=ARG_TYPE_REAL) ! -A, --alt <alt>
437+
args(OPT_VERBOSE) = arg_type('verbose', short='V', type=ARG_TYPE_LOGICAL) ! -V, --verbose
420438

421439
! Read command-line arguments.
422440
rc = dm_arg_read(args, version_callback)
423441
if (dm_is_error(rc)) return
424442

425443
! CRUD operation.
426-
mask = [ (args(i)%passed, i = 1, OP_LAST) ]
444+
mask = [ (args(i)%passed, i = OPT_CREATE, OPT_DELETE) ]
427445
n = count(mask)
428446

429447
rc = E_INVALID
@@ -435,64 +453,63 @@ integer function read_args(app) result(rc)
435453
return
436454
end if
437455

438-
! Get entity type (node, sensor, target).
439-
app%operation = sum(merge([1, 2, 3, 4], 0, mask))
456+
app%operation = sum(merge([OP_CREATE, OP_READ, OP_UPDATE, OP_DELETE], 0, mask))
440457
call dm_arg_get(args(app%operation), type)
441458
app%type = dm_type_from_name(type)
442459

443460
! Get remaining command-line arguments.
444-
call dm_arg_get(args(5), app%database)
461+
call dm_arg_get(args(OPT_DATABASE), app%database)
445462

446463
select case (app%type)
447464
case (TYPE_NODE)
448465
! Get node attributes.
449-
call dm_arg_get(args( 6), app%node%id)
450-
call dm_arg_get(args( 7), app%node%name, passed=app%mask(ATTR_NAME))
451-
call dm_arg_get(args( 8), app%node%meta, passed=app%mask(ATTR_META))
452-
call dm_arg_get(args(13), app%node%x, passed=app%mask(ATTR_X))
453-
call dm_arg_get(args(14), app%node%y, passed=app%mask(ATTR_Y))
454-
call dm_arg_get(args(15), app%node%z, passed=app%mask(ATTR_Z))
455-
call dm_arg_get(args(16), app%node%lon, passed=app%mask(ATTR_LON))
456-
call dm_arg_get(args(17), app%node%lat, passed=app%mask(ATTR_LAT))
457-
call dm_arg_get(args(18), app%node%alt, passed=app%mask(ATTR_ALT))
466+
call dm_arg_get(args(OPT_ID), app%node%id)
467+
call dm_arg_get(args(OPT_NAME), app%node%name, passed=app%mask(ATTR_NAME))
468+
call dm_arg_get(args(OPT_META), app%node%meta, passed=app%mask(ATTR_META))
469+
call dm_arg_get(args(OPT_X), app%node%x, passed=app%mask(ATTR_X))
470+
call dm_arg_get(args(OPT_Y), app%node%y, passed=app%mask(ATTR_Y))
471+
call dm_arg_get(args(OPT_Z), app%node%z, passed=app%mask(ATTR_Z))
472+
call dm_arg_get(args(OPT_LON), app%node%lon, passed=app%mask(ATTR_LON))
473+
call dm_arg_get(args(OPT_LAT), app%node%lat, passed=app%mask(ATTR_LAT))
474+
call dm_arg_get(args(OPT_ALT), app%node%alt, passed=app%mask(ATTR_ALT))
458475

459476
case (TYPE_SENSOR)
460477
! Get sensor attributes.
461-
call dm_arg_get(args( 6), app%sensor%id)
462-
call dm_arg_get(args( 7), app%sensor%name, passed=app%mask(ATTR_NAME))
463-
call dm_arg_get(args( 8), app%sensor%meta, passed=app%mask(ATTR_META))
464-
call dm_arg_get(args( 9), app%sensor%node_id, passed=app%mask(ATTR_NODE))
465-
call dm_arg_get(args(10), app%sensor%sn, passed=app%mask(ATTR_SN))
466-
call dm_arg_get(args(11), sensor, passed=app%mask(ATTR_TYPE), default=SENSOR_TYPE_NAMES(SENSOR_TYPE_NONE))
467-
call dm_arg_get(args(13), app%sensor%x, passed=app%mask(ATTR_X))
468-
call dm_arg_get(args(14), app%sensor%y, passed=app%mask(ATTR_Y))
469-
call dm_arg_get(args(15), app%sensor%z, passed=app%mask(ATTR_Z))
470-
call dm_arg_get(args(16), app%sensor%lon, passed=app%mask(ATTR_LON))
471-
call dm_arg_get(args(17), app%sensor%lat, passed=app%mask(ATTR_LAT))
472-
call dm_arg_get(args(18), app%sensor%alt, passed=app%mask(ATTR_ALT))
478+
call dm_arg_get(args(OPT_ID), app%sensor%id)
479+
call dm_arg_get(args(OPT_NAME), app%sensor%name, passed=app%mask(ATTR_NAME))
480+
call dm_arg_get(args(OPT_META), app%sensor%meta, passed=app%mask(ATTR_META))
481+
call dm_arg_get(args(OPT_NODE), app%sensor%node_id, passed=app%mask(ATTR_NODE))
482+
call dm_arg_get(args(OPT_SN), app%sensor%sn, passed=app%mask(ATTR_SN))
483+
call dm_arg_get(args(OPT_TYPE), sensor, passed=app%mask(ATTR_TYPE), default=SENSOR_TYPE_NAMES(SENSOR_TYPE_NONE))
484+
call dm_arg_get(args(OPT_X), app%sensor%x, passed=app%mask(ATTR_X))
485+
call dm_arg_get(args(OPT_Y), app%sensor%y, passed=app%mask(ATTR_Y))
486+
call dm_arg_get(args(OPT_Z), app%sensor%z, passed=app%mask(ATTR_Z))
487+
call dm_arg_get(args(OPT_LON), app%sensor%lon, passed=app%mask(ATTR_LON))
488+
call dm_arg_get(args(OPT_LAT), app%sensor%lat, passed=app%mask(ATTR_LAT))
489+
call dm_arg_get(args(OPT_ALT), app%sensor%alt, passed=app%mask(ATTR_ALT))
473490

474491
app%sensor%type = dm_sensor_type_from_name(sensor)
475492

476493
case (TYPE_TARGET)
477494
! Get target attributes.
478-
call dm_arg_get(args( 6), app%target%id)
479-
call dm_arg_get(args( 7), app%target%name, passed=app%mask(ATTR_NAME))
480-
call dm_arg_get(args( 8), app%target%meta, passed=app%mask(ATTR_META))
481-
call dm_arg_get(args(12), app%target%state, passed=app%mask(ATTR_STATE))
482-
call dm_arg_get(args(13), app%target%x, passed=app%mask(ATTR_X))
483-
call dm_arg_get(args(14), app%target%y, passed=app%mask(ATTR_Y))
484-
call dm_arg_get(args(15), app%target%z, passed=app%mask(ATTR_Z))
485-
call dm_arg_get(args(16), app%target%lon, passed=app%mask(ATTR_LON))
486-
call dm_arg_get(args(17), app%target%lat, passed=app%mask(ATTR_LAT))
487-
call dm_arg_get(args(18), app%target%alt, passed=app%mask(ATTR_ALT))
495+
call dm_arg_get(args(OPT_ID), app%target%id)
496+
call dm_arg_get(args(OPT_NAME), app%target%name, passed=app%mask(ATTR_NAME))
497+
call dm_arg_get(args(OPT_META), app%target%meta, passed=app%mask(ATTR_META))
498+
call dm_arg_get(args(OPT_STATE), app%target%state, passed=app%mask(ATTR_STATE))
499+
call dm_arg_get(args(OPT_X), app%target%x, passed=app%mask(ATTR_X))
500+
call dm_arg_get(args(OPT_Y), app%target%y, passed=app%mask(ATTR_Y))
501+
call dm_arg_get(args(OPT_Z), app%target%z, passed=app%mask(ATTR_Z))
502+
call dm_arg_get(args(OPT_LON), app%target%lon, passed=app%mask(ATTR_LON))
503+
call dm_arg_get(args(OPT_LAT), app%target%lat, passed=app%mask(ATTR_LAT))
504+
call dm_arg_get(args(OPT_ALT), app%target%alt, passed=app%mask(ATTR_ALT))
488505

489506
case default
490507
rc = E_INVALID
491508
call dm_error_out(rc, 'invalid data type ' // trim(type) // ' (either node, sensor, or target)')
492509
return
493510
end select
494511

495-
call dm_arg_get(args(19), app%verbose)
512+
call dm_arg_get(args(OPT_VERBOSE), app%verbose)
496513

497514
! Validate options.
498515
rc = E_INVALID

config/dmfs.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ observs = {
3636
receivers = { "dmdb" },
3737
requests = {
3838
{
39-
name = "get-temp"
39+
name = "get_temp"
4040
request = owfs_path,
4141
pattern = "(?<temp>[-+0-9\\.]+)",
4242
delay = 500,

config/dmpipe.conf.sample

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ observ = {
3232
-- Read CPU temperature.
3333
--
3434
{
35-
name = "get-cpu-temp",
35+
name = "get_cpu_temp",
3636
request = "sysctl hw.acpi.thermal.tz0.temperature",
3737
pattern = "[.a-z]+: (?<tz0>[-+0-9\\.,]+)C",
3838
delay = 0,
@@ -48,7 +48,7 @@ observ = {
4848
-- Read laptop battery status.
4949
--
5050
{
51-
name = "get-battery",
51+
name = "get_battery",
5252
request = "sysctl hw.acpi.battery.life",
5353
pattern = "[.a-z]+: (?<battery>[0-9]+)",
5454
delay = 0,

config/dmserial.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ observ_meter = {
9797
receivers = { "dmdb" },
9898
requests = {
9999
{
100-
name = "get-values",
100+
name = "get_values",
101101
request = "Meter\\r",
102102
delimiter = "\\n",
103103
pattern = "^\\s*(?<temp>[-0-9.]+)\\s.C\\s+.t\\s+(?<humrel>[-0-9.]+)\\s%\\s+.t\\s+(?<humabs>[-0-9.]+)\\sg.m3.t\\s+(?<dew>[-0-9.]+)\\s.C\\s+.t\\s+(?<wetbulb>[-0-9.]+)",

guide/guide.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,7 +3347,7 @@ an additional `\`. Simply replace every occurance of `\` with `\\`:
33473347
....
33483348

33493349
Set the regular expression pattern as value of attribute `pattern` in request
3350-
`get-values` of observation `meter`. The complete configuration file of
3350+
`get_values` of observation `meter`. The complete configuration file of
33513351
*dmserial* is listed below.
33523352

33533353
[source,lua]
@@ -3372,7 +3372,7 @@ observs = {
33723372
receivers = { }, -- List of receivers (up to 16).
33733373
requests = { -- List of requests (up to 8).
33743374
{
3375-
name = "start-sensor", -- Request name (required).
3375+
name = "start_sensor", -- Request name (required).
33763376
request = "\\r", -- Raw request to send to sensor.
33773377
delimiter = "\\n", -- Response delimiter.
33783378
pattern = "", -- RegEx pattern of the response.
@@ -3389,7 +3389,7 @@ observs = {
33893389
receivers = { }, -- List of receivers (up to 16).
33903390
requests = { -- List of requests (up to 8).
33913391
{
3392-
name = "stop-meter", -- Request name (required).
3392+
name = "stop_meter", -- Request name (required).
33933393
request = "s\\r", -- Raw request to send to sensor.
33943394
delimiter = "", -- Response delimiter.
33953395
pattern = "", -- RegEx pattern of the response.
@@ -3404,7 +3404,7 @@ observs = {
34043404
receivers = { "dmdb" }, -- List of receivers (up to 16).
34053405
requests = { -- List of requests (up to 8).
34063406
{
3407-
name = "get-values", -- Request name (required).
3407+
name = "get_values", -- Request name (required).
34083408
request = "Meter\\r", -- Raw request to send to sensor.
34093409
delimiter = "\\r", -- Response delimiter.
34103410
pattern = "^\\s*(?<temp>[-0-9.]+)\\s.C\\s+.t\\s+(?<humrel>[-0-9.]+)\\s%\\s+.t\\s+(?<humabs>[-0-9.]+)\\sg.m3.t\\s+(?<dew>[-0-9.]+)\\s.C\\s+.t\\s+(?<wetbulb>[-0-9.]+)",
@@ -3594,7 +3594,7 @@ dmfs = {
35943594
disabled = false, -- Skip job.
35953595
onetime = false, -- Run job only once.
35963596
observation = { -- Observation to execute (required).
3597-
name = "get-temp", -- Observation name (required).
3597+
name = "get_temp", -- Observation name (required).
35983598
target_id = target_id, -- Target id (required).
35993599
receivers = { "dmdb" }, -- List of receivers (up to 16).
36003600
requests = { -- List of files to read.
@@ -5488,7 +5488,7 @@ jobs = {
54885488
delay = 5 * 1000,
54895489
observation = {
54905490
-- Initialise RTS and send responses to dmdb.
5491-
name = "init-tps",
5491+
name = "init_tps",
54925492
target_id = "p99",
54935493
receivers = { "dmdb" },
54945494
requests = {
@@ -5509,7 +5509,7 @@ jobs = {
55095509
delay = 10 * 1000,
55105510
observation = {
55115511
-- Set position and start measurement, send responses to dmdb.
5512-
name = "get-p01",
5512+
name = "get_p01",
55135513
target_id = "p01",
55145514
receivers = { "dmdb" },
55155515
requests = {

src/dm_ve.f90

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ module dm_ve
248248
!! print '("Record is invalid")'
249249
!! end if
250250
!!
251+
!! call dm_ve_frame_reset(frame)
251252
!! return
252253
!! end if
253254
!!
@@ -256,8 +257,6 @@ module dm_ve
256257
!! print '("Label: ", a, " Value: ", a)', frame%label, trim(frame%value)
257258
!! print '("Name: ", a, " Value: ", f8.1)', response%name, response%value
258259
!! end if
259-
!!
260-
!! call dm_ve_frame_reset(frame)
261260
!! ```
262261
!!
263262
!! ## References
@@ -896,11 +895,7 @@ pure elemental subroutine dm_ve_frame_read(frame, response, field_type, error)
896895

897896
case (RESPONSE_TYPE_LOGICAL)
898897
rc = E_NONE
899-
if (frame%value == 'ON') then
900-
response%value = dm_to_real64(.true.)
901-
else
902-
response%value = dm_to_real64(.false.)
903-
end if
898+
response%value = dm_to_real64((frame%value == 'ON'))
904899
end select
905900

906901
response%error = rc

0 commit comments

Comments
 (0)