Skip to content

Commit

Permalink
refactoring [v0.3.4]
Browse files Browse the repository at this point in the history
 methods end
  • Loading branch information
SUNsung committed Sep 11, 2024
1 parent 24edfa2 commit 5fba1ab
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 214 deletions.
4 changes: 2 additions & 2 deletions 0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ func TestConnect(t *testing.T) {
t.Log(a.Method.Device.Get.SerialNumber())
t.Log(a.Method.Bill.Get.Self())
t.Log(a.Method.Application.Get.PN())
t.Log(a.Method.Get.BootPN())
t.Log(a.Method.Other.GetBootPN())
t.Log(a.Method.Device.Get.Type())

t.Log(a.Method.Get.BNFStatus().String())
t.Log(a.Method.BNF.Get.Status().String())

default:
continue
Expand Down
219 changes: 7 additions & 212 deletions methods.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package mpost

import (
"errors"
"github.com/hard-soft-ware/mpost/acceptor"
"github.com/hard-soft-ware/mpost/consts"
"github.com/hard-soft-ware/mpost/enum"
"github.com/hard-soft-ware/mpost/hook"
"time"
)

////////////////////////////////////

type MethodsObj struct {
a *MpostObj

Get MethodsGetObj
Set MethodsSetObj

Enable *MethodsEnableObj
Application *MethodsApplicationObj
Audit *MethodsAuditObj
Expand All @@ -27,22 +15,16 @@ type MethodsObj struct {
Device *MethodsDeviceObj
Orientation *MethodsOrientationObj
Variant *MethodsVariantObj
Timeout *MethodsTimeoutObj
BNF *MethodsBNFObj

Timeout *MethodsTimeoutObj
Other *MethodsOtherObj
}

type MethodsGetObj struct{ a *MpostObj }

type MethodsSetObj struct{ a *MpostObj }

////

func (a *MpostObj) newMethods() *MethodsObj {
obj := MethodsObj{}

obj.a = a
obj.Get.a = a
obj.Set.a = a
obj := MethodsObj{a: a}

obj.Enable = obj.newEnable()
obj.Application = obj.newApplication()
Expand All @@ -54,199 +36,12 @@ func (a *MpostObj) newMethods() *MethodsObj {
obj.Device = obj.newDevice()
obj.Orientation = obj.newOrientation()
obj.Variant = obj.newVariant()

obj.Timeout = obj.newTimeout()
obj.BNF = obj.newBNF()

obj.Other = obj.newOther()

return &obj
}

////////////////

func (m *MethodsObj) Calibrate() {
m.a.Log.Method("Calibrate", nil)
if !acceptor.Connected {
m.a.Log.Err("Calibrate", errors.New("Calibrate called when not connected"))
return
}

if acceptor.Device.State != enum.StateIdling {
m.a.Log.Err("Calibrate", errors.New("Calibrate allowed only when DeviceState == Idling"))
return
}

payload := []byte{consts.CmdCalibrate.Byte(), 0x00, 0x00, 0x00}

acceptor.SuppressStandardPoll = true
acceptor.Device.State = enum.StateCalibrateStart

hook.Raise.Calibrate.Start()

hook.CalibrateProgress = true

startTickCount := time.Now()

for {
reply, err := m.a.SendSynchronousCommand(payload)
if err != nil {
m.a.Log.Err("Calibrate", errors.New("Failed to send synchronous command during calibration"))
return
}

if len(reply) == 11 && (reply[2]&0x70) == 0x40 {
break
}

if time.Since(startTickCount) > CalibrateTimeout {
hook.Raise.Calibrate.Finish()
return
}
}
}

////

func (m *MethodsGetObj) Connected() bool {
m.a.Log.Method("GetConnected", nil)
return acceptor.Connected
}

func (m *MethodsGetObj) DocType() enum.DocumentType {
m.a.Log.Method("GetDocType", nil)
return m.a.DocType
}

func (m *MethodsGetObj) Version() string {
m.a.Log.Method("GetVersion", nil)
return acceptor.Version
}

func (m *MethodsGetObj) AutoStack() bool {
m.a.Log.Method("GetAutoStack", nil)
return acceptor.AutoStack
}

func (m *MethodsGetObj) HighSecurity() bool {
m.a.Log.Method("GetHighSecurity", nil)
return acceptor.HighSecurity
}

func (m *MethodsGetObj) BNFStatus() enum.BNFStatusType {
m.a.Log.Method("Getting BNF status", nil)
err := acceptor.Verify(acceptor.Cap.BNFStatus, "BNFStatus")

if err != nil {
m.a.Log.Err("GetBNFStatus", err)
return enum.BNFStatusUnknown
}

payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxBNFStatus.Byte()}

reply, err := m.a.SendSynchronousCommand(payload)
if err != nil {
m.a.Log.Err("GetBNFStatus", err)
return enum.BNFStatusUnknown
}

if len(reply) == 9 {
if reply[3] == 0 {
return enum.BNFStatusNotAttached
} else {
if reply[4] == 0 {
return enum.BNFStatusOK
} else {
return enum.BNFStatusError
}
}
}

return enum.BNFStatusUnknown
}

func (m *MethodsGetObj) BootPN() string {
m.a.Log.Method("GetBootPN", nil)

err := acceptor.Verify(acceptor.Cap.BootPN, "GetBootPN")
if err != nil {
m.a.Log.Err("GetBootPN", err)
return ""
}

payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorBootPartNumber.Byte()}

reply, err := m.a.SendSynchronousCommand(payload)
if err != nil {
m.a.Log.Err("GetBootPN", err)
return ""
}

if len(reply) == 14 {
s := string(reply[3:12]) // Extracting the substring from byte slice
return s
}

return ""
}

//

func (m *MethodsGetObj) CapAssetNumber() bool {
m.a.Log.Method("GetCapAssetNumber", nil)
return acceptor.Cap.AssetNumber
}

func (m *MethodsGetObj) CapEscrowTimeout() bool {
m.a.Log.Method("GetCapEscrowTimeout", nil)
return acceptor.Cap.EscrowTimeout
}

func (m *MethodsGetObj) CapFlashDownload() bool {
m.a.Log.Method("GetCapFlashDownload", nil)
return acceptor.Cap.FlashDownload
}

func (m *MethodsGetObj) CapPupExt() bool {
m.a.Log.Method("GetCapPupExt", nil)
return acceptor.Cap.PupExt
}

func (m *MethodsGetObj) CapTestDoc() bool {
m.a.Log.Method("GetCapTestDoc", nil)
return acceptor.Cap.TestDoc
}

func (m *MethodsGetObj) CapBNFStatus() bool {
m.a.Log.Method("GetCapBNFStatus", nil)
return acceptor.Cap.BNFStatus
}

func (m *MethodsGetObj) CapCalibrate() bool {
m.a.Log.Method("GetCapCalibrate", nil)
return acceptor.Cap.Calibrate
}

func (m *MethodsGetObj) CapBookmark() bool {
m.a.Log.Method("GetCapBookmark", nil)
return acceptor.Cap.Bookmark
}

func (m *MethodsGetObj) CapNoPush() bool {
m.a.Log.Method("GetCapNoPush", nil)
return acceptor.Cap.NoPush
}

func (m *MethodsGetObj) CapBootPN() bool {
m.a.Log.Method("GetCapBootPN", nil)
return acceptor.Cap.BootPN
}

////

func (m *MethodsSetObj) AutoStack(v bool) {
m.a.Log.Method("SetAutoStack", nil)
acceptor.AutoStack = v
}

func (m *MethodsSetObj) HighSecurity(v bool) {
m.a.Log.Method("SetHighSecurity", nil)
acceptor.HighSecurity = v
}
66 changes: 66 additions & 0 deletions methods_BNF.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package mpost

import (
"github.com/hard-soft-ware/mpost/acceptor"
"github.com/hard-soft-ware/mpost/consts"
"github.com/hard-soft-ware/mpost/enum"
)

////////////////////////////////////

type MethodsBNFObj struct {
a *MpostObj
Get MethodsBNFGetObj
}

type MethodsBNFGetObj struct{ a *MpostObj }

func (m *MethodsObj) newBNF() *MethodsBNFObj {
obj := MethodsBNFObj{}

obj.a = m.a
obj.Get.a = m.a

return &obj
}

////////////////

func (m *MethodsBNFGetObj) Status() enum.BNFStatusType {
m.a.Log.Method("Getting BNF status", nil)
err := acceptor.Verify(acceptor.Cap.BNFStatus, "BNFStatus")

if err != nil {
m.a.Log.Err("GetBNFStatus", err)
return enum.BNFStatusUnknown
}

payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxBNFStatus.Byte()}

reply, err := m.a.SendSynchronousCommand(payload)
if err != nil {
m.a.Log.Err("GetBNFStatus", err)
return enum.BNFStatusUnknown
}

if len(reply) == 9 {
if reply[3] == 0 {
return enum.BNFStatusNotAttached
} else {
if reply[4] == 0 {
return enum.BNFStatusOK
} else {
return enum.BNFStatusError
}
}
}

return enum.BNFStatusUnknown
}

//

func (m *MethodsBNFGetObj) CapStatus() bool {
m.a.Log.Method("GetCapBNFStatus", nil)
return acceptor.Cap.BNFStatus
}
Loading

0 comments on commit 5fba1ab

Please sign in to comment.