Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
meshtastic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOTA
meshtastic
Commits
dd0daba6
Commit
dd0daba6
authored
1 year ago
by
echicken
Browse files
Options
Downloads
Patches
Plain Diff
Added (as yet unused) chunkText function to split long text messages across multiple payloads.
parent
82b9136c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib/device.ts
+43
-0
43 additions, 0 deletions
src/lib/device.ts
with
43 additions
and
0 deletions
src/lib/device.ts
+
43
−
0
View file @
dd0daba6
...
...
@@ -14,6 +14,49 @@ export function getRandomId(): number {
return
Math
.
floor
(
Math
.
random
()
*
4294967295
);
}
function
chunkText
(
str
:
string
):
string
[]
{
const
arr
=
str
.
trim
().
split
(
/
(\s
+
)
/
);
const
ret
:
string
[]
=
[];
let
s
:
string
=
''
;
// @ts-expect-error shut up
const
t
=
new
TextEncoder
();
for
(
let
n
=
0
;
n
<
arr
.
length
;
n
++
)
{
const
ss
=
s
+
arr
[
n
];
const
u8a
=
t
.
encode
(
ss
);
if
(
u8a
.
byteLength
<
MAX_PAYLOAD
)
{
// We are not yet at capacity
s
=
ss
;
// Grow the current string, do not append to ret since we still have space left
}
else
if
(
u8a
.
byteLength
===
MAX_PAYLOAD
)
{
// We've reached capacity,
ret
.
push
(
ss
.
trim
());
// Append current string to ret
s
=
''
;
// Reset current string
}
else
{
// Appending this word to the current string would exceed capacity
const
st
=
s
.
trim
();
if
(
st
!==
''
)
ret
.
push
(
st
);
// Append the current string to ret as long as it isn't empty
s
=
''
;
// Reset current string
const
u8a
=
t
.
encode
(
arr
[
n
]);
if
(
u8a
.
byteLength
>
MAX_PAYLOAD
)
{
// This word alone exceeds payload length, break it up
for
(
let
l
=
0
;
l
<
arr
[
n
].
length
;
l
++
)
{
// Walk it character-by-character
const
ss
=
s
+
arr
[
n
].
substring
(
l
,
l
+
1
);
// Temp value for current string + this char
const
u8a
=
t
.
encode
(
ss
);
if
(
u8a
.
byteLength
>
MAX_PAYLOAD
)
{
// If adding this char to the current string would exceed capacity
const
st
=
s
.
trim
();
if
(
st
!==
''
)
ret
.
push
(
st
);
// Push the current string to ret if non empty
s
=
arr
[
n
].
substring
(
l
,
l
+
1
);
// Set current string to this char
}
else
{
// We can add this char to the current string
s
=
ss
;
}
}
}
else
{
// Add this word to the current string
s
+=
arr
[
n
];
}
}
if
(
n
===
arr
.
length
-
1
)
{
const
st
=
s
.
trim
();
// Ditto
if
(
st
!==
''
)
ret
.
push
(
s
);
}
}
return
ret
;
}
export
default
abstract
class
Device
extends
js
.
global
.
EventEmitter
{
/** @param debug Enable debug logging (all sent and received frames, etc. - noisy) */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment