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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
use crate::dsl::dsl_syntax::*;
use crate::dto::*;
use crate::{DivoomAPIError, DivoomAPIResult, PixooClient, PixooCommandBuilder};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "animation-builder")]
use crate::{
DivoomAnimationBuilder, DivoomAnimationResourceLoader, DivoomAnimationTemplateManager,
};
use crate::dsl::DivoomDslOperation;
#[cfg(feature = "animation-builder")]
use tiny_skia::BlendMode;
pub struct DivoomDslRunner<'a> {
device_client: &'a PixooClient,
command_builder: Option<PixooCommandBuilder>,
#[cfg(feature = "animation-builder")]
template_manager: Arc<DivoomAnimationTemplateManager>,
}
impl DivoomDslRunner<'_> {
#[cfg(feature = "animation-builder")]
pub fn new(
device_client: &PixooClient,
template_manager: Arc<DivoomAnimationTemplateManager>,
) -> DivoomDslRunner {
let command_builder = device_client.start_batch();
DivoomDslRunner {
device_client,
command_builder: Some(command_builder),
template_manager,
}
}
#[cfg(not(feature = "animation-builder"))]
pub fn new(device_client: &PixooClient) -> DivoomDslRunner {
let command_builder = device_client.start_batch();
DivoomDslRunner {
device_client,
command_builder: Some(command_builder),
}
}
pub async fn batch_operations(
&mut self,
operations: &[DivoomDslOperation],
) -> DivoomAPIResult<()> {
for operation in operations {
self.batch_operation(operation).await?;
}
Ok(())
}
pub async fn batch_operation(&mut self, operation: &DivoomDslOperation) -> DivoomAPIResult<()> {
match &operation.command {
DivoomDeviceCommand::Channel(channel_command) => {
self.batch_channel_command(operation, channel_command)
}
DivoomDeviceCommand::System(system_command) => {
self.batch_system_command(operation, system_command)
}
DivoomDeviceCommand::Tool(tool_command) => {
self.batch_tool_command(operation, tool_command)
}
DivoomDeviceCommand::Animation(animation_command) => {
self.batch_animation_command(operation, animation_command)
.await
}
DivoomDeviceCommand::Batch(batch_command) => {
self.batch_batch_command(operation, batch_command)
}
DivoomDeviceCommand::Raw { request } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.send_raw_request(request.clone()),
);
Ok(())
}
}
}
#[allow(dead_code)]
pub async fn execute(mut self) -> DivoomAPIResult<()> {
self.command_builder.take().unwrap().execute().await
}
#[allow(dead_code)]
pub(crate) fn build(mut self) -> (usize, String) {
let (_, command_count, payload) = self.command_builder.take().unwrap().build();
(command_count, payload)
}
fn batch_channel_command(
&mut self,
_: &DivoomDslOperation,
command: &DivoomDeviceChannelCommand,
) -> DivoomAPIResult<()> {
match command {
DivoomDeviceChannelCommand::Set { channel_type } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.select_channel(*channel_type),
)
}
DivoomDeviceChannelCommand::SetClock { clock_id } => {
self.command_builder =
Some(self.command_builder.take().unwrap().select_clock(*clock_id))
}
DivoomDeviceChannelCommand::SetCloudChannel { channel_type } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.select_cloud_channel(*channel_type),
)
}
DivoomDeviceChannelCommand::SetCustomPage { page_index } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.select_custom_page(*page_index),
)
}
DivoomDeviceChannelCommand::SetVisualizer { visualizer_index } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.select_visualizer(*visualizer_index),
)
}
}
Ok(())
}
fn batch_system_command(
&mut self,
_: &DivoomDslOperation,
command: &DivoomDeviceSystemCommand,
) -> DivoomAPIResult<()> {
match command {
DivoomDeviceSystemCommand::SetTime { utc } => {
self.command_builder =
Some(self.command_builder.take().unwrap().set_device_time(*utc))
}
DivoomDeviceSystemCommand::SetBrightness { brightness } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_brightness(*brightness),
)
}
DivoomDeviceSystemCommand::SetHourMode { mode } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_hour_mode(*mode),
)
}
DivoomDeviceSystemCommand::SetHighLightMode { mode } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_high_light_mode(*mode),
)
}
DivoomDeviceSystemCommand::SetMirrorMode { mode } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_mirror_mode(*mode),
)
}
DivoomDeviceSystemCommand::SetRotationAngle { mode } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_rotation_angle(*mode),
)
}
DivoomDeviceSystemCommand::SetScreenPowerState { power_state } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_screen_power_state(*power_state),
)
}
DivoomDeviceSystemCommand::SetTemperatureUnit { unit } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_temperature_unit(*unit),
)
}
DivoomDeviceSystemCommand::SetTimeZone { time_zone } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_time_zone(time_zone.to_string()),
)
}
DivoomDeviceSystemCommand::SetWeatherArea {
longitude,
latitude,
} => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_weather_area(longitude.to_string(), latitude.to_string()),
)
}
DivoomDeviceSystemCommand::SetWhiteBalance { r, g, b } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_device_white_balance(*r, *g, *b),
)
}
}
Ok(())
}
fn batch_tool_command(
&mut self,
_: &DivoomDslOperation,
command: &DivoomDeviceToolCommand,
) -> DivoomAPIResult<()> {
match command {
DivoomDeviceToolCommand::Countdown {
minute,
second,
action,
} => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_countdown_tool(*minute, *second, *action),
)
}
DivoomDeviceToolCommand::Noise { action } => {
self.command_builder =
Some(self.command_builder.take().unwrap().set_noise_tool(*action))
}
DivoomDeviceToolCommand::Scoreboard {
blue_score,
red_score,
} => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_scoreboard_tool(*blue_score, *red_score),
)
}
DivoomDeviceToolCommand::Stopwatch { action } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.set_stopwatch_tool(*action),
)
}
DivoomDeviceToolCommand::Buzzer {
play_total_time,
active_time_in_cycle,
off_time_in_cycle,
} => {
self.command_builder = Some(self.command_builder.take().unwrap().play_buzzer(
*play_total_time,
*active_time_in_cycle,
*off_time_in_cycle,
));
}
}
Ok(())
}
async fn batch_animation_command(
&mut self,
operation: &DivoomDslOperation,
command: &DivoomDeviceAnimationCommand,
) -> DivoomAPIResult<()> {
match command {
DivoomDeviceAnimationCommand::Gif(gif_animation_command) => {
self.batch_gif_animation_commands(operation, gif_animation_command)
}
DivoomDeviceAnimationCommand::Image(image_animation_command) => {
self.batch_image_animation_commands(operation, image_animation_command)
.await
}
DivoomDeviceAnimationCommand::Text(text_animation_command) => {
self.batch_text_animation_commands(operation, text_animation_command)
}
}
}
fn batch_gif_animation_commands(
&mut self,
_: &DivoomDslOperation,
command: &DivoomDeviceGifAnimationCommand,
) -> DivoomAPIResult<()> {
match command {
DivoomDeviceGifAnimationCommand::Play(gif) => {
if let Some(local_gif_file) = &gif.file {
self.command_builder =
Some(self.command_builder.take().unwrap().play_gif_file(
DivoomFileAnimationSourceType::LocalFile,
local_gif_file.clone(),
));
} else if let Some(local_gif_folder) = &gif.folder {
self.command_builder =
Some(self.command_builder.take().unwrap().play_gif_file(
DivoomFileAnimationSourceType::LocalFolder,
local_gif_folder.clone(),
));
} else if let Some(gif_url) = &gif.url {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.play_gif_file(DivoomFileAnimationSourceType::Url, gif_url.clone()),
);
} else {
return Err(DivoomAPIError::ParameterError(
"The source of GIF is not set!".into(),
));
}
}
}
Ok(())
}
async fn batch_image_animation_commands(
&mut self,
operation: &DivoomDslOperation,
image_animation_command: &DivoomDeviceImageAnimationCommand,
) -> DivoomAPIResult<()> {
match image_animation_command {
DivoomDeviceImageAnimationCommand::ResetId => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.reset_next_animation_id(),
)
}
#[cfg(feature = "animation-builder")]
DivoomDeviceImageAnimationCommand::RenderGif {
size: canvas_size,
speed_in_ms,
fit,
rotation,
opacity,
..
} => {
let animation_builder =
DivoomAnimationBuilder::new(*canvas_size, Duration::from_millis(*speed_in_ms))?;
let gif_file_data = operation.resource_loader.lock().unwrap().as_mut().next()?;
let gif = DivoomAnimationResourceLoader::from_gif_buf(&gif_file_data.data)?;
let animation = animation_builder
.draw_frames_fit(&gif, 0, *fit, *rotation, *opacity, BlendMode::default())
.build();
let animation_id = self.device_client.get_next_animation_id().await?;
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.send_image_animation(animation_id, animation),
);
}
#[cfg(feature = "animation-builder")]
DivoomDeviceImageAnimationCommand::RenderFiles {
size: canvas_size,
speed_in_ms,
fit,
rotation,
opacity,
..
} => {
let animation: DivoomImageAnimation;
let mut animation_builder =
DivoomAnimationBuilder::new(*canvas_size, Duration::from_millis(*speed_in_ms))?;
let file_resource = operation.resource_loader.lock().unwrap().as_mut().next()?;
if file_resource.name.ends_with(".gif") {
let gif = DivoomAnimationResourceLoader::from_gif_buf(&file_resource.data)?;
animation = animation_builder
.draw_frames_fit(&gif, 0, *fit, *rotation, *opacity, BlendMode::default())
.build();
} else {
let image = DivoomAnimationResourceLoader::from_image_buf(&file_resource.data)?;
animation_builder.build_frame(0).draw_frame_fit(
&image,
*fit,
*rotation,
*opacity,
BlendMode::default(),
);
animation = animation_builder.build();
}
let animation_id = self.device_client.get_next_animation_id().await?;
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.send_image_animation(animation_id, animation),
);
}
#[cfg(feature = "animation-builder")]
DivoomDeviceImageAnimationCommand::RenderTemplate {
template_name,
parameters,
per_frame_parameters,
} => {
let parsed_parameters: HashMap<String, String> = serde_json::from_str(parameters)?;
let parsed_per_frame_parameters: HashMap<usize, HashMap<String, String>> = serde_json::from_str(per_frame_parameters)?;
let animation = self
.template_manager
.render_template(template_name, &parsed_parameters, &parsed_per_frame_parameters)?;
let animation_id = self.device_client.get_next_animation_id().await?;
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.send_image_animation(animation_id, animation),
);
}
}
Ok(())
}
fn batch_text_animation_commands(
&mut self,
_: &DivoomDslOperation,
text_animation_command: &DivoomDeviceTextAnimationCommand,
) -> DivoomAPIResult<()> {
match text_animation_command {
DivoomDeviceTextAnimationCommand::Clear => {
self.command_builder =
Some(self.command_builder.take().unwrap().clear_all_text_area())
}
DivoomDeviceTextAnimationCommand::Set(text_animation) => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.send_text_animation(text_animation.as_animation()),
)
}
}
Ok(())
}
fn batch_batch_command(
&mut self,
_: &DivoomDslOperation,
batch_command: &DivoomDeviceBatchCommand,
) -> DivoomAPIResult<()> {
match batch_command {
DivoomDeviceBatchCommand::RunUrl { command_url } => {
self.command_builder = Some(
self.command_builder
.take()
.unwrap()
.execute_commands_from_url(command_url.to_string()),
)
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dsl::dsl_parser::DivoomDslParser;
use crate::PixooClient;
use std::{env, fs};
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dsl_runner_should_batch_channel_commands() {
let client = PixooClient::new("127.0.0.1").unwrap();
let mut dsl_runner = new_divoom_dsl_runner(&client);
let operations = vec![
DivoomDslParser::parse("channel set clock").unwrap(),
DivoomDslParser::parse("channel set cloud").unwrap(),
DivoomDslParser::parse("channel set visualizer").unwrap(),
DivoomDslParser::parse("channel set customPage").unwrap(),
DivoomDslParser::parse("channel set-clock 100").unwrap(),
DivoomDslParser::parse("channel set-cloud-channel gallery").unwrap(),
DivoomDslParser::parse("channel set-cloud-channel fav").unwrap(),
DivoomDslParser::parse("channel set-cloud-channel artist").unwrap(),
DivoomDslParser::parse("channel set-custom-page 2").unwrap(),
DivoomDslParser::parse("channel set-visualizer 5").unwrap(),
];
dsl_runner.batch_operations(&operations).await.unwrap();
run_dsl_runner_parser_test(
dsl_runner,
"test_data/dsl_runner_tests/channel_commands.json",
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dsl_runner_should_batch_system_commands() {
let client = PixooClient::new("127.0.0.1").unwrap();
let mut dsl_runner = new_divoom_dsl_runner(&client);
let operations = vec![
DivoomDslParser::parse("system set-brightness 80").unwrap(),
DivoomDslParser::parse("system set-time 10000").unwrap(),
DivoomDslParser::parse("system set-high-light-mode on").unwrap(),
DivoomDslParser::parse("system set-hour-mode 12h").unwrap(),
DivoomDslParser::parse("system set-mirror-mode on").unwrap(),
DivoomDslParser::parse("system set-rotation-angle 90").unwrap(),
DivoomDslParser::parse("system set-screen-power-state off").unwrap(),
DivoomDslParser::parse("system set-temperature-unit c").unwrap(),
DivoomDslParser::parse("system set-time-zone \"America/Los Angeles\"").unwrap(),
DivoomDslParser::parse("system set-weather-area \"-122.0\" 47.0").unwrap(),
DivoomDslParser::parse("system set-white-balance 100 150 200").unwrap(),
];
dsl_runner.batch_operations(&operations).await.unwrap();
run_dsl_runner_parser_test(
dsl_runner,
"test_data/dsl_runner_tests/system_commands.json",
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dsl_runner_should_batch_tool_commands() {
let client = PixooClient::new("127.0.0.1").unwrap();
let mut dsl_runner = new_divoom_dsl_runner(&client);
let operations = vec![
DivoomDslParser::parse("tool countdown start 10 30").unwrap(),
DivoomDslParser::parse("tool countdown stop").unwrap(),
DivoomDslParser::parse("tool noise start").unwrap(),
DivoomDslParser::parse("tool noise stop").unwrap(),
DivoomDslParser::parse("tool scoreboard 1 2").unwrap(),
DivoomDslParser::parse("tool stopwatch start").unwrap(),
DivoomDslParser::parse("tool stopwatch stop").unwrap(),
DivoomDslParser::parse("tool stopwatch reset").unwrap(),
DivoomDslParser::parse("tool buzzer").unwrap(),
DivoomDslParser::parse("tool buzzer 500 -a 100 -o 200").unwrap(),
];
dsl_runner.batch_operations(&operations).await.unwrap();
run_dsl_runner_parser_test(dsl_runner, "test_data/dsl_runner_tests/tool_commands.json");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dsl_runner_should_batch_animation_commands() {
let client = PixooClient::new("127.0.0.1").unwrap();
let mut dsl_runner = new_divoom_dsl_runner(&client);
let operations = vec![
DivoomDslParser::parse("animation gif play --file d:\\1.gif").unwrap(),
DivoomDslParser::parse("animation gif play --folder d:\\1").unwrap(),
DivoomDslParser::parse("animation gif play --url http://example.com/1.gif").unwrap(),
DivoomDslParser::parse("animation image reset-id").unwrap(),
DivoomDslParser::parse("animation text clear").unwrap(),
DivoomDslParser::parse("animation text set 0 -x 0 -y 0 -d left -f 1 -w 32 \"test string\" -r 100 -g 150 -b 150 -a middle").unwrap(),
];
dsl_runner.batch_operations(&operations).await.unwrap();
run_dsl_runner_parser_test(
dsl_runner,
"test_data/dsl_runner_tests/animation_commands.json",
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dsl_runner_should_batch_batch_commands() {
let client = PixooClient::new("127.0.0.1").unwrap();
let mut dsl_runner = new_divoom_dsl_runner(&client);
let operations =
vec![DivoomDslParser::parse("batch run-url http://example.com/commands.txt").unwrap()];
dsl_runner.batch_operations(&operations).await.unwrap();
run_dsl_runner_parser_test(dsl_runner, "test_data/dsl_runner_tests/batch_commands.json");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dsl_runner_should_batch_raw_commands() {
let client = PixooClient::new("127.0.0.1").unwrap();
let mut dsl_runner = new_divoom_dsl_runner(&client);
let operations = vec![DivoomDslParser::parse(
"raw \"{ \\\"Command\\\": \\\"Tools/SetStopWatch\\\", \\\"Status\\\": 1 }\"",
)
.unwrap()];
dsl_runner.batch_operations(&operations).await.unwrap();
run_dsl_runner_parser_test(dsl_runner, "test_data/dsl_runner_tests/raw_commands.json");
}
#[cfg(feature = "animation-builder")]
fn new_divoom_dsl_runner(pixoo_client: &PixooClient) -> DivoomDslRunner {
let template_manager = Arc::new(DivoomAnimationTemplateManager::new(".").unwrap());
DivoomDslRunner::new(&pixoo_client, template_manager)
}
fn run_dsl_runner_parser_test(dsl_runner: DivoomDslRunner, reference_file_path: &str) {
let (_, request_body) = dsl_runner.build();
let actual: serde_json::Value =
serde_json::from_str(&request_body).expect("Parsing request body failed!");
if env::var("DIVOOM_API_GENERATE_TEST_DATA").is_ok() {
let formatted_request_body =
serde_json::to_string_pretty(&actual).expect("Serialize commands failed!");
fs::write(reference_file_path, formatted_request_body).unwrap_or_else(|_| {
panic!(
"Generate test data file failed! Path = {}",
reference_file_path
)
});
return;
}
let reference_commands_text =
fs::read_to_string(reference_file_path).expect("Reading reference file failed!");
let expected: serde_json::Value =
serde_json::from_str(&reference_commands_text).expect("Parsing reference data failed!");
assert_eq!(actual, expected);
}
}