-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlwm2m-arduino.js
147 lines (123 loc) · 4.84 KB
/
lwm2m-arduino.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var five = require("johnny-five"),
lwm2mClient = require('lwm2m-node-lib').client,
async = require('async'),
apply = async.apply,
config = require('./config'),
globalDeviceInfo,
board = new five.Board({
repl: false
}),
analogSensors = [];
function handleWrite(objectType, objectId, resourceId, value, callback) {
console.log('Value written:\n--------------------------------\n');
console.log('-> ObjectType: %s', objectType);
console.log('-> ObjectId: %s', objectId);
console.log('-> ResourceId: %s', resourceId);
console.log('-> Written value: %s', value);
if (value == 0) {
(new five.Led(resourceId)).off();
} else {
(new five.Led(resourceId)).on();
}
callback(null);
}
function handleRead(objectType, objectId, resourceId, value, callback) {
console.log('Value read:\n--------------------------------\n');
console.log('-> ObjectType: %s', objectType);
console.log('-> ObjectId: %s', objectId);
console.log('-> ResourceId: %s', resourceId);
console.log('-> Read Value: %s', value);
callback(null, value);
}
function setHandlers(deviceInfo) {
lwm2mClient.setHandler(deviceInfo.serverInfo, 'write', handleWrite);
lwm2mClient.setHandler(deviceInfo.serverInfo, 'read', handleRead);
}
function connect(config, callback) {
console.log('Trying to connect to Lightweight M2M Server');
lwm2mClient.register(config.server.host, config.server.port, config.server.path, config.client.endpoint, function (error, deviceInfo) {
if (error) {
console.log('Failed to connect');
callback(error);
} else {
setHandlers(deviceInfo);
globalDeviceInfo = deviceInfo;
callback(null, deviceInfo);
}
});
}
function createDataHandler(analogUri, i, transform) {
console.log('Creating handler for analog sensor on Object [%s] resource[%d]', analogUri, i);
return function() {
var value = (transform)?eval(transform.replace('VALUE', this.value)):this.value;
lwm2mClient.registry.setResource(analogUri, i, value, function (error) {
if (error) {
console.log('Error writting resource %s/%s: ', analogUri, i, error);
}
});
};
}
board.on('ready', function() {
lwm2mClient.init(config);
var analogUri = '/' + config.mapping.objectType + '/' + config.mapping.analogInstance,
digitalUri = '/' + config.mapping.objectType + '/' + config.mapping.digitalInstance,
connectionFlow = [
apply(lwm2mClient.registry.create, digitalUri),
apply(lwm2mClient.registry.create, analogUri),
];
function prepareDigital(i) {
console.log('Creating handler for digital entries on Object [%s] resource[%d]', analogUri, i);
connectionFlow.push(apply(lwm2mClient.registry.setResource, digitalUri, i, 1));
}
function prepareAnalog(i, transform) {
var sensor = new five.Sensor({
pin: "A" + i,
freq: config.data.frequency
});
connectionFlow.push(apply(lwm2mClient.registry.setResource, analogUri, i, 9));
analogSensors.push(sensor);
sensor.on('data', createDataHandler(analogUri, i, transform));
}
function prepareMotion(i) {
var motion = new five.IR.Motion(i);
connectionFlow.push(apply(lwm2mClient.registry.setResource, digitalUri, i, 9));
motion.on("calibrated", function(err, ts) {
motion.on("motionstart", function(err, ts) {
lwm2mClient.registry.setResource(digitalUri, i, 'MOVING', function (error) {
if (error) {
console.log('Error writting motion value %s/%s: ', analogUri, i, error);
}
});
});
motion.on("motionend", function(err, ts) {
lwm2mClient.registry.setResource(digitalUri, i, 'STILL', function (error) {
if (error) {
console.log('Error writting motion value %s/%s: ', analogUri, i, error);
}
});
});
});
}
for (var i = 0; i < config.data.pinMap.length; i++) {
var pinObj = config.data.pinMap[i];
switch(pinObj.type) {
case 'analogSensor':
prepareAnalog(pinObj.id, pinObj.transform);
break;
case 'digitalInput':
prepareDigital(pinObj.id);
break;
case 'motion':
prepareMotion(pinObj.id);
break;
}
}
connectionFlow.push(apply(connect, config));
async.series(connectionFlow, function (error) {
if (error) {
console.log('There was an error starting the server');
} else {
console.log('Server started succesfully');
}
});
});