Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pi.hub
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
iot
pi.hub
Commits
6806afe8
Commit
6806afe8
authored
Jun 27, 2017
by
Martin Kotula
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IsOccupied computed based on last readings
parent
cd4c9bb3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
11 deletions
+53
-11
UI.Web.Dashboard/src/app/sensors.config.js
+7
-0
UI.Web.Dashboard/src/components/sensor/sensorViewModel.js
+46
-11
No files found.
UI.Web.Dashboard/src/app/sensors.config.js
View file @
6806afe8
define
({
"Sensor 1"
:
{
name
:
"Chill room"
},
__default__
:
{
minReadingsCount
:
3
,
readingExpiry
:
3
*
60
,
//in seconds
lastReadings
:
10
//number of last readings taken into account when calculating isOccupied
}
});
\ No newline at end of file
UI.Web.Dashboard/src/components/sensor/sensorViewModel.js
View file @
6806afe8
define
([
'knockout'
,
"app/sensors.config"
,
"lodash"
],
function
(
ko
,
sensorsConfig
,
_
)
{
define
([
'knockout'
,
"app/sensors.config"
,
"lodash"
],
function
(
ko
,
sensorsConfig
,
_
)
{
function
SensorViewModel
(
sensorData
)
{
this
.
name
=
getName
(
sensorData
.
id
);
this
.
name
=
getName
(
sensorData
.
id
);
this
.
readings
=
ko
.
observableArray
([]);
this
.
config
=
getConfig
(
sensorData
.
id
);
this
.
update
(
sensorData
);
this
.
isOccupied
=
ko
.
computed
(
_
.
bind
(
function
()
{
var
lastReading
=
_
.
last
(
this
.
readings
());
this
.
isOccupied
=
ko
.
computed
(
_
.
bind
(
calculateIsOcupied
,
this
));
var
clearReadingsPeriodically
=
_
.
bind
(
function
()
{
this
.
clearExpiredReadings
();
setTimeout
(
clearReadingsPeriodically
,
10
*
1000
);
},
this
);
clearReadingsPeriodically
();
}
function
calculateIsOcupied
()
{
var
readings
=
this
.
readings
();
if
(
readings
.
length
<
this
.
config
.
minReadingsCount
)
return
undefined
;
var
lastReadings
=
_
.
slice
(
_
.
orderBy
(
readings
,
"timestamp"
,
"desc"
),
0
,
this
.
config
.
lastReadings
);
if
(
!
lastReading
)
return
undefined
;
var
occupiedReadingsCount
=
0
;
var
notOccupiedReadingsCount
=
0
;
return
lastReading
.
isOccupied
;
_
.
forEach
(
lastReadings
,
function
(
reading
)
{
if
(
reading
.
isOccupied
)
{
occupiedReadingsCount
++
;
}
else
{
notOccupiedReadingsCount
++
;
}
});
},
this
))
;
return
occupiedReadingsCount
>
notOccupiedReadingsCount
;
}
SensorViewModel
.
prototype
.
update
=
function
(
sensorData
){
SensorViewModel
.
prototype
.
clearExpiredReadings
=
function
()
{
var
currentDate
=
new
Date
();
var
expiryInMiliseconds
=
this
.
config
.
readingExpiry
*
1000
;
this
.
readings
.
remove
(
function
(
reading
)
{
return
currentDate
-
reading
.
timestamp
>
expiryInMiliseconds
;
});
};
SensorViewModel
.
prototype
.
update
=
function
(
sensorData
)
{
this
.
readings
.
push
(
_
.
pick
(
sensorData
,
[
"isOccupied"
,
"timestamp"
]));
this
.
timestamp
=
ko
.
observable
(
sensorData
.
timestamp
);
};
function
getName
(
id
){
function
getName
(
id
)
{
return
(
sensorsConfig
[
id
]
&&
sensorsConfig
[
id
].
name
)
||
id
;
}
function
getConfig
(
id
)
{
return
_
.
merge
(
sensorsConfig
[
id
]
||
{},
sensorsConfig
.
__default__
);
}
return
SensorViewModel
;
});
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment