Mastering the IoT with C++ and JavaScript – Günter Obiltschnig – Meeting C++ 2015

hi good afternoon everybody I'm Quinto Vinick some of you may already know me from my open-source work in C++ so my first question is anyone here who've heard of the polka C++ libraries already yeah the number is definitely definitely growing so yeah basically what I've been done doing for the last 10 years is beside my work on vocal libraries of course I've been building lots of stuff based on the PACU libraries so on one hand split a couple of C++ frameworks that I'm also going to talk a bit later in this talk I spilled some cloud services for secure mode access for the internet of things also with the poke libraries and my latest project I'm going to talk about today is marking i/o which is basically the Polka libraries taken as foundation and basically taken to a whole new level for IOT development with some special features that makes it make it very easy for you to build software for so-called IT gateways and IOT devices for the german-speaking part of you must be interested in the latest special issue of the X Mega seen on development for Internet of Things it also contains an article by me about how to build software for the IOT using C++ so the first question that I'm going to try to answer is what is the Internet of Things anyone has an idea okay sir actually actually was wasn't an embedded software engineering conference for the last three days and there are a couple of people actually ask the question what does IOT mean so despite the fact that the term IOT has been used in in the media for software engineers for the last three years or so still not that widespread so let's see what what I what he actually means basically the Internet of Things is the network of physical objects or things things embedded with electronic software sensors and connectivity to achieve greater value and service by exchanging data with other devices and the things also have the property that they are uniquely identifiable and actually the term Internet of Things is quite old it was formed in 1999 by Kevin Ashton and actually back then Internet of Things was used for some kind of different applications if some of you may remember back then we had this RFID technology which was soon to be come a part of your product so the the big vision was when you go to a supermarket you just put everything you buy into your trolley and go to the cashier and then the cashier is actually just some kind of device that finds out what you have port and then automatically charges your credit card for various reasons that this vision hasn't yet come true so the term Internet of Things is now being reused for something a bit different so this is basically what the Internet of Things is all about of course since in its name it's about things which are connected smart products that combine processors microprocessors microcontrollers sensors and of course software with communications technology technology of course and then there's also the Internet in the name internet of things which are the networks which can be wired networks wireless networks like smaller sensor networks like Bluetooth SIG P but also larger scale networks like 3G 4G and so on and and of course we also need some destination where all this data gets sent to and the whole computing infrastructure to support cloud nowadays where we hope to collect all this data that we get from the millions of sensors and make some sense of it and actually IOT isn't some kind of revolution it's more of an evolution so back then in mid 90s we had the web where we started using web browsers but the web browser was basically usually run on a PC so you had to go through your PC to connect using an old modem to the Internet and then you could surf the web then in the middle of the two case we had mobile widths on one hand the mobile networks become amount more powerful and of course was the introduction of the iPhone and Android devices and this basically means that the internet became part of our personal life so with the little device you always have basically the internet with you and then the IOT is basically the next step in that evolution where the Internet is not restricted to special devices but basically becomes integrated in in almost any device of daily use there are some funny predictions regarding the number of connected devices that will soon see around us the predictions range everywhere from 20 billion to 50 billion if you want to achieve this by 2020 we'll probably get things going and get up to speed so I think this these numbers are actually a bit very optimistic the other question of course is why should we do the IOT the ideas I mean of course as engineer would like to say yeah because it's a tough play with all this new cool technology but of course there has to be some kind of business sense behind it and of course there has to be some benefit for the customer and also some benefit for the business for the manufacturer it says the device and this is actually the big question right now in the whole IOT space so basically the technical problems are basically all solved all the technical foundations that we need to build a IOT are basically there in some cases before we need to wait until some technologies get even more cheap but we need no special new inventions in order to to achieve the IOT and what LTS gives us basically to pick opportunities for businesses the first one is to transform existing business processes things like predictive maintenance which means if there is some machine doing some work we put a lot of sensors into the machine and through the data collected by the sensors try to find out early if something in the machine might go wrong make break might need servicing or might need exchange of a part then of course better use realizations of machines so it's easy to detect when machines go idle and and do petter planning to utilize them better and of course higher productivity there's also this nice concept of the digital twin that we've seen the IOT but basically on one hand you have the physical device machine the car whatever and on the other hand in in the cloud in the data center you have a complete virtual representation of the machine including live data of every part of the machine to see how it works how the environment is and so on and of course also new business model basically the anything as a service so basically the idea is instead of selling a machine at one point in time getting some money for it and then maybe have some service contract or whatever the idea is not to sell the machine but to sell the service of that the machine actually performs so if the machine performs some kind of reduction step then the customer pays 50 cent for every item that goes through the machine and basically the manufacturer of the machine just provides through the machine that service it he takes care that the Machine keeps running that every consumables are refused that parts are updated and so on there are of course other business models just probably have seen the nest intelligent thermostat and as we probably know nester the crowd the Google and this makes us some interesting sauce so I think when a smoke alarm is going off Google AdWords just pitched me a fire extinguisher and an offer for temporary housing but one of the essential ideas behind the Internet of Things is that things actually become software platforms and a good example that we see is the Tesla basically what Tesla did is they did not build a car basically they built a smartphone put some Pig batteries and wheels on it and sell it as a mobility solution say it that way and what you see if we have a Tesla is there are software updates and this software updates come over the Internet over the air it's each Tesla has a built in mobile internet module so it's basically online all the time and every few months you get a new software update that provides new features in your car so the last software update actually provided the feature that the car can basically drive certain in certain situations the car can drive alone it's called autopilot and if you look at YouTube there are a couple of funny videos of people leaving the front seat and getting on the back seat and filming how the car is driving itself and there are other videos where the car is driving on a curvy back side road and and suddenly it starts turning into the oncoming traffic which is not so funny but the big difference is what what Tesla is doing here they are putting a technology that might not be that well developed already outside into the customers and what they are gaining is they're getting an immense amount of data back from the cars from the 50,000 cars they already have driving around everywhere and you can use all this data to make the software even better so how does the typical IOT architecture looks like so on the first side you have this sensors and devices we say usually devices powered by smaller microcontrollers you have a bit too little 32-bit microcontrollers things are usually usually programmed in C some C++ and you have smaller network technologies like SiC PEO example in this case this is a battery power chickpea sense or also Bluetooth and couple of other technologies and usually it doesn't make sense to connect every little device directly to the Internet the first hand it's very hard to manage such a system these things don't have user interfaces you cannot easily configure network addresses and things like that so you need other technologies and also there's a cost factor you can put a Wi-Fi module or Ethernet into each of these devices that just cost a few euros or dollars and therefore we have the concept of the IOT gateway which is usually a linux capable device that on one hand connects to these little devices and on the other hand connects to the Internet and also can do some interesting things with the data it gets in that of course you have the mobile clients that talk to the devices you have web-based clients talking to devices and on the business side you have so-called Big Data where I try to make a sensor for the data and if you look at the technology stack you can see that actually what makes the Internet of Things interesting from a technology point of view is you have to deal with all the different layers from the smallest embedded systems up to the big enterprise systems and with all the different network technologies in between and of course there also is the very interesting question of how can we make everything secure you've probably heard of that car that got hacked in the United States there was an article by that was published in Wired magazine of some of the violet writers writing a cheap and a team of hackers gaining control of the chip while he was driving so at first they got control of the car and switched radio of the car ready and switched the car radio to an hip-hop station R then maybe they deterred on the wipers and finally they disability breaks and this was when it was too much for the guy behind to stop the car he turned it to the ditch and if you look at the different security advisories that are floating around the net you can see that even things like 6p which was you might guess designed by network professionals know what you are doing even sick P has very severe security issues and to make matters worse there are now even who search search engines devoted to finding actually thanks on the internet so there is a showdown where we can for example search for that cans search for industrial control devices and so on and yeah if you go to the side and and enter some search terms there's some couple of interesting research that turn up I mean it gets worse when you find your own devices there but at least you should know that these tools exist so and now let's get to some more interesting parts why we're all here we're here to talk about C++ so I'm going to talk a little bit about my newest open source project and how it was all built so the interesting thing is about Makena Y I have combined both the C plus password and also the JavaScript word and I'm going to talk about some of the challenges that I had to solve in order to achieve this later on but basically this is the idea behind the newest project so basically you going to write software for these kind of devices and in order to write software for so-called IOT gateways you have to talk to the different sensors there are many many different protocols and technologies that you can use to talk to them and you're going to talk to cloud services there are only two at the moment important technologies that use here HTTP and mqtt and you can also integrate things like mobile clients and so on and if we look at part at how everything is built it's of course over pasted on the poker libraries and on top of that we have added a couple of different frameworks on one hand what what we want to do is a building software for a kind of an IOT gateway devices you want to be able to update the software even when the devices in the field so the time very building software for an embedded device in the device get chipped and then the software never gets updated again except maybe for some very urgent security fixes these times are over in the internet of things so customers are basically expecting updates of the features of your devices as I said earlier devices are becoming software platforms and in order to build extensible software platforms you need some flexibility and this is the whole idea behind the so-called open service platform it's basically a module system for c++ module system less in the ideal that the new modules probably proposed for c++ 17 but modules in terms of deployment unit so what you basically can do is you can take your c++ build shared libraries some metadata put everything together in a zip file add some HTML files for example some configuration files and take the whole thing and download it to your device if some of you are familiar bit with the the Java ecosystem Java has a similar technology called OSGi and usually I'm not a big fan of the child world but occasionally even the Java guys have good ideas and OSGi was one of those years so I took some inspiration from from there then we have technology the core remoting and it's basically a framework that allows you to do inter process communication so have someone in one some class in one process call a different class or methods of a different class in a different process this also allows you to build things like soap and rest web services in C++ but it also it's also used in order to preach the Press Pass word to the JavaScript world as we see later and finally I've taken the v8 engine from Google which is also used in the Chrome browser and in no chairs and integrated it with everything so this gives you the possibility to write some parts of your applications that very quickly in JavaScript so actually the whole thing here there is nothing I achieve specific in it it's a pretty nice platform that it can use to build any kind of super specification from from a client application to a server application so to make the whole thing IOT specific i've added some additional frameworks and classes for example we have c++ interfaces for different kind of sensors like temperature sensors accelerometers GPS receivers and so on so basically order the usual sensors did you need in in most applications you can of course also define your own sensor classes then we have support for different protocols like MQTT the coop protocol is coming soon which is kind of web services for smaller devices it's basically a binary version of HTTP HTTP and and chasten optimized for very small devices and for sensor networks that provide limited bandwidth then we have a couple of services for example you can post twitter messages or send SMS messages seems like that and finally we have a built-in web server that already provides a couple of good in web applications that you can use to quickly get started and you can also add your own web pages the whole thing is open source and the apache license this time so a transfer for poco I used the boost license now I have decided for for the additional parts used apache license because with the new project i don't not only want to reach c++ guys i want which protocol unity and and the apache license is just widen owned in in the whole developer ecosystem in boost license yeah everything is built-in in C++ of course for best performance and efficiency although javascript is used for some parts of the web interface plus of course on the client side of the web but also on the server side of the web interface um it's all based on quite major proven code base Boca libraries have been used for more than 10 years now in all different kinds of applications the Google v8 engine is very widely used especially on the server side with our node.js tool tools then we have Eclipse power which is a simplement ation of the MQTT protocol which we have wrapping in in C++ use sequel light so they can that you can put in small database directly on your device using some well-known technologies from the JavaScript client-side browser-based development like angular jQuery and so on and yeah as a set there is a built-in semi-automatic C++ Java Script bridge which I'm going to talk a bit later on the whole thing once on your typical Linux based devices like Raspberry Pi big bone into edit song and and so on also of course on our custom devices and another nice feature is you can further that everything on your desktop Linux system on or on your OS X system and then easily move your code good advice this is a quick overview of this different sensors that are already implemented and the web services that you can talk to and of course everything is not just done for playing around the background is to build a solid platform that can be used to build professional devices of course and there are different kinds of features that make it possible so for example a big topic is integrate integration of mobile devices with embedded systems and a good way to do this is put a REST API on your device and use the REST API from your mobile apps this is something that you can do you also have a kind of an eps concept on the device so you or your partners and customers can write little apps in JavaScript that run in your device you can build things like app stores around that and in this way product provide additional services and also you can fully customize the web interface so the web interface that you see here is just the default interface but you can fully change it to whatever you need to so to make things more interesting I'm going to show you a quick demo before I'm going into the C++ details of integrating with JavaScript so for the demo I have everything running here on my Mac and also I have a couple of devices with me for example this is sensor or combined sensor for temperature humidity and lightness that uses the ZigBee protocol to talk wirelessly to my Mac and on my Mac here I have a little thump USB stick that access a and point of the the ZigBee Network I also have some nothing here that's called tinker Forge and this is basically a set of modular hardware components that you can use to easily prototype some device without having to burn your fingers with the surgery Aeryn and tink approach is basically an open hardware project so they have designed about 100 different modules and the this rental point of the module is the so called master which is a little device that has a USB billion and the microcontroller and it uses a USB to talk to PC or Mac or any other Linux based device and they basically have a standardized interface to different kinds of sensors again temperature humidity motion sense or also input devices like rotary dials GPS receivers and couple couple more more than one more than 100 different modules at this time and they also have a very nice a PE deaths can be used from 20 different programming languages also of course from C and C++ and we have wrapped is a PE or API also in Makena yo so this is great for getting doing the first step with actual sensors on a desktop system so the first thing when you start mocking I own your device is to look in screen after web interface you can log in there and what they then see is a screen that's very familiar from a smartphone so in basically we have different kind of apps that are running on your device which can be implemented in JavaScript or C++ so for example we have a console viewer that allows you to see all the debug output of your application directly in a browser so when you're doing some playing around with your device you don't have to have an secure session session opening to your device to actually see the log output you can see it directly in your browser then we have an overview we will see all the different devices that are connected to your system so for example here we see the down below this is actually the experience or you can also see there is the current value that the sensor measures and if I put my finger on on the light sensor you should see the illuminance value dropping down to zero or almost zero it takes a little time because the sensor only sends that data every couple of seconds to in order to save battery there's also the think afford sensors visible here again we have fear think of fruits license or GPS sensor which of course doesn't have a GPS signal in here the humidity sensor and here for example that the rotary encoder so if I turn the dial you should see the value change as well then you have the concept of punters so basically the whole makino consists of about 40 different bundles and each panel provides certain features to the whole system and this makes the whole system very modulized extensively so basically in your own device just add the bundles that you actually need and so this makes it very scalable from very small devices to dream bigger devices and you can for example click on one bundle see some meter information about it like the version numbers and the dependency source or the dependency management built in and you can also click on install and install the new bundle and this can be done by taking a punt from my desktop and dragging it here and then bun gets uploaded to the device you can start it and we go back to the start screen you see there's a new icon for the new bundle that you uploaded and it's a go to debt there's actually a large visualization of this rotary encoder here so as I turn this you see also the needle on the dial move and this is actually using a WebSocket connection in order to get the sensor data from here through here to the browser in we're near real-time and another nice thing that we have is the so called brake round big round which is basically a JavaScript editor built into the browser and you can use this to write some JavaScript code then click on run and run the code directly on your device so in this case in this example we have some code that can send SMS messages and in order to do this we were basically the idea behind this demo is we are checking the light sensor and if the light goes be below a certain threshold we are sending someone an SMS message so down here is some code to that defines the co-op function when measure that the value of measured part lighting sensor changes and if the value goes be below a threshold the code Center is a math function and this function basically cause web service provided by video to actually deliver a SMS message so there was a quick overview any questions so far yep and do you planning to support also Bluetooth low-energy devices yes at least on Linux we have some basic support for Bluetooth Low Energy devices built-in it's based on the Linux blue stack and one of the devices that already integrated is the the texas instruments and so tech may be a further heard of it so i can actually use the TSN so tech already with mocking io if they have a bluetooth LE tooth photo adapter that's supported by the linux blues okay thank you yep aiyo may I see the mark in Ohio as a competitor for nodejs into let's say I'm imaging solution where you are dealing with web services you are using machina to get to get to arrest jail and Jason's and after that to pass to see pass pass model to make the hot intensive processing is it possible to do you see is it do you know solutions well like you know I all it is used in such scenario Web Services and back-end with high C C++ but the connection between Web Services and C++ to be done by machina oh so I'm talking about the back end not the device but no voices for not yes yeah you you can create as Pasadena yeah and you can pass through from web to your C++ code you can pass data and to get processes and get it back on callbacks and so on I'm thinking if machina yo it is good for it it was used once into such solution to make to make the the connection between the web and sip as first world here I'm trying to point because I was thinking to that solution when I took in consider also not chase you can use machina also for this kind of things on the server side I don't see machina o as a direct competitor to to no Jess no chase is more on first thing focused on the server side of things yes not so much on on devices although it's also being used on Raspberry Pi and similar devices it's what I asked you if do you see as doable reliable in this part or not I do yeah and basically the main difference between noches and machina OS the amount of c++ basically in machina oughta pick ideas to implement all the backend stuff in c++ and only have JavaScript as a scripting layer for application level business logic kind of stuff and no GS is more of as little as required in C++ but everything else that's why that's why I'm still as he taking in using into my project idea the not just just because I see a limited it's just an idea it's an extension you can do some little things but if you are thinking too complex back-end then they should be done in C++ then I see it a bit trickier maybe this ugly should be just there for for immortal that so another big difference is basically node.js is due to its JavaScript focus basically single threaded and uses a lot of asynchronous i/o and and and events faced architectural additions which makes it very hard to implement certain things that require some computing and stuff like that and machina with more my teeth reading page so you can have multiple threads running their own JavaScript scripts and so on yes well they have some ways to get water yes indeed just an additional credits for usual stuff it's too less it so let's take a look into what's basically on the inside of machina you ready seen these pictures so just quick more information about the different things that are out of these pictures so of course there are the focal libraries which have started in 2004 in the meantime poker has become a pretty nice project we moved to github end of 2012 and since then basically the contributions have kind of exploded and now I'm in a nice situation that machina always basically driven by a community so if it worked we spoke you probably also know Alex who is in the United States and also talks at different conferences there about Poe point what it does is it so he does a lot of the community work right now and I do a bit of the more end decision-making and the longer so much actual coding although I do I'm still the one who is finally responsible for putting the releases out which is kind of the not so fun work and making sure everything works when when the time comes to ease innovation we have about 100 in more than 100 contributors now who have contributed different parts to poco from a few lines of codes for a toxic stew complete libraries that are now part of poco everything is not boost license and you can find it on github and and poco projects a dork the next thing of the Google v8 engine which is also thanks to mostly no GSI quite widely used C++ project it's basically a C++ library that you can integrate into your own code base reasonably easy integrate enjoy on code base there are a few issues that we're going to talk about it has great performance because what it does is it compiles JavaScript to native machine code for Intel and arm and lips architectures and it was under the bsd license which makes it very easy to use in any kind of project open source and commercial then we have the remoting framework which is basically if you have done some work in java or the net there are similar technologies basically frameworks that allows you to call methods across process boundaries and with remoting you can do web services you can do quick very fast efficient tcp based communication and you also use it for the c++ java script over and then there is the open service platform which already talked about and also showed you what what it can do with uploading new bundles and and this kind of things so how can we combine C++ and JavaScript it's actually quite an interesting task to do because we have two very very ring words coming together on the one hand for example javascript is inherently single threaded and uses garbage collection while c++ especially when you're using poke is multi-threaded especially when you're doing things like the web server then there's the question how we can provide C++ objects to your chava script code which is kind of easy if you have a static C++ object that stays there over the entire time of the JavaScript it gets interesting if you allow your Chava script to create JavaScript objects that are actually backed by a C++ object it gets interesting mostly because you don't know in C++ when the JavaScript average character decides to remove the JavaScript object and when this happens you also have to remove to delete a C++ object and v8 has some mechanism built in you can reduce the record weight function when the garbage collector hits hits your object and then in that pocket function you can also destroy your super special object but there is some optimization in v8 if your script ends and there are still objects on the JavaScript heap then the the chava script heap as a whole gets just freed basically and you never get a call back telling you you have to actually delete your corresponding C++ objects so basically what you have to do is you have to manually track every C++ objects that created through JavaScript and when the script ends destroy object this is again in an architecture like no G is not an issue because in no GS the JavaScript no GS runs runs for the entire lifetime of the no GS process so if your process terminates you don't care about basically the de structures of your C++ version of checks being hard but it's very different in machina over a script can run for a limited amount of time for example you can have a script that runs when the the web browser exists a certain resource on your billion web server in this case the script was just for the couple of smack milliseconds it takes to build the result and then it's done and if you imagine having a couple of hundred thousands calls to the web server you have to be very careful about cleaning everything properly up so as I said things quite easy if you just access static objects from a JavaScript you don't have to take care of cleaning things up or at least you just clean your known objects up when the script is done the things get interested interesting when when you actually create objects in JavaScript that have simple pass objects behind them next I'm going to show you how you can actually make C++ objects available to JavaScript using the v8 engine there is the concept in the aid of function and object templates and these are basically C++ classes provided privy a8 that you use in order to make a super puzzle check the available JavaScript and in order to simplify working with that we have created a wrapper class helper class called weapon that you can use to simplify turbo-boosted and basically what you do whenever you want to make a simple pass class available in Java pain to Chava script you create a subclass of the wrapper class for your specific service brass class you have to implement a virtual function that's defined in the wrapper class that's called object template and you also also have of course to implement wrapper methods for every C++ method that you write this is how the wrapper basically looks like as you can see we have a virtual function called the object called object template which you have to override in a subclass and then we have functions like web native web native persistent which is actually a template that dude work for you most of the time so basically what you have to do first is you need to call the rep native function for a static object that's quite easy you just create your object template which you create in your original function in a subclass and then you call the new instance function from the returned v8 object template class in the time you said it's a bit more work if you have to deal with objects data created at runtime in this case we passed the C++ object into the function which recorded the native object and then we again create an object template using the the virtual method we call new instance like in the first part and then what we need to do is we need to create a v8 external object which actually references our C++ object so we put in the native to point to our superclass of chicken here and then we've joined everything what we also have to do is manage all our objects that we create and for this we are using the web native persistent template function where we again pass in a pointer to or a c++ native object then we create a new weak persistent wrapper instance which is a template never show you soon we call the web native that I'll show you earlier and set a few things and this way the C++ objects basically become part of JavaScript and this is the weak persistent wrapper class that takes care of managing the last time of a C++ object as you see here we have a rich history class that basically collects all the C++ objects that were created during JavaScript execution and that takes care of order properly releasing your C++ class objects the way this all looks like is in the java application in a java script you have the charm script object this javascript object has via the v8 external class basically a pointer to a C++ object that resides on the c++ heap there is also the weak persistent repre instance which basically takes care of the last time management which also has a pointer to a C++ object and there is also a reference from a javascript object to the weak persistent web object and what happens is when the garbage collector hits your javascript object the v8 engine will give you a call back to your weak persistent repo object and in this callback function you can delete your JavaScript object this is how it basically looks like for a simple C++ class in the poco libraries we have a class called HTML form which allows you to store the form parameters that you get from the web server and when some of these are the form of passes some parameters and it basically has it has a couple of methods but two methods are has to check if a certain field is there of well is there and get to get the actual value of the field as a string and the JavaScript we call it this way if our form has a field called name we extract the name of the field for that the boko HTML form class we need to provide wrapper function that's called HTML form wrapper which is derived from our wrapper class we have to override the object template function and we have to provide a static method for every C++ method that we want to expose the JavaScript as JavaScript method in this case we have has has field in cat field and in the wheel implementation a couple of more methods and basically what you do is in your object template method a couple of lines of boilerplate code that I'm not going into the details and basically what you do is once you have your chava script object which is created here where your object template you set basically a javascript property for every method that you want to expose in this case we set a property for half-filled and one nano one forget field and then we simply return this object templates to the v8 engine and the other interesting thing is how to implement this wrapper functions like get field when javascript code calls the get field method basically this get field function gets called which gets a v8 function call back info object that contains or the parameters basically a couple of other things we can see if we have the correct number of arguments then what we do is we extract the underlying C++ object in this case the Polka net HTML form using the unwrap native function that's implemented in our replicas then we convert the Chava script arguments to string the name and if an optional second argument is provided we can use the default value and then we just called a c+ first method of our HTML form object the end between the result now this gets a bit boring if you have to do it for hundreds more of C++ classes so what I have done is I have taken basically the remoting framework that I've talked about and sort basically a call from JavaScript to C++ is very similar to a remote method call because you have to do things like this a realization of the c+ pass parameters to something that's digestible per chava script and vice versa and so we're basically using this and what you do is you create the JavaScript test in this case an interface West and so a very simple interface that has an event or signal slot for for value changes so with value change you can define a corporate function either in C plus in C++ that gets called whenever the value changes and you can also you also have a method for getting the current sensor value and then you run everything to a code generator and you end up with lots of source files so basically this is the sensor class and everything else is basically required for doing the nasty work of translating between JavaScript and C++ so basically in a traditional remote procedure call you would have something like a proxy object on the client side that goes with transport the does the actual network communication to a lifter in the listener in the other process which tend us again civilization and cause through a service skeleton through your actual source object in JavaScript basically just becomes the JavaScript wrapper object which invokes by the existing remoting infrastructure your service object and as a result in JavaScript you can obtain yourself object how this is done is shown quickly in a few seconds and then you can define a JavaScript callback function for the value change event and you can also invoke the functions of the sense of a even object like ready or value all without having to do manual implementation of these wrapper classes basically enough for the fun part showing some JavaScript code that's usually used in your machina o based applications so the first question is how can I actually find this C++ object from a JavaScript code and for this purpose we using OSP and always beep has beside the concept of bundles was a concept of services and the service registry and the idea is every bundle can register an hour to a number of services and services can have a name and services can also have an hour to a number of properties that you can use to find the service and basically all the services that are available in Makena Oh have I using this automatic wrapper mechanism so they are also available in JavaScript code so what we do here is we write a little JavaScript module that searches for different kinds of sensors and returns then them inside the JavaScript object so what we do is we go to the with registry which is a global object they will live in your JavaScript and called the find method and they search for a service that has a property I ordered Mac another quantity is luminance so this basically means gives me a light sensor and I don't care whether this is a sickly light sensor or Bluetooth large sensor or some other kind of rights and so I just want some service that acts as a light sensor and this will usually find fewer or more instances of the service and then you can in this case we're just taking the first run that we find and getting the service instance and designing it – or JavaScript object we do the same for temperature and humidity and there's also the possibility if you know the specific name of a service you can also use the find by name method to find very specific instance another thing that you can do is you can access secret light database through the PACU data library which is exposed to JavaScript using the DB session op checks so we create in our script in you a new DB session object also get some configuration data from the applications configuration file which actually uses the poco util abstract configuration framework which some of you may know so this is also available to JavaScript and then we write a simple data logger that basically periodically reach value from the sensor and writes the sensor to a sequel a database and for this we can use the DB session object and execute some secret statement from a JavaScript code it basically works the same as the typical data layer in the in polka libraries we also have a set interval in JavaScript as part of machine IO that allows you to implement implement periodic tasks and in this case we periodically get the current sensor value and write into the database here is another another sample and another thing that you can do is write so called JavaScript circuits which are basically JavaScript scripts that are executed when the web in certain pages on the web server are called and in this case you're writing it a little script that gets the data from the database like here record set is give assertion execute the execute a select statement on the database then pack everything into a JavaScript object convert the javascript object to a JSON document and center JSON document to the browser here is the final sample of how to send data to a web service is an MQTT again MQTT client in machina I always a service so using the service register to find and empty the D client for a specific service in this case Amazon then we create a payload in form of a JSON document and we sent the payload to the server okay let's keep the last sample any questions how do you execute the JavaScript do you create a new thread for every script basically yes okay and all function calls are synchronous not like no choice well we have actually a mixture there are some you have seen there is this event mechanism where we can define kopeck functions so this is basically a way you can have your JavaScript core non-blocking functions and get notified when many various a little better call back but the basic idea is that not everything has to be completely asynchronous because everything is running in its own thread so it makes it a bit easier to write certain things okay thank you so what are capacity requirements to run machina oh is it has it only been used on Linux platform so far or has it been ported to smaller platforms as well running any sort of air close like red X or whatever right now it only works on Linux and Mac OS and theoretically it can also work on Windows but we haven't built a bit infrastructure for that so basically the dakotas is cross-platform but the bit system is not yet completely available for Windows poco has been ported two weeks works and it could also be ported to maybe thread X or something like that but this hasn't been done so far I guess the major issue here is with poco it the amount of flash storage that we have in your device because there is Polk who is using C++ the world C++ infrastructure and so we have a certain minimum size of the executable so basically if you use the poke web server the minimum that you get out and our best estimates about two megabytes and so it's not really suited for very small systems but from there is nothing that would prevent folks from being ported to another artists just basically the work needs to be done and the system requirements should be of course there in regards to especially memory it's not so much an issue of CPU performance so we have actually run poker based applications on a Linux system in advance and then arm7 with im9 with seventy-five megahertz and it was for eight mega eight megabytes of RAM and four megabytes of flesh thank you if I understood this correctly I saw in the last slide all communication between the client and the server is stumped to a string and sent as a string and re-evaluate it on a server mr. true because he had this json stringify there so all the data is put into a string yeah of course it's sent to the server it depends on the specific service did you think so the most cloud services for that provide some IOT services actually use JSON as civilization format and right now I put a little restriction in Makena IO is that we cannot really did good with binary data but again if yeah I could imagine that on the sensors maybe you need to rely on very precise floating-point values and that could be a bit of a problem right because when you put a floating point value into string and reevaluate it again afterwards and put it back to float on a server it could lose precision and could not be so you can has ever been a problem or for most sensors actually it's another problem because if you take typical temperature sensors they're not very precise yeah and that's it don't need more than than two fractional okay so precision is issues are it could be a fourth for for certain things it could be an issue but then you again you always have the possibility of doing this kind of things in C++ and and use the full floating-point but but you can transport the value between the client and the server also we are non string or via binary if you have protocol that supports binary so this could be used okay thanks okay if you still have any more questions I'll be around for the afternoon so you can grab me outside and

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *