Skip to content

Commit abf55ff

Browse files
authored
Merge pull request #1 from erodewald/base-features
Base features
2 parents 1122b22 + 698f9c8 commit abf55ff

12 files changed

+14145
-3
lines changed

.circleci/config.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
version: 2.1
2+
3+
orbs:
4+
codecov: codecov/codecov@1.0.4
5+
build-helpers: style/build-helpers@1.3.1
6+
7+
references:
8+
attach_workspace: &attach_workspace
9+
attach_workspace:
10+
at: ~/repo
11+
persist_to_workspace: &persist_to_workspace
12+
persist_to_workspace:
13+
root: ~/repo
14+
paths: .
15+
16+
executors:
17+
albus:
18+
docker:
19+
- image: circleci/node:10.15.1
20+
working_directory: ~/repo
21+
22+
jobs:
23+
install_dependencies:
24+
executor: albus
25+
steps:
26+
- *attach_workspace
27+
- checkout
28+
- restore_cache:
29+
keys:
30+
- v1-dependencies-{{ checksum "package.json" }}
31+
# fallback to using the latest cache if no exact match is found
32+
- v1-dependencies-
33+
- run: npm install
34+
- save_cache:
35+
paths:
36+
- node_modules
37+
key: v1-dependencies-{{ checksum "package.json" }}
38+
- *persist_to_workspace
39+
test:
40+
executor: albus
41+
steps:
42+
- *attach_workspace
43+
- run: npm test
44+
- codecov/upload:
45+
file: .nyc_output/*.json
46+
- *persist_to_workspace
47+
publish:
48+
executor: albus
49+
steps:
50+
- *attach_workspace
51+
- checkout
52+
- run: npx semantic-release
53+
54+
workflows:
55+
build:
56+
jobs:
57+
- install_dependencies:
58+
context: pi
59+
filters:
60+
branches:
61+
only: /.*/
62+
- test:
63+
context: pi
64+
requires:
65+
- install_dependencies
66+
filters:
67+
branches:
68+
only: /.*/
69+
- publish:
70+
context: pi
71+
requires:
72+
- test
73+
filters:
74+
branches:
75+
only: master

.sgcrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}

.vscode/launch.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{
3+
"version": "0.2.0",
4+
"configurations": [
5+
{
6+
"type": "node",
7+
"request": "launch",
8+
"name": "Mocha All",
9+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
10+
"args": [
11+
"--timeout",
12+
"999999",
13+
"--colors",
14+
"'${workspaceFolder}/{,!(node_modules)/}*/*.test.js'"
15+
],
16+
"console": "integratedTerminal",
17+
"internalConsoleOptions": "neverOpen"
18+
},
19+
{
20+
"type": "node",
21+
"request": "launch",
22+
"name": "Mocha Current File",
23+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
24+
"args": [
25+
"--timeout",
26+
"999999",
27+
"--colors",
28+
"${file}"
29+
],
30+
"console": "integratedTerminal",
31+
"internalConsoleOptions": "neverOpen"
32+
}
33+
]
34+
}

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at pi-team@smartthings.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2019 SmartThings, Inc.
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.
@@ -198,4 +198,4 @@
198198
distributed under the License is distributed on an "AS IS" BASIS,
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201-
limitations under the License.
201+
limitations under the License.

README.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
11
# firestore-context-store-nodejs
2-
Stores SmartApp configuration and auth tokens for use in app-initiated calls
2+
3+
<p>
4+
<a href="https://npmjs.org/package/@smartthings/firestore-context-store"><img src="https://badge.fury.io/js/@smartthings/firestore-context-store.svg"></a>
5+
<a href="https://circleci.com/gh/SmartThingsCommunity/firestore-context-store-nodejs.svg?style=svg"><img src="https://circleci.com/gh/SmartThingsCommunity/firestore-context-store-nodejs.svg?style=svg"></a>
6+
<a href="https://david-dm.org/SmartThingsCommunity/firestore-context-store-nodejs"><img src="https://david-dm.org/SmartThingsCommunity/firestore-context-store-nodejs.svg?theme=shields.io"></a>
7+
<a href="https://codecov.io/gh/SmartThingsCommunity/firestore-context-store-nodejs"><img src="https://codecov.io/gh/SmartThingsCommunity/firestore-context-store-nodejs/branch/master/graph/badge.svg" /></a>
8+
</p>
9+
10+
> Stores SmartApp configuration and auth tokens for use in app-initiated calls
11+
12+
Used by the [SmartApp SDK](https://github.com/SmartThingsCommunity/smartapp-sdk-nodejs) to store IDs and access tokens for an installed instance of a SmartApp
13+
and retrieves that information for use in asynchronous API calls. The use of a context store
14+
is only needed when SmartApps have to call the SmartThings API in response to external
15+
events. SmartApps that only response to lifecycle events from the SmartThings platform
16+
will automatically have the proper context without the app having to store it.
17+
18+
The context stored by this module consists of the following data elements:
19+
20+
- **installedAppId**: the UUID of the installed app instance. This is the primary key of the table.
21+
- **locationId**: the UUID of the location in which the app is installed
22+
- **authToken**: the access token used in calling the API
23+
- **refreshToken**: the refresh token used in generating a new access token when one expires
24+
- **clientId**: the SmartApp's client ID, used in generating a new access token
25+
- **clientSecret**: the SmartApp's client secret, used in generating a new access token
26+
- **config**: the current installed app instance configuration, i.e. selected devices, options, etc.v
27+
28+
## Installation
29+
30+
```shell
31+
npm install @smartthings/firestore-context-store --save
32+
```
33+
34+
## Usage
35+
36+
### A note about usage
37+
38+
This package currently targets initializing Cloud Firestore from your own server by using a Service Account. If you are hosted on something like [Firebase Cloud Functions](https://firebase.google.com/docs/functions/) or [Google Cloud Platform](https://cloud.google.com/), we don't currently support those methodologies – feel free to contribute!
39+
40+
To use this module to add Firebase Cloud Firestore context storage to your SmartApp, you should:
41+
42+
1. **Generate a service account**. We use the Firebase Admin SDK. Go to IAM & admin > Service accounts in the Cloud Platform Console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK in the next step.
43+
44+
1. **`require()` your Google Services service account JSON file.** Be certain that you don't ever commit this file to a public repository.
45+
46+
1. **Create a context store instance** with the service account object and the document collection name and pass it to the SmartApp SDK object. For example, the following code:
47+
48+
```javascript
49+
const smartapp = require('@smartthings/smartapp');
50+
const FirestoreDBContextStore = require('@smartthings/firestore-context-store');
51+
const serviceAccount = require('./googleservices-YOURAPPNAME-AND-ID.json')
52+
53+
smartapp
54+
.contextStore(new FirestoreDBContextStore(serviceAccount, 'installedapps'))
55+
.configureI18n()
56+
.page('mainPage', (page) => {
57+
...
58+
```
59+
60+
This will use a document collection named `installedapps` to store documents as keyed by the `installedAppId` value.
61+
62+
### Firebase Console
63+
64+
From the Firebase Cloud Firestore console, you will see a new document collection with persisted contexts.
65+
66+
![img](docs/example_console.png)
67+
68+
## More about SmartThings
69+
70+
If you are not familiar with SmartThings, we have
71+
[extensive on-line documentation](https://smartthings.developer.samsung.com/develop/index.html).
72+
73+
To create and manage your services and devices on SmartThings, create an account in the
74+
[developer workspace](https://devworkspace.developer.samsung.com/).
75+
76+
The [SmartThings Community](https://community.smartthings.com/c/developers/) is a good place share and
77+
ask questions.
78+
79+
There is also a [SmartThings reddit community](https://www.reddit.com/r/SmartThings/) where you
80+
can read and share information.
81+
82+
## License and Copyright
83+
84+
Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
85+
86+
Copyright 2019 SmartThings, Inc.

config/release.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
analyzeCommits: {
3+
preset: 'eslint'
4+
},
5+
generateNotes: {
6+
preset: 'eslint'
7+
}
8+
}

docs/example_console.png

93 KB
Loading

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
3+
const admin = require('firebase-admin')
4+
5+
module.exports = class FirestoreDBContextStore {
6+
constructor(serviceAccount, collectionName) {
7+
if (!serviceAccount) {
8+
throw new Error('Need a valid serviceAccount.json')
9+
}
10+
11+
this.serviceAccount = serviceAccount
12+
this.collectionName = collectionName || 'installedApps'
13+
14+
admin.initializeApp({
15+
credential: admin.credential.cert(this.serviceAccount)
16+
})
17+
this.db = admin.firestore()
18+
this.installedApps = this.db.collection(this.collectionName)
19+
}
20+
21+
get(installedAppId) {
22+
return new Promise((resolve, reject) => {
23+
this.installedApps
24+
.doc(installedAppId)
25+
.get()
26+
.then(snapshot => {
27+
resolve(snapshot.data())
28+
})
29+
.catch(reject)
30+
})
31+
}
32+
33+
put(params) {
34+
return new Promise((resolve, reject) => {
35+
this.installedApps
36+
.doc(params.installedAppId)
37+
.set(params, {merge: true})
38+
.then(resolve)
39+
.catch(reject)
40+
})
41+
}
42+
43+
update(installedAppId, params) {
44+
return new Promise((resolve, reject) => {
45+
this.installedApps
46+
.doc(installedAppId)
47+
.update(params, {merge: true})
48+
.then(resolve)
49+
.catch(reject)
50+
})
51+
}
52+
53+
delete(installedAppId) {
54+
return new Promise((resolve, reject) => {
55+
this.installedApps
56+
.doc(installedAppId)
57+
.delete()
58+
.then(resolve)
59+
.catch(reject)
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)