• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lets-fiware / node-red-contrib-letsfiware-NGSI / 4155629602

pending completion
4155629602

Pull #69

github

GitHub
Merge 934d10d20 into 774a33e7e
Pull Request #69: ADD feature to check FIWARE GE type

594 of 594 branches covered (100.0%)

Branch coverage included in aggregate %.

9 of 9 new or added lines in 3 files covered. (100.0%)

948 of 948 relevant lines covered (100.0%)

4.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/nodes/NGSI/source/source.js
1
/*
2
   MIT License
3

4
   Copyright 2022-2023 Kazuhito Suda
5

6
   This file is part of node-red-contrib-letsfiware-NGSI
7

8
   https://github.com/lets-fiware/node-red-contrib-letsfiware-NGSI
9

10
   Permission is hereby granted, free of charge, to any person obtaining a copy
11
   of this software and associated documentation files (the "Software"), to deal
12
   in the Software without restriction, including without limitation the rights
13
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
   copies of the Software, and to permit persons to whom the Software is
15
   furnished to do so, subject to the following conditions:
16

17
   The above copyright notice and this permission notice shall be included in all
18
   copies or substantial portions of the Software.
19

20
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
   SOFTWARE.
27
 */
28

29
'use strict';
30

31
const lib = require('../../../lib.js');
1✔
32

33
const getEntities = async function (param) {
1✔
34
  let page = param.config.page;
5✔
35
  let count = param.config.count;
5✔
36
  const limit = param.config.limit;
5✔
37

38
  do {
5✔
39
    const options = {
5✔
40
      method: 'get',
41
      baseURL: param.host,
42
      url: param.pathname,
43
      headers: await lib.buildHTTPHeader(param),
44
      params: lib.buildSearchParams(param.config),
45
    };
46

47
    try {
5✔
48
      const res = await lib.http(options);
5✔
49
      if (res.status === 200) {
4✔
50
        if (count <= 0) {
3✔
51
          count = Number(res.headers['fiware-total-count']);
2✔
52
          if (count <= 0) {
2✔
53
            break;
1✔
54
          }
55
        }
56
        param.buffer.send(res.data);
2✔
57
        page++;
2✔
58
      } else {
59
        this.error(`Error while retrieving entities: ${res.status} ${res.statusText}`);
1✔
60
        break;
1✔
61
      }
62
    } catch (error) {
63
      this.error(`Exception while retrieving entities: ${error}`);
1✔
64
      break;
1✔
65
    }
66
  } while (page * limit < count);
67

68
  param.buffer.close();
5✔
69
};
70

71
const nobuffering = {
1✔
72
  node: null,
73
  open: function (node) {
74
    this.node = node;
11✔
75
    return this;
11✔
76
  },
77
  send: function (entities) {
78
    this.node.send({ payload: entities });
5✔
79
  },
80
  close: function () {},
81
  out: function (entities) {
82
    this.node.send({ payload: entities });
1✔
83
  },
84
};
85

86
const buffering = {
1✔
87
  node: null,
88
  entities: [],
89
  open: function (node) {
90
    this.node = node;
6✔
91
    this.entities = [];
6✔
92
    return this;
6✔
93
  },
94
  send: function (entities) {
95
    this.entities = this.entities.concat(entities);
3✔
96
  },
97
  close: function () {
98
    if (this.entities.length > 0) {
4✔
99
      this.node.send({ payload: this.entities });
2✔
100
    }
101
  },
102
  out: function (entities) {
103
    this.node.send({ payload: entities });
1✔
104
  },
105
};
106

107
module.exports = function (RED) {
1✔
108
  function NGSISource(config) {
109
    RED.nodes.createNode(this, config);
5✔
110
    var node = this;
5✔
111

112
    const openAPIsConfig = RED.nodes.getNode(config.openapis);
5✔
113

114
    node.on('input', async function (msg) {
5✔
115
      if (openAPIsConfig.geType !== 'orion') {
5✔
116
        node.error('FIWARE GE type not Orion');
1✔
117
        return;
1✔
118
      }
119

120
      if (!msg.payload) {
4✔
121
        msg.payload = {};
1✔
122
      } else if (typeof msg.payload === 'string') {
3✔
123
        msg.payload = { idPattern: msg.payload.trim() };
2✔
124
      }
125

126
      const defaultConfig = {
4✔
127
        service: openAPIsConfig.service.trim(),
128
        servicepath: config.servicepath.trim(),
129
        keyValues: config.mode !== 'normalized',
130
        type: config.entitytype.trim(),
131
        idPattern: config.idpattern.trim(),
132
        attrs: config.attrs.trim(),
133
        q: config.query.trim(),
134
        count: 0,
135
        limit: 100,
136
        page: 0,
137
      };
138

139
      const param = {
4✔
140
        host: openAPIsConfig.apiEndpoint,
141
        pathname: '/v2/entities',
142
        buffer: config.buffering === 'off' ? nobuffering.open(node):buffering.open(node),
4✔
143
        getToken: openAPIsConfig.getToken === null ? null : openAPIsConfig.getToken.bind(openAPIsConfig),
4✔
144
        config: Object.assign(defaultConfig, msg.payload),
145
      };
146

147
      await getEntities.call(node, param);
4✔
148
    });
149
  }
150
  RED.nodes.registerType('NGSI source', NGSISource);
5✔
151
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc