File size: 1,965 Bytes
b1acbf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function toInfluxDBFormat(measurement, tags, fields, timestamp = null) {
    // Validate input
    if (typeof measurement !== 'string' || measurement.length === 0) {
      throw new Error('Measurement must be a non-empty string');
    }
    if (typeof tags !== 'object' || typeof fields !== 'object') {
      throw new Error('Tags and fields must be objects');
    }
    if (Object.keys(fields).length === 0) {
      throw new Error('At least one field is required');
    }
  
    // Escape special characters in measurement
    const escapedMeasurement = measurement.replace(/,|\s/g, '\\$&');
  
    // Process tags
    const tagString = Object.entries(tags)
      .map(([key, value]) => `${escapeKey(key)}=${escapeValue(value)}`)
      .join(',');
  
    // Process fields
    const fieldString = Object.entries(fields)
      .map(([key, value]) => `${escapeKey(key)}=${formatValue(value)}`)
      .join(',');
  
    // Construct the line protocol string
    let lineProtocol = escapedMeasurement;
    if (tagString) lineProtocol += `,${tagString}`;
    lineProtocol += ` ${fieldString}`;
    if (timestamp !== null) lineProtocol += ` ${timestamp}`;
  
    return lineProtocol;
  }
  
  function escapeKey(key) {
    return key.replace(/,|=|\s/g, '\\$&');
  }
  
  function escapeValue(value) {
    return String(value).replace(/,|=|\s/g, '\\$&');
  }
  
  function formatValue(value) {
    if (typeof value === 'string') return `"${value.replace(/"/g, '\\"')}"`;
    if (typeof value === 'number') return isFinite(value) ? String(value) : 'null';
    if (typeof value === 'boolean') return value ? 'true' : 'false';
    return 'null';
  }
  
  // Example usage
  const measurement = "fan";
  const tags = { location: 'Chicago', sensor: 'S1' };
  const fields = { temperature: 72.5, humidity: 60, description: 'Partly cloudy', current:msg.payload};
  
  const influxDBLine = toInfluxDBFormat(measurement, tags, fields);
  msg.payload = influxDBLine;
  return msg;