[소스코드] 리쉐니에 스마트센서 GraphQL Examples (Javascript) - GraphQLGetDeviceList.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) - GraphQLGetDeviceList…

페이지 정보

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

본문

GraphQLGetDeviceList.js Uses graphql-request to retrieve the list of devices connected to your server.

 

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

    const query = `

    {

        deviceManager { 

            deviceList {

                parent macId tag

            } 

        }

    }

    `;

  

    // execute query and print device list

    const deviceList = await queryClient.request(query);

    console.log(deviceList.deviceManager.deviceList);  

}


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.