Class: TestPack1::DataController

Inherits:
BaseController show all
Defined in:
lib/test_pack_1/controllers/data_controller.rb

Overview

DataController

Class Attribute Summary collapse

Attributes inherited from BaseController

#http_call_back, #http_client

Instance Method Summary collapse

Methods inherited from BaseController

#execute_request, #initialize, #validate_parameters, #validate_response

Constructor Details

This class inherits a constructor from TestPack1::BaseController

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



12
13
14
# File 'lib/test_pack_1/controllers/data_controller.rb', line 12

def instance
  @instance
end

Instance Method Details

#get_data(device_ids, data_signal_ids, timestamp_start, timestamp_end, resolution = nil, aggregate = nil, calculation = nil) ⇒ Object

Gets data for multiple devices and data signals in the given resolution. This request can also be made using the POST method, with a JSON request body instead of query parameters. get data for. signals to get data for. to get data for. Timestamps ending with ‘Z’ are treated as UTC. Other timestamps are treated as local time in your system-configured time zone. get data for. Timestamps ending with ‘Z’ are treated as UTC. Other timestamps are treated as local time in your system-configured time zone. resolution. should be aggregated with regards to device(s) or site(s). calculation used when aggregating data, both over time and across devices. The default is the data signal default.

Parameters:

  • device_ids (List of Integer)

    Required parameter: Which devices to

  • data_signal_ids (List of Integer)

    Required parameter: Which data

  • timestamp_start (DateTime)

    Required parameter: The first timestamp

  • timestamp_end (DateTime)

    Required parameter: The last timestamp to

  • resolution (ResolutionEnum) (defaults to: nil)

    Optional parameter: The desired data

  • aggregate (AggregateModeEnum) (defaults to: nil)

    Optional parameter: How the data

  • calculation (CalculationModeEnum) (defaults to: nil)

    Optional parameter: The

Returns:

  • List of DataItem response from the API call



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
# File 'lib/test_pack_1/controllers/data_controller.rb', line 108

def get_data(device_ids,
             data_signal_ids,
             timestamp_start,
             timestamp_end,
             resolution = nil,
             aggregate = nil,
             calculation = nil)
  # Prepare query url.

  _path_url = '/data.json'
  _query_builder = Configuration.get_base_uri
  _query_builder << _path_url
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'deviceIds' => device_ids,
      'dataSignalIds' => data_signal_ids,
      'timestampStart' => timestamp_start,
      'timestampEnd' => timestamp_end,
      'resolution' => resolution,
      'aggregate' => aggregate,
      'calculation' => calculation
    },
    array_serialization: Configuration.array_serialization
  )
  _query_url = APIHelper.clean_url _query_builder
  # Prepare headers.

  _headers = {
    'accept' => 'application/json'
  }
  # Prepare and execute HttpRequest.

  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  CustomQueryAuth.apply(_request)
  _context = execute_request(_request)
  # Validate response against endpoint and global error codes.

  if _context.response.status_code == 400
    raise APIException.new(
      'The request cannot be fulfilled due to bad syntax.',
      _context
    )
  elsif _context.response.status_code == 401
    raise APIException.new(
      'One of the following: * The request is missing a valid API key. *' \
      ' The API key does not authorize access the requested' \
      ' data. Devices   or data signals can be limited. ',
      _context
    )
  elsif _context.response.status_code == 405
    raise APIException.new(
      'The HTTP method is not allowed for the endpoint.',
      _context
    )
  elsif _context.response.status_code == 429
    raise APIException.new(
      'The API key has been used in too many requests in a given amount' \
      ' of time. The following headers will be set in the' \
      ' response: * X-Rate-Limit-Limit - The total number of' \
      ' allowed requests for this period. *' \
      ' X-Rate-Limit-Remaining - The remaining number of' \
      ' requests for this period. * X-Rate-Limit-Reset - The' \
      ' number of seconds left until the end of this period. ',
      _context
    )
  end
  validate_response(_context)
  # Return appropriate response type.

  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  decoded.map { |element| DataItem.from_hash(element) }
end

#get_data_per_category(device_ids, data_signal_id, timestamp_start, timestamp_end, aggregate = nil, category = nil) ⇒ Object

Gets signal data aggregated per availability contract category. This request can also be made using the POST method, with a JSON request body instead of query parameters. get data for. data for; only Lost Production signals are supported at the moment. to get data for. Timestamps ending with ‘Z’ are treated as UTC. Other timestamps are treated as local time in your system-configured time zone. get data for. Timestamps ending with ‘Z’ are treated as UTC. Other timestamps are treated as local time in your system-configured time zone. should be aggregated with regards to device(s) or site(s). status categories to include. By default all categories are included.

Parameters:

  • device_ids (List of Integer)

    Required parameter: Which devices to

  • data_signal_id (Integer)

    Required parameter: Which signal to get

  • timestamp_start (DateTime)

    Required parameter: The first timestamp

  • timestamp_end (DateTime)

    Required parameter: The last timestamp to

  • aggregate (AggregateModeEnum) (defaults to: nil)

    Optional parameter: How the data

  • category (List of StatusCategoryEnum) (defaults to: nil)

    Optional parameter: Which

Returns:

  • DataPerCategoryResponse response from the API call



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
# File 'lib/test_pack_1/controllers/data_controller.rb', line 279

def get_data_per_category(device_ids,
                          data_signal_id,
                          timestamp_start,
                          timestamp_end,
                          aggregate = nil,
                          category = nil)
  # Prepare query url.

  _path_url = '/datapercategory.json'
  _query_builder = Configuration.get_base_uri
  _query_builder << _path_url
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'deviceIds' => device_ids,
      'dataSignalId' => data_signal_id,
      'timestampStart' => timestamp_start,
      'timestampEnd' => timestamp_end,
      'aggregate' => aggregate,
      'category' => category
    },
    array_serialization: Configuration.array_serialization
  )
  _query_url = APIHelper.clean_url _query_builder
  # Prepare headers.

  _headers = {
    'accept' => 'application/json'
  }
  # Prepare and execute HttpRequest.

  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  CustomQueryAuth.apply(_request)
  _context = execute_request(_request)
  # Validate response against endpoint and global error codes.

  if _context.response.status_code == 400
    raise APIException.new(
      'The request cannot be fulfilled due to bad syntax.',
      _context
    )
  elsif _context.response.status_code == 401
    raise APIException.new(
      'One of the following: * The request is missing a valid API key. *' \
      ' The API key does not authorize access the requested' \
      ' data. Devices   or data signals can be limited. ',
      _context
    )
  elsif _context.response.status_code == 405
    raise APIException.new(
      'The HTTP method is not allowed for the endpoint.',
      _context
    )
  elsif _context.response.status_code == 429
    raise APIException.new(
      'The API key has been used in too many requests in a given amount' \
      ' of time. The following headers will be set in the' \
      ' response: * X-Rate-Limit-Limit - The total number of' \
      ' allowed requests for this period. *' \
      ' X-Rate-Limit-Remaining - The remaining number of' \
      ' requests for this period. * X-Rate-Limit-Reset - The' \
      ' number of seconds left until the end of this period. ',
      _context
    )
  end
  validate_response(_context)
  # Return appropriate response type.

  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  DataPerCategoryResponse.from_hash(decoded)
end

#get_data_signals(device_ids) ⇒ Object

Gets authorized data signals for one or more devices. This request can also be made using the POST method, with a JSON request body instead of query parameters. get data signals for.

Parameters:

  • device_ids (List of Integer)

    Required parameter: What devices to

Returns:

  • List of DataSignalItem response from the API call



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
# File 'lib/test_pack_1/controllers/data_controller.rb', line 25

def get_data_signals(device_ids)
  # Prepare query url.

  _path_url = '/datasignals.json'
  _query_builder = Configuration.get_base_uri
  _query_builder << _path_url
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'deviceIds' => device_ids
    },
    array_serialization: Configuration.array_serialization
  )
  _query_url = APIHelper.clean_url _query_builder
  # Prepare headers.

  _headers = {
    'accept' => 'application/json'
  }
  # Prepare and execute HttpRequest.

  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  CustomQueryAuth.apply(_request)
  _context = execute_request(_request)
  # Validate response against endpoint and global error codes.

  if _context.response.status_code == 400
    raise APIException.new(
      'The request cannot be fulfilled due to bad syntax.',
      _context
    )
  elsif _context.response.status_code == 401
    raise APIException.new(
      'One of the following: * The request is missing a valid API key. *' \
      ' The API key does not authorize access the requested' \
      ' data. Devices   or data signals can be limited. ',
      _context
    )
  elsif _context.response.status_code == 405
    raise APIException.new(
      'The HTTP method is not allowed for the endpoint.',
      _context
    )
  elsif _context.response.status_code == 429
    raise APIException.new(
      'The API key has been used in too many requests in a given amount' \
      ' of time. The following headers will be set in the' \
      ' response: * X-Rate-Limit-Limit - The total number of' \
      ' allowed requests for this period. *' \
      ' X-Rate-Limit-Remaining - The remaining number of' \
      ' requests for this period. * X-Rate-Limit-Reset - The' \
      ' number of seconds left until the end of this period. ',
      _context
    )
  end
  validate_response(_context)
  # Return appropriate response type.

  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  decoded.map { |element| DataSignalItem.from_hash(element) }
end

#get_real_time_data(device_ids, data_signal_ids, aggregate = nil, calculation = nil) ⇒ Object

Gets the most recent high-resolution data point for each specified device and data signal. This request can also be made using the POST method, with a JSON request body instead of query parameters. get data for. signals to get data for. should be aggregated with regards to device(s) or site(s). calculation used when aggregating data, both over time and across devices. The default is the data signal default.

Parameters:

  • device_ids (List of Integer)

    Required parameter: Which devices to

  • data_signal_ids (List of Integer)

    Required parameter: Which data

  • aggregate (AggregateModeEnum) (defaults to: nil)

    Optional parameter: How the data

  • calculation (CalculationModeEnum) (defaults to: nil)

    Optional parameter: The

Returns:

  • List of DataRealTimeItem response from the API call



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
# File 'lib/test_pack_1/controllers/data_controller.rb', line 193

def get_real_time_data(device_ids,
                       data_signal_ids,
                       aggregate = nil,
                       calculation = nil)
  # Prepare query url.

  _path_url = '/realtimedata.json'
  _query_builder = Configuration.get_base_uri
  _query_builder << _path_url
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'deviceIds' => device_ids,
      'dataSignalIds' => data_signal_ids,
      'aggregate' => aggregate,
      'calculation' => calculation
    },
    array_serialization: Configuration.array_serialization
  )
  _query_url = APIHelper.clean_url _query_builder
  # Prepare headers.

  _headers = {
    'accept' => 'application/json'
  }
  # Prepare and execute HttpRequest.

  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  CustomQueryAuth.apply(_request)
  _context = execute_request(_request)
  # Validate response against endpoint and global error codes.

  if _context.response.status_code == 400
    raise APIException.new(
      'The request cannot be fulfilled due to bad syntax.',
      _context
    )
  elsif _context.response.status_code == 401
    raise APIException.new(
      'One of the following: * The request is missing a valid API key. *' \
      ' The API key does not authorize access the requested' \
      ' data. Devices   or data signals can be limited. ',
      _context
    )
  elsif _context.response.status_code == 405
    raise APIException.new(
      'The HTTP method is not allowed for the endpoint.',
      _context
    )
  elsif _context.response.status_code == 429
    raise APIException.new(
      'The API key has been used in too many requests in a given amount' \
      ' of time. The following headers will be set in the' \
      ' response: * X-Rate-Limit-Limit - The total number of' \
      ' allowed requests for this period. *' \
      ' X-Rate-Limit-Remaining - The remaining number of' \
      ' requests for this period. * X-Rate-Limit-Reset - The' \
      ' number of seconds left until the end of this period. ',
      _context
    )
  end
  validate_response(_context)
  # Return appropriate response type.

  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  decoded.map { |element| DataRealTimeItem.from_hash(element) }
end

#instanceObject



15
16
17
# File 'lib/test_pack_1/controllers/data_controller.rb', line 15

def instance
  self.class.instance
end