Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
POS/src/essae-weight-reader.js at develop · Posnic/POS · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Posnic
POS
Repository navigation
Code
Issues
396
(396)
Pull requests
14
(14)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
POS
/
src
/
essae-weight-reader.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
232 lines (198 loc) · 7.25 KB
develop
Breadcrumbs
POS
/
src
/
essae-weight-reader.js
Copy path
Top
File metadata and controls
Code
Blame
232 lines (198 loc) · 7.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Essae weighing scale over TCP/IP.
*
* Ported from the implementation used on a real SI-810 (Posnic/desktop), kept
* behaviour-for-behaviour because it is proven against the device: the polling
* commands, the two frame shapes, the two-reading average and the reconnect
* loop all match. What is new here is that every value is configurable rather
* than baked in, with the previously hard-coded values as defaults.
*
* Frames the scale answers with:
* extended 0x22 0x03 <len16> ... <float32LE net weight kg>
* simple 0x66 <2 bytes> <uint16LE grams>
*
* Serial (RS-232) scales are handled separately by scale-parser.js.
*/
const
net
=
require
(
'net'
)
;
const
{
BrowserWindow
}
=
require
(
'electron'
)
;
const
CMD_SIMPLE
=
Buffer
.
from
(
[
0x11
,
0x01
,
0x00
,
0x00
,
0xEE
,
0xFF
]
)
;
const
CMD_EXT
=
Buffer
.
from
(
[
0x33
,
0x02
,
0x01
,
0x00
,
0xCA
,
0xFF
,
0x25
,
0xDB
,
0xFF
]
)
;
const
DEFAULTS
=
{
ip
:
'192.168.0.103'
,
port
:
4321
,
debug
:
false
,
pollIntervalMs
:
30
,
// gap between poll cycles
readTimeoutMs
:
350
,
// how long to wait for a frame
reconnectDelayMs
:
2000
,
historySize
:
5
,
minChangeKg
:
0.0001
,
// ignore jitter smaller than this
}
;
class
EssaeWeightReader
{
constructor
(
options
=
{
}
)
{
this
.
config
=
{
...
DEFAULTS
,
...
options
}
;
this
.
running
=
false
;
this
.
socket
=
null
;
this
.
weightHistory
=
[
]
;
this
.
lastReported
=
null
;
this
.
reconnectTimer
=
null
;
this
.
mainWindow
=
null
;
this
.
lastError
=
null
;
}
/** Apply new settings; reconnects if running so changes take effect at once. */
configure
(
options
=
{
}
)
{
const
wasRunning
=
this
.
running
;
if
(
wasRunning
)
this
.
stop
(
)
;
this
.
config
=
{
...
this
.
config
,
...
options
}
;
if
(
wasRunning
)
this
.
start
(
)
;
return
this
.
config
;
}
getConfig
(
)
{
return
{
...
this
.
config
}
;
}
setMainWindow
(
window
)
{
this
.
mainWindow
=
window
;
}
isConnected
(
)
{
return
this
.
socket
!==
null
&&
!
this
.
socket
.
destroyed
;
}
status
(
)
{
return
{
running
:
this
.
running
,
connected
:
this
.
isConnected
(
)
,
lastWeight
:
this
.
lastReported
,
lastError
:
this
.
lastError
,
config
:
this
.
getConfig
(
)
,
}
;
}
start
(
)
{
if
(
this
.
running
)
return
;
this
.
running
=
true
;
this
.
lastError
=
null
;
this
.
_connect
(
)
;
}
stop
(
)
{
this
.
running
=
false
;
if
(
this
.
reconnectTimer
)
{
clearTimeout
(
this
.
reconnectTimer
)
;
this
.
reconnectTimer
=
null
;
}
if
(
this
.
socket
)
{
this
.
socket
.
destroy
(
)
;
this
.
socket
=
null
;
}
this
.
weightHistory
=
[
]
;
}
_log
(
...
args
)
{
if
(
this
.
config
.
debug
)
console
.
log
(
'[EssaeScale]'
,
...
args
)
;
}
_connect
(
)
{
if
(
!
this
.
running
)
return
;
this
.
_log
(
`connecting to
${
this
.
config
.
ip
}
:
${
this
.
config
.
port
}
`
)
;
this
.
socket
=
new
net
.
Socket
(
)
;
this
.
socket
.
setNoDelay
(
true
)
;
this
.
socket
.
on
(
'error'
,
(
err
)
=>
{
this
.
lastError
=
err
.
message
;
this
.
_log
(
'socket error:'
,
err
.
message
)
;
}
)
;
this
.
socket
.
on
(
'close'
,
(
)
=>
{
if
(
!
this
.
running
)
return
;
this
.
_log
(
'connection closed, retrying'
)
;
this
.
_scheduleReconnect
(
)
;
}
)
;
this
.
socket
.
connect
(
this
.
config
.
port
,
this
.
config
.
ip
,
(
)
=>
{
this
.
lastError
=
null
;
this
.
_log
(
'connected'
)
;
this
.
_pollLoop
(
)
;
}
)
;
}
_scheduleReconnect
(
)
{
if
(
this
.
reconnectTimer
||
!
this
.
running
)
return
;
if
(
this
.
socket
)
{
this
.
socket
.
destroy
(
)
;
this
.
socket
=
null
;
}
this
.
reconnectTimer
=
setTimeout
(
(
)
=>
{
this
.
reconnectTimer
=
null
;
this
.
_connect
(
)
;
}
,
this
.
config
.
reconnectDelayMs
)
;
}
async
_pollLoop
(
)
{
while
(
this
.
running
&&
this
.
isConnected
(
)
)
{
try
{
await
this
.
_pollWeight
(
)
;
await
this
.
_sleep
(
this
.
config
.
pollIntervalMs
)
;
}
catch
(
err
)
{
this
.
lastError
=
err
.
message
;
this
.
_log
(
'poll failed:'
,
err
.
message
)
;
this
.
_scheduleReconnect
(
)
;
return
;
}
}
}
async
_pollWeight
(
)
{
if
(
!
this
.
isConnected
(
)
)
return
;
// Ask for both frame types; the scale answers with whichever it supports.
this
.
socket
.
write
(
CMD_SIMPLE
)
;
await
this
.
_sleep
(
this
.
config
.
pollIntervalMs
)
;
let
data
=
(
await
this
.
_readWithTimeout
(
this
.
config
.
readTimeoutMs
)
)
||
Buffer
.
alloc
(
0
)
;
this
.
socket
.
write
(
CMD_EXT
)
;
await
this
.
_sleep
(
50
)
;
const
extData
=
await
this
.
_readWithTimeout
(
this
.
config
.
readTimeoutMs
)
;
if
(
extData
)
data
=
Buffer
.
concat
(
[
data
,
extData
]
)
;
if
(
!
data
.
length
)
return
;
this
.
_log
(
'raw:'
,
data
.
toString
(
'hex'
)
.
slice
(
0
,
80
)
)
;
const
extFrame
=
this
.
_extendedFrame
(
data
)
;
if
(
extFrame
&&
extFrame
.
length
>=
4
)
{
try
{
this
.
_processWeight
(
Math
.
round
(
extFrame
.
readFloatLE
(
0
)
*
1000
)
/
1000
)
;
return
;
}
catch
(
err
)
{
this
.
_log
(
'extended parse failed:'
,
err
.
message
)
;
}
}
const
simple
=
this
.
_parseSimple66
(
data
)
;
if
(
simple
!==
null
)
{
this
.
_processWeight
(
simple
)
;
return
;
}
this
.
_log
(
'no known frame in:'
,
data
.
slice
(
0
,
40
)
.
toString
(
'hex'
)
)
;
}
_readWithTimeout
(
timeoutMs
)
{
return
new
Promise
(
(
resolve
)
=>
{
if
(
!
this
.
socket
||
this
.
socket
.
destroyed
)
return
resolve
(
null
)
;
let
data
=
Buffer
.
alloc
(
0
)
;
const
onData
=
(
chunk
)
=>
{
data
=
Buffer
.
concat
(
[
data
,
chunk
]
)
;
}
;
this
.
socket
.
on
(
'data'
,
onData
)
;
setTimeout
(
(
)
=>
{
if
(
this
.
socket
)
this
.
socket
.
removeListener
(
'data'
,
onData
)
;
resolve
(
data
.
length
?
data
:
null
)
;
}
,
timeoutMs
)
;
}
)
;
}
_extendedFrame
(
data
)
{
const
idx
=
data
.
indexOf
(
Buffer
.
from
(
[
0x22
,
0x03
]
)
)
;
if
(
idx
<
0
||
data
.
length
<
idx
+
6
)
return
null
;
const
length
=
data
[
idx
+
2
]
+
(
data
[
idx
+
3
]
<<
8
)
;
const
end
=
idx
+
6
+
length
+
2
;
return
data
.
length
>=
end
?
data
.
slice
(
idx
+
6
,
end
-
2
)
:
null
;
}
_parseSimple66
(
data
)
{
const
idx
=
data
.
indexOf
(
0x66
)
;
if
(
idx
<
0
||
data
.
length
<
idx
+
6
)
return
null
;
try
{
const
grams
=
data
.
slice
(
idx
,
idx
+
6
)
.
readUInt16LE
(
2
)
;
return
Math
.
round
(
(
grams
/
1000
)
*
1000
)
/
1000
;
}
catch
{
return
null
;
}
}
_processWeight
(
kg
)
{
if
(
!
Number
.
isFinite
(
kg
)
)
return
;
this
.
weightHistory
.
push
(
kg
)
;
if
(
this
.
weightHistory
.
length
>
this
.
config
.
historySize
)
this
.
weightHistory
.
shift
(
)
;
// Average of the last two readings: the device jitters by a gram or two
// and billing should not chase that.
const
n
=
this
.
weightHistory
.
length
;
const
value
=
n
>=
2
?
Math
.
round
(
(
(
this
.
weightHistory
[
n
-
1
]
+
this
.
weightHistory
[
n
-
2
]
)
/
2
)
*
1000
)
/
1000
:
Math
.
round
(
kg
*
1000
)
/
1000
;
if
(
this
.
lastReported
!==
null
&&
Math
.
abs
(
value
-
this
.
lastReported
)
<
this
.
config
.
minChangeKg
)
return
;
this
.
lastReported
=
value
;
this
.
_broadcast
(
value
)
;
}
_broadcast
(
weight
)
{
// Same channel and shape the serial scale uses, so the sales screen does
// not care which kind of scale the shop has.
const
payload
=
{
raw
:
`Essae
${
weight
}
kg`
,
weight
,
stable
:
true
,
// the device reports settled values
source
:
'essae-tcp'
,
timestamp
:
new
Date
(
)
.
toISOString
(
)
,
isValid
:
true
,
}
;
for
(
const
win
of
BrowserWindow
.
getAllWindows
(
)
)
{
if
(
win
.
isDestroyed
(
)
)
continue
;
try
{
win
.
webContents
.
send
(
'usb-data'
,
payload
)
;
}
catch
(
err
)
{
this
.
_log
(
'broadcast failed:'
,
err
.
message
)
;
}
}
}
_sleep
(
ms
)
{
return
new
Promise
(
(
resolve
)
=>
setTimeout
(
resolve
,
ms
)
)
;
}
}
module
.
exports
=
{
EssaeWeightReader
,
ESSAE_DEFAULTS
:
DEFAULTS
}
;
You can’t perform that action at this time.