Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33-pai
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ligaishun
chain33-pai
Commits
b198d674
Commit
b198d674
authored
Sep 19, 2019
by
szh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加GetDevStatus接口
parent
3e81abf5
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
294 additions
and
94 deletions
+294
-94
Makefile
Makefile
+1
-1
main.go
main.go
+30
-44
client.go
pkg/chain33/client.go
+42
-0
util.go
pkg/util/util.go
+34
-1
pai.go
routers/api/v1/pai.go
+33
-0
router.go
routers/router.go
+42
-48
pai.go
service/pai_service/pai.go
+112
-0
No files found.
Makefile
View file @
b198d674
...
...
@@ -13,7 +13,7 @@ lint:
golint ./...
clean
:
rm
-rf
go-gin-example
rm
-rf
chain33-pai
go clean
-i
.
help
:
...
...
main.go
View file @
b198d674
...
...
@@ -4,23 +4,17 @@ import (
"fmt"
"log"
"net/http"
"bufio"
"github.com/gin-gonic/gin"
"chain33-pai/pkg/logging"
"chain33-pai/pkg/setting"
"chain33-pai/routers"
"chain33-pai/pkg/util"
"os/exec"
"io"
"strings"
"net"
"time"
)
var
cache
map
[
string
]
string
func
init
()
{
cache
=
make
(
map
[
string
]
string
,
0
)
getPaiConfig
(
"cat"
,
"/proc/cpuinfo"
,
"|"
,
"grep Serial"
)
setting
.
Setup
()
//models.Setup()
logging
.
Setup
()
...
...
@@ -35,9 +29,8 @@ func init() {
// @license.name MIT
// @license.url https://chain33-pai/blob/master/LICENSE
func
main
()
{
fmt
.
Println
(
"Serial:"
,
cache
)
gin
.
SetMode
(
setting
.
ServerSetting
.
RunMode
)
go
broadcast
()
routersInit
:=
routers
.
InitRouter
()
readTimeout
:=
setting
.
ServerSetting
.
ReadTimeout
writeTimeout
:=
setting
.
ServerSetting
.
WriteTimeout
...
...
@@ -56,6 +49,8 @@ func main() {
server
.
ListenAndServe
()
select
{}
// If you want Graceful Restart, you need a Unix system and download github.com/fvbock/endless
//endless.DefaultReadTimeOut = readTimeout
//endless.DefaultWriteTimeOut = writeTimeout
...
...
@@ -72,44 +67,34 @@ func main() {
}
func
getPaiConfig
(
command
string
,
arg
...
string
)
(
config
map
[
string
]
string
,
err
error
)
{
//获取操作系统版本信息
if
_
,
ok
:=
cache
[
"serial"
];
ok
{
return
cache
,
nil
}
list
:=
make
(
map
[
string
]
string
,
0
)
cmd
:=
exec
.
Command
(
"cat"
,
arg
...
)
stdout
,
err
:=
cmd
.
StdoutPipe
()
//fmt.Println(stdout)
func
broadcast
()
{
ip
,
err
:=
util
.
GetLocalIP
()
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
err
log
.
Fatal
(
err
)
}
defer
stdout
.
Close
()
if
err
:=
cmd
.
Start
();
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
err
// 这里设置发送者的IP地址,自己查看一下自己的IP自行设定
laddr
:=
net
.
UDPAddr
{
IP
:
ip
.
IP
,
Port
:
8804
,
}
rd
:=
bufio
.
NewReader
(
stdout
)
for
{
line
,
err
:=
rd
.
ReadString
(
'\n'
)
if
err
!=
nil
||
io
.
EOF
==
err
{
break
}
else
{
fmt
.
Println
(
"line"
,
line
)
l
:=
strings
.
Split
(
line
,
":"
)
if
len
(
l
)
==
2
{
k
:=
strings
.
ToLower
(
strings
.
TrimRight
(
strings
.
Trim
(
strings
.
Replace
(
l
[
0
],
" "
,
" "
,
-
1
),
" "
),
" "
))
v
:=
strings
.
Trim
(
strings
.
Replace
(
l
[
1
],
"
\n
"
,
""
,
-
1
),
" "
)
fmt
.
Println
(
"KEY:"
,
k
,
"VAL:"
,
v
)
if
_
,
ok
:=
list
[
k
];
!
ok
{
list
[
k
]
=
v
}
cache
[
k
]
=
v
}
}
// 这里设置接收者的IP地址为广播地址
raddr
:=
net
.
UDPAddr
{
IP
:
net
.
IPv4
(
255
,
255
,
255
,
255
),
Port
:
8804
,
}
conn
,
err
:=
net
.
DialUDP
(
"udp"
,
&
laddr
,
&
raddr
)
if
err
!=
nil
{
println
(
err
.
Error
())
return
}
return
list
,
nil
for
{
_
,
err
:=
conn
.
Write
([]
byte
(
"hello world"
))
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
log
.
Println
(
"send hello world"
)
time
.
Sleep
(
time
.
Second
)
}
}
\ No newline at end of file
pkg/chain33/client.go
0 → 100644
View file @
b198d674
package
chain33
import
(
"github.com/33cn/chain33/types"
"google.golang.org/grpc"
"context"
)
type
PaiClient
struct
{
}
var
(
paiClient
types
.
Chain33Client
paiNetgrpcAddr
=
"localhost:8802"
)
func
init
()
{
maxReceLimit
:=
grpc
.
WithMaxMsgSize
(
30
*
1024
*
1024
)
conn
,
err
:=
grpc
.
Dial
(
paiNetgrpcAddr
,
grpc
.
WithInsecure
(),
maxReceLimit
)
if
err
!=
nil
{
panic
(
err
)
}
paiClient
=
types
.
NewChain33Client
(
conn
)
}
func
(
p
*
PaiClient
)
GetWalletStatus
()
(
*
types
.
WalletStatus
,
error
)
{
return
paiClient
.
GetWalletStatus
(
context
.
Background
(),
&
types
.
ReqNil
{})
}
func
(
p
*
PaiClient
)
GetPeerInfo
()
(
*
types
.
PeerList
,
error
)
{
return
paiClient
.
GetPeerInfo
(
context
.
Background
(),
&
types
.
ReqNil
{})
}
func
(
p
*
PaiClient
)
IsNtpClockSync
()
(
*
types
.
Reply
,
error
)
{
return
paiClient
.
IsNtpClockSync
(
context
.
Background
(),
&
types
.
ReqNil
{})
}
func
(
p
*
PaiClient
)
GetNetInfo
()
(
*
types
.
NodeNetInfo
,
error
)
{
return
paiClient
.
NetInfo
(
context
.
Background
(),
&
types
.
ReqNil
{})
}
\ No newline at end of file
pkg/util/util.go
View file @
b198d674
package
util
import
"chain33-pai/pkg/setting"
import
(
"chain33-pai/pkg/setting"
"net"
"errors"
)
// Setup Initialize the util
func
Setup
()
{
jwtSecret
=
[]
byte
(
setting
.
AppSetting
.
JwtSecret
)
}
// 获取本机网卡IP
func
GetLocalIP
()
(
ipv4
*
net
.
IPNet
,
err
error
)
{
var
(
addrs
[]
net
.
Addr
addr
net
.
Addr
ipNet
*
net
.
IPNet
// IP地址
isIpNet
bool
)
// 获取所有网卡
if
addrs
,
err
=
net
.
InterfaceAddrs
();
err
!=
nil
{
return
}
// 取第一个非lo的网卡IP
for
_
,
addr
=
range
addrs
{
// 这个网络地址是IP地址: ipv4, ipv6
if
ipNet
,
isIpNet
=
addr
.
(
*
net
.
IPNet
);
isIpNet
&&
!
ipNet
.
IP
.
IsLoopback
()
{
// 跳过IPV6
if
ipNet
.
IP
.
To4
()
!=
nil
{
ipv4
=
ipNet
// 192.168.1.1
return
}
}
}
err
=
errors
.
New
(
"ERR_NO_LOCAL_IP_FOUND"
)
return
}
\ No newline at end of file
routers/api/v1/pai.go
0 → 100644
View file @
b198d674
package
v1
import
(
"github.com/gin-gonic/gin"
"net/http"
"chain33-pai/pkg/app"
"chain33-pai/pkg/e"
"chain33-pai/service/pai_service"
)
func
GetDevdetail
(
c
*
gin
.
Context
)
{
appG
:=
app
.
Gin
{
C
:
c
}
var
pai
pai_service
.
Pai
ok
:=
pai
.
GetConfig
()
if
!
ok
{
appG
.
Response
(
http
.
StatusOK
,
e
.
ERROR
,
nil
)
}
appG
.
Response
(
http
.
StatusOK
,
e
.
SUCCESS
,
map
[
string
]
interface
{}{
"serial"
:
pai
.
Serial
,
})
}
func
GetDevstatus
(
c
*
gin
.
Context
)
{
appG
:=
app
.
Gin
{
C
:
c
}
var
pai
pai_service
.
Pai
err
:=
pai
.
GetDevstatus
()
if
err
!=
nil
{
appG
.
Response
(
http
.
StatusOK
,
e
.
ERROR
,
nil
)
}
appG
.
Response
(
http
.
StatusOK
,
e
.
SUCCESS
,
pai
)
}
\ No newline at end of file
routers/router.go
View file @
b198d674
package
routers
import
(
"net/http"
"github.com/gin-gonic/gin"
_
"chain33-pai/docs"
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
"chain33-pai/middleware/jwt"
"chain33-pai/pkg/export"
"chain33-pai/pkg/qrcode"
"chain33-pai/pkg/upload"
"chain33-pai/routers/api"
"chain33-pai/routers/api/v1"
)
...
...
@@ -23,43 +12,48 @@ func InitRouter() *gin.Engine {
r
.
Use
(
gin
.
Logger
())
r
.
Use
(
gin
.
Recovery
())
r
.
StaticFS
(
"/export"
,
http
.
Dir
(
export
.
GetExcelFullPath
()))
r
.
StaticFS
(
"/upload/images"
,
http
.
Dir
(
upload
.
GetImageFullPath
()))
r
.
StaticFS
(
"/qrcode"
,
http
.
Dir
(
qrcode
.
GetQrCodeFullPath
()))
r
.
GET
(
"/auth"
,
api
.
GetAuth
)
r
.
GET
(
"/swagger/*any"
,
ginSwagger
.
WrapHandler
(
swaggerFiles
.
Handler
))
r
.
POST
(
"/upload"
,
api
.
UploadImage
)
apiv1
:=
r
.
Group
(
"/api/v1"
)
apiv1
.
Use
(
jwt
.
JWT
())
{
//获取标签列表
apiv1
.
GET
(
"/tags"
,
v1
.
GetTags
)
//新建标签
apiv1
.
POST
(
"/tags"
,
v1
.
AddTag
)
//更新指定标签
apiv1
.
PUT
(
"/tags/:id"
,
v1
.
EditTag
)
//删除指定标签
apiv1
.
DELETE
(
"/tags/:id"
,
v1
.
DeleteTag
)
//导出标签
r
.
POST
(
"/tags/export"
,
v1
.
ExportTag
)
//导入标签
r
.
POST
(
"/tags/import"
,
v1
.
ImportTag
)
//获取文章列表
apiv1
.
GET
(
"/articles"
,
v1
.
GetArticles
)
//获取指定文章
apiv1
.
GET
(
"/articles/:id"
,
v1
.
GetArticle
)
//新建文章
apiv1
.
POST
(
"/articles"
,
v1
.
AddArticle
)
//更新指定文章
apiv1
.
PUT
(
"/articles/:id"
,
v1
.
EditArticle
)
//删除指定文章
apiv1
.
DELETE
(
"/articles/:id"
,
v1
.
DeleteArticle
)
//生成文章海报
apiv1
.
POST
(
"/articles/poster/generate"
,
v1
.
GenerateArticlePoster
)
}
//r.StaticFS("/export", http.Dir(export.GetExcelFullPath()))
//r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
//r.StaticFS("/qrcode", http.Dir(qrcode.GetQrCodeFullPath()))
//r.GET("/auth", api.GetAuth)
//r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//r.POST("/upload", api.UploadImage)
apiv1
:=
r
.
Group
(
"/pai"
)
//获取树莓派基本信息
apiv1
.
POST
(
"/devdetail"
,
v1
.
GetDevdetail
)
//获取树莓派基本信息
apiv1
.
POST
(
"/devstatus"
,
v1
.
GetDevstatus
)
//apiv1.Use(jwt.JWT())
//{
//
// //获取标签列表
// apiv1.GET("/tags", v1.GetTags)
// //新建标签
// apiv1.POST("/tags", v1.AddTag)
// //更新指定标签
// apiv1.PUT("/tags/:id", v1.EditTag)
// //删除指定标签
// apiv1.DELETE("/tags/:id", v1.DeleteTag)
// //导出标签
// r.POST("/tags/export", v1.ExportTag)
// //导入标签
// r.POST("/tags/import", v1.ImportTag)
//
// //获取文章列表
// apiv1.GET("/articles", v1.GetArticles)
// //获取指定文章
// apiv1.GET("/articles/:id", v1.GetArticle)
// //新建文章
// apiv1.POST("/articles", v1.AddArticle)
// //更新指定文章
// apiv1.PUT("/articles/:id", v1.EditArticle)
// //删除指定文章
// apiv1.DELETE("/articles/:id", v1.DeleteArticle)
// //生成文章海报
// apiv1.POST("/articles/poster/generate", v1.GenerateArticlePoster)
//}
return
r
}
service/pai_service/pai.go
0 → 100644
View file @
b198d674
package
pai_service
import
(
"os/exec"
"fmt"
"io"
"strings"
"bufio"
"chain33-pai/pkg/chain33"
"chain33-pai/pkg/logging"
"github.com/33cn/chain33/types"
)
type
Pai
struct
{
Hardware
string
Revision
string
Serial
string
//PeerList *types.PeerList
LocalLastHeight
int64
LastHeight
int64
IsNtpSync
bool
WalletStatus
*
types
.
WalletStatus
NetInfo
*
types
.
NodeNetInfo
}
func
(
p
*
Pai
)
GetConfig
()
bool
{
if
p
.
Serial
!=
""
{
return
true
}
config
,
err
:=
getPaiConfig
(
"cat"
,
"/proc/cpuinfo"
)
if
err
!=
nil
{
return
false
}
if
_
,
ok
:=
config
[
"serial"
];
ok
{
p
.
Serial
=
config
[
"serial"
]
p
.
Hardware
=
config
[
"hardware"
]
p
.
Revision
=
config
[
"revision"
]
return
true
}
return
false
}
func
(
p
*
Pai
)
GetDevstatus
()
error
{
client
:=
&
chain33
.
PaiClient
{}
peerinfo
,
err
:=
client
.
GetPeerInfo
()
if
err
!=
nil
{
logging
.
Error
(
"GetDevstatus peerinfo err"
,
err
)
return
err
}
for
_
,
v
:=
range
peerinfo
.
Peers
{
if
p
.
LastHeight
<
v
.
Header
.
Height
{
p
.
LastHeight
=
v
.
Header
.
Height
}
if
v
.
Self
{
p
.
LocalLastHeight
=
v
.
Header
.
Height
}
}
sync
,
err
:=
client
.
IsNtpClockSync
()
if
err
!=
nil
{
logging
.
Error
(
"GetDevstatus IsNtpClockSync err"
,
err
)
return
err
}
p
.
IsNtpSync
=
sync
.
IsOk
netinfo
,
err
:=
client
.
GetNetInfo
()
if
err
!=
nil
{
logging
.
Error
(
"GetDevstatus GetNetInfo err"
,
err
)
return
err
}
p
.
NetInfo
=
netinfo
return
nil
}
func
getPaiConfig
(
command
string
,
arg
...
string
)
(
config
map
[
string
]
string
,
err
error
)
{
//获取操作系统版本信息
list
:=
make
(
map
[
string
]
string
,
0
)
cmd
:=
exec
.
Command
(
"cat"
,
arg
...
)
stdout
,
err
:=
cmd
.
StdoutPipe
()
//fmt.Println(stdout)
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
err
}
defer
stdout
.
Close
()
if
err
:=
cmd
.
Start
();
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
err
}
rd
:=
bufio
.
NewReader
(
stdout
)
for
{
line
,
err
:=
rd
.
ReadString
(
'\n'
)
if
err
!=
nil
||
io
.
EOF
==
err
{
break
}
else
{
fmt
.
Println
(
"line"
,
line
)
l
:=
strings
.
Split
(
line
,
":"
)
if
len
(
l
)
==
2
{
k
:=
strings
.
ToLower
(
strings
.
TrimRight
(
strings
.
Trim
(
strings
.
Replace
(
l
[
0
],
" "
,
" "
,
-
1
),
" "
),
" "
))
v
:=
strings
.
Trim
(
strings
.
Replace
(
l
[
1
],
"
\n
"
,
""
,
-
1
),
" "
)
fmt
.
Println
(
"KEY:"
,
k
,
"VAL:"
,
v
)
if
_
,
ok
:=
list
[
k
];
!
ok
{
list
[
k
]
=
v
}
}
}
}
return
list
,
nil
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment