Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
add set defaults function and update release to version v0.3.0-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Serve committed Feb 24, 2018
1 parent a49daec commit b24351a
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 75 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const addClusterStateTransitions = require('../../helpers/resolveClusters');

const clusters = {
whiteCluster: ["Running", "Paused", "Pausing"],
transparentCluster: ["whiteCluster", "Suspending", "Suspended", "Unsuspending"],
Expand Down Expand Up @@ -83,35 +85,6 @@ let StateTransitions = {

};

function addClusterStateTransitions() {
for (let key in StateTransitions) {
//console.log(key);
// if it is a cluster
if (clusters[key])
resolveCluster(key, StateTransitions[key]);
}
}

function resolveCluster(clusterName, followUpStates) {
clusters[clusterName].forEach(function (item) {
// recursive if the item itself is a clusters
if (clusters[item])
return resolveCluster(item, followUpStates);
// if it is a single state
addFollowUpStatesToSingleState(item, followUpStates);
});
}

function addFollowUpStatesToSingleState(state, followUpStates) {
// add multiple followUpStates to state
for (let key in followUpStates) {
if (followUpStates.hasOwnProperty(key))
if (!StateTransitions[state])
return new Error("Probable typo: " + state + " is not defined in StateTransitions.");
StateTransitions[state][key] = followUpStates[key];
}
}

addClusterStateTransitions();
addClusterStateTransitions(StateTransitions, clusters);
exports.stateTransitions = StateTransitions;
exports.initialState = "Aborted";
173 changes: 173 additions & 0 deletions default-configurations/smart4i/ServerStructure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
exports.baseNode = "RootFolder";

exports.moduleStructure = {
"mi5":{
type: 'Folder',
content: {
"$(moduleName)": {
type: 'Folder',
content: {
identification:{
type: 'Folder',
content: {}
},
skill: {
type: 'Folder',
content: {}
},
state: {
type: 'Folder'
},
variables: {
type: 'Folder'
}
}
}
}
}
};

exports.skillStructure = {
"$(skillName)": {
type: 'Folder',
content: {
state: {
type: 'Folder'
},
inputParameters: {
type: 'Folder'
},
outputParameters: {
type: 'Folder'
}
}
}
};

exports.stateStructure = {
stateMachine: {
type: 'Folder',
content: {
operationalState: {
type: 'Variable',
dataType: 'String',
initValue: '17'
},
errorAcknowledge: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
errorMessage: {
type: 'Folder',
content: {
messageID: {
type: 'Variable',
dataType: 'Integer',
initValue: 0
},
messageType: {
type: 'Variable',
dataType: 'String',
initValue: ''
},
messageText: {
type: 'Variable',
dataType: 'String',
initValue: ''
},
messageTime: {
type: 'Variable',
dataType: 'String',
initValue: ''
},
}
},
stateTransition: {
type: 'Folder',
content: {
start: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
pause: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
resume: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
suspend: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
unsuspend: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
hold: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
unhold: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
stop: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
abort: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
clear: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
reset: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
},
done: {
type: 'Variable',
dataType: 'Boolean',
initValue: false
}
}
}
}
}
};


exports.stateVariablesStructure = {};

exports.skillParametersStructure = {};

exports.skillParametersStructure = {};

exports.pathToModule = ["mi5", "$(moduleName)"];
exports.pathToModuleIdentification = ["mi5", "$(moduleName)", "identification"];
exports.pathToModuleStates = ["mi5", "$(moduleName)", "state"];
exports.pathToModuleStateVariables = ["mi5", "$(moduleName)", "variables"];
exports.pathToSkillsBaseFolder = ["mi5", "$(moduleName)", "skill"];
exports.pathToSkillStates = ["$(skillName)","state"];
exports.pathToSkillInputParameters = ["$(skillName)","inputParameters"];
exports.pathToSkillOutputParameters = ["$(skillName)","outputParameters"];
exports.pathToStateTransitions = ["stateMachine", "stateTransition"];
exports.pathToOperationalState = ["stateMachine", "operationalState"];


95 changes: 95 additions & 0 deletions default-configurations/smart4i/ValidStateTransitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const addClusterStateTransitions = require('../../helpers/resolveClusters');

const clusters = {
/*whiteCluster: ["Running", "Paused", "Pausing"],
transparentCluster: ["whiteCluster", "Suspending", "Suspended", "Unsuspending"],
yellowCluster: ["transparentCluster", "Idle", "Starting", "Completing", "Complete", "Resetting", "Holding", "Held", "Unholding"],
redCluster: ["yellowCluster", "Clearing", "Stopped", "Stopping"]*/
};

let StateTransitions = {
// Idle
0: {
start: "Starting" // Running
},
Starting: {
done: 2 // Running
},
// Running
2: {
done: 12 // Complete
},/*
Pausing: {
suspend: "Suspending"
},
Paused: {
resume: "Running"
},
Suspending: {
done: "Suspended"
},
Suspended: {
unsuspend: "Unsuspended"
},
Unsuspended: {
done: "Running"
},
Unsuspending: {
suspend: "Suspended"
},
Completing: {
done: "Complete"
},*/
// Complete
12: {
reset: "Resetting"
},
/*
Aborting: {
done: "Aborted"
},*/
// Aborted
17: {
clear: "Clearing",
reset: "Resetting"
},/*
Stopping: {
done: "Stopped"
},*/
// Stopped
15: {
reset: "Resetting"
},
/*
Holding: {
done: "Held"
},
Held: {
unhold: "Unholding"
},
Unholding: {
done: "Running"
},*/
Clearing: {
done: 15 // Stopped
},
Resetting: {
done: "0" // Idle
},/*
whiteCluster: {
suspend: "Suspending"
},
transparentCluster: {
hold: "Holding"
},
yellowCluster: {
stop: "Stopping"
},
redCluster: {
abort: "Aborting"
}*/
};

addClusterStateTransitions(StateTransitions, clusters);
exports.stateTransitions = StateTransitions;
exports.initialState = 17;
35 changes: 35 additions & 0 deletions helpers/resolveClusters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Adds the state transitions noted by clusters to the state transitions object.
* @param StateTransitions
* @param clusters
*/
function addClusterStateTransitions(StateTransitions, clusters) {
for (let key in StateTransitions) {
//console.log(key);
// if it is a cluster
if (clusters[key])
resolveCluster(key, StateTransitions[key], StateTransitions, clusters);
}
}

function resolveCluster(clusterName, followUpStates, StateTransitions, clusters) {
clusters[clusterName].forEach(function (item) {
// recursive if the item itself is a clusters
if (clusters[item])
return resolveCluster(item, followUpStates, StateTransitions, clusters);
// if it is a single state
addFollowUpStatesToSingleState(item, followUpStates, StateTransitions);
});
}

function addFollowUpStatesToSingleState(state, followUpStates, StateTransitions) {
// add multiple followUpStates to state
for (let key in followUpStates) {
if (followUpStates.hasOwnProperty(key))
if (!StateTransitions[state])
return new Error("Probable typo: " + state + " is not defined in StateTransitions.");
StateTransitions[state][key] = followUpStates[key];
}
}

module.exports = addClusterStateTransitions;
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports.Mi5Module = require('./models/mi5-module');
module.exports.Mi5Skill = require('./models/mi5-skill');
module.exports.Mi5mqttPublisher = require('./lib/mqttPublisher');
module.exports.Mi5mqttPublisher = require('./lib/mqttPublisher');
module.exports.setDefaults = require('./models/setDefaults').setDefaults;
module.exports.resolveClusters = require('./helpers/resolveClusters');
14 changes: 11 additions & 3 deletions lib/mqttPublisher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mqtt = require('mqtt');


function publish(modules, endpointUrl){
function publish(modules, endpointUrl, stateTranslations = {}){
let client = mqtt.connect(endpointUrl);

/**
Expand All @@ -12,17 +12,25 @@ function publish(modules, endpointUrl){
modules.forEach((module) => {
module.onAny(function(event, value) {
client.subscribe('mi5/'+module.name+'/#');
client.publish('mi5/'+module.name+'/state/'+event, JSON.stringify(value));
client.publish('mi5/'+module.name+'/state/'+translateEvent(event), JSON.stringify(value));
});
module.skills.forEach((skill) => {
skill.onAny((event, value) => {
let parameters = {};
skill.inputParameters.forEach((parameter) => {
parameters[parameter.name] = parameter.getValue();
});
client.publish('mi5/'+module.name+'/skill/'+skill.name+'/state/'+event, JSON.stringify(parameters));
client.publish('mi5/'+module.name+'/skill/'+skill.name+'/state/'+translateEvent(event), JSON.stringify(parameters));
});
});

function translateEvent(event){
let translation = stateTranslations[event];
if(typeof translation === 'undefined')
return event;
else
return translation;
}
});

/**
Expand Down
Loading

0 comments on commit b24351a

Please sign in to comment.