Skip to content

Commit 6e72c4b

Browse files
committed
wip #44
1 parent ff0161b commit 6e72c4b

File tree

9 files changed

+497
-392
lines changed

9 files changed

+497
-392
lines changed

src/common/coderbot.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
class CoderBot {
2+
constructor(CB, APIv1, APIv2, axios, store) {
3+
this.CB = CB + APIv2;
4+
this.CBv1 = CB + APIv1;
5+
this.$axios = axios;
6+
this.$store = store;
7+
}
8+
9+
load() {
10+
const p1 = this.loadSettings();
11+
const p2 = this.loadMusicPackages();
12+
const p3 = this.loadCNNModels();
13+
return Promise.all([p1, p2, p3]);
14+
}
15+
16+
async loadMusicPackages() {
17+
const result = await this.$axios.get(`${this.CB}/listMusicPackages`);
18+
const packagesInstalled = [];
19+
const music_packages = JSON.parse(result.data);
20+
Object.entries(music_packages).forEach((key) => {
21+
const package_key = key[0];
22+
const music_package = key[1];
23+
const names = [music_package.name_IT, package_key];
24+
if (music_package.category == 'instrument') {
25+
packagesInstalled.push([names, 'instrument']);
26+
} else if (music_package.category == 'animal') {
27+
packagesInstalled.push([names, 'animal']);
28+
}
29+
});
30+
this.$store.commit('setMusicPackages', packagesInstalled);
31+
}
32+
33+
async loadCNNModels() {
34+
const result = await this.$axios.get(`${this.CB}/listCNNModels`);
35+
const cnnModels = [];
36+
const cnn_models = JSON.parse(result.data);
37+
Object.entries(cnn_models).forEach((entry) => {
38+
const model_key = entry[0];
39+
// const model_data = entry[1];
40+
cnnModels.push({ key: model_key, text: model_key });
41+
});
42+
this.$store.commit('setCNNModels', cnnModels);
43+
}
44+
45+
async loadSettings() {
46+
const result = await this.$axios.get(`${this.CBv1}/config`);
47+
// handle success
48+
const data = {};
49+
const remoteConfig = result.data;
50+
data.power = [remoteConfig.move_power_angle_1, remoteConfig.move_power_angle_2, remoteConfig
51+
.move_power_angle_3
52+
];
53+
data.ctrl_hud_image = remoteConfig.ctrl_hud_image;
54+
data.cv_image_factor = remoteConfig.cv_image_factor;
55+
data.camera_color_object_size_max = remoteConfig.camera_color_object_size_max;
56+
data.camera_color_object_size_min = remoteConfig.camera_color_object_size_min;
57+
data.camera_exposure_mode = remoteConfig.camera_exposure_mode;
58+
data.camera_framerate = remoteConfig.camera_framerate;
59+
data.camera_jpeg_bitrate = remoteConfig.camera_jpeg_bitrate;
60+
data.camera_jpeg_quality = remoteConfig.camera_jpeg_quality;
61+
data.camera_path_object_size_max = remoteConfig.camera_path_object_size_max;
62+
data.camera_path_object_size_min = remoteConfig.camera_path_object_size_min;
63+
data.cnn_default_model = remoteConfig.cnn_default_model;
64+
data.btnFun = remoteConfig.button_func;
65+
data.wifiMode = remoteConfig.wifi_mode;
66+
data.wifiSSID = remoteConfig.wifi_ssid;
67+
data.wifiPsw = remoteConfig.wifi_psk;
68+
data.motorMode = remoteConfig.move_motor_mode;
69+
data.trimFactor = remoteConfig.move_motor_trim;
70+
data.startSound = remoteConfig.sound_start;
71+
data.stopSound = remoteConfig.sound_stop;
72+
data.shutterSound = remoteConfig.sound_shutter;
73+
data.startupProgram = remoteConfig.load_at_start;
74+
data.progLevel = remoteConfig.prog_level;
75+
data.moveFwdElapse = remoteConfig.move_fw_elapse;
76+
data.moveFwdSpeed = remoteConfig.move_fw_speed;
77+
data.moveTurnElapse = remoteConfig.move_tr_elapse;
78+
data.moveTurnSpeed = remoteConfig.move_tr_speed;
79+
data.ctrlFwdElapse = remoteConfig.ctrl_fw_elapse;
80+
data.ctrlFwdSpeed = remoteConfig.ctrl_fw_speed;
81+
data.ctrlTurnElapse = remoteConfig.ctrl_tr_elapse;
82+
data.ctrlTurnSpeed = remoteConfig.ctrl_tr_speed;
83+
data.audioLevel = remoteConfig.audio_volume_level;
84+
this.$store.commit('setSettings', data);
85+
}
86+
87+
async saveSettings(settings) {
88+
/* eslint-enable */
89+
const legacySettings = new URLSearchParams({
90+
ctrl_hud_image: settings.ctrl_hud_image,
91+
cv_image_factor: settings.cv_image_factor,
92+
camera_color_object_size_max: settings.camera_color_object_size_max,
93+
camera_color_object_size_min: settings.camera_color_object_size_min,
94+
camera_exposure_mode: settings.camera_exposure_mode,
95+
camera_framerate: settings.camera_framerate,
96+
camera_jpeg_bitrate: settings.camera_jpeg_bitrate,
97+
camera_jpeg_quality: settings.camera_jpeg_quality,
98+
camera_path_object_size_max: settings.camera_path_object_size_max,
99+
camera_path_object_size_min: settings.camera_path_object_size_min,
100+
cnn_default_model: settings.cnn_default_model,
101+
wifi_mode: settings.wifiMode,
102+
wifi_ssid: settings.wifiSSID,
103+
wifi_psk: settings.wifiPsw,
104+
move_power_angle_1: settings.power[0],
105+
move_power_angle_2: settings.power[1],
106+
move_power_angle_3: settings.power[2],
107+
button_func: settings.btnFun,
108+
move_motor_mode: settings.motorMode,
109+
move_motor_trim: settings.trimFactor,
110+
sound_start: settings.startSound,
111+
sound_stop: settings.stopSound,
112+
sound_shutter: settings.shutterSound,
113+
load_at_start: settings.startupProgram,
114+
prog_level: settings.progLevel,
115+
move_fw_elapse: settings.moveFwdElapse,
116+
move_fw_speed: settings.moveFwdSpeed,
117+
move_tr_elapse: settings.moveTurnElapse,
118+
move_tr_speed: settings.moveTurnSpeed,
119+
ctrl_fw_elapse: settings.ctrlFwdElapse,
120+
ctrl_fw_speed: settings.ctrlFwdSpeed,
121+
ctrl_tr_elapse: settings.ctrlTurnElapse,
122+
ctrl_tr_speed: settings.ctrlTurnSpeed,
123+
audio_volume_level: settings.audioLevel,
124+
});
125+
this.$store.commit('setSettings', settings);
126+
return this.$axios.post(`${this.CBv1}/config`, legacySettings);
127+
}
128+
129+
reboot() {
130+
return this.$axios.get(`${this.CBv1}/bot`, {
131+
params: {
132+
cmd: 'reboot'
133+
}
134+
});
135+
}
136+
137+
halt() {
138+
return this.$axios.get(`${this.CBv1}/bot`, {
139+
params: {
140+
cmd: 'halt'
141+
}
142+
});
143+
}
144+
145+
getInfoAndStatus() {
146+
const p1 = this.$axios.get(`${this.CB}/status`)
147+
.then((response) => {
148+
this.$store.commit('setStatus', response.data);
149+
});
150+
const p2 = this.$axios.get(`${this.CB}/info`)
151+
.then((response) => {
152+
this.$store.commit('setInfo', response.data);
153+
});
154+
const p3 = this.$axios.get(`${this.CB}/logs`)
155+
.then((response) => {
156+
this.$store.commit('setLogs', response.data);
157+
});
158+
return Promise.all([p1, p2, p3]);
159+
}
160+
161+
deleteMusicPackage(pkgNameID) {
162+
return this.$axios.post(`${this.CB}/deleteMusicPackage`, {
163+
package_name: pkgNameID,
164+
});
165+
}
166+
167+
restoreSettings() {
168+
return this.$axios.post(`${this.CB}/restoreSettings`);
169+
}
170+
171+
updateFromPackage(formdata, config) {
172+
return this.$axios.post(`${this.CB}/updateFromPackage`, formdata, config);
173+
}
174+
175+
reset() {
176+
return this.$axios.post(`${this.CB}/reset`);
177+
}
178+
179+
test(checkedTests) {
180+
return this.$axios.post(`${this.CB}/testCoderbot`, {
181+
params: checkedTests
182+
});
183+
}
184+
185+
saveWifiParams(wifiMode, wifiSSID, wifiPsw) {
186+
const valuesAsString = new URLSearchParams({
187+
wifi_mode: wifiMode,
188+
wifi_ssid: wifiSSID,
189+
wifi_psk: wifiPsw,
190+
});
191+
return this.$axios.post(`${this.CBv1}/wifi`, valuesAsString);
192+
}
193+
194+
updatePackages(formdata) {
195+
return this.$axios.post(`${this.CB}/updatePackages`, formdata);
196+
}
197+
}
198+
199+
export default CoderBot;

src/components/Activity.vue

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export default {
320320
exec: {},
321321
},
322322
log: null,
323-
settings: {},
323+
settings: null,
324324
snackText: null,
325325
snackbar: false,
326326
drawer: false,
@@ -371,8 +371,7 @@ export default {
371371
}
372372
},
373373
mounted() {
374-
this.loadMusicPackages();
375-
374+
this.settings = this.$store.getters.settings;
376375
// Get the activity
377376
const axios = this.$axios;
378377
const {
@@ -431,7 +430,6 @@ export default {
431430
label: this.$i18n.t('message.activity_program_import'),
432431
type: 'text',
433432
},
434-
435433
],
436434
capsSwitch: false,
437435
codeFont: 'ubuntumono',
@@ -444,7 +442,12 @@ export default {
444442
fontSize: 'Medio',
445443
name: this.$i18n.t('message.activity_program_title'),
446444
showName: true,
445+
maxBlocks: null,
447446
};
447+
const toolboxLevel = this.settings.progLevel;
448+
// Decode it and get the clean serialized XML as plain string
449+
this.toolbox = require(`../assets/toolbox_${toolboxLevel}.json`);
450+
this.settings.maxBlocks = null; // default
448451
} else {
449452
console.log('Loading activity', this.$route.params.name);
450453
this.saved = true;
@@ -455,17 +458,18 @@ export default {
455458
}).then((response) => {
456459
console.log('Activity loaded', response.data);
457460
this.activity = response.data;
458-
this.settings.activity = response.data;
461+
this.settings.maxBlocks = this.activity.maxBlocks;
459462
this.updateCssProps();
460463
461464
let toolboxJSON = null;
462465
if (this.activity.toolbox == null) {
463-
const toolboxLevel = this.settings.prog_level;
466+
const toolboxLevel = this.settings.progLevel;
464467
// Decode it and get the clean serialized XML as plain string
465468
toolboxJSON = require(`../assets/toolbox_${toolboxLevel}.json`);
466469
} else {
467470
toolboxJSON = this.activity.toolbox;
468471
}
472+
console.log(this.settings);
469473
this.toolbox = toolboxJSON;
470474
});
471475
}
@@ -477,16 +481,6 @@ export default {
477481
setInterval(() => {
478482
this.pollStatus();
479483
}, 1000);
480-
481-
// Get the legacy configuration and initialize Blockly
482-
axios.get(`${this.CBv1}/config`)
483-
.then((response) => {
484-
this.settings = { ...this.settings, ...response.data };
485-
if (this.toolbox == null) {
486-
const toolboxLevel = this.settings.prog_level;
487-
this.toolbox = require(`../assets/toolbox_${toolboxLevel}.json`);
488-
}
489-
});
490484
},
491485
beforeRouteLeave(to, from, next) {
492486
if (this.dirty) {
@@ -555,24 +549,6 @@ export default {
555549
};
556550
},
557551
558-
loadMusicPackages() {
559-
this.$axios.get(`${this.CB}/listMusicPackages`).then((result) => {
560-
this.settings.music_instruments = [];
561-
this.settings.music_animals = [];
562-
const music_packages = JSON.parse(result.data);
563-
Object.entries(music_packages).forEach((key) => {
564-
const package_key = key[0];
565-
const music_package = key[1];
566-
const names = [music_package.name_IT, package_key];
567-
if (music_package.category == 'instrument') {
568-
this.settings.music_instruments.push(names);
569-
} else if (music_package.category == 'animal') {
570-
this.settings.music_animals.push(names);
571-
}
572-
});
573-
});
574-
},
575-
576552
exportProgram() {
577553
// Create a blob object and simulate a click event starting the download
578554
const data = JSON.stringify(this.getProgramData());
@@ -698,7 +674,6 @@ export default {
698674
name: program,
699675
},
700676
}).then((data) => {
701-
console.log(data.data.dom_code);
702677
this.$refs.workspace.loadProgram(data.data.dom_code);
703678
this.$data.isDefault = data.data.default;
704679
});
@@ -783,7 +758,7 @@ export default {
783758
// POST /program/save
784759
const options = this.activity;
785760
786-
const { dom_code, code } = this.workspace.getProgramData();
761+
const { dom_code, code } = this.$refs.workspace.getProgramData();
787762
788763
axios.post(`${CB}/exec`, {
789764
name: 'run program',

0 commit comments

Comments
 (0)