$wyapp.on ('load', function (project) { debug ('Load'); mixpanel.track ('Project Load', { language:project.language }); library.storeWorkingProject (project.id); softwareEditor.language = project.language; setSizes (); if (project.language === "nodejs") { softwareEditor.getSession().setMode ('ace/mode/javascript'); } else if (project.language === "python") { softwareEditor.getSession().setMode ('ace/mode/python'); } else if (project.language === "shell") { softwareEditor.getSession().setMode ('ace/mode/sh'); } else if (project.language === "visual") { program.load (project); } $timeout (function () { $scope.project = project; }); });
$wydevice.on ('message', function (t, p) { if (t==='i') { if ($scope.device.category !== p.c || $scope.device.network !== p.i) { mixpanel.track ('Device Data', { category: p.c }); } $timeout (function () { if (p.r) $scope.running = p.r; else $scope.running = false; if (settings.LABEL[p.c]) { $scope.device.category = p.c; $scope.device.network = p.i; } else { $scope.device.category = 'board'; $scope.device.network = p.i; } }); } if (t === 'capabilities') { $timeout (function () { $scope.device.capabilities = p; }); } });
$mdDialog.show(message).then(function() { $scope.project.schematics = ''; mixpanel.track ('Schematics Erase', { language: $scope.project.language }); library.storeSchematics ($scope.project.id, $scope.project.schematics); }, function() {
this.install = function (device) { $wydevice.send ('install', {a:'i', c:device}); $scope.runinstall = true; mixpanel.track ('Install', { category: $wydevice.device.category }); };
fileReader.onload = function (value) { $scope.project.schematics = value.target.result; mixpanel.track ('Schematics Load', { language: $scope.project.language }); library.storeSchematics ($scope.project.id, $scope.project.schematics); };
app.controller('TaskManagerController', function($scope, $timeout, $mdDialog, $wydevice){ debug ('Registering'); $scope.tasks = []; mixpanel.track ('Task Manager', { category: $wydevice.device.category }); var that = this; var message = function (t, p) { if (t === 'tm') { $timeout (function () { $scope.tasks = p; }); } }; $wydevice.on ('message', message); $wydevice.send ('tm', {a:'run'}); /*var status = function (status) { if (status !== 'CONNECTED' || status !== 'PING') { that.exit (); } }; $wydevice.on ('status', status); */ this.stop = function (task) { debug ('Stopping '+task.PID); $timeout (function () { task.stopping = true; }); $wydevice.send ('tm', {a:'exit', PID:task.PID}); mixpanel.track ('Task Stop',{ category: $wydevice.device.category }); }; this.exit = function () { debug ('Exiting'); $mdDialog.hide (); $wydevice.send ('tm', {a:'stop'}); // $wydevice.removeListener ('status', status); $wydevice.removeListener ('message', message); }; });
$wyapp.on ('stop', function () { debug ('Stop'); $wydevice.send ('p', {a:'stop'}); mixpanel.track ('Project Stop', { category: $wydevice.device.category, language: $scope.project.language }); });
$wyapp.on ('firmware', function (firmware) { debug ('Firmware'); $timeout (function () { $scope.project.firmware = firmware; }); mixpanel.track ('Project Load Firmware', { langauge: $scope.project.language, }); });
this.stop = function (task) { debug ('Stopping '+task.PID); $timeout (function () { task.stopping = true; }); $wydevice.send ('tm', {a:'exit', PID:task.PID}); mixpanel.track ('Task Stop',{ category: $wydevice.device.category }); };
function run (firmware, port) { var makefile = settings.MAKEFILE_STOARGE[$scope.project.language]+'firmware:\n\t'; var runmessage = {a:'start', l:$scope.project.language, p:$scope.project.main}; if (firmware && port) { runmessage.f = $scope.project.firmware; makefile = makefile + settings.MAKE_FIRMWARE[$scope.device.category]('app_project', firmware, port); $scope.device.firmware = firmware; $scope.device.port = port; mixpanel.track ('Project Run', { category: $wydevice.device.category, language: $scope.project.language, flash: true }); } else { makefile = makefile+'\n'; mixpanel.track ('Project Run', { category: $wydevice.device.category, language: $scope.project.language, flash: false }); } runmessage.m = makefile; shell.reset (); $wydevice.send ('p', runmessage); $timeout (function () { $scope.showXterm = true; $timeout (function () { $(window).trigger ('resize'); }); }); }
this.uninstall = function (p, l) { p.loading = true; $scope.runmanager = true; $scope.runoutput = ''; $wydevice.send ('pm', {a:'u', p:p.n, l:l}); $scope.runpackage = p.n; $scope.runlanguage = l; mixpanel.track ('Package Uninstall', { category: $wydevice.device.category, package: p.n, langauge: l }); };
this.name = function () { mixpanel.track ('Board Rename', { }); $mdDialog.hide (); var renameBoardDialog = $mdDialog.show({ controller: function ($scope) { $scope.name = $wydevice.device.name; this.rename = function () { debug ('Rename board '+$wydevice.device.name); if ($scope.name.trim().length >= 1) { $wydevice.send ('n', {n:$scope.name.trim()}); $mdDialog.hide (); } else { debug ("Board name is unacceptable"); } }; this.cancel = function () { $mdDialog.hide (); }; }, controllerAs: 'a', templateUrl: '/public/views/name.html', // parent: angular.element(window.body), // targetEvent: ev, clickOutsideToClose:true, fullscreen: false }); };
this.connect = function () { scope.serialPort.ip = $scope.device.ip; scope.serialPort.port = $scope.device.port; users[$scope.device.ip] = $scope.device.username; ip = $scope.device.ip; port = $scope.device.port; var type = 'chrome-socket'; if ($scope.device.secure) { type = 'chrome-ssh'; port = $scope.device.secureport; } $wydevice.connect ($scope.device.ip, {type:type, port: port, username:$scope.device.username, password:$scope.device.password, category:scope.serialPort.category, platform: scope.serialPort.platform}); $mdDialog.hide (); mixpanel.track ('SerialPort Connect', { style: 'address', type: type }); };
$wydevice.on ('status', function (status) { debug ('Status '+status); // console.log (status); mixpanel.track ('Status', { category: $scope.device.category, status: status }); $timeout (function () { $scope.connected = (status === 'INSTALL' || status === 'PING' || status === 'CONNECTING' || status === 'SEPARATOR' || status === 'CONNECTED'); if ($scope.connected === false) { $scope.device.category = 'board'; $scope.device.capabilities = null; $scope.update = false; } $scope.status = status; $scope.device.platform = $wydevice.device.platform; if (status === 'CONNECTED' || status === 'INSTALL') $scope.shell_disable = false; }); });
app.controller ('ResistorColorCodeController', function ($scope, $mdDialog) { debug ('Registering'); //Start first page part $scope.colors = ['Black', 'Brown', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Violet', 'Gray', 'White']; $scope.multiplier = [ { color:'Black', value: 1 }, { color:'Brown', value: 10 }, { color:'Red', value: 100 }, { color:'Orange', value: 1000 }, { color:'Yellow', value: 10000 }, { color:'Green', value: 100000 }, { color:'Blue', value: 1000000 }, { color:'Violet', value: 10000000 }, { color:'Gold', value:0.1 }, { color:'Silver', value:0.01 }]; $scope.tolerance = [ { color:'Black', value:0 }, { color:'Brown', value:1 }, { color:'Red', value:2 }, { color:'Green', value:0.5 }, { color:'Blue', value:0.25 }, { color:'Violet', value:0.10 }, { color:'Gray', value:0.05 }, { color:'Gold', value:5 }, { color:'Silver', value:10 }]; $scope.value = { number: 4, color1: 0, color2: 0, color3: 0, color4: 1, color5: 0 }; $scope.r = 0; $scope.u = 0; $scope.$watchCollection ('value', function () { var value = parseInt($scope.value.color1)*10 + parseInt($scope.value.color2); // debug (value); if ($scope.value.number === '5') { value = value*10 + parseInt($scope.value.color3); // debug (value); } value = value * parseFloat($scope.value.color4); // debug (value); if (value > 1000000) { value = value / 1000000; // debug (value); $scope.u = 'M'; } else if (value > 1000) { value = value / 1000; // debug (value); $scope.u = 'K'; } else { $scope.u = ''; } $scope.r = value; }); //End first page part //Start second page part //All variables are prefixed by "second" mixpanel.track ('Resistor Color Code', {}); $scope.second={}; $scope.second.value={}; $scope.second.tolerance = [ { color:0, value:"0%" }, { color:1, value:"1%" }, { color:2, value:"2%" }, { color:5, value:"0.5%" }, { color:6, value:"0.25%" }, { color:7, value:"0.10%" }, { color:8, value:"0.05%" }, { color:11, value:"5%" }, { color:22, value:"10%" }]; $scope.second.value.number = 220; $scope.second.value.stripes = '4'; $scope.second.value.tolerance= 11; //last stripe $scope.second.stripe1 = 2; $scope.second.stripe2 = 2; $scope.second.stripe3 = 0; $scope.second.stripe4 = 0; $scope.second.r = 220; $scope.second.u = ""; $scope.$watchCollection ('second.value', function () { var value = parseFloat($scope.second.value.number); if (isNaN(value) || value > 99999999999 || ((value < 0.1) && ($scope.second.value.stripes === '4')) || ((value < 1) && ($scope.second.value.stripes !== '4'))) { $scope.second.r = 0; //bad value $scope.second.u = ""; $scope.second.stripe1 = 0; $scope.second.stripe2 = 0; $scope.second.stripe3 = 0; $scope.second.stripe4 = 0; } else { value *= 10000; //workaround for float var digit1 = 0; var digit2 = 0; var digit3 = 0; var multiplier = 0; //debug (value); if ($scope.second.value.stripes === '4') { while (Math.trunc(value / 100) !== 0) { multiplier += 1; value = Math.trunc(value / 10); } digit2 = value % 10; digit1 = Math.trunc(value / 10); multiplier -= 4; //workaround $scope.second.r = (digit1 * 10 + digit2) * Math.pow(10,multiplier); $scope.second.stripe3 = multiplier; } else { while (Math.trunc(value / 1000) !== 0) { multiplier += 1; value = Math.trunc(value / 10); } digit3 = value % 10; value = Math.trunc(value / 10); digit2 = value % 10; digit1 = Math.trunc(value / 10); multiplier -= 4; //workaround $scope.second.r = ((digit1 * 10 + digit2) * 10 + digit3) * Math.pow(10,multiplier); $scope.second.stripe3 = digit3; $scope.second.stripe4 = multiplier; } $scope.second.stripe1 = digit1; $scope.second.stripe2 = digit2; //gold and silver stripes name match if ($scope.second.stripe3 == -1) { $scope.second.stripe3 = 11; } if ($scope.second.stripe3 == -2) { $scope.second.stripe3 = 22; } if ($scope.second.stripe4 == -1) { $scope.second.stripe4 = 11; } if ($scope.second.stripe4 == -2) { $scope.second.stripe4 = 22; } if ($scope.second.r > 1000000) { $scope.second.r = $scope.second.r / 1000000; // debug (value); $scope.second.u = 'M'; } else if ($scope.second.r > 1000) { $scope.second.r = $scope.second.r / 1000; // debug (value); $scope.second.u = 'K'; } else { $scope.second.u = ''; } } }); //end second page part this.exit = function () { $mdDialog.hide (); }; });
app.controller('PackageManagerController', function($scope, $timeout, $wydevice, $mdDialog){ debug ('Registering'); mixpanel.track ('Package Manager', { category: $wydevice.device.category }); function listPackages (language) { $wydevice.send ('pm', {a: 'p', l:language}); } $scope.LANGUAGES = settings.LANGUAGES; $scope.packages = {}; $scope.runmanager = false; $scope.runoutput = ''; $scope.runpackage = ''; $scope.runlanguage = ''; _.each ($scope.LANGUAGES, function (language) { if (language.packagemanager.enable === true) { debug ('Request package '+language.title); $scope.packages[language.title] = []; listPackages(language.title); } }); var that = this; var message = function (t, p) { if (t === 'pm') { if (p.a === 'p') { debug ('Receive package for '+p.l); if (!p.e) { $timeout (function () { $scope.packages[p.l] = p.p; var readonly = []; var available = {}; for (var index = 0; index < $scope.LANGUAGES.length; index++) { // debug ($scope.LANGUAGES[index].title); if ($scope.LANGUAGES[index].title.toLowerCase() === p.l.toLowerCase()) { readonly = $scope.LANGUAGES[index].packagemanager.readonly; available = _.clone ($scope.LANGUAGES[index].packagemanager.available); } } // debug (readonly); var available_packages = []; _.each ($scope.packages[p.l], function (v) { if (_.indexOf (readonly, v.n)>=0) { v.readonly = true; } else { v.readonly = false; } v.loading = false; var title = v.n.toLowerCase (); if (available[title]) { v.d = available[title].description; v.w = available[title].website; delete available[title]; } // debug (v); }); _.each (available, function (v, n) { debug (v); $scope.packages[p.l].push ({n:n, d:v.description, loading:false, w:v.website}); }); }); } } else if (p.a === 'i') { if (p.e !== undefined) { $timeout (function () { if (p.e === 0) that.runexit (); $scope.runpackage = ''; listPackages (p.l); }); } else if (p.err) { $timeout (function () { $scope.runoutput = $scope.runoutput + p.err; }); } else if (p.out) { $timeout (function () { $scope.runoutput = $scope.runoutput + p.out; }); } } } }; $wydevice.on ('message', message); this.runexit = function () { $scope.runmanager = false; $scope.runerror = ''; $scope.runoutput = ''; }; this.install = function (p, l) { p.loading = true; $scope.runmanager = true; $scope.runoutput = ''; $wydevice.send ('pm', {a:'i', p:p.n, l:l}); $scope.runpackage = p.n; $scope.runlanguage = l; mixpanel.track ('Package Install', { category: $wydevice.device.category, package: p.n, langauge: l }); }; this.uninstall = function (p, l) { p.loading = true; $scope.runmanager = true; $scope.runoutput = ''; $wydevice.send ('pm', {a:'u', p:p.n, l:l}); $scope.runpackage = p.n; $scope.runlanguage = l; mixpanel.track ('Package Uninstall', { category: $wydevice.device.category, package: p.n, langauge: l }); }; this.exit = function () { if ($scope.runmanager === true) { $scope.runmanager = false; if ($scope.runpackage !== '') { $wydevice.send ('pm', {a:'s', p:$scope.runpackage, l:$scope.runlanguage}); } $scope.runpackage = ''; $scope.runlanguage = ''; } else { $mdDialog.hide (); $wydevice.removeListener ('message', message); } }; });
this.open = function () { debug ('Opening serialport '+$scope.serialPort); // console.log ('Opening serialport '+$scope.serialPort); if ($scope.serialPort.path === 'chrome') { $wydevice.connect ($scope.serialPort.path, {type:'chrome'}); mixpanel.track ('SerialPort Connect',{ style:'chrome', device: 'chrome' }); } else if ($scope.serialPort.path) { $wydevice.connect ($scope.serialPort.path); mixpanel.track ('SerialPort Connect',{ style:'serial', device: $scope.serialPort.path }); } else { var scope = $scope; $mdDialog.show({ controller: function ($scope) { $scope.device = { ip: (scope.serialPort.ip.length>0?scope.serialPort.ip:ip), port: (scope.serialPort.port >= 0?scope.serialPort.port:port), secureport: (scope.serialPort.secureport >= 0?scope.serialPort.secureport:secureport), username: users[(scope.serialPort.ip.length>0?scope.serialPort.ip:ip)] || '', category: scope.serialPort.category }; $scope.device.secure = true; this.connect = function () { scope.serialPort.ip = $scope.device.ip; scope.serialPort.port = $scope.device.port; users[$scope.device.ip] = $scope.device.username; ip = $scope.device.ip; port = $scope.device.port; var type = 'chrome-socket'; if ($scope.device.secure) { type = 'chrome-ssh'; port = $scope.device.secureport; } $wydevice.connect ($scope.device.ip, {type:type, port: port, username:$scope.device.username, password:$scope.device.password, category:scope.serialPort.category, platform: scope.serialPort.platform}); $mdDialog.hide (); mixpanel.track ('SerialPort Connect', { style: 'address', type: type }); }; this.exit = function () { $mdDialog.hide (); }; }, controllerAs: 'a', templateUrl: '/public/views/authenticate.html', // parent: angular.element(document.body), // targetEvent: ev, escapeToClose: false, clickOutsideToClose: false, fullscreen: false }); } };
app.controller ('AppController', function ($scope, $wyapp, $timeout, $wydevice, $filter) { debug ('Registering'); document.title = $filter('translate')('__MSG_appName__'); mixpanel.track ('Startup'); $scope.running = false; $scope.update = false; $scope.connected = false; $scope.device = { category: 'board' }; $scope.label = settings.LABEL; $scope.status = 'DISCONNECTED'; $scope.project_disable = true; $scope.shell_disable = true; $scope.project_name = ""; this.status = function () { return 'status label c-status label-'+$scope.status; }; $wyapp.on ('load', function (project) { $timeout (function () { $scope.project_disable = false; $scope.project_name = project.title; }); }); $wydevice.on ('update', function () { debug ('Update for device'); $scope.update = true; }); $wydevice.on ('message', function (t, p) { if (t==='i') { if ($scope.device.category !== p.c || $scope.device.network !== p.i) { mixpanel.track ('Device Data', { category: p.c }); } $timeout (function () { if (p.r) $scope.running = p.r; else $scope.running = false; if (settings.LABEL[p.c]) { $scope.device.category = p.c; $scope.device.network = p.i; } else { $scope.device.category = 'board'; $scope.device.network = p.i; } }); } if (t === 'capabilities') { $timeout (function () { $scope.device.capabilities = p; }); } }); $wydevice.on ('status', function (status) { debug ('Status '+status); // console.log (status); mixpanel.track ('Status', { category: $scope.device.category, status: status }); $timeout (function () { $scope.connected = (status === 'INSTALL' || status === 'PING' || status === 'CONNECTING' || status === 'SEPARATOR' || status === 'CONNECTED'); if ($scope.connected === false) { $scope.device.category = 'board'; $scope.device.capabilities = null; $scope.update = false; } $scope.status = status; $scope.device.platform = $wydevice.device.platform; if (status === 'CONNECTED' || status === 'INSTALL') $scope.shell_disable = false; }); }); setBoxSize (); chrome.app.window.current().onClosed.addListener (function () { $wydevice.disconnect (); }); });
this.connect = function (device) { if (!device) device = {ip:'', port:7000, secureport:22}; debug ('Connecting to '+device); if (device.platform === 'chrome') { $scope.connection = 'chrome'; $wydevice.connect ('', {type:'chrome'}); mixpanel.track ('SerialPort Connect',{ style:'chrome', device: 'chrome' }); } else if (device.platform === 'serial') { $scope.connection = device.ip; $wydevice.connect (device.ip); mixpanel.track ('SerialPort Connect',{ style:'serial', device: device.ip }); } else { var scope = $scope; $mdDialog.show({ controller: function ($scope) { $scope.device = { ip: (device.ip.length>0?device.ip:ip), port: (device.port >= 0?device.port:port), secureport: (device.secureport >= 0?device.secureport:secureport), username: users[(device.ip.length>0?device.ip:ip)] || '', category: device.category }; $scope.device.secure = true; this.connect = function () { // device.ip = $scope.device.ip; // device.port = $scope.device.port; users[$scope.device.ip] = $scope.device.username; ip = $scope.device.ip; port = $scope.device.port; var type = 'chrome-socket'; if ($scope.device.secure) { type = 'chrome-ssh'; port = $scope.device.secureport; } scope.connection = ip; $wydevice.connect ($scope.device.ip, {type:type, port: port, username:$scope.device.username, password:$scope.device.password, category:device.category, platform: device.platform}); $mdDialog.hide (); mixpanel.track ('SerialPort Connect', { style: 'address', type: type }); }; this.exit = function () { $mdDialog.hide (); }; }, controllerAs: 'a', templateUrl: '/public/views/authenticate.html', // parent: angular.element(document.body), // targetEvent: ev, escapeToClose: false, clickOutsideToClose: false, fullscreen: false }); } };