Commit 825e39b7 by Kornkitt Poolsup

newdevice with default schema and shadow

parent 5ca4ef73
GQL_TOKEN=
GQL_URI=http://n1.node.nexpie.io:32281/
GQL_TOKEN=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJjdHgiOnsidXNlcmlkIjoiVTU0NDA3OTI0NTQwMSIsImNsaWVudGlkIjoiYzJlMjY5OTA2OGQ1ODk2NjEwOTFmMDgyM2RmNjUwNWMifSwic2NvcGUiOltdLCJpYXQiOjE2NTg3Mjk1MjcsIm5iZiI6MTY1ODcyOTUyNywiZXhwIjoxNjU4ODE1OTI3LCJleHBpcmVJbiI6ODY0MDAsImp0aSI6Ijk0TkJmZHN6IiwiaXNzIjoiY2VyOnVzZXJ0b2tlbiJ9.GYFEe3xrf92mOV6yBfdbQyT4ITUW6fIAkEsKcnQ1KS_qNYxQkAYSimw2xqAIUSWlGVChLhctT69PsMC9A0aZbA
GQL_URI=https://gqlv2.nexpie.io/
const { GraphQLClient, gql } = require('graphql-request');
const md5 = require('md5');
const args = process.argv.slice(2);
const dotenv = require('dotenv');
dotenv.config();
const token = process.env.GQL_TOKEN;
const uri = process.env.GQL_URI;
console.log(token);
console.log(uri);
//console.log('args: ', args);
async function mutationGQL(){
try{
const PROJECTID = args[0];
const HWCODE = args[1];
const MODEL = args[2];
const SN = "SN"+md5(MODEL.toLowerCase()+"-b4dewx-"+HWCODE);
const DESCRIPTION = (MODEL+"-"+HWCODE).toUpperCase();
console.log("PROJECT ID: "+PROJECTID);
console.log("HW CODE: "+HWCODE);
console.log("SN: "+md5(SN));
const newdev_variables = {
projectid: PROJECTID,
description: DESCRIPTION,
tag: SN,
tags_a: MODEL
}
const mutation_newdev = `
mutation {
createDevice(
projectid: "${newdev_variables.projectid}"
deviceinfo: {
description: "${newdev_variables.description}"
tag: "${newdev_variables.tag}"
tags: {
A: "${newdev_variables.tags_a}"
}
}
){
alias
deviceid
tag
tags
description
status
}
}
`
console.log(mutation_newdev)
const client = new GraphQLClient(uri);
client.setHeader('authorization', 'Bearer '+token);
const data = await client.request(mutation_newdev, newdev_variables);
if(data.createDevice.tag === newdev_variables.tag){
data.status = "success";
data.code = 200;
console.log(data);
const schema_variables = {
deviceid: data.createDevice.deviceid,
value: {
"additionalProperties": true,
"properties": {
"POWER": {
"operation": {
"store": {
"ttl": "90d"
}
}
}
}
}
}
const shadow_variables = {
"deviceid": data.createDevice.deviceid,
"data": {
CURRENT: 0,
POWER: 0,
VOLTAGE: 0,
appstatus: false,
cloudversion: "fw-h1100-1.0.0.bin",
countdown: {
status: false
},
deviceversion: "fw-h1100-1.0.0.bin",
plugstatus: false,
timer: []
},
"timestamp": Date.now(),
"mode": "REPLACE"
}
const mutation_schema = `
mutation {
updateSchema(
deviceid: "${data.createDevice.deviceid}",
value: \"{\\\"additionalProperties\\\":true,\\\"properties\\\":{\\\"POWER\\\":{\\\"operation\\\":{\\\"store\\\":{\\\"ttl\\\":\\\"90d\\\"}}}}}\"
){
deviceid
value
}
}
`
const mutation_shadow = `
mutation {
writeShadow (
deviceid: "${data.createDevice.deviceid}",
data: \"{\\\"CURRENT\\\": 0, \\\"POWER\\\": 0, \\\"VOLTAGE\\\": 0, \\\"appstatus\\\": false, \\\"cloudversion\\\": \\\"fw-${newdev_variables.tags_a}-1.0.0.bin\\\", \\\"countdown\\\": { \\\"status\\\": false},\\\"deviceversion\\\": \\\"fw-${newdev_variables.tags_a}-1.0.0.bin\\\", \\\"plugstatus\\\": false, \\\"timer\\\": []}\",
timestamp: ${shadow_variables.timestamp},
mode: ${shadow_variables.mode}
){
deviceid
data
rev
modified
}
}
`
const schemadata = await client.request(mutation_schema, schema_variables);
const shadowdata = await client.request(mutation_shadow, shadow_variables);
if(schemadata.updateSchema.deviceid !== schema_variables.deviceid ||
shadowdata.writeShadow.deviceid !== shadow_variables.deviceid){
data.status = "fail";
data.code = 500;
}
console.log(schemadata, shadowdata);
}else{
data.status = "fail";
data.code = 500;
}
console.dir(data);
}catch(e){
console.dir(e);
}
}
if(args.length === 3){
mutationGQL();
}else{
console.log("node newDevice.js <PROJECT ID> <HW CODE> <MODEL>");
}
{
"name": "gql-anitech",
"version": "1.0.0",
"lockfileVersion": 1,
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "gql-anitech",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.0",
"graphql": "^16.3.0",
"graphql-request": "^4.0.0",
"md5": "^2.3.0"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"node_modules/charenc": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz",
"integrity": "sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=",
"engines": {
"node": "*"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/cross-fetch": {
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz",
"integrity": "sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==",
"dependencies": {
"node-fetch": "2.6.7"
}
},
"node_modules/crypt": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz",
"integrity": "sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=",
"engines": {
"node": "*"
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/dotenv": {
"version": "16.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.0.tgz",
"integrity": "sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==",
"engines": {
"node": ">=12"
}
},
"node_modules/extract-files": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/extract-files/-/extract-files-9.0.0.tgz",
"integrity": "sha512-CvdFfHkC95B4bBBk36hcEmvdR2awOdhhVUYH6S/zrVj3477zven/fJMYg7121h4T1xHZC+tetUpubpAhxwI7hQ==",
"engines": {
"node": "^10.17.0 || ^12.0.0 || >= 13.7.0"
},
"funding": {
"url": "https://github.com/sponsors/jaydenseric"
}
},
"node_modules/form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/graphql": {
"version": "16.3.0",
"resolved": "https://registry.npmjs.org/graphql/-/graphql-16.3.0.tgz",
"integrity": "sha512-xm+ANmA16BzCT5pLjuXySbQVFwH3oJctUVdy81w1sV0vBU0KgDdBGtxQOUd5zqOBk/JayAFeG8Dlmeq74rjm/A==",
"engines": {
"node": "^12.22.0 || ^14.16.0 || >=16.0.0"
}
},
"node_modules/graphql-request": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/graphql-request/-/graphql-request-4.0.0.tgz",
"integrity": "sha512-cdqQLCXlBGkaLdkLYRl4LtkwaZU6TfpE7/tnUQFl3wXfUPWN74Ov+Q61VuIh+AltS789YfGB6whghmCmeXLvTw==",
"dependencies": {
"cross-fetch": "^3.0.6",
"extract-files": "^9.0.0",
"form-data": "^3.0.0"
},
"peerDependencies": {
"graphql": "14 - 16"
}
},
"node_modules/is-buffer": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w=="
},
"node_modules/md5": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/md5/-/md5-2.3.0.tgz",
"integrity": "sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==",
"dependencies": {
"charenc": "0.0.2",
"crypt": "0.0.2",
"is-buffer": "~1.1.6"
}
},
"node_modules/mime-db": {
"version": "1.51.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.51.0.tgz",
"integrity": "sha512-5y8A56jg7XVQx2mbv1lu49NR4dokRnhZYTtL+KGfaa27uq4pSTXkwQkFJl4pkRMyNFz/EtYDSkiiEHx3F7UN6g==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.34",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.34.tgz",
"integrity": "sha512-6cP692WwGIs9XXdOO4++N+7qjqv0rqxxVvJ3VHPh/Sc9mVZcQP+ZGhkKiTvWMQRr2tbHkJP/Yn7Y0npb3ZBs4A==",
"dependencies": {
"mime-db": "1.51.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/node-fetch": {
"version": "2.6.7",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
"integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
}
},
"dependencies": {
"asynckit": {
"version": "0.4.0",
......
const { GraphQLClient, gql } = require('graphql-request');
const md5 = require('md5');
const args = process.argv.slice(2);
const dotenv = require('dotenv');
dotenv.config();
const token = process.env.GQL_TOKEN;
const uri = process.env.GQL_URI;
console.log(token);
console.log(uri);
//console.log('args: ', args);
async function mutationGQL(){
try{
const DEVICEID = args[0];
console.log("DEVICE ID: "+DEVICEID);
const variables = {
deviceid: DEVICEID,
value: {
"additionalProperties": true,
"properties": {
"POWER": {
"operation": {
"store": {
"ttl": "90d"
}
}
}
}
}
}
const mutation = `
mutation {
updateSchema(
deviceid: "${variables.deviceid}",
value: \"{\\\"additionalProperties\\\":true,\\\"properties\\\":{\\\"POWER\\\":{\\\"operation\\\":{\\\"store\\\":{\\\"ttl\\\":\\\"90d\\\"}}}}}\"
){
deviceid
value
}
}
`
// value: "{\\\"additionalProperties\\\":true,\\\"properties\\\":{\\\"POWER\\\":{\\\"operation\\\":{\\\"store\\\":{\\\"ttl\\\":\\\"90d\\\"}}}}}"
console.log(mutation);
const client = new GraphQLClient(uri);
client.setHeader('authorization', 'Bearer '+token);
const data = await client.request(mutation, variables);
if(data.updateSchema.deviceid === variables.deviceid){
data.status = "success";
data.code = 200;
}else{
data.status = "fail";
data.code = 500;
}
// console.log(data.updateSchema.value, variables.value);
console.dir(data);
}catch(e){
console.dir(e);
}
}
if(args.length === 1){
mutationGQL();
}else{
console.log("node createDevice.js <PROJECT ID> <HW CODE>");
}
const { GraphQLClient, gql } = require('graphql-request');
const md5 = require('md5');
const args = process.argv.slice(2);
const dotenv = require('dotenv');
dotenv.config();
const token = process.env.GQL_TOKEN;
const uri = process.env.GQL_URI;
console.log(token);
console.log(uri);
//console.log('args: ', args);
async function mutationGQL(){
try{
const DEVICEID = args[0];
console.log("DEVICE ID: "+DEVICEID);
const variables = {
"deviceid": DEVICEID,
"data": {
CURRENT: 0,
POWER: 0,
VOLTAGE: 0,
appstatus: false,
cloudversion: "fw-h1100-1.0.0.bin",
countdown: {
status: false
},
deviceversion: "fw-h1100-1.0.0.bin",
plugstatus: false,
timer: []
},
"timestamp": Date.now(),
"mode": "REPLACE"
}
const mutation = `
mutation {
writeShadow (
deviceid: "${variables.deviceid}",
data: \"{\\\"CURRENT\\\": 0, \\\"POWER\\\": 0, \\\"VOLTAGE\\\": 0, \\\"appstatus\\\": false, \\\"cloudversion\\\": \\\"fw-h1100-1.0.0.bin\\\", \\\"countdown\\\": { \\\"status\\\": false},\\\"deviceversion\\\": \\\"fw-h1100-1.0.0.bin\\\", \\\"plugstatus\\\": false, \\\"timer\\\": []}\",
timestamp: ${variables.timestamp},
mode: ${variables.mode}
){
deviceid
data
rev
modified
}
}
`
// console.log(variables, mutation);
const client = new GraphQLClient(uri);
client.setHeader('authorization', 'Bearer '+token);
const data = await client.request(mutation, variables);
// console.log(data.writeShadow.data, variables.data);
if(data.writeShadow.deviceid === variables.deviceid){
data.status = "success";
data.code = 200;
}else{
data.status = "fail";
data.code = 500;
}
console.dir(data);
}catch(e){
console.dir(e);
}
}
if(args.length === 1){
mutationGQL();
}else{
console.log("node createDevice.js <PROJECT ID> <HW CODE>");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment