Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
parnic committed Sep 3, 2023
0 parents commit c55b948
Show file tree
Hide file tree
Showing 13 changed files with 4,475 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"comma-dangle": 0,
"indent": [2, 4],
"max-len": "off",
"radix": [2, "as-needed"],
"no-console": 0,
"linebreak-style": "off",
"prettier/prettier": 0,
"quotes": ["error", "single"],
"jsdoc/require-jsdoc": 0
},
"settings": {
"import/core-modules": [ "node_helper" ]
},
"env": {
"browser": true,
"node": true,
"es6": true
}
}
29 changes: 29 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm run lint
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

saved.json
8 changes: 8 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"default": true,
"no-duplicate-header": {
"siblings_only": true
},
"line-length": false,
"no-inline-html": false
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2023-09-03

### Added

- Initial module release.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
211 changes: 211 additions & 0 deletions MMM-Ring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/* Magic Mirror
* Module: MMM-Ring
*
* By parnic https://github.com/parnic/MMM-Ring
* MIT Licensed.
*/

Module.register('MMM-Ring',{
defaults: {
refreshToken: undefined,
pins: undefined, // array of arrays, e.g. [[1,2,3,4], [0,0,0,0]],
requirePinToArm: false,
requirePinToDisarm: true,
},

start: function() {
this.sendSocketNotification('RING_CONFIG', this.config);

this.createKeypad();
},

getStyles: function() {
return ['ring.css'];
},

getDom: function() {
if (!this.initialized) {
let wrapper = document.createElement('div');
wrapper.innerHTML = 'Loading Ring...';
wrapper.className += 'dimmed light small text-center';

return wrapper;
} else if (this.changingMode) {
let wrapper = document.createElement('div');
// todo: make this look nicer. an overlay on the button or something instead.
wrapper.innerHTML = 'Changing alarm mode...';
wrapper.classList.add('dimmed', 'light', 'small', 'text-center');

return wrapper;
} else if (this.ringState !== 'good') {
let wrapper = document.createElement('div');
if (this.ringState === 'invalid-config') {
wrapper.textContent = 'Configuration is invalid. Please verify the refresh token is set correctly.';
} else if (this.ringState === 'connect-failed') {
wrapper.textContent = 'Unable to connect to your Ring system. Please verify the refresh token is set correctly.';
} else if (this.ringState === 'no-panel') {
wrapper.textContent = 'No panel was detected in your system. Unable to use module.';
}
wrapper.classList.add('dimmed', 'light', 'small', 'text-center');
return wrapper;
}

let wrapper = document.createElement('div');

let btn = document.createElement('button');
btn.classList.add('ring-control', 'light');
btn.addEventListener('click', (evt) => {
const isDisarming = this.alarmMode !== 'none';
const isArming = !isDisarming;
if (isDisarming) {
this.desiredMode = 'none';
if (this.config.requirePinToDisarm && Array.isArray(this.config.pins)) {
this.showKeypad(true);
return;
} else {
Log.info('MMM-Ring: no PIN required to disarm or no pins specified in config - disarming alarm');
}

this.setAlarmMode(this.desiredMode);
return;
}

this.desiredMode = 'some';
if (this.config.requirePinToArm && Array.isArray(this.config.pins)) {
this.showKeypad(true);
return;
} else {
Log.info('MMM-Ring: no PIN required to arm or no pins specified in config - arming alarm home');
}

this.setAlarmMode(this.desiredMode);
});
wrapper.appendChild(btn);

let btnStatus = document.createElement('div');
btnStatus.innerHTML = this.alarmMode === 'none' ?
'Status: <i class="fa fa-lock-open" aria-hidden="true"></i> Disarmed' :
'Status: <i class="fa fa-lock" aria-hidden="true"></i> Armed';
btn.appendChild(btnStatus);

let btnLabel = document.createElement('div');
btnLabel.textContent = this.alarmMode === 'none' ? 'Press to arm Home' : 'Press to disarm';
btn.appendChild(btnLabel);

return wrapper;
},

showKeypad: function(show) {
const pinBg = document.getElementById('ring-pin-bg');
if (show) {
pinBg.classList.remove('ring-d-none');
} else {
pinBg.classList.add('ring-d-none');
}
},

createKeypad: function() {
let bg = document.createElement('div');
bg.classList.add('ring-pin-bg', 'ring-d-none');
bg.id = 'ring-pin-bg';
bg.addEventListener('click', (evt) => bg.classList.add('ring-d-none'));

let wrapper = document.createElement('div');
bg.appendChild(wrapper);
wrapper.classList.add('ring-pin-outer');

let header = document.createElement('div');
wrapper.appendChild(header);
header.classList.add('light', 'text-center', 'ring-pin-header');
header.textContent = 'Enter alarm code';

let container = document.createElement('div');
container.classList.add('ring-pin-container');
wrapper.appendChild(container);

for (let i = 1; i <= 10; i++) {
let btn = document.createElement('button');
container.appendChild(btn);

btn.textContent = (i % 10).toString();
btn.classList.add('ring-control', 'light', 'ring-pin-btn');
btn.addEventListener('click', (evt) => this.clickedKeypadButton(btn, evt));
}

const elems = document.getElementsByTagName('html');
const root = elems[0];
root.appendChild(bg);
},

clickedKeypadButton: function(btn, evt) {
if (!this.pressedButtons) {
this.pressedButtons = [];
}

const pressed = parseInt(btn.textContent);
this.pressedButtons.push(pressed);

evt.stopPropagation();

const longestPin = this.config.pins.reduce((longest, val) => val.length > longest ? val.length: longest, 0);
while (this.pressedButtons.length > longestPin) {
this.pressedButtons.shift();
}

for (const pin of this.config.pins) {
if (this.pressedButtons.length < pin.length) {
continue;
}

const startIdx = this.pressedButtons.length - pin.length;
const endIdx = startIdx + pin.length;
if (this.pressedButtons.slice(startIdx, endIdx).every((val, idx) => val === pin[idx])) {
Log.info(`MMM-Ring: matched PIN, changing alarm mode to ${this.desiredMode}`);
this.showKeypad(false);
this.setAlarmMode(this.desiredMode);
this.pressedButtons.length = 0;
return;
}
}

Log.info('MMM-Ring: no PINs matched entered sequence. Doing nothing.');
},

setAlarmMode: function(mode) {
this.sendSocketNotification('RING_SET_ALARM_MODE', mode);
this.changingMode = mode;
this.updateDom();
},

socketNotificationReceived: function(notification, payload) {
if (notification === 'RING_ALARM_MODE_CHANGED') {
console.log('Alarm mode changed to', payload);
this.ringState = 'good';
this.initialized = true;
this.changingMode = false;
this.alarmMode = payload;
this.updateDom();
} else if (notification === 'RING_INVALID_CONFIG') {
this.ringState = 'invalid-config';
} else if (notification === 'RING_CONNECT_FAILED') {
this.ringState = 'connect-failed';
} else if (notification === 'RING_NO_PANEL_DETECTED') {
this.ringState = 'no-panel';
}
},

translateMode: function(mode) {
switch (mode) {
case 'some':
return 'Armed Home';

case 'all':
return 'Armed Away';

case 'none':
return 'Disarmed';
}

return 'Unknown';
},
});
Loading

0 comments on commit c55b948

Please sign in to comment.