-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvx.go
238 lines (205 loc) · 5.53 KB
/
nvx.go
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
233
234
235
236
237
238
package ssp
import (
"fmt"
"os"
"strconv"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type NVx struct {
conn *Connection
FirmwareVersion string `json:"firmware_version"`
DatasetVersion string `json:"dataset_version"`
channels []Channel
}
func DialNVx(portName string) (*NVx, error) {
conn, err := Dial(portName, 0)
if err != nil {
return nil, fmt.Errorf("failed to connect to NVx at %s: %w", portName, err)
}
return &NVx{
conn: conn,
channels: make([]Channel, 0, 5),
}, nil
}
func (nv *NVx) Close() error {
return nv.conn.Close()
}
func (nv *NVx) Init() error {
{
_, err := nv.conn.Sync()
if err != nil {
return fmt.Errorf("sync failed: %w", err)
}
}
{
_, err := nv.conn.SetChannelInhibits(0xFFFF)
if err != nil {
return fmt.Errorf("setting channel inhibits failed: %w", err)
}
}
{
version, err := nv.conn.GetFirmwareVersion()
if err != nil {
return fmt.Errorf("failed to read firmware version: %w", err)
}
nv.FirmwareVersion = version.FirmwareVersion
log.Info().Str("firmware_version", version.FirmwareVersion).Msgf("read firmware version")
}
{
version, err := nv.conn.GetDatasetVersion()
if err != nil {
return fmt.Errorf("failed to read dataset version: %w", err)
}
nv.DatasetVersion = version
log.Info().Str("dataset_version", version).Msgf("read dataset version")
}
var l zerolog.Logger
{
resp, err := nv.conn.SetupRequest()
if err != nil {
return err
}
l = log.With().
Str("country_code", resp.CountryCode).
Str("firmware_version", resp.FirmwareVersion).
Uint8("unit_type", uint8(resp.UnitType)).
Logger()
l.Info().
Msgf("setup found unit: %s", resp.UnitType.String())
if resp.UnitType != BanknoteValidator {
return fmt.Errorf("unit-type is not supported: %s", resp.UnitType)
}
setup, err := resp.BanknoteValidator()
if err != nil {
return err
}
if log.Debug().Enabled() {
supportedNotes := make([]string, len(setup.Channels))
for i := range setup.Channels {
supportedNotes[i] = strconv.Itoa(int(setup.Channels[i].Value))
}
l.Debug().
Strs("supported_notes", supportedNotes).
Uint8("num_channels", setup.NumberOfChannels).
Msg("validator set up")
}
if setup.ProtocolVersion < 6 {
// @todo check currency
log.Info().Msgf("flashed currency is %s", setup.CountryCode)
}
nv.channels = setup.Channels
}
{
resp, err := nv.conn.Enable()
if err != nil {
return err
}
log.Debug().Str("code", resp.Code.String()).Msg("enabled device")
}
return nil
}
func (nv *NVx) Poll(c chan os.Signal) {
ticker := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-c:
return
case <-ticker.C:
resp, err := nv.conn.Poll()
if err != nil {
log.Fatal().Err(err).Msg("")
}
log.Trace().Str("code", resp.Code.String()).Msg("polled")
if resp.Code != OK {
log.Fatal().Msg("poll failed")
}
if err := nv.handlePollResponse(resp); err != nil {
log.Fatal().Err(err).Msg("")
}
}
}
}
func (nv *NVx) handlePollResponse(r *PollResponse) error {
var (
channelNo uint8
)
for i := 0; i < len(r.Args); i++ {
code := PollEventResponseCode(r.Args[i])
l := log.With().Str("code", code.String()).Logger()
l.Trace().Msgf("processing poll-event")
// All events with have a channel as single byte arg
switch code {
case ReadNote: // 1 byte showing the channel of the recognised note. This will be zero if the note has not yet been recognised
fallthrough
case CreditNote: // 1 byte showing the channel of the credited note.
fallthrough
case FraudAttempt: // 1 byte showing the channel of the note that was in process when the fraud was detected. This will be zero if the note has not yet been recognised
fallthrough
case NoteClearedFromFront: // 1 byte showing the channel of the note rejected (0 if not known)
fallthrough
case NoteClearedToCashbox: // 1 byte showing the channel of the note stacked (0 if not known)
i += 1
if i > len(r.Args) {
l.Fatal().Msg("got no params, but expected one")
}
channelNo = r.Args[i]
if channelNo == 0 {
l.Trace().Msg("note has not yet been recognised")
}
}
switch code {
case SafeNoteJam: // no data
fallthrough
case UnsafeNoteJam: // no data
fallthrough
case StackerFull: // no data
fallthrough
case SlaveReset: // no data
fallthrough
case Disabled: // no data
l.Warn().Msg(code.Desc())
}
switch code {
case ReadNote:
case CreditNote:
if err := nv.handleCredit(channelNo); err != nil {
l.Warn().Err(err).Uint8("channel", channelNo).Msg("failed to handle credit")
}
case NoteRejected:
case Disabled:
}
}
return nil
}
func (nv *NVx) handleCredit(ch uint8) error {
_, disErr := nv.conn.Disable()
if disErr != nil {
log.Fatal().Err(disErr).Msg("failed to disable device")
}
chIndex := ch - 1
if chIndex > uint8(len(nv.channels)) {
log.Fatal().Msgf("ch %d > %d", ch, len(nv.channels))
}
channel := nv.channels[chIndex]
log.Info().Uint32("note_value", channel.Value).Msgf("received note")
cresp, err := nv.conn.GetCounters()
if err != nil {
log.Fatal().Err(err).Msg("")
}
if log.Debug().Enabled() {
log.Debug().
Uint8("number_of_counters_set", cresp.NumberOfCountersSet).
Uint32("notes_rejected_count", cresp.NotesRejected).
Uint32("notes_stacked_count", cresp.NotesStacked).
Uint32("notes_stored_count", cresp.NotesStored).
Uint32("notes_transferred_count", cresp.NotesTransferred).
Msg("checked counters")
}
_, enErr := nv.conn.Enable()
if enErr != nil {
log.Fatal().Err(enErr).Msg("")
}
return nil
}