-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHHisItem.js
More file actions
92 lines (83 loc) · 2.07 KB
/
HHisItem.js
File metadata and controls
92 lines (83 loc) · 2.07 KB
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
//
// Copyright (c) 2015, Shawn Jacobson
// Licensed under the Academic Free License version 3.0
//
// Ported from @see {@link https://bitbucket.org/brianfrank/haystack-java|Haystack Java Toolkit}
//
// History:
// 21 Mar 2015 Shawn Jacobson Creation
//
var HDict = require('./HDict');
/**
* HHisItem is used to model a timestamp/value pair
* @see {@link http://project-haystack.org/doc/Ops#hisRead|Project Haystack}
*
* @constructor
* @extends {HDict}
* @param {HDateTime} ts
* @param {HVal} val
*/
function HHisItem(ts, val) {
/** Timestamp of history sample */
this.ts = ts;
/** Value of history sample */
this.val = val;
this.size = 2;
}
HHisItem.prototype = Object.create(HDict.prototype);
module.exports = HHisItem;
/**
* @param {string} name
* @param {boolean} checked
* @returns {HVal}
*/
HHisItem.prototype.get = function(name, checked) {
if (name === "ts") return this.ts;
if (name === "val") return this.val;
if (!checked) return null;
throw new Error("Unknown Name: " + name);
};
/**
* @memerof HHisItem
* returns {iterator}
*/
HHisItem.prototype.iterator = function() {
function hasNext() {
return cur < 1;
}
function next() {
++cur;
if (cur === 0) return new HDict.MapEntry("ts", this.ts);
if (cur === 1) return new HDict.MapEntry("val", this.val);
throw new Error("No Such Element");
}
var cur = -1;
};
/**
* Construct from timestamp, value
* @static
* @param {HDateTime} ts
* @param {HVal} val
* @return {HHisItem}
*/
HHisItem.make = function(ts, val) {
if (ts === 'undefined' || ts === null || val === 'undefined' || val === null)
throw new Error("ts or val is undefined");
return new HHisItem(ts, val);
};
/**
* Map HGrid to HHisItem[]. Grid must have ts and val columns.
* @static
* @param {HGrid} grid
* @return {HHisItem[]}
*/
HHisItem.gridToItems = function(grid) {
var ts = grid.col("ts");
var val = grid.col("val");
var items = [];
for (var i = 0; i < grid.numRows(); i++) {
var row = grid.row(i);
items[i] = new HHisItem(row.get(ts, true), row.get(val, false));
}
return items;
};