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 10 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
20 changes: 20 additions & 0 deletions examples/Dummy_Device/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Dummy_Device</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="js/ida.js"></script>
JasonChenGt marked this conversation as resolved.
Show resolved Hide resolved
<script src="../../build-web/iottalkjs-web.js"></script>

</head>

<body>
<legend>Dummy_Sensor</legend>
<legend class='IDF_value'>_____________</legend>
<legend>Dummy_Control</legend>
<legend class='ODF_value'>_____________</legend>
</body>

</html>
32 changes: 32 additions & 0 deletions examples/Dummy_Device/js/ida.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$(function () {
function Dummy_Sensor() {
var number = Math.floor((1 + Math.random()) * 0x10000);
$('.IDF_value')[0].innerText = number;
return number;
}

function Dummy_Control(data) {
$('.ODF_value')[0].innerText = data[0];
}

var profile = {
'api_url': 'https://iottalk2.tw/csm',
'device_model': 'Dummy_Device',
'device_addr': 'c96ca71c-9e48-2a23-2868-acb420a2f105',
'device_name': 'Dummy',
'persistent_binding': true,
'idf_list': [Dummy_Sensor],
'odf_list': [Dummy_Control],
'interval': {
'Dummy_Sensor': 100,
}
};

/*******************************************************************/
function ida_init() {
}
var ida = {
'ida_init': ida_init,
};
iottalkjs.dai(profile, ida);
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "iottalk-js",
"version": "2.0.4",
"description": "IoTtalk javascript library SDK",
"main": "dan2.js",
"main": "iottalkjs.js",
"directories": {
"example": "examples"
},
"scripts": {
"start": "npx webpack -w --mode development",
"build": "npm run build:node && npm run build:web",
"build:node": "npx babel src --out-dir build-node && npx ncc build build-node/dan2.js -o build-node/dist && mv build-node/dist/index.js build-node/dist/dan2.js",
"build:node": "npx babel src --out-dir build-node && npx ncc build build-node/index.js -o build-node/dist && mv build-node/dist/index.js build-node/dist/iottalkjs.js",
"build:web": "npx webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
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';

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
}
}
196 changes: 196 additions & 0 deletions src/dai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import DeviceFeature from './device-feature.js';
import { push, register, deregister } from './dan2.js'
import { RegistrationError, ArgumentError } from './exceptions.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'] || false;
username = profile['username'];
extra_setup_webpage = profile['extra_setup_webpage'] || '';
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'] || 1;
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] || push_interval;
console.debug('%s : %s [message / %s ms]', df_name, flags[df_name], _df_interval);
let _push_interval = setInterval(
(() => {
if (flags[df_name]) {
let _data = device_features[df_name].push_data();
push(df_name, _data);
}
else {
clearInterval(_push_interval);
}
}), _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 new RegistrationError('api_url is required.');

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

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

if (Object.keys(device_features).length === 0)
throw new RegistrationError('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('dai', msg);

register(api_url, msg, init_callback);

window.onbeforeunload = function () {
try {
if (!persistent_binding) {
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 new RegistrationError(`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