[소스코드] 리쉐니에 스마트센서 GraphQL Examples (Javascript) - GraphQLGetVibrationData.js > Download

본문 바로가기
사이트 내 전체검색

전체메뉴

Download
Home > Information > Download
Q&A
Please leave a question.
more
CUSTOMER CENTER
TEL
070-8285-2269
Mail
reshenie@naver.com

[소스코드] 리쉐니에 스마트센서 GraphQL Examples (Javascript) - GraphQLGetVibrationD…

페이지 정보

작성자 최고관리자 댓글 0건 조회 20회 작성일 20-05-03 09:31

본문

GraphQLGetVibrationData.js Uses graphql-request to retrieve and plot vibration data of a specific time period.

 

import { request, GraphQLClient } from 'graphql-request'; // npm install --save graphql-request

import rq from 'request-promise-native'; // npm install --save request

                                         // npm install --save request-promise-native



function parseCookies(request){

    var list = {};

    var rc = request['headers']['set-cookie'][0];

    rc && rc.split(';').forEach(function(cookie){

        var parts = cookie.split('=');

        list[parts.shift().trim()] = decodeURI(parts.join('='));

    });

    return list;

}


async function createGraphQLClient(url){

    var host = url.split('//')[1].split('/')[0];

    var options = {

        uri: url,

        resolveWithFullResponse: true,

        headers: {

            'Host': host,

            'Accept': 'text/html',

        }

    };

    var response = await rq.get(options);

    var parsedCookie = parseCookies(response);

    var cookie = await rq.cookie('csrftoken=' + parsedCookie['csrftoken']);

    

    const client = new GraphQLClient(

        url,

        {

            credentials: 'include',

            headers: {

                'x-csrftoken': parsedCookie['csrftoken'],

                'Cookie': cookie,

                'Content-Type' : 'application/json',

                'Accept' : 'application/json'

            }

        }

    );

    return client;

}


async function main() {

    // create client (replace xx.xx.xx.xx with the IP address of your server)

    const url = 'http://xx.xx.xx.xx:8000/graphql';

    const queryClient = await createGraphQLClient(url);

    

    // construct query to retrieve vibration time stamps

    // replace macId xx:xx:xx:xx with the macId of your sensor

    // replace start and end with the desired start and end dates

    // limit limits the number of dates returned

    // axis allows to select data from only 1 or multiple axes

    const query = `

    { deviceManager { device(macId:"xx:xx:xx:xx") {

    __typename

    ... on GrapheneVibrationCombo {vibrationTimestampHistory(start:"2019-01-01T00:00:00.000000+00:00", end:"2019-02-01T00:00:00.000000+00:00", limit:100, axis:"X")}

    }}}

    `;

    

    // execute query and retrieve vibration time stamps

    var result = await queryClient.request(query);

    var timeStamps = result.deviceManager.device.vibrationTimestampHistory;

    console.log(timeStamps);

    

    // retrieve vibration data according to the vibration time stamps

    // replace macId xx:xx:xx:xx with the macId of your sensor

    var arrayLength = timeStamps.length;

    for (var i = 0; i < arrayLength; i++) {

        // construct query

        const query = `

        { deviceManager { device(macId:"xx:xx:xx:xx") {

        __typename

        ... on GrapheneVibrationCombo { vibrationArray(isoDate: "` + timeStamps[i] + `") {

        numSamples rawSamples sampleRate formatRange axis }}

        }}}

        `;

        

        // execute query

        var result = await queryClient.request(query);

        

        // retrieve raw acceleration data and format range

        var data = result.deviceManager.device.vibrationArray.rawSamples;

        var frange = result.deviceManager.device.vibrationArray.formatRange;

        

        // convert raw acceleration data to 'g' units

        var dataLength = data.length;

        for (var k = 0; k < dataLength; k++) {

            data[k] = data[k]/512.0*frange

        }

        console.log(data);

    }

}


main().catch(error => console.error(error))

댓글목록

등록된 댓글이 없습니다.

Company : Reshenie (Manufacturing Engineering Group)
Address : R. 406, Ace Gwanggyo Tower 3rd, 1286, Uidong, Yeongtong-gu, Suwon-si, Gyeonggi-do, S. Korea
TEL : 070-8285-2269 E-mail : reshenie@naver.com
Copyright © 2018 Reshenie All rights reserved.