Skip to content

Commit

Permalink
Optimize the serial port reading process
Browse files Browse the repository at this point in the history
  • Loading branch information
bclswl0827 committed Apr 26, 2024
1 parent 1f0e2f3 commit a124a14
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Starting from v2.2.5, all notable changes to this project will be documented in this file.

## v2.12.2

- Always use fallback locale if the preferred locale is not available
- Optimize the serial port reading process

## v2.12.1

- Sort earthquake event source API by name in frontend
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.12.1
v2.12.2
15 changes: 11 additions & 4 deletions driver/serial/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import (
"bytes"
"fmt"
"io"
"math"
"time"
)

func Filter(port io.ReadWriteCloser, signature []byte, retry int) ([]byte, error) {
func Filter(port io.ReadWriteCloser, signature []byte) ([]byte, error) {
header := make([]byte, len(signature))

for i := 0; i < retry; i++ {
port.Read(header)
for i := 0; i < math.MaxUint8; i++ {
_, err := port.Read(header)
if err != nil {
return nil, err
}

if bytes.Equal(header, signature) {
return nil, nil
return header, nil
} else {
time.Sleep(time.Millisecond)
}
}

Expand Down
6 changes: 3 additions & 3 deletions driver/serial/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

func Open(device string, baud int) (io.ReadWriteCloser, error) {
port, err := serial.Open(device,
serial.WithHUPCL(false),
serial.WithHUPCL(true),
serial.WithDataBits(8),
serial.WithBaudrate(baud),
serial.WithReadTimeout(500),
serial.WithWriteTimeout(500),
serial.WithReadTimeout(5),
serial.WithWriteTimeout(5),
serial.WithParity(serial.NoParity),
serial.WithStopBits(serial.OneStopBit),
)
Expand Down
2 changes: 1 addition & 1 deletion feature/geophone/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func (g *Geophone) Read(port io.ReadWriteCloser, conf *config.Conf, packet *Packet, packetLen int) error {
// Filter frame header
_, err := serial.Filter(port, SYNC_WORD[:], 128)
_, err := serial.Filter(port, SYNC_WORD[:])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion feature/geophone/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func (g *Geophone) Reset(port io.ReadWriteCloser) error {
return err
}

serial.Filter(port, ACK_WORD[:], 128)
serial.Filter(port, ACK_WORD[:])
return nil
}
4 changes: 2 additions & 2 deletions frontend/dist/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": {
"main.css": "./static/css/main.7a040865.css",
"main.js": "./static/js/main.9e9bedf8.js",
"main.js": "./static/js/main.c4da8e75.js",
"static/css/594.d6bfd15f.chunk.css": "./static/css/594.d6bfd15f.chunk.css",
"static/js/594.7755917b.chunk.js": "./static/js/594.7755917b.chunk.js",
"static/js/846.88ce4ddb.chunk.js": "./static/js/846.88ce4ddb.chunk.js",
Expand Down Expand Up @@ -49,6 +49,6 @@
},
"entrypoints": [
"static/css/main.7a040865.css",
"static/js/main.9e9bedf8.js"
"static/js/main.c4da8e75.js"
]
}
2 changes: 1 addition & 1 deletion frontend/dist/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><link rel="apple-touch-icon" href="./logo192.png"/><link rel="manifest" href="./manifest.json"/><style>.public-loading{width:100%;height:100%;display:flex;position:fixed;align-items:center;flex-direction:column;justify-content:center}.public-loading span{color:#1f2937;margin-top:20px;font-weight:600;font-size:1.25rem}.public-loading svg{animation:rotate 1s linear infinite}@keyframes rotate{100%{transform:rotate(360deg)}}</style><script defer="defer" src="./static/js/main.9e9bedf8.js"></script><link href="./static/css/main.7a040865.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div class="public-loading"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="#6b21a8" width="70px"><path d="M304 48a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zm0 416a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM48 304a48 48 0 1 0 0-96 48 48 0 1 0 0 96zm464-48a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM142.9 437A48 48 0 1 0 75 369.1 48 48 0 1 0 142.9 437zm0-294.2A48 48 0 1 0 75 75a48 48 0 1 0 67.9 67.9zM369.1 437A48 48 0 1 0 437 369.1 48 48 0 1 0 369.1 437z"/></svg> <span>Loading...</span></div><div id="root"></div></body></html>
<!doctype html><html><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><link rel="apple-touch-icon" href="./logo192.png"/><link rel="manifest" href="./manifest.json"/><style>.public-loading{width:100%;height:100%;display:flex;position:fixed;align-items:center;flex-direction:column;justify-content:center}.public-loading span{color:#1f2937;margin-top:20px;font-weight:600;font-size:1.25rem}.public-loading svg{animation:rotate 1s linear infinite}@keyframes rotate{100%{transform:rotate(360deg)}}</style><script defer="defer" src="./static/js/main.c4da8e75.js"></script><link href="./static/css/main.7a040865.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div class="public-loading"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="#6b21a8" width="70px"><path d="M304 48a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zm0 416a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM48 304a48 48 0 1 0 0-96 48 48 0 1 0 0 96zm464-48a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM142.9 437A48 48 0 1 0 75 369.1 48 48 0 1 0 142.9 437zm0-294.2A48 48 0 1 0 75 75a48 48 0 1 0 67.9 67.9zM369.1 437A48 48 0 1 0 437 369.1 48 48 0 1 0 369.1 437z"/></svg> <span>Loading...</span></div><div id="root"></div></body></html>
2 changes: 0 additions & 2 deletions frontend/dist/static/js/main.9e9bedf8.js

This file was deleted.

2 changes: 2 additions & 0 deletions frontend/dist/static/js/main.c4da8e75.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions frontend/src/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
REACT_APP_VERSION=v2.12.1
REACT_APP_RELEASE=7133eaa1-20240409233944
REACT_APP_VERSION=v2.12.2
REACT_APP_RELEASE=1f0e2f3d-20240426130534
2 changes: 1 addition & 1 deletion frontend/src/src/helpers/app/getRelease.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const getRelease = () => {
return process.env.REACT_APP_RELEASE || "unknown";
return process.env.REACT_APP_RELEASE ?? "unknown";
};
2 changes: 1 addition & 1 deletion frontend/src/src/helpers/app/getVersion.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const getVersion = () => {
return process.env.REACT_APP_VERSION || "unknown";
return process.env.REACT_APP_VERSION ?? "custom build";
};
12 changes: 6 additions & 6 deletions frontend/src/src/helpers/i18n/getCurrentLocale.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { i18n } from "i18next";
import { i18nConfig } from "../../config/i18n";

export const getCurrentLocale = async (i18n: Promise<i18n>) => {
const lang = (await i18n).language;
if (lang === "en") {
return "en-US";
}

return lang;
const currentLang = (await i18n).language;
const availableLocales = Object.keys(i18nConfig.resources);
return availableLocales.includes(currentLang)
? currentLang
: i18nConfig.fallback;
};
9 changes: 7 additions & 2 deletions frontend/src/src/helpers/i18n/setUserLocale.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { i18n } from "i18next";
import { i18nConfig } from "../../config/i18n";

export const setUserLocale = async (i18n: Promise<i18n>, lang: string) =>
(await i18n).changeLanguage(lang);
export const setUserLocale = async (i18n: Promise<i18n>, lang: string) => {
const availableLocales = Object.keys(i18nConfig.resources);
(await i18n).changeLanguage(
availableLocales.includes(lang) ? lang : i18nConfig.fallback
);
};

0 comments on commit a124a14

Please sign in to comment.