Skip to content

Commit

Permalink
Merge pull request #1 from jirrick/next
Browse files Browse the repository at this point in the history
Version 0.2.0
  • Loading branch information
jirrick committed Sep 24, 2017
2 parents d4f7da0 + 6c423bc commit e0f39ab
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -12,7 +12,7 @@
4
],
"linebreak-style": [
"error",
"warn",
"unix"
],
"quotes": [
Expand Down
3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "quido",
"version": "0.1.0",
"version": "0.2.0",
"description": "quido server",
"main": "server.js",
"scripts": {
Expand All @@ -14,6 +14,7 @@
"express-prettify": "0.0.8",
"helmet": "^3.8.1",
"mongoose": "^4.11.11",
"round-to": "^2.0.0",
"strong-error-handler": "^2.2.0"
}
}
210 changes: 210 additions & 0 deletions src/class/boardClasses.js
@@ -0,0 +1,210 @@
const roundTo = require('round-to');

class OutputGroup {
constructor(data = {}) {
Object.assign(this, data);

this.maxValue = data.minValue + (Math.pow(2, data.outs.length) - 1) * data.increment;
this.value = data.minValue;
}

setValue(value) {
let result = '';
//check value is number
if (typeof value === 'number') {
//check value is in bounds
if (value >= this.minValue && value <= this.maxValue) {
//check if value can be reached
if ((value - this.minValue) % this.increment == 0) {
this.value = value;
result = 'success';
} else
result = 'value not reachable';
} else
result = 'value out of bounds';
} else
result = 'not a number';
return result;
}

getValue() {
const length = this.outs.length;
const baseValue = (this.value - this.minValue) / this.increment;
const binary = baseValue.toString(2);
const padded = '0'.repeat(length) + binary;
return (padded.slice(-length));
}
}

class InputGroup {
constructor(data = {}) {
Object.assign(this, data);
}

parse(inputs, counters) {
let result = -1;
if (this.ins.length == 2) { //ANALOG
//get counter values (convert index from one base to zero base)
const cntBase = counters[this.ins[0] - 1];
const cntValue = counters[this.ins[1] - 1];

//parse only when base counter is over treshold
if (cntBase >= this.treshold) {
result = this.multiplier * ((cntValue / cntBase) - 1);
}
}
else if (this.ins.length == 1) { //BIT
// just parse state of first (only) defined input (convert index from one base to zero base)
const bitNumber = this.ins[0] - 1;
result = parseInt(inputs[bitNumber]);
}
return result;
}

resetValues(counters){
// by default do nothing
let result = Array(counters.length).fill(0);
if (this.ins.length == 2) { //ANALOG
//get counter values (convert index from one base to zero base)
const indexBase = this.ins[0] - 1;
const indexValue = this.ins[1] - 1;

const cntBase = counters[indexBase];
const cntValue = counters[indexValue];

//reset only when base counter is over treshold
if (cntBase >= this.treshold) {
result[indexBase] = cntBase;
result[indexValue] = cntValue;
}
}
return result;
}
}

class Board {
constructor(data = {}) {
//parse data from config
Object.assign(this, data);

//create outClasses
let outClass = [];
for (let group of data.output_groups) {
outClass.push(new OutputGroup(group));
}
this.outputClasses = outClass;

//create inClasses
let inClass = [];
for (let group of data.input_groups) {
inClass.push(new InputGroup(group));
}
this.inputClasses = inClass;

//array containing values to be subtracted from board counters
this.resetCounters = Array(this.inputs).fill(0);
}

setOutput(name, value) {
let result = '';
// check that output classes are initialized and contains requested output group
if (this.outputClasses != null) {
const outGroup = this.outputClasses.find(group => group.name === name);
if (outGroup != null) {
// return the result
result = outGroup.setValue(value);
}
else {
result = 'group not found';
}
}
else {
result = 'not initialized';
}
return result;
}

getResponse() {
//go through all output classes and substitute their output (deafult output = NOP)
let outputString = 'x'.repeat(this.outputs);
for (let group of this.outputClasses) {
//save group output
const groupOut = group.getValue();
let bitCount = 0;
let indexOne;
for (indexOne of group.outs) {
const indexZero = --indexOne;
//check output bounds and overwriting of result
if (indexZero >= 0 && indexZero < this.outputs && outputString[indexZero] === 'x') {
outputString = setCharAt(outputString, indexZero, groupOut[bitCount]);
bitCount++;
}
}
}

//go through all counter reset values and create reset string if needed (value bigger than 0)
let resetString = '';
for (var i = 0, len = this.resetCounters.length; i < len; i++) {
if (this.resetCounters[i] > 0){
resetString += ` cnt${i+1}="${this.resetCounters[i]}"`;
this.resetCounters[i] = 0;
}
}

//build the response
const result = `<?xml version="1.0" encoding="ISO-8859-1"?><root><set outs="${outputString}"${resetString} /></root>`;
return result;
}

getValue(name) {
//deafult output -1
let result = -1;

// check that output classes are initialized and contains requested group
if (this.outputClasses != null) {
const outGroup = this.outputClasses.find(group => group.name === name);
if (outGroup != null) {
// return the result
result = outGroup.value;
}
}
return result;
}

parseInput(inputs, counters) {
//output array of parsed values
let result = [];

//go through all input classes
for (let group of this.inputClasses) {
//create input object
const parsedValue = group.parse(inputs, counters);

//only add value when valid (larger than zero)
if (parsedValue >= 0) {
const inputObject = {
name: group.name,
value: roundTo(parsedValue, 1)
};
result.push(inputObject);

const resetValues = group.resetValues(counters);
this.resetCounters.forEach(function(item, index, arr) {
arr[index] = item + resetValues[index];
});
}
}
return result;
}
}

module.exports = {
OutputGroup: OutputGroup,
InputGroup: InputGroup,
Board: Board
};

function setCharAt(str, index, chr) {
if (index > str.length - 1) return str;
return str.substr(0, index) + chr + str.substr(index + 1);
}
22 changes: 21 additions & 1 deletion src/config-template.js
Expand Up @@ -6,7 +6,27 @@ const config = {
name: 'name',
mac: 'mac',
inputs: 8,
outputs: 8
input_groups: [
{
name: 'testIn1',
ins: [7, 8], //ANALOG - first base, second value
multiplier: 5,
treshold: 1000
},
{
name: 'testInA',
ins: [1] //BIT
}
],
outputs: 8,
output_groups: [
{
name: 'testOut',
outs: [1, 2, 3],
minValue: 15,
increment: 2
}
]
}
],

Expand Down
51 changes: 41 additions & 10 deletions src/controllers/boardController.js
@@ -1,23 +1,54 @@
'use strict';

const config = require('../config'),
levyController = require('./levyController'),
pravyController = require('./pravyController');
const server = require('../server'),
mongoose = require('mongoose'),
Logs = mongoose.model('Logs');

//Parse board request
exports.parse = function (req, res) {
//verify board
const req_name = req.query.name;
const req_mac = req.query.mac;
const boardInfo = config.boards.find(board => board.name === req_name && board.mac === req_mac);
const boardInfo = server.boards.find(board => board.name === req_name && board.mac === req_mac);

if (boardInfo != null) {
//do different stuff for different board
if (req_name === 'LEVY')
levyController.process(boardInfo, req, res);
else if (req_name === 'PRAVY')
pravyController.process(boardInfo, req, res);
//console.log(req.originalUrl);

//parse counters into array
const inputCounters = [];
for (let i = 1; i <= boardInfo.inputs; i++) {
const key = `cnt${i}`;
const value = parseInt(req.query[key]);
inputCounters.push(value);
}
const ins = req.query.ins;

//add temp to parsed inputs
const parsedInputs = boardInfo.parseInput(ins, inputCounters);
const temp = new Object();
temp.name = 'temp';
temp.value = req.query.tempV;
parsedInputs.push(temp);

//create data object
const data = new Object();
data.name = req.query.name;
data.inputs = parsedInputs;

//Create Log item and save to mongo
const newLog = new Logs(data);
newLog.save(function (err, data) {
if (err)
console.error(err);
console.log(data);
});

//send response
const reply = boardInfo.getResponse();
//console.log(reply);
res.set('Content-Type', 'text/xml');
res.send(reply);
}
else
res.status(400).send('Unknown board!');
};
};
50 changes: 0 additions & 50 deletions src/controllers/levyController.js

This file was deleted.

0 comments on commit e0f39ab

Please sign in to comment.