Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues #34 #35

Merged
merged 30 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
03c1a19
new dan2 and new dai profile
JasonChenGt Feb 22, 2021
12d7f35
new dan2 and dai
JasonChenGt Feb 25, 2021
28e6460
Update context.js
JasonChenGt Mar 2, 2021
9522f2e
add new line at end of file
JasonChenGt Mar 2, 2021
1d2e18b
change module name to iottalkjs
JasonChenGt Mar 3, 2021
f5bddc5
dan: check whether the online message is published
JasonChenGt Mar 8, 2021
127de63
new Dummy Device example
JasonChenGt Mar 9, 2021
67c85c1
Update index.html
JasonChenGt Mar 9, 2021
e810755
Update index.js
JasonChenGt Mar 9, 2021
6d19044
add exceptions
JasonChenGt Mar 9, 2021
19800bf
rewrite dai
JasonChenGt Mar 10, 2021
ac1a6e9
rename dan.js
JasonChenGt Mar 16, 2021
d0a1eba
change the interval unit to seconds
JasonChenGt Mar 16, 2021
fb1ca00
change coding style
JasonChenGt Mar 17, 2021
9e8e91a
rewrite dan
JasonChenGt Mar 17, 2021
4767273
remove old version callback
JasonChenGt Mar 23, 2021
7c5372f
change register arguments
JasonChenGt Mar 28, 2021
092ccf5
change register arguments
JasonChenGt Mar 30, 2021
204fdda
check if document exists
JasonChenGt Mar 30, 2021
4622132
remove new Promise
JasonChenGt Apr 8, 2021
6366062
subscribe return Promise
JasonChenGt Apr 8, 2021
7382e93
use promise in on_connect()
JasonChenGt Apr 9, 2021
768fa7c
return reject in promise
JasonChenGt Apr 10, 2021
f6c68a9
remove disconnect info
JasonChenGt Apr 10, 2021
99237ab
use df function in df_list
JasonChenGt Apr 10, 2021
0d69e86
Dummy Device
JasonChenGt Apr 11, 2021
bc496b0
use Array.isArray in parse_df_profile()
JasonChenGt Apr 12, 2021
8e2bf31
change ida to sa
JasonChenGt Apr 12, 2021
576f495
use df_func_name() in parse_df_profile()
JasonChenGt Apr 12, 2021
afbcb08
Update index.html
JasonChenGt Apr 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ChannelPool from './channel-pool.js'
JasonChenGt marked this conversation as resolved.
Show resolved Hide resolved

export default class {

constructor() {
this.url = null;
this.app_id = null;
this.name = null;
this.mqtt_host = null;
this.mqtt_port = null;
this.mqtt_username = null;
this.mqtt_password = null;
this.mqtt_client = null;
this.i_chans = new ChannelPool();
this.o_chans = new ChannelPool();
this.rev = null;
this.on_signal = null;
this.on_data = null;
this.on_register = null;
this.on_deregister = null;
this.on_connect = null;
this.on_disconnect = null;
// this._mqueue = queue.Queue() # storing the MQTTMessageInfo from ``publish``
JasonChenGt marked this conversation as resolved.
Show resolved Hide resolved
}
}
JasonChenGt marked this conversation as resolved.
Show resolved Hide resolved
194 changes: 194 additions & 0 deletions src/dai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import DeviceFeature from './device-feature.js'

let api_url;
JasonChenGt marked this conversation as resolved.
Show resolved Hide resolved
let device_model;
let device_addr;
let device_name;
let persistent_binding;
let username;
let extra_setup_webpage;
let device_webpage;

let register_callback;
let on_register;
let on_deregister;
let on_connect;
let on_disconnect;

let push_interval;
let interval;

let device_features = {};
let flags = {};

export const dai = function (profile, ida) {
api_url = profile['api_url'];
device_model = profile['device_model'];
device_addr = profile['device_addr'];
device_name = profile['device_name'];
persistent_binding = profile['persistent_binding'] ? profile['persistent_binding'] : false;
username = profile['username'];
extra_setup_webpage = profile['extra_setup_webpage'] ? profile['extra_setup_webpage'] : '';
device_webpage = profile['device_webpage'] ? profile['device_webpage'] : '';

register_callback = profile['register_callback'];
on_register = profile['on_register'];
on_deregister = profile['on_deregister'];
on_connect = profile['on_connect'];
on_disconnect = profile['on_disconnect'];

push_interval = profile['push_interval'] ? profile['push_interval'] : 1;
interval = profile['interval'] ? profile['interval'] : {};

parse_df_profile(profile, 'idf');
parse_df_profile(profile, 'odf');


function push_data(df_name) {
if (device_features[df_name].push_data == null)
return;
let _df_interval = interval[df_name] ? interval[df_name] : push_interval;
console.debug('%s : %s [message / %s ms]', df_name, flags[df_name], _df_interval);
let intervalID = setInterval(
(() => {
if (flags[df_name]) {
let _data = device_features[df_name].push_data();
dan2.push(df_name, _data);
}
else {
clearInterval(intervalID)
}
}), _df_interval
)
}

function on_signal(signal, df_list) {
console.log('Receive signal : ', signal, df_list);
if ('CONNECT' == signal) {
df_list.forEach(df_name => {
if (!flags[df_name]) {
flags[df_name] = true;
push_data(df_name);
}
});
}
else if ('DISCONNECT' == signal) {
df_list.forEach(df_name => {
flags[df_name] = false;
});
}
else if ('SUSPEND' == signal) {
// Not use
}
else if ('RESUME' == signal) {
// Not use
}
return true;
}

function on_data(df_name, data) {
try {
device_features[df_name].on_data(data);
} catch (err) {
console.error(err);
return false;
}
return true;
}

function init_callback(result) {
console.log('register:', result);
document.title = device_name;
ida.ida_init();
}

if (!api_url)
throw 'api_url is required';

if (!device_model)
throw 'device_model not given.';

if (persistent_binding && !device_addr)
throw 'In case of `persistent_binding` set to `True`, ' +
'the `device_addr` should be set and fixed.'

if (Object.keys(device_features).length === 0)
throw 'Neither idf_list nor odf_list is empty.';

let msg = {
'on_signal': on_signal,
'on_data': on_data,
'accept_protos': ['mqtt'],
'id': device_addr,
'idf_list': profile['idf_list'],
'odf_list': profile['odf_list'],
'name': device_name,
'profile': {
'model': device_model,
'u_name': username,
'extra_setup_webpage': extra_setup_webpage,
'device_webpage': device_webpage,
},
'register_callback': register_callback,
'on_register': on_register,
'on_deregister': on_deregister,
'on_connect': on_connect,
'on_disconnect': () => {
df_list.forEach(df_name => {
flags[df_name] = false;
});
console.debug('on_disconnect: _flag = %s', flags);
if (on_disconnect) {
return on_disconnect;
}
}
}

console.log(msg);

dan2.register(api_url, msg, init_callback);

window.onbeforeunload = function () {
try {
if (!persistent_binding) {
dan2.deregister();
}
} catch (error) {
console.error('dai process cleanup exception: %s', error);
}
};
};

const parse_df_profile = function (profile, typ) {
for (let i = 0; i < profile[`${typ}_list`].length; i++) {
let df_name;
let param_type;
let on_data;
let push_data;
if (typeof profile[`${typ}_list`][i] == 'function') {
df_name = profile[`${typ}_list`][i].name;
param_type = null;
on_data = push_data = profile[`${typ}_list`][i];
profile[`${typ}_list`][i] = profile[`${typ}_list`][i].name;
}
else if (typeof profile[`${typ}_list`][i] == 'object' && profile[`${typ}_list`][i].length == 2) {
df_name = profile[`${typ}_list`][i][0].name;
param_type = profile[`${typ}_list`][i][1];
on_data = push_data = profile[`${typ}_list`][i][0];
profile[`${typ}_list`][i][0] = profile[`${typ}_list`][i][0].name;
}
else {
throw `Invalid ${typ}_list, usage: [df_name, ...] or [[df_name, type], ...]`;
}

let df = new DeviceFeature({
'df_name': df_name,
'df_type': typ,
'param_type': param_type,
'push_data': push_data,
'on_data': on_data
});

device_features[df_name] = df;
}
}
Loading