├── lib └── .gitkeep ├── examples └── voltage-log.js ├── config └── atom.json.sample ├── package.json ├── bin └── molecule └── README.md /lib/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/voltage-log.js: -------------------------------------------------------------------------------- 1 | module.exports = function($) { 2 | $({ type: 'BusVoltage' }) 3 | .on('meta:voltage', function(unit, voltage) { 4 | console.log('voltage is', voltage); 5 | }); 6 | }; -------------------------------------------------------------------------------- /config/atom.json.sample: -------------------------------------------------------------------------------- 1 | { 2 | protocolFile: './protocol.json', 3 | unitConfigFile: './units.json', 4 | monitor: { 5 | port: 1201 6 | }, 7 | command: { 8 | tcpPort: 1202 9 | }, 10 | udp: [ 11 | { 12 | address: '192.168.10.60', 13 | port: 1100 14 | } 15 | ], 16 | legacy: [ 17 | { 18 | port: 1200 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "molecule.js", 3 | "version": "1.0.0", 4 | "description": "User Controller Project for atom.js", 5 | "author": "Mattias Runge , Björn Ramberg ", 6 | "repository": { "type": "git", "url": "git://github.com/bjorne/molecule.js.git" }, 7 | "dependencies": { 8 | "atom.js": "~2.0.0" 9 | }, 10 | "devDependencies": { 11 | "mocha": "~1.1.0", 12 | "should": "~0.6.3", 13 | "jshint": "~0.7.1" 14 | }, 15 | "engines": { "node": ">= 0.6.0" }, 16 | "license": "GPLv2" 17 | } 18 | -------------------------------------------------------------------------------- /bin/molecule: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs'), 4 | path = require('path'); 5 | 6 | var atom = require('atom.js'); 7 | 8 | var config; 9 | try { 10 | config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config/atom.json'))); 11 | } catch (ex) { 12 | console.log('atom.json is invalid or could not be found'); 13 | console.log('Error was:', ex.toString()); 14 | process.exit(1); 15 | } 16 | 17 | var server = new atom.Atom(config); 18 | 19 | server.on('error', function(error) { 20 | console.log('An error occured in Atom: ' + error); 21 | }); 22 | 23 | var libPath = path.join(__dirname, '../lib'); 24 | fs.readdirSync(libPath).forEach(function(name) { 25 | if (name.match(/\.js$/)) { 26 | require(path.join(libPath, name))(server.unitSelector); 27 | } 28 | }); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Molecule.js 2 | 3 | Molecule is project skeleton for adding custom control logic to and 4 | running [Atom.js](https://github.com/mattiasrunge/atom.js). Molecule 5 | comes bundled with a sample configuration for Atom as well as example 6 | control logic scripts. 7 | 8 | ## Getting started 9 | 10 | ### Set up environment 11 | 12 | In order to get started, clone this repository and create your own 13 | branch. 14 | 15 | $ git clone https://github.com/bjorne/molecule.js 16 | $ cd molecule.js 17 | $ git branch 18 | 19 | By creating your own branch, you can freely commit and push changes 20 | and easily share your code with other users. 21 | 22 | _(TODO)_ Copy `config/atom.json.sample` to `config/atom.json` and make 23 | the changes necessary to suit your environment. 24 | 25 | Run `npm install` to install the necessary packages. 26 | 27 | ### Scripting basics 28 | 29 | A molecule script resides under the `lib/` directory and exports a 30 | function which will automatically be invoked by molecule upon startup, 31 | like so: 32 | 33 | module.exports = function($) { 34 | // logic goes here 35 | } 36 | 37 | The `$` argument is the __unit selector__ function. If you've every 38 | used jQuery, you should feel quite at home. The unit selector function 39 | is used to find __units__ - which are abstractions of modules and 40 | functions in the CAN network. For example, there is a unit called 41 | `BusVoltage`. We can select all `BusVoltage` units using 42 | 43 | $({ type: 'BusVoltage' }) 44 | 45 | The result of the `$` function is a __unit selection__ upon which you 46 | can apply commands or listen for events. Suppose, we would like to 47 | print the bus voltage in the console when a new value is received. The 48 | script could look like this: 49 | 50 | module.exports = function($) { 51 | $({ type: 'BusVoltage' }) 52 | .on('meta:voltage', function(unit, voltage) { 53 | console.log('current voltage is ' + voltage); 54 | }); 55 | }; 56 | 57 | The `meta:` prefix indicates we are listening for __metadata 58 | changes__. Whenever a unit receives a new value, the metadata will be 59 | updated and an event is emitted. 60 | 61 | Once you see everything is working, it may be a good idea to commit. 62 | 63 | Checkout the `examples/` directory for more examples of scripts. 64 | 65 | ### Launching 66 | 67 | Now, in order to try out the code, just run 68 | 69 | $ ./bin/molecule 70 | 71 | and see your logic coming to work. 72 | 73 | ## Development notes 74 | 75 | When developing Atom alongside Molecule it is handy to point Molecule 76 | directly to the Atom source directory. You can do this using `npm 77 | link`. 78 | 79 | $ cd /path/to/atom.js 80 | $ npm link 81 | $ cd /path/to/molecule.js 82 | $ npm link atom.js 83 | --------------------------------------------------------------------------------