Google Firebase functions to iot device send config

387 views Asked by At

I am trying to use firebase functions to send config to an Iot device.Here is the tutorial which i am trying to use.This is the Node js code which i am deploying

The tutorial says after, after deployment whatever change i will do to my firebase database it will get sent to the iot device as device config.

registryid is INTERNET_OF_BULBS. Iot device name is Iot-device-1.

The actual code in the tutorial doesnt work for deployment.So I removed the import modules part and used require. The tutorial does these imports.

import * as admin from "firebase-admin";
import * as functions from 'firebase-functions';
import { runInDebugContext } from 'vm';
import { DeviceManager } from './devices';
how do i import { DeviceManager } from './devices';

so i am doing only 2 imports.Otherwise ,if i use import in my index.js it gives error when deploying. 1:1 error Parsing error: 'import' and 'export' may appear only with 'sourceType: module' Below is my index.js file.

const admin =require('firebase-admin');
const functions =require('firebase-functions');

admin.initializeApp();

// create a device manager instance with a registry id, optionally pass a region
const dm = new DeviceManager('INTERNET_OF_BULBS');
const firestore = admin.firestore();
// start cloud function
exports.configUpdate = functions.firestore
  // assumes a document whose ID is the same as the deviceid
  .document('device-configs/Iot-device-1')
  .onWrite((change,context) => {
    var beforeData = change.before.val(); // data before the write
    var afterData = change.after.val();
    if (context) {
      await dm.setAuth();
      console.log(context.params.deviceId);
      // get the new config data
      const configData = change.after.data();
      return dm.updateConfig(context.params.deviceId, configData);
    } else {
      throw(Error("no context from trigger"));
    }

  })

Here is the error on my shell when i am trying to deploy,the above code.

firebase deploy
=== Deploying to 'wifi-lights-88783'...
i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /home/suposhmehta/fire/functions
> eslint .
/home/suposhmehta/fire/functions/index.js
  17:13  error  Parsing error: Unexpected token dm
✖ 1 problem (1 error, 0 warnings)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/suposhmehta/.npm/_logs/2018-11-10T09_38_15_585Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/nvm/versions/node/v8.9.4/bin/node',
1 verbose cli   '/usr/local/nvm/versions/node/v8.9.4/bin/npm',
1 verbose cli   '--prefix',
1 verbose cli   '/home/suposhmehta/fire/functions',
1 verbose cli   'run',
1 verbose cli   'lint' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /usr/local/nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/suposhmehta/fire/functions/node_
modules/.bin:/home/suposhmehta/gopath/bin:/google/gopath/bin:/google/google-cloud-sdk/bin:/usr/local/go/bin:/opt/gradle/bin:/opt/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbi
n:/usr/bin:/sbin:/bin:/usr/local/nvm/versions/node/v8.9.4/bin:/google/go_appengine:/google/google_appengine
9 verbose lifecycle functions@~lint: CWD: /home/suposhmehta/fire/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'eslint .' ]
11 silly lifecycle functions@~lint: Returned: code: 1  signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `eslint .`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid functions@
15 verbose cwd /home/suposhmehta/fire
16 verbose Linux 4.14.33+
17 verbose argv "/usr/local/nvm/versions/node/v8.9.4/bin/node" "/usr/local/nvm/versions/node/v8.9.4/bin/npm" "--prefix" "/home/suposhmehta/fire/functions" "run" "lint"
18 verbose node v8.9.4
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error functions@ lint: `eslint .`
22 error Exit status 1
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

How do i get rid of these issues? Please Help.

0

There are 0 answers