-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposapi.go
152 lines (121 loc) · 3.31 KB
/
posapi.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
package ebarimt
/*
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
*/
import (
"C"
)
import (
"encoding/json"
"fmt"
"sync"
"unsafe"
"github.com/peakdot/dl"
)
type PosAPI struct {
Lib *dl.DL
checkAPI func() *C.char
getInformation func() *C.char
callFunction func(function *C.char, params *C.char) *C.char
put func(params *C.char) *C.char
returnBill func(params *C.char) *C.char
sendData func() *C.char
mux sync.Mutex
}
func Open(path ...string) (*PosAPI, error) {
so_path := "/usr/lib/libPosAPI.so"
if len(path) > 0 {
so_path = path[0]
}
lib, err := dl.Open(so_path, dl.RTLD_LAZY)
if err != nil {
fmt.Println("err", err.Error())
return nil, err
}
var checkAPIC func() *C.char
lib.Sym("checkApi", &checkAPIC)
var getInformationC func() *C.char
lib.Sym("getInformation", &getInformationC)
var callFunctionC func(function *C.char, params *C.char) *C.char
lib.Sym("callFunction", &callFunctionC)
var putC func(params *C.char) *C.char
lib.Sym("put", &putC)
var returnBillC func(params *C.char) *C.char
lib.Sym("returnBill", &returnBillC)
var sendDataC func() *C.char
lib.Sym("sendData", &sendDataC)
return &PosAPI{
Lib: lib,
checkAPI: checkAPIC,
getInformation: getInformationC,
callFunction: callFunctionC,
put: putC,
returnBill: returnBillC,
sendData: sendDataC,
}, nil
}
func (p *PosAPI) Close() {
p.Lib.Close()
}
func (p *PosAPI) CheckAPI() (*APIOutput, error) {
cResult := p.checkAPI()
defer C.free(unsafe.Pointer(cResult))
var response APIOutput
if err := json.Unmarshal([]byte(C.GoString(cResult)), &response); err != nil {
return nil, err
}
return &response, nil
}
func (p *PosAPI) GetInformation() (*InformationOutput, error) {
var response InformationOutput
cResult := p.getInformation()
defer C.free(unsafe.Pointer(cResult))
if err := json.Unmarshal([]byte(C.GoString(cResult)), &response); err != nil {
return nil, err
}
return &response, nil
}
func (p *PosAPI) CallFunction(function string, params string) string {
// var response
cParams := C.CString(params)
defer C.free(unsafe.Pointer(cParams))
cFunction := C.CString(function)
defer C.free(unsafe.Pointer(cFunction))
cResult := p.callFunction(cFunction, cParams)
defer C.free(unsafe.Pointer(cResult))
result := C.GoString(cResult)
return result
}
func (p *PosAPI) Put(params string) (*PutOutput, error) {
var response PutOutput
cParams := C.CString(params)
defer C.free(unsafe.Pointer(cParams))
cResult := p.put(cParams)
defer C.free(unsafe.Pointer(cParams))
if err := json.Unmarshal([]byte(C.GoString(cResult)), &response); err != nil {
return nil, err
}
return &response, nil
}
func (p *PosAPI) ReturnBill(params string) (*BillOutput, error) {
cParams := C.CString(params)
defer C.free(unsafe.Pointer(cParams))
cResult := p.returnBill(cParams)
defer C.free(unsafe.Pointer(cParams))
var response BillOutput
if err := json.Unmarshal([]byte(C.GoString(cResult)), &response); err != nil {
return nil, err
}
return &response, nil
}
func (p *PosAPI) SendData() (*DataOutput, error) {
var response DataOutput
cResult := p.sendData()
defer C.free(unsafe.Pointer(cResult))
if err := json.Unmarshal([]byte(C.GoString(cResult)), &response); err != nil {
return nil, err
}
return &response, nil
}