Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titlejavascript

github

Code Block
languagejs
// Example For Insert Standard Data for tag API by using request in nodejs
// written by yeony kim
// sensor1, sensor2 is applied in lake

// process.env.NODE_TLS_REJECT_UNAUTHORIZED ="0";

const express = require('express');
const app =
express();
const API_KEY ="YOUR_API_TOKEN";
const LAKE_ID ="YOUR_LAKE_ID";
const URL ="https://" + LAKE_ID +"CLOUD_VENDOR="CLOUD_VENDOR";
const CLOUD_REGION="CLOUD_REGION";
const URL=`https://${CLOUD_VENDOR}.${CLOUD_REGION}.machlake.com/v1/lakes/values/standard"${LAKE_ID}/values`;

//var CASErequest - Insert Data with nano date time string format

app.get(
    '/insert_standard_with_nano_date_time_string', function(req, res) {
        const Data = {
            tag_name: 'sensor1',
            date_format: 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
            values: [["2021-01-06 17:00:00 001:000:000", 1.0], ["2021-01-06 17:00:00 002:000:000", 1.5], ["2021-01-06 17:00:00 003:000:000", 2.0]]
        }
        var request = require('request');
        var option = {
   = require('request');

/* ------------------------------------------------------------------------------------------------- */

// CASE - Insert Data with nano date time string format

var Data = {
    tag_name: "sensor1",
    date_format: "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn",
    values: [
        url: URL,
["2021-01-06 17:00:00 001:000:000", 1.0], 
        ["2021-01-06  headers: {17:00:00 002:000:000", 1.5], 
        ["2021-01-06 17:00:00 003:000:000", 2.0]
    'Content-Type': 'application/json',
]
}

request.post({
    url: URL, 
    headers: {
  'x-api-key': API_KEY     "Content-Type": "application/json",
      },  "x-api-key": API_KEY
    },
    body: JSON.stringify(Data)
 
      };, 
       request.post( option, function(error, response, body) {
        console.log(body);
   if (!error) {
       // Return Format 
        // {
  res.writeHead(200);      //     "success": true,
      res.end(body);  //     "reason": "append success",
       console.log(body) //   {  "data": {"fail": 0,"success": 3},"status":"success"}
        //    } else
{                 console.log(error)
            }
        })
    }
)

// CASE - Insert Data with time format is });

/* ------------------------------------------------------------------------------------------------- */

// CASE - Insert Data with time format is empty but data format in data is nano date time string

app.get(
    '/insert_standard_with_nano_date_time_string_diff_setting', function(req, res) {
        const var Data = {
       
    tag_name: '"sensor1'",
    values: [
      values:  [["2021-01-06 17:00:00 004:000:000", 1.0], 
        ["2021-01-06 17:00:00 005:000:000", 1.5], 
        ["2021-01-06 17:00:00 006:000:000", 2.0]
    ]
}

request.post({
    url: }URL, 
    headers: {
 var request = require('request');    "Content-Type": "application/json",
   var option = {  "x-api-key": API_KEY
    },
    urlbody: URL,JSON.stringify(Data)
    }, 
    function(error, response, headers:body) {
        console.log(body);
        'Content-Type': 'application/json',
 // Return Format 
        //     'x-api-key': API_KEY{
        //     "success": }true,
        //    body "reason": JSON.stringify(Data)
"append success",
       }; //     "data":   request.post( option, function(error, response, body) {{"fail": 0,"success": 3}
        // }
     if (!error) {
                res.writeHead(200);
                res.end(body);
                console.log(body) // {"data":{"fail":0,"success":3},"status":"success"}
     });

/* ------------------------------------------------------------------------------------------------- */

// CASE - Insert Data with utc-0 time zone and time format is empty but data format in data is nano date time string

var Data = {
    timezone: "Africa/Abidjan",
    tag_name: "sensor1",
    values: [
      } else {
   ["2021-01-06 8:00:00 007:000:000", 1.0], 
        ["2021-01-06 8:00:00   console.log(error)008:000:000", 1.5], 
        ["2021-01-06 8:00:00  }009:000:000", 2.0]
    ]
   })

request.post({
  } ) url: //URL, CASE
- Insert Data with utc-0 time zone and time format is empty but data format in data is nano date time string

app.get(
    '/insert_standard_with_nano_date_time_string_utc0', function(req, resheaders: {
        "Content-Type": "application/json",
        "x-api-key": API_KEY
    },
    body: JSON.stringify(Data)
    }, 
    function(error, response, body) {
        const Data = {console.log(body);
        // Return Format 
   tag_name: 'sensor1',    // {
       values: [["2021-01-06 8:00:00 007:000:000", 1.0], ["2021-01-06 8:00:00 008:000:000", 1.5], ["2021-01-06 8:00:00 009:000:000", 2.0]] //     "success": true,
        // }    "reason": "append success",
  var request = require('request');   //      var option = {
            url: URL,
            headers: {"data": {"fail": 0,"success": 3}
        // }
    });

'Content-Type': 'application/json',
                'x-api-key': API_KEY,
                'Use-Timezone': 'Africa/Abidjan'
            },
            body: JSON.stringify(Data)
        };
        request.post( option, function(error, response, body) {/* ------------------------------------------------------------------------------------------------- */

// CASE - Insert Data with date time string format

var Data = {
    tag_name: "sensor2",
    date_format: "YYYY-MM-DD HH24:MI:SS",
    values: [
        ["2021-01-06 17:00:00", 1.0], 
        ["2021-01-06 17:00:01", 1.5], 
        ["2021-01-06 17:00:02", 2.0]
   if (!error) { ]
}

request.post({
    url: URL, 
    headers: {
         res.writeHead(200);"Content-Type": "application/json",
        "x-api-key": API_KEY
    },
    body: resJSON.endstringify(body);Data)
    }, 
    function(error, response, body) {
        console.log(body);
        // {"data":{"fail":0,"success":3},"status":"success"}
Return Format 
        // {
} else {      //     "success": true,
    console.log(error)    //     "reason": "append success",
 }       //  })   "data": {"fail": 0,"success": 3}
)  // CASE - Insert Data with date// time}
string format  app.get(
 });

  '/insert_standard_with_date_time_string', function(req, res) {
        const Data = {
            tag_name: 'sensor2',
            date_format: 'YYYY-MM-DD HH24:MI:SS',
            values: [["2021-01-06 17:00:00", 1.0], ["2021-01-06 17:00:01", 1.5], ["2021-01-06 17:00:02", 2.0]]
        }
        var request = require('request');
        var option = {
            url: URL,
            headers: {
                'Content-Type': 'application/json',
                'x-api-key': API_KEY,
            },
            body: JSON.stringify(Data)
        };
        request.post( option, function(error, response, body) {
            if (!error) {
                res.writeHead(200);
                res.end(body);
                console.log(body) // {"data":{"fail":0,"success":3},"status":"success"}
            } else {
                console.log(error)
            }
        })
    }
)

// CASE - Insert Data with nano-timestamp

app.get(
    '/insert_standard_with_nano_timestamp', function(req, res) {
        const Data = {
            tag_name: 'sensor2',
            values: [[1609920003000000000, 1.0], [1609920004000000000, 1.5], [1609920005000000000, 2.0]]
        }
        var request = require('request');
        var option = {
            url: URL,
            headers: {
                'Content-Type': 'application/json',
                'x-api-key': API_KEY,
            },
            body: JSON.stringify(Data)
        };
        request.post( option, function(error, response, body) {
            if (!error) {
                res.writeHead(200);
                res.end(body);
                console.log(body) // {"data":{"fail":0,"success":3},"status":"success"}
            } else {
                console.log(error)
            }
        })
    }
)


// CASE - Insert Data with utc-0 time zone and date time string format but data format in data is nano date time string

app.get(
    '/insert_standard_with_date_time_string_diff_setting_utc0', function(req, res) {
        const Data = {
            tag_name: 'sensor2',
            date_format: 'YYYY-MM-DD HH24:MI:SS',
/* ------------------------------------------------------------------------------------------------- */

// CASE - Insert Data with nano-timestamp

var Data = {
    tag_name: "sensor2",
    values: [
        [1609920003000000000, 1.0], 
          values: [["2021-01-06 17:00:00 007:000:000", 1.0], ["2021-01-06 17:00:00 008:000:000"[1609920004000000000, 1.5], ["2021-01-06 17:00:00 009:000:000" 
        [1609920005000000000, 2.0]
    ]
}
request.post({
    url:  }URL, 
    headers: {
        "Content-Type": "application/json",
 var request = require('request');    "x-api-key": API_KEY
   var },
  option = {
body: JSON.stringify(Data)
    }, 
     url: URLfunction(error, response, body) {
        console.log(body);
 headers: {      // Return Format 
       'Content-Type': 'application/json',
 // {
        //       'x-api-key': API_KEY"success": true,
           //     "Use-Timezonereason": "Africa/Abidjan"append success",
        //    },             body: JSON.stringify(Data)
"data": {"fail": 0,"success": 3}
       }; // }
    });

request.post( option, function(error, response, body) {
            if (!error) {
                res.writeHead(200);
                res.end(body);
      /* ------------------------------------------------------------------------------------------------- */

// CASE - Insert Data with utc-0 time zone and date time string format but data format in data is nano date time string
var Data = {
    timezone: "Africa/Abidjan",
    tag_name: "sensor2",
    date_format: "YYYY-MM-DD HH24:MI:SS",
    values: [
         console.log(body) // {"data":{"fail":0,"success":3},"status":"success"}
["2021-01-06 17:00:00 007:000:000", 1.0], 
        ["2021-01-06  } else {17:00:00 008:000:000", 1.5], 
                console.log(error)
    ["2021-01-06 17:00:00 009:000:000", 2.0]
    ]
}
request.post({
}    url: URL, 
  })  headers: {
 } )      

app.listen(8888, function() {"Content-Type": "application/json",
      console.log('http://127.0.0.1:8888/insert_standard_with_nano_date_time_string is result for insert standard data with nano date time string') "x-api-key": API_KEY
    },
    body: consoleJSON.log('http://127.0.0.1:8888/insert_standard_with_nano_date_time_string_diff_setting is result for insert standard data with nano date time string with diff settings')stringify(Data)
    }, 
    function(error, response, body) {
        console.log('http://127.0.0.1:8888/insert_standard_with_nano_date_time_string_utc0 is result for insert standard data with nano date time string with diff settings with utc-0')body);
        // Return Format 
        console.log('http://127.0.0.1:8888/insert_standard_with_date_time_string is result for insert standard data with date time string')// {
        //     "success": true,
        console.log('http://127.0.0.1:8888/insert_standard_with_nano_timestamp is result for insert standard data with nano timestamp')//     "reason": "append success",
        console.log('http://127.0.0.1:8888/insert_standard_with_date_time_string_diff_setting_utc0 is result for insert standard data with date time string with utc-0')
})//     "data": {"fail": 0,"success": 3}
        // }
    });
Expand
titlepython

github

Code Block
languagepy
"""
Example For Insert Standard Data for tag API by using request in python
written by yeony kim
sensor1, sensor2 is applied in lake
"""

import requests

API_KEY = "YOUR_API_TOKEN"
LAKE_ID = "YOUR_LAKE_ID"
URL = f"https://{LAKE_ID}.machlake.com/lakes/values/standard"

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY
}

# CASE - Insert Data with nano date time string format

params = {
    'tag_name': 'sensor1',
    'date_format': 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
    'values': [["2021-01-06 17:00:00 001:000:000", 1.0], ["2021-01-06 17:00:00 002:000:000", 1.5], ["2021-01-06 17:00:00 003:000:000", 2.0]]
}

response = requests.post(URL, headers=headers,  json=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"fail":0,"success":3},"status":"success"}

# CASE - Insert Data with time format is empty but data format in data is nano date time string

params = {
    'tag_name': 'sensor1',
    'values': [["2021-01-06 17:00:00 004:000:000", 1.0], ["2021-01-06 17:00:00 005:000:000", 1.5], ["2021-01-06 17:00:00 006:000:000", 2.0]]
}

response = requests.post(URL, headers=headers,  json=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"fail":0,"success":3},"status":"success"}


# CASE - Insert Data with utc-0 time zone and time format is empty but data format in data is nano date time string

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
    'Use-Timezone': 'Africa/Abidjan'
}

params = {
    'tag_name': 'sensor1',
    'values': [["2021-01-06 8:00:00 007:000:000", 1.0], ["2021-01-06 8:00:00 008:000:000", 1.5], ["2021-01-06 8:00:00 009:000:000", 2.0]]
}

response = requests.post(URL, headers=headers,  json=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"fail":0,"success":3},"status":"success"}


# CASE - Insert Data with date time string format

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
}

params = {
    'tag_name': 'sensor2',
    'date_format': 'YYYY-MM-DD HH24:MI:SS',
    'values': [["2021-01-06 17:00:00", 1.0], ["2021-01-06 17:00:01", 1.5], ["2021-01-06 17:00:02", 2.0]]
}

response = requests.post(URL, headers=headers,  json=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"fail":0,"success":3},"status":"success"}


# CASE - Insert Data with nano-timestamp

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
}

params = {
    'tag_name': 'sensor2',
    'values': [[1609920003000000000, 1.0], [1609920004000000000, 1.5], [1609920005000000000, 2.0]]
}

response = requests.post(URL, headers=headers,  json=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"fail":0,"success":3},"status":"success"}


# CASE - Insert Data with utc-0 time zone and date time string format but data format in data is nano date time string

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
    "Use-Timezone": "Africa/Abidjan"
}

params = {
    'tag_name': 'sensor2',
    'date_format': 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
    'values': [["2021-01-06 17:00:00 007:000:000", 1.0], ["2021-01-06 17:00:00 008:000:000", 1.5], ["2021-01-06 17:00:00 009:000:000", 2.0]]
}

response = requests.post(URL, headers=headers,  json=params, verify=False)
print(response.content.decode('utf-8'))  # {"data":{"fail":0,"success":3},"status":"success"}

...