-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.sleep_unit.c
96 lines (79 loc) · 2 KB
/
interpreter.sleep_unit.c
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 - 2022 TheVice
*
*/
#include "sleep_unit.h"
#include "buffer.h"
#include "common.h"
#include "conversion.h"
#include "date_time.h"
#define HOURS_POSITION 0
#define MILLISECONDS_POSITION 1
#define MINUTES_POSITION 2
#define SECONDS_POSITION 3
#define MAX_POSITION (SECONDS_POSITION + 2)
uint8_t sleep_unit_get_attributes_and_arguments_for_task(
const uint8_t*** task_attributes, const uint8_t** task_attributes_lengths,
uint8_t* task_attributes_count, void* task_arguments)
{
static const uint8_t* attributes[] =
{
(const uint8_t*)"hours",
(const uint8_t*)"milliseconds",
(const uint8_t*)"minutes",
(const uint8_t*)"seconds"
};
/**/
static const uint8_t attributes_lengths[] = { 5, 12, 7, 7 };
/**/
return common_get_attributes_and_arguments_for_task(
attributes, attributes_lengths,
COUNT_OF(attributes),
task_attributes, task_attributes_lengths,
task_attributes_count, task_arguments);
}
uint8_t sleep_unit_evaluate_task(void* task_arguments, uint8_t verbose)
{
(void)verbose;
if (NULL == task_arguments)
{
return 0;
}
uint8_t counter = 0;
uint64_t milliseconds = 0;
void* argument = NULL;
while (NULL != (argument = buffer_buffer_data(task_arguments, counter++)))
{
if (!buffer_size(argument))
{
continue;
}
const uint8_t* start = buffer_uint8_t_data(argument, 0);
const uint8_t* finish = start + buffer_size(argument);
const uint64_t argument_value = int64_parse(start, finish);
switch (counter - 1)
{
case HOURS_POSITION:
milliseconds += 1000 * timespan_from_hours((double)argument_value);
break;
case MILLISECONDS_POSITION:
milliseconds += argument_value;
break;
case MINUTES_POSITION:
milliseconds += 1000 * timespan_from_minutes((double)argument_value);
break;
case SECONDS_POSITION:
milliseconds += 1000 * argument_value;
break;
default:
break;
}
}
if (MAX_POSITION != counter)
{
return 0;
}
return sleep_for((uint32_t)milliseconds);
}