Skip to content

Commit

Permalink
up to 1.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
thun888 committed Oct 5, 2023
1 parent a639024 commit b189d04
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ COPY --from=builder /app/main /app/main

EXPOSE 6789

CMD ["./main","--config=/app/data/config.json","--basedirectory=/app/data/"]
CMD ["./main","--config=/app/data/config.json","--basedirectory=/app/data/","--databasepath=/app/data/database.db"]
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

## Mirouter-ui

> 🕊️ 基于小米路由器API的展示面板
> 😎 基于小米路由器API的展示面板
[![Docker Pulls](https://img.shields.io/docker/pulls/thun888/mirouter-ui)](https://hub.docker.com/r/thun888/mirouter-ui)
[![Release And Docker](https://github.com/Mirouterui/mirouter-ui/actions/workflows/buildapp.yml/badge.svg)](https://github.com/Mirouterui/mirouter-ui/actions/workflows/buildapp.yml)
[![Build DEV version](https://github.com/Mirouterui/mirouter-ui/actions/workflows/buildapp-dev.yml/badge.svg)](https://github.com/Mirouterui/mirouter-ui/actions/workflows/buildapp-dev.yml)

将本程序部署在小米路由器的网络环境中,配置完成即可食用

后端基于Golang,多平台兼容
后端基于`Golang`,多平台兼容

已在小米路由器R1D,R4A上测试通过

部分新路由无法获取cpu占用,如红米AX6000,AX1800。可在路由器上运行解决
部分新路由无法获取cpu占用,如红米AX6000,AX1800。可在路由器上运行解决

## 图片展示

Expand All @@ -38,7 +39,9 @@

![Snipaste_2023-08-25_13-33-54](https://github.com/Mirouterui/mirouter-ui/assets/63234268/0926dafd-a63e-4ee6-bc61-f381c1dfc199)

#### 历史数据统计

![history_index](./otherfile/images/history_index.png)
## 部署

### Docker
Expand Down Expand Up @@ -72,7 +75,7 @@

复制双引号里的内容粘贴到`config.json`对应栏目中,并填上密码(路由器后台密码)

![image](https://github.com/Mirouterui/mirouter-ui/assets/63234268/e2f1a583-e271-484d-9597-e4beecdfeff4)
![image](./otherfile/images/config.png)


> config.json 会在初次运行时自动下载
Expand All @@ -83,7 +86,9 @@
| 配置名 | 默认值 | 解释 |
| ------ | ------ | ------------------------------------------------------------ |
| dev | [] | 路由器信息,参阅`dev项` |
| history | | 历史记录相关功能,参阅`history项` |
| tiny | false | 启用后,不再下载静态文件,需搭配[在线前端](http://mrui.hzchu.top:8880/)使用 |
| flushTokenTime | 1800 | 刷新token时间间隔(s) |
| port | 6789 | 网页页面端口号 |
| debug | true | debug模式,建议在测试正常后关闭 |

Expand All @@ -96,13 +101,21 @@
| ip | 192.168.31.1 | 路由器IP |
| routerunit | false | 启用后,程序通过`gopsutil`库获取CPU占用 |

**history**项:

| 配置名 | 默认值 | 解释 |
| ---------- | -------------------------------- | --------------------------------------- |
| enable | false | 是否启用历史数据统计 |
| sampletime | 300 | 采样时间间隔(s) |
| maxsaved | 8640 | 最多记录条数 |

命令行参数:

| 参数 | 解释 |
| --------------- | -------------------------------- |
| --config | 配置文件路径 |
| --config | 配置文件路径,默认为“./config.json” |
| --basedirectory | 基础目录路径,在里面存放静态文件 |

| --databasepath | 数据库路径,默认为“./database.db” |


然后运行程序
Expand All @@ -121,6 +134,16 @@

[windows守护进程工具--nssm详解 - 与f - 博客园 (cnblogs.com)](https://www.cnblogs.com/fps2tao/p/16433588.html)

### Todo

- [x] 历史数据统计
- [x] 深色模式
- [x] 多路由支持
- [x] 快捷更新
- [ ] 设备小工具

> 主要功能已完成开发,接下来随缘更新😶‍🌫️
## Stars~

[![Stars~](https://starchart.cc/mirouterui/mirouter-ui.svg)](https://starchart.cc/mirouterui/mirouter-ui)
Expand Down
6 changes: 2 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"routerunit": false
}],
"history": {
"enable": true,
"enable": false,
"sampletime": 300,
"maxdeleted": 360,
"databasepath": "./database.db"

"maxsaved": 8640
},
"flushTokenTime": 1800,
"tiny": false,
Expand Down
30 changes: 24 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
Version string
databasepath string
flushTokenTime int64
maxdeleted int64
maxsaved int64
historyEnable bool
sampletime int64
)
Expand All @@ -62,7 +62,7 @@ type Config struct {
}

func init() {
dev, debug, port, tiny, basedirectory, databasepath, flushTokenTime, maxdeleted, historyEnable, sampletime = config.GetConfigInfo()
dev, debug, port, tiny, basedirectory, flushTokenTime, databasepath, maxsaved, historyEnable, sampletime = config.GetConfigInfo()
tokens = make(map[int]string)
routerNames = make(map[int]string)
hardwares = make(map[int]string)
Expand All @@ -78,7 +78,12 @@ func getconfig(c echo.Context) error {
IP string `json:"ip"`
RouterUnit bool `json:"routerunit"`
}

type History struct {
Enable bool `json:"enable"`
MaxDeleted int64 `json:"maxsaved"`
Databasepath string `json:"databasepath"`
Sampletime int64 `json:"sampletime"`
}
devsNoPassword := []DevNoPassword{}
for _, d := range dev {
devNoPassword := DevNoPassword{
Expand All @@ -88,14 +93,21 @@ func getconfig(c echo.Context) error {
}
devsNoPassword = append(devsNoPassword, devNoPassword)
}
history := History{}
history.Enable = historyEnable
history.MaxDeleted = maxsaved
history.Databasepath = databasepath
history.Sampletime = sampletime
return c.JSON(http.StatusOK, map[string]interface{}{
"code": 0,
"tiny": tiny,
"port": port,
"debug": debug,
// "token": token,
"dev": devsNoPassword,
"ver": Version,
"dev": devsNoPassword,
"history": history,
"flushTokenTime": flushTokenTime,
"ver": Version,
})
}

Expand Down Expand Up @@ -192,6 +204,12 @@ func main() {
if err != nil {
return c.JSON(http.StatusOK, map[string]interface{}{"code": 1100, "msg": "参数错误"})
}
if !historyEnable {
return c.JSON(http.StatusOK, map[string]interface{}{
"code": 1101,
"msg": "历史数据未开启",
})
}
history := database.Getdata(databasepath, routernum)
return c.JSON(http.StatusOK, history)
})
Expand Down Expand Up @@ -244,7 +262,7 @@ func main() {
if historyEnable {
go func() {
for range time.Tick(time.Duration(sampletime) * time.Second) {
database.Savetodb(databasepath, dev, tokens, maxdeleted)
database.Savetodb(databasepath, dev, tokens, maxsaved)
}
}()
}
Expand Down
27 changes: 13 additions & 14 deletions modules/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
historyEnable bool
Version string
dev []Dev
maxdeleted int64
maxsaved int64
flushTokenTime int64
sampletime int64
)
Expand All @@ -39,10 +39,9 @@ type Dev struct {
RouterUnit bool `json:"routerunit"`
}
type History struct {
Enable bool `json:"enable"`
MaxDeleted int64 `json:"maxdeleted"`
Databasepath string `json:"databasepath"`
Sampletime int64 `json:"sampletime"`
Enable bool `json:"enable"`
MaxDeleted int64 `json:"maxsaved"`
Sampletime int64 `json:"sampletime"`
}
type Config struct {
Dev []Dev `json:"dev"`
Expand All @@ -53,18 +52,19 @@ type Config struct {
FlushTokenTime int64 `json:"flushTokenTime"`
}

func GetConfigInfo() (dev []Dev, debug bool, port int, tiny bool, basedirectory string, databasepath string, flushTokenTime int64, maxdeleted int64, historyEnable bool, sampletime int64) {
func GetConfigInfo() (dev []Dev, debug bool, port int, tiny bool, basedirectory string, flushTokenTime int64, databasepath string, maxsaved int64, historyEnable bool, sampletime int64) {
flag.StringVar(&configPath, "config", "", "配置文件路径")
flag.StringVar(&basedirectory, "basedirectory", "", "基础目录路径")
flag.StringVar(&databasepath, "databasepath", "", "数据库路径")
flag.Parse()
appPath, err := os.Executable()
checkErr(err)
if configPath == "" {
appPath, err := os.Executable()
if err != nil {
panic(err)
}
configPath = filepath.Join(filepath.Dir(appPath), "config.json")
}

if databasepath == "" {
databasepath = filepath.Join(filepath.Dir(appPath), "database.db")
}
logrus.Info("配置文件路径为:" + configPath)
data, err := os.ReadFile(configPath)
if err != nil {
Expand All @@ -91,8 +91,7 @@ func GetConfigInfo() (dev []Dev, debug bool, port int, tiny bool, basedirectory
debug = config.Debug
port = config.Port
tiny = config.Tiny
databasepath = config.History.Databasepath
maxdeleted = config.History.MaxDeleted
maxsaved = config.History.MaxDeleted
historyEnable = config.History.Enable
sampletime = config.History.Sampletime
flushTokenTime = config.FlushTokenTime
Expand All @@ -113,7 +112,7 @@ func GetConfigInfo() (dev []Dev, debug bool, port int, tiny bool, basedirectory
time.Sleep(5 * time.Second)
os.Exit(1)
}
return dev, debug, port, tiny, basedirectory, databasepath, flushTokenTime, maxdeleted, historyEnable, sampletime
return dev, debug, port, tiny, basedirectory, flushTokenTime, databasepath, maxsaved, historyEnable, sampletime
}

func checkErr(err error) {
Expand Down
14 changes: 7 additions & 7 deletions modules/database/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type History struct {
gorm.Model
Ip string
RouterNum int
CPU float64
Cpu float64
Cpu_tp int
Mem float64
UpSpeed float64
Expand Down Expand Up @@ -61,8 +61,8 @@ func CheckDatabase(databasepath string) {
// - databasepath: the path to the database.
// - dev: an array of device configurations.
// - tokens: a map of token IDs to strings.
// - maxdeleted: the maximum number of records to delete.
func Savetodb(databasepath string, dev []config.Dev, tokens map[int]string, maxdeleted int64) {
// - maxsaved: the maximum number of records to delete.
func Savetodb(databasepath string, dev []config.Dev, tokens map[int]string, maxsaved int64) {
db, err := gorm.Open(sqlite.Open(databasepath), &gorm.Config{})
checkErr(err)
for i, d := range dev {
Expand All @@ -71,15 +71,15 @@ func Savetodb(databasepath string, dev []config.Dev, tokens map[int]string, maxd
cpu, cpu_tp, mem, upSpeed, downSpeed, upTotal, downTotal, deviceNum := getDeviceStats(i, tokens, ip)
var count int64
db.Model(&History{}).Where("router_num = ?", routerNum).Count(&count)
if count >= maxdeleted {
if count >= maxsaved {
logrus.Debug("删除历史数据")
db.Exec("DELETE FROM histories WHERE router_num = ? AND created_at = (SELECT MIN(created_at) FROM histories WHERE router_num = ? );", routerNum, routerNum)

}
db.Create(&History{
Ip: ip,
RouterNum: routerNum,
CPU: cpu,
Cpu: cpu,
Cpu_tp: cpu_tp,
Mem: mem,
UpSpeed: upSpeed,
Expand Down Expand Up @@ -107,8 +107,8 @@ func Getdata(databasepath string, routernum int) []History {
// - ip: The IP address of the router.
//
// Returns:
// - cpuload: The CPU load.
// - cpu_tp: The CPU temperature.
// - cpuload: The Cpu load.
// - cpu_tp: The Cpu temperature.
// - memusage: The memory usage.
// - upspeed: The upload speed.
// - downspeed: The download speed.
Expand Down
Binary file added otherfile/images/config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added otherfile/images/history_index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b189d04

Please sign in to comment.