-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmist_mod_array.c
More file actions
144 lines (118 loc) · 4.61 KB
/
mist_mod_array.c
File metadata and controls
144 lines (118 loc) · 4.61 KB
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
/**
* Basic example for sending multiple values of the same type.
*
* Two different scenarios are possible:
* 1) A sequence of events - for example 10 temperature readings taken
* every 60 seconds.
* 2) A number of readings of the same type taken from different data channels
* - for example readings taken from 10 different temperature sensors at the
* same time.
*
* Copyright Thinnect Inc. 2020
* @license MIT
*/
#include "mist_middleware.h"
#include "dt_types.h"
#include "MLE.h"
#include "platform.h"
#include <inttypes.h>
#include <string.h>
#include "yxktime.h"
#include "loglevels.h"
#define __MODUUL__ "m_m_a"
#define __LOG_LEVEL__ (LOG_LEVEL_mist_mod_array & BASE_LOG_LEVEL)
#include "log.h"
#define DATA_ARRAY_ELEMENTS 10
static bool m_parallel;
static mist_module_t m_example_array_module;
static uint16_t format_sequential_array (uint8_t buffer[], uint16_t length)
{
ml_encoder_t enc;
if(ML_SUCCESS == MLE_initialize(&enc, buffer, length))
{
// Values: 10.0, 20.0, 30.0, 40.0. 50.0, 60.0, 70.0, 80.0, 90.0, 1000.0
//static int16_t values[DATA_ARRAY_ELEMENTS];
static int16_t values[DATA_ARRAY_ELEMENTS] = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
// Timestamp offsets from the dt_timestamp_utc listed below for each
// element of the values array
static int16_t times[] = {-60,-120,-180,-240,-300,-360,-420,-480,-540,-600};
uint8_t index1 = MLE_appendOV(&enc, dt_data, dt_temperature_C);
uint8_t index2 = MLE_appendOS(&enc, dt_value, index1);
// Append the array object, array element size is determined from buffer
// size and array object value (number of elements)
MLE_appendOSVB(&enc, dt_array, index2, sizeof(values)/sizeof(int16_t), (uint8_t*)values, sizeof(values));
// All the values in the array have been multiplied by 10, so tell
// the receiver to divide by 10
MLE_appendOSV(&enc, dt_exp, index2, -1);
// Production timestamp array, the values are sequential, timestamps
// offset from dt_timestamp_utc
uint8_t index3 = MLE_appendOS(&enc, dt_production_start, index1);
MLE_appendOSVB(&enc, dt_array, index3, sizeof(times)/sizeof(int16_t), (uint8_t*)times, sizeof(times));
// Add an UTC timestamp with the epoch at 2000-01-01
MLE_appendOSV(&enc, dt_timestamp_utc, index1, 604401880); // time_yxk(time(NULL)));
return MLE_finalize(&enc);
}
return 0;
}
static uint16_t format_parallel_array (uint8_t buffer[], uint16_t length)
{
ml_encoder_t enc;
if(ML_SUCCESS == MLE_initialize(&enc, buffer, length))
{
// Values: 10.0, 20.0, 30.0, 40.0. 50.0, 60.0, 70.0, 80.0, 90.0, 1000.0
static int16_t values[DATA_ARRAY_ELEMENTS] = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
uint8_t index1 = MLE_appendOV(&enc, dt_data, dt_temperature_C);
uint8_t index2 = MLE_appendOS(&enc, dt_value, index1);
// Append the array object, array element size is determined from buffer
// size and array object value (number of elements)
MLE_appendOSVB(&enc, dt_array, index2, sizeof(values)/sizeof(int16_t), (uint8_t*)values, sizeof(values));
// All the values in the array have been multiplied by 10, so tell
// the receiver to divide by 10
MLE_appendOSV(&enc, dt_exp, index2, -1);
// The array contains data elements with the same timestamp from
// different channels (sensors)
MLE_appendOS(&enc, dt_parallel, index1);
// A parallel array is interpreted by the gateway and split into
// different channels for the JSON output format. The data types end up
// being dt_temperature_C_0, dt_temperature_C_1 ...
// This transformation is currently not reversible
// Add an UTC timestamp with the epoch at 2000-01-01
MLE_appendOSV(&enc, dt_timestamp_utc, index1, 604401880); // time_yxk(time(NULL)));
return MLE_finalize(&enc);
}
return 0;
}
static mist_error_t array_data_func (
mist_item_type_t itype, void * input, uint16_t input_length,
mist_item_type_t * otype, void * output, uint16_t output_size, uint16_t * output_length)
{
if (MIST_ITEM_NULL == itype)
{
*otype = MIST_ITEM_MOTEXML;
if (m_parallel)
{
*output_length = format_parallel_array(output, output_size);
}
else
{
*output_length = format_sequential_array(output, output_size);
}
return MIST_SUCCESS;
}
return MIST_FAIL;
}
bool mist_mod_array_init (bool parallel)
{
m_parallel = parallel;
// Register the example sensor
m_example_array_module.data_type = dt_temperature_C;
m_example_array_module.function = array_data_func;
memset(m_example_array_module.uuid, 0, UUID_LENGTH);
mist_error_t result = mist_register_handler(&m_example_array_module);
if (MIST_SUCCESS != result)
{
err1("mist reg %d", result);
return false;
}
return true;
}