Cyber-Physical and IoT Systems
Transcript of Cyber-Physical and IoT Systems
Cyber-Physical and IoT Systems FFrroomm SSoocckkeettss ttoo WWooTT
TThhee BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm
Προηγμένες Τεχνικές Προγραμματισμού
Διαδίκτυο των Αντικειμένων (IoT) Εργαστηριακή Άσκηση
Kleanthis Thramboulidis Prof. of Software and System Engineering
Electrical and Computer Engineering
University of Patras
https://sites.google.com/site/thramboulidiskleanthis
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 2/68 21/05/2019
Cyber-Physical and IoT Systems
FFrroomm SSoocckkeettss ttoo WWooTT
TThhee BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm
22ηη ΈΈκκδδοοσσηη,, 22001188
KKlleeaanntthhiiss TThhrraammbboouulliiddiiss
CCooppyyrriigghhtt ©© 22001166,, 22001177,, 22001188,, 22001199 KKlleeaanntthhiiss TThhrraammbboouulliiddiiss
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 3/68 21/05/2019
ΠΡΟΛΟΓΟΣ
Το Διαδίκτυο των Αντικειμένων (Internet of Things-IoT) εισάγει ένα σύνολο από
νέες Τεχνολογίες που επηρεάζουν την διαδικασία ανάπτυξης εφαρμογών και
κυβερνοφυσικών συστημάτων (cyber-physical systems) και αποτελεί μια πρόκληση
για το Electrical and Computer Engineering.
H εξοικείωση με τις βασικές έννοιες και τις τεχνολογίες του IoT αποτελεί σημαντικό
εφόδιο για έναν Electrical and Computer Engineer.
Η άσκηση αυτή αναπτύχθηκε σε πρώτη φάση για να χρησιμοποιηθεί στα πλαίσια του
μαθήματος Προηγμένες Τεχνικές Προγραμματισμού, του 4ου έτους του
Προγράμματος Σπουδών του Τμήματος Ηλεκτρολόγων Μηχανικών και Τεχνολογίας
Υπολογιστών της Πολυτεχνικής Σχολής του Πανεπιστημίου Πατρών. Στη συνέχεια
επεκτάθηκε για να καλύψει ανάγκες του μαθήματος Διαδίκτυο των Αντικειμένων
(IoT) του 5ου έτους του προγράμματος Σπουδών του Τμήματος Ηλεκτρολόγων
Μηχανικών και Τεχνολογίας Υπολογιστών.
Αισθάνομαι την ανάγκη να ευχαριστήσω τους φοιτητές του τμήματος που
συνεισέφεραν με τον ένα ή τον άλλο τρόπο στην ανάπτυξη της άσκησης αυτής. More
specifically thanks are due to Theodoros Foradis for the development of the
Temperature Sensor PCB, Socrates Zouras for the development of the TempSensor
library that simplifies the integration with the Temperature Sensor PCB, Giannis
Bouloubasis for his contribution in the development of the generic state machine and
Hlias Tsitsikas for his contribution to the definition of dependencies of a project from
GitHub.
Μπορείτε να πάρετε μια εικόνα αξιοποίησης των τεχνολογιών του Διαδικτύου
Αντικειμένων (ΙοΤ) της ομάδας Software and System Engineering ανατρέχοντας
στους παρακάτω ιστότοπους και ιστοσελίδες.
https://sites.google.com/site/UML4IoT/home
https://sites.google.com/view/cyber-physical-microservice/
Case studies
Liqueur Plant System
https://sites.google.com/site/uml4iot/liqueur-plant-case-study
IKEA Gregor Chair Assembly System
https://sites.google.com/view/cyber-physical-microservice/gregor-chair-case-
study?authuser=0
Στο δημοσιευμένο έργο που έχει σχέση με το αντικείμενο της άσκησης
περιλαμβάνονται μεταξύ των άλλων και οι παρακάτω εργασίες, τις οποίες μπορείτε
να βρείτε στην ιστοσελίδα https://sites.google.com/site/thramboulidiskleanthis/papers:
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 4/68 21/05/2019
K. Thramboulidis, D. Vachtsevanou, I. Kontou, “CPuS-IoT: A cyber-physical microservice and IoT-based
framework for manufacturing assembly systems” Annual Reviews in Control, March 2019. K. Thramboulidis, D. Vachtsevanou, A. Solanos, “Cyber-Physical Microservices:An IoT-based Framework for Manufacturing Systems", 1st IEEE International Conference on Industrial Cyber-Physical Systems (ICPS 2018), Saint Petersburg, Russia, May 15-18, 2018. K. Thramboulidis, I. Kontou, D. Vachtsevanou, “Towards an IoT-based Framework for Evolvable Assembly Systems", 16th IFAC Symposium on Information Control Problems in Manufacturing (INCOM 2018), 11-13 June 2018, Bergamo, Italy.
K. Thramboulidis, P. Bochalis, J. Bouloumpasis, “A framework for MDE of IoT-BasedManufacturing Cyber-Physical Systems”, The 7th International Conference on the Internet of Things (IoT 2017), October 22–25, 2017, Linz, Austria. Foradis, T. and Thramboulidis, K. (2017) From Mechatronic Components to Industrial Automation Things: An IoT Model for Cyber-Physical Manufacturing Systems. Journal of Software Engineering and Applications, 10, 734-753. https://doi.org/10.4236/jsea.2017.108040 F. Christoulakis, K. Thramboulidis, “IoT-based Integration of IEC 61131 Industrial Automation Systems”, IEEE 25th Inter. Symposium on Industrial Electronics (ISIE 2016), Santa Clara, CA, June 2016. K. Thramboulidis, F. Christoulakis, “UML4IoT—A UML-based approach to exploit IoT in cyber-physical manufacturing systems”, Computers In Industry, Volume 82, October 2016, Pages 259–272.
Μια πλήρη λίστα εργασιών μπορείτε να βρείτε στην ιστοσελίδα
https://sites.google.com/site/thramboulidiskleanthis/papers
Για κάθε παρατήρηση, σχόλιο, διόρθωση, μπορείτε να απευθυνθείτε στο email
Ο Συγγραφέας
Kleanthis Thramboulidis
Prof. of Software and System Engineering
Electrical and Computer Engineering
University of Patras https://sites.google.com/site/thramboulidiskleanthis
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 5/68 21/05/2019
Περιεχόμενα
1. Εισαγωγή ................................................................................................................ 7 2. Στόχος ..................................................................................................................... 7 3. Προετοιμασία ......................................................................................................... 8
4. Διαδικασία εκτέλεσης ............................................................................................. 9 5. 1η έκδοση BPMS - Centralized application ......................................................... 10 6. Δράση 1η - Ανάπτυξη echo Client/Server με χρήση sockets ............................... 12 7. Δράση 2η - Client on Raspberry ........................................................................... 12 8. 2η έκδοση BPMS as Distributed application ........................................................ 13
8.1 localhost ........................................................................................................ 13 8.2 Using Raspberry ............................................................................................ 13 8.3 Text based vs. binary protocols ..................................................................... 13
8.4 Implementing Text based protocols in Java .................................................. 14 9. 3η έκδοση BPMS - Centralized with Local storage - Serialization ...................... 15 10. 4η έκδοση BPMS – Distributed with Local Storage ......................................... 16 11. Δράση 3η - Ανάπτυξη απλού web server με java ............................................. 16 12. Δράση 4η - Servlet Container Apache Tomcat – Jetty ..................................... 16
12.1 Εγκατάσταση του Tomcat στο Eclipse ...................................................... 17
12.2 To Hello World servlet .............................................................................. 17 12.3 Δημιουργία servlet ..................................................................................... 17 12.4 Tomcat ως service ..................................................................................... 18
12.5 Άσκηση ...................................................................................................... 18 13. 5η έκδοση – Centralized using JSON ................................................................ 19
14. 6η έκδοση – Distributed based on JSON ........................................................... 19 15. 7η έκδοση – A proxy-based implementation..................................................... 20
16. Δράση 5η - JDBC API ..................................................................................... 21 16.1 JDBC driver for MS SQL Server .............................................................. 21 16.2 JDBC driver and DBMS ............................................................................ 21
17. 8η έκδοση – Distributed using a DBMS ........................................................... 22 17.1 localhost ..................................................................................................... 22
17.2 remote database (fog computing) .............................................................. 26 18. Δράση 6η - Raspberry – Interfacing με τον φυσικό κόσμο .............................. 28
18.1 Pi4J Project ................................................................................................ 28 18.2 Device I/O API .......................................................................................... 28
18.3 Παράδειγμα χρήσης του GPIO .................................................................. 29 19. 9η έκδοση – Προσθέτοντας αισθητήρες στο Raspberry ................................... 30
19.1 Temperature PCB ...................................................................................... 30 19.2 ΑΣΚΗΣΕΙΣ ................................................................................................ 32
20. Δράση 7η - HTTP server .................................................................................. 34 20.1 http request line ......................................................................................... 34 20.2 A draft http response.................................................................................. 34
20.3 Άσκηση ...................................................................................................... 35 21. Δράση 8η - Jetty Server and Servlet Container ............................................... 36
21.1 Eclipse Jetty servlet container ................................................................... 36 21.2 Ασκήσεις Jetty και TemperaturePCB ........................................................ 36 21.3 Οδηγίες εκτέλεσης Ασκήσεων .................................................................. 37
22. Δράση 9η - OMA Lightweight M2M (LwM2M) ............................................. 47 22.1 Απλός leshan server ................................................................................... 47
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 6/68 21/05/2019
22.2 Απλός leshan Client ................................................................................... 49
22.3 Δουλεύοντας με τον demo leshan server ................................................... 53 23. Εκδοση 10η - Leshan based Temperature PCB ................................................ 57
23.1 Άσκηση TemperaturePCB-6 (TempSencorPCB_LeshanClientV1) .......... 57
23.2 Άσκηση TemperaturePCB-7 (TempSencorPCB_LeshanClientV2) .......... 57 23.3 Άσκηση TemperaturePCB-8 (TempSencorPCB_LeshanClientV3) .......... 57 23.4 Άσκηση TemperaturePCB-9. .................................................................... 57 23.5 Άσκηση TemperaturePCB-10. .................................................................. 57 23.6 Άσκηση TemperaturePCB-11. .................................................................. 57
23.7 Άσκηση TemperaturePCB-12 ................................................................... 57 23.8 Java Application as Leshan server............................................................. 57 23.9 Άσκηση Liqueur Plant Smart Silo ............................................................. 57
24. Δράση 10η - Azure ........................................................................................... 65 25. 11η έκδοση – Distributed based on Cloud ........................................................ 66
25.1 Using the Azure Storage Emulator ............................................................ 66 25.2 Using the Azure Cloud computing platform services. ............................. 66
26. 12η έκδοση – Using Bluetooth .......................................................................... 67 27. 13η έκδοση – Using an Android device for the Health Monitoring App ......... 68 28. 14η έκδοση – Using the Azure IoT Hub ........................................................... 68
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 7/68 21/05/2019
1. Εισαγωγή
Η αρτηριακή πίεση απασχολεί όλο και περισσότερους συνανθρώπους μας, πολλοί
από τους οποίους είναι αναγκασμένοι να την παρακολουθούν συστηματικά.
Μια αναζήτηση στο διαδίκτυο θα μας δώσει διάφορες εφαρμογές που έχουν στόχο να
εξυπηρετήσουν την ανάγκη αυτή. Για παράδειγμα, η ιστοσελίδα
https://play.google.com/store/apps/details?id=com.szyk.myheart&hl=el δίνει μια
εφαρμογή Android που “βοηθά στον έλεγχο της αρτηριακής πίεσης και βελτιώνει τη
μέθοδο της αντιμετώπισης της υπέρτασης”.
Το σχήμα 1 δίνει ένα γενικό πλαίσιο με πιθανές εναλλακτικές διαμορφώσεις
(configuartions) ενός συστήματος παρακολούθησης αρτηριακής πίεσης (Blood
Pressure Monitoring System – BPMS).
Fig. 1. Blood Presure Monitoring System (BPMS).
Για την ομαλότερη διεξαγωγή της εργαστηριακής άσκησης μελετήστε προσεκτικά το
κείμενο αυτό ανατρέχοντας και στις πρόσθετες πηγές που σας παραπέμπει.
2. Στόχος
H εργαστηριακή άσκηση έχει ως στόχο:
• την μελέτη της μετάβασης από τα sockets στο WοΤ μέσα από την ανάπτυξη
ενός κατανεμημένου συστήματος καταγραφής και παρακολούθησης πίεσης με
βάση το μοντέλο client/server,
• την εξέταση και υλοποίηση διαφόρων σεναρίων διαμόρφωσης του
συστήματος, και
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 8/68 21/05/2019
• την αξιοποίηση των Http και του ΟΜΑ Lightweight M2M IoT protocols.
Blood Pressure Monitoring
“The American Heart Association recommends home monitoring for all people with
high blood pressure to help the healthcare provider determine whether treatments are
working. Home monitoring (self-measured blood pressure) is not a substitute for
regular visits to your physician.
Why keep a blood pressure journal?
One blood pressure measurement is like a snapshot. It only tells what your blood
pressure is at that moment. A record of readings taken over time provides a “ time-
lapse” picture of your blood pressure that can help you partner with your physician to
ensure that your treatments to lower high blood pressure (HBP or hypertension) are
working.” Source: American Heart Association
Dedicated APP for smartphones and tablets
“In the attempt to improve doctor-patient interaction and to increase awareness on
hypertension and its associated risks the European Society of Hypertension, in
collaboration with the Italian Society of Hypertension, has published a dedicated
APP for smartphones and tablets.
This is the only APP on hypertension currently validated and supported by the
European Society of Hypertension.” Source: European Society of Hypertension
3. Προετοιμασία
Στην φάση της προετοιμασίας θα πρέπει να
εξοικειωθείτε με το Raspberry Pi το οποίο θα
χρησιμοποιηθεί για την ανάπτυξη του συστήματος.
“The Raspberry Pi is a low cost, credit-card sized
computer that plugs into a computer monitor or
TV, and uses a standard keyboard and mouse.”
Ανατρέξτε στην ιστοσελίδα του Raspberry Pi
http://www.raspberrypi.org/
Σχετικά με την Χρήση του RPi στον χώρο του ΚΥΠΕΣ
Παράκληση όπως η χρήση των raspberry γίνεται αποκλειστικά και μόνο για τους
λόγους του εργαστηρίου. Ως εκ τούτου, δεν θα πρέπει να γίνονται δοκιμές/αλλαγές οι
οποίες δεν περιλαμβάνονται στις εργαστηριακές ασκήσεις και σε όσα αναφέρονται
κατά τις παραδόσεις από τον διδάσκοντα. Πιο συγκεκριμένα:
- MHN Αλλάζετε το password του χρήστη pi
- MHN Αφαιρείτε την SD κάρτα
- MHN Κάνετε update/upgrade το λειτουργικό σύστημα
- MHN Διαγράφετε αρχεία του λειτουργικού συστήματος
Οι συσκευές σας έχουν παραδοθεί λειτουργικές και θα πρέπει να παραμείνουν στην
ίδια κατάσταση και μετά το πέρας του εξαμήνου!
Raspberry Pi and Secure Shell (SSH)
“You can access the command line of a Raspberry Pi remotely from another computer
or device on the same network using SSH.”
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 9/68 21/05/2019
https://www.raspberrypi.org/documentation/remote-access/ssh/
Για πρόσβαση από windows μπορείτε να χρησιμοποιήσετε PuTTY
https://www.raspberrypi.org/documentation/remote-access/ssh/windows.md
Επιπλέον σας συνιστώ να κάνετε μια επανάληψη στο υλικό του μαθήματος
Αντικειμενοστρεφής Τεχνολογία το σχετικό με την υποστήριξη της Java για
Networking.
4. Δομή και Διαδικασία εκτέλεσης
H άσκηση είναι οργανωμένη σε ένα σύνολο δράσεων που αποσκοπούν στην
εγκατάσταση και εξοικείωση με εργαλεία που χρησιμοποιούνται για την ανάπτυξη
των διάφορων εκδόσεων του συστήματος. Η κάθε έκδοση αποτελεί μια αυτοτελή
πλήρως λειτουργική διαμόρφωση ενός πιθανού BPMS. Κάθε έκδοση συνήθως
χρησιμοποιείται ως βάση για την ανάπτυξη μιας περισσότερο λειτουργικής επόμενης
έκδοσης. Για την πιο αποτελεσματική εκτέλεση της εργαστηριακής άσκησης
συνιστάται η εκτέλεση των δράσεων και της ανάπτυξης των διαφόρων εκδόσεων με
την σειρά που δίνονται στη συνέχεια. Η σειρά μπορεί να διαφοροποιηθεί ανάλογα με
τα επιδιωκόμενα αποτελέσματα και τον τελικό στόχο αυτού που εκτελεί την άσκηση.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 10/68 21/05/2019
5. 1η έκδοση - BPMS as Centralized application
H πρώτη αυτή έκδοση του συστήματος BPMS αποτελείται από ένα απλό πιεσόμετρο
και μια εφαρμογή, την Health monitoring application, η οποία τρέχει σε έναν
προσωπικό υπολογιστή (σχήμα 2).
Ο χρήστης του πιεσόμετρου καταχωρεί στην Health monitoring application τις
μετρήσεις που παίρνει με αυτό.
Fig. 2. Παραδοσιακό Blood Presure Monitoring System (BPMS).
H Health monitoring application επιτρέπει στον χρήστη να δει στην οθόνη τις
μετρήσεις που έχουν καταχωρηθεί, να τις εκτυπώσει, να τις ταξινομήσει, να
αναζητήσει συγκεκριμένη καταχώρηση, να τις σώσει σε αρχείο και να φορτώσει
μετρήσεις από αρχείο.
Τα στοιχεία που καταχωρούνται για κάθε μέτρηση είναι:
• Ημερομηνία,
• Ώρα,
• συστολική πίεση (mmHg), -systolicBP
• διαστολική πίεση (mmHg), - diastolicBP
• σφυγμός/λεπτό, heartRate
• user id
C version
Σε πρώτη φάση και προαιρετικά μπορείτε να υλοποιήσετε την Health monitoring
application με βάση το procedural programming paradigm και την C.
Στην περίπτωση αυτή αξιοποιήστε την ανάλογη άσκηση Address Book με το υλικό
που θα βρείτε
a) στην σειρά διαφανειών (AddressBookLab.pdf) και
b) στην τεκμηρίωση της πρώτης εργαστηριακής άσκησης που θα βρείτε στην
ιστοσελίδα (web page) με τις εργαστηριακές ασκήσεις στον ιστότοπο (web
site) του μαθήματος.
H έκδοση αυτή έχει ως στόχο την επανάληψη στην γλώσσα C, η οποία θα
χρησιμοποιηθεί στη δεύτερη ενότητα του μαθήματος “low level or system
programming” για την οποία και αποτελεί προετοιμασία.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 11/68 21/05/2019
Στα πλαίσια της επανάληψης στην C, την οποία σας συνιστώ, χρήσιμη θα σας φανεί
όσον αφορά την εφαρμογή της αφαιρετικότητας στις διεργασίες (procedural
abstraction), της οργάνωσης του προγραμματος, και της δόμησης της main, η
εργαστηριακή άσκηση “Lab 1. [Reverse Polish notation Calculator using C]” την
οποία θα βρείτε στην παρακάτω ιστοσελίδα
http://seg.ece.upatras.gr/Courses/OT/OTExercises.htm
Java version
Σε ένα επόμενο βήμα θα αναπτύξετε την Health monitoring application με βάση το
αντικειμενοστρεφές παράδειγμα και την γλώσσα Java.
H έκδοση αυτή σας συνιστώ να υποστηρίζει τα παρακάτω:
1. Εισαγωγή μέτρησης
2. Εμφάνιση στην οθόνη όλων των καταχωρημένων μετρησεων
3. Αποθήκευση των μετρήσεων σε αρχείο (BPBook).
4. Φόρτωση μετρήσεων από αρχείο.
Στην φάση αυτή δεν απαιτείται να δημιουργήσετε την δική σας λίστα. Αξιοποιήστε
την βασική βιβλιοθήκη της Java.
Επιπλέον δεν απαιτείται να δημιουργήσετε γραφική διεπαφή για την εισαγωγή
μέτρησης καθώς σε επόμενες φάσεις αυτό δεν θα είναι απαραίτητο.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 12/68 21/05/2019
6. 1η Δράση - Ανάπτυξη echo Client/Server με χρήση
sockets
Αναπτύξτε έναν απλό TCP echo server. Μπορείτε να αξιοποιήσετε το υλικό
(διαφάνειες, εργαστηριακή άσκηση) του μαθήματος Αντικειμενοστρεφούς
Τεχνολογίας που αφορούσε το συγκεκριμένο θέμα.
Αναπτύξτε έναν TCP echo client για να ελέγξετε και να επιδείξετε τη λειτουργία του
Server σας.
Σε πρώτη φάση αναπτύξτε και ελέγξτε τη λειτουργικότητα server και client στο
localhost.
Τροποποιήστε τον server σας ώστε να είναι σε θέση να εξυπηρετεί ταυτόχρονα
περισσότερους από έναν client.
7. 2η Δράση - Client on Raspberry
Αξιοποιήστε το Raspberry Pi σας και διαμορφώστε το σύστημα σας σύμφωνα με το
σχήμα 3. Στους σταθμούς εργασίας του ΚΥΠΕΣ έχετε τη δυνατότητα να
χρησιμοποιήσετε τα ports 6601-6620.
Fig. 3. TCP Client/Server System.
Την IP του Raspberry Pi σας μπορείτε να βρείτε χρησιμοποιώντας την υπηρεσία που
παρέχει το servlet RPIIdentificationServer/RPIServer το οποίο τρέχει στον server
segroot.ece.upatras.gr στο port 8081 (Deprecated). Εναλλακτικά χρησιμοποιήστε την
ίδια υπηρεσία από το IP : 150.140.189.34 (host name : ssegvm1.ece.upatras.gr)
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 13/68 21/05/2019
8. 2η έκδοση - BPMS as Distributed application
Στην 2η έκδοση του συστήματος ο χρήστης δεν θα έχει την ευθύνη να καταχωρεί την
μέτρηση στην health monitoring application αλλά αυτό θα γίνεται αυτόματα. Η
μέτρηση θα μεταφέρεται αυτόματα από το πιεσόμετρο στην health monitoring
application.
Πιο αναλυτικά το σύστημα BPMS θα αποτελείται από μια εφαρμογή που θα
εξομοιώνει τη λειτουργία του πιεσόμετρου, θα δημιουργεί δηλαδή εικονικές
μετρήσεις τις οποίες λειτουργώντας ως client θα τις αποστέλλει στην health
monitoring application η οποία θα λειτουργεί ως server.
Την έκδοση αυτή θα αναπτύξετε σε 2 φάσεις:
Α) αξιοποιώντας το localhost, και
Β) αξιοποιώντας ένα Raspberry για την εκτέλεση του client και Network sockets για
την επικοινωνία μεταξύ server και client.
8.1 localhost
Αναπτύξτε και ελέγξτε τη λειτουργικότητα των client και server στο localhost.
O χρήστης να έχει μέσω μιας εφαρμογής (με Gui), την Health Monitoring
Application, βασικές λειτουργίες διαχείρισης των μετρήσεων.
8.2 Using Raspberry
Χρησιμοποιήστε το Raspbery Pi για την εκτέλεση του client. Διαμορφώστε το
σύστημα σας όπως στο σχήμα 4. Ελέγξτε τη λειτουργικότητα του συστήματος σας.
Fig. 4. TCP Client/Server based Blood Pressure Monitoring System.
8.3 Text based vs. binary protocols
Different network applications have different methods of encapsulating data. One
method very common with Internet protocols is a text oriented representation that
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 14/68 21/05/2019
transmits requests and responses as lines of ASCII text, terminated by a newline
character (and usually a carriage return character).
Typical examples are FTP (File Transfer Protocol), SMTP (Simple Mail Transfer
Protocol), or the finger protocol.
Text based protocol (or plain text protocol) is a communication protocol whose
content representation is in human-readable format.
Text-based protocols are typically optimized for human parsing and interpretation,
and are therefore suitable whenever human inspection of protocol contents is required,
such as during debugging and during early protocol development design phases.
Binary protocols have inherent benefits for use in a computer environment (such as
ease of mechanical parsing and improved bandwidth utilization). Binary protocols
have the advantage of terseness, which translates into speed of transmission and
interpretation. Source: wikipedia
8.4 Implementing Text based protocols in Java
OutputStream is used for writing data from your application to the other end of the
socket. OutputStream out = socket.getOutputStream();
It is convenient to wrap the OutputStream a more convenient class like
DataOutputStream or OutputStreamWriter. For performance reasons, … buffer it.
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.write("TURN_LEFT\r\n");
writer.flush(); // flush the output so you’ll be sure the
//command is sent over the network
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 15/68 21/05/2019
9. 3η έκδοση - BPMS as Centralized with Local storage
(Using Serialization)
Επεκτείνατε τη λειτουργικότητα της 2ης έκδοσης της Health Monitoring Application
σας επιτρέποντας στον χρήστη να αποθηκεύει και φορτώνει βιβλία μετρήσεων
(BPMeasurementsBook).
Αξιοποιήστε την υποστήριξη της Java για μετατροπή της κατάστασης αντικειμένου
σε byte stream. Αξιοποιήστε το σχετικό ένθετο για Serialization.
Serialization
Serialization is the process of translating data structures or object state into a format that can
be stored (for example, in a file or memory buffer, or transmitted across a network connection
link) and reconstructed later in the same or another computer environment.
When the resulting series of bits is reread according to the serialization format, it can be used
to create a semantically identical clone of the original object.
For many complex objects, such as those that make extensive use of references, this process is
not straightforward.
Serialization of object-oriented objects does not include any of their associated methods with
which they were previously linked.
The process of serializing an object is also called marshalling an object. The opposite
operation, extracting a data structure from a series of bytes, is deserialization (which is also
called unmarshalling). Source: Wikipedia.
Binary and XML Serialization
“Either binary or XML serialization can be used. In binary serialization, all members, even
those that are read-only, are serialized, and performance is enhanced. XML serialization
provides more readable code, as well as greater flexibility of object sharing and usage for
interoperability purposes.” Source: Microsoft msdn.
Serializable Objects
“A Java object is serializable if its class or any of its superclasses implements either the
java.io.Serializable interface or its subinterface, java.io.Externalizable.
Deserialization is the process of converting the serialized form of an object back into a copy
of the object.
The Java platform specifies a default way by which serializable objects are serialized. A
(Java) class can override this default serialization and define its own way of serializing
objects of that class.
The Object Serialization Specification describes object serialization in detail.” Source: Oracle
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 16/68 21/05/2019
10. 4η έκδοση BPMS – Distributed with Local Storage
Αξιοποιήστε την υποστήριξη της Java για μετατροπή της κατάστασης αντικειμένου
σε byte stream που χρησιμοποιήσατε παραπάνω για την επικοινωνία client/server της
2ης έκδοσης.
Επεκτείνατε την λειτουργικότητα της 2ης έκδοσης της εφαρμογής σας
(κατανεμημένης έκδοσης) προσθέτοντας λειτουργικότητα αποθήκευσης και
φόρτωσης βιβλίου μετρήσεων. Η διαμόρφωση του συστήματος παραμένει η ίδια
(σχήμα 4). Μια πιο ευέλικτη διαμόρφωση διαχωρίζει την λειτουργικότητα
αποθήκευσης (storage server) από την διαχείριση των μετρήσεων (user app).
11. 3η Δράση - Ανάπτυξη απλού web server με java
Αναπτύξτε μια Java εφαρμογή που θα σας επιτρέπει να έχετε μέσα από έναν browser
πρόσβαση σε ιστοσελίδες στο Raspberry Pi σας.
Ένα πολύ απλό ελαφρύ web server (A small web server, less than 4kb in size!) σε
μορφή jar μπορείτε να βρείτε στην ιστοσελίδα http://www.jibble.org/miniwebserver/
12. 4η Δράση - Servlet Container Apache Tomcat –
Jetty
Στα πλαίσια της δράσης αυτής θα χρησιμοποιήσετε τον Apache Tomcat.
Εναλλακτικά, αντί για τον tomcat και με βάση άλλη φιλοσοφία (δες 8η Δράση),
μπορείτε να χρησιμοποιήσετε τον jetty o οποίος είναι ένας http server και servlet
container που μπορεί να ενσωματωθεί στην εφαρμογή σας.
.
Ο Tomcat θα χρησιμοποιηθεί στα
επόμενα βήματα της ανάπτυξης του
συστήματος σας.
Apache Tomcat
“The Apache Tomcat® software is an open source implementation of the Java Servlet, JavaServer
Pages, Java Expression Language and Java WebSocket technologies. The Java Servlet, JavaServer
Pages, Java Expression Language and Java WebSocket specifications are developed under the Java
Community Process.
The Apache Tomcat software is developed in an open and participatory environment and released
under the Apache License version 2. The Apache Tomcat project is intended to be a collaboration of
the best-of-breed developers from around the world. We invite you to participate in this open
development project.
Apache Tomcat software powers numerous large-scale, mission-critical web applications across a
diverse range of industries and organizations. Some of these users and their stories are listed on the
PoweredBy wiki page.
Apache Tomcat, Tomcat, Apache, the Apache feather, and the Apache Tomcat project logo are
trademarks of the Apache Software Foundation.” Source: http://tomcat.apache.org/
Ο Tomcat είναι εγκατεστημένος και στον server με IP 150.140.189.34 και host name
ssegvm1.ece.upatras.gr και αποκρίνεται στο port 8081.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 17/68 21/05/2019
Ελέγξτε αν ο tomcat αποκρίνεται στο παραπάνω port.
Στον server αυτόν τρέχει και η υπηρεσία που έχετε ήδη χρησιμοποιήσει για να πάρετε
την IP του Raspberry σας. Η υπηρεσία παρέχεται από το servlet με όνομα RPIServer
που είναι τοποθετημένο στο ευρετήριο RPIIdentificationServer.
Ελέγξτε στον browser σας το παρακάτω URL
http://ssegvm1.ece.upatras.gr:8081/SsegApt/
Το URI αυτό αποτελεί τον identifier του αρχείου index.html που βρίσκεται στο
ευρετήριο SsegApt.
Το αρχείο index.html σας δίνεται για να το χρησιμοποιήσετε στις δοκιμές που θα
κάνετε με τον Tomcat στα πλαίσια της δράσης αυτής αλλά και στην διαδικασία
ανάπτυξης του δικού σας http server (Δράση 7).
12.1 Εγκατάσταση του Tomcat στο Eclipse
Κατεβάστε από την επίσημη ιστοσελίδα τον Tomcat Server
http://tomcat.apache.org/
Στο ΚΥΠΕΣ ο Tomcat είναι στον δίσκο C.
Ενημερώστε το Eclipse για τον Apache Tomcat Server.
Ακολουθήστε τις οδηγίες που δίνονται στην ιστοσελίδα
Installing Apache Tomcat Server https://www.eclipse.org/webtools/jst/components/ws/1.0/tutorials/InstallTomcat/InstallTomcat.html
Για την ολοκλήρωση της εγκατάστασης του Tomcat στο Eclipse απαιτούνται
ορισμένες ακόμη ενέργειες.
Ακολουθήστε τις οδηγίες στο τμήμα «Copy the ROOT (Default) Web App into
Eclipse» της ιστοσελίδας
Tutorial: Installing Tomcat 7 and Using it with Eclipse http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html
12.2 To Hello World servlet
Κατεβάστε και μελετήστε το HelloWorld example servlet ακολουθώντας τις οδηγίες
στο τμήμα «Import and Test a Sample App» της ιστοσελίδας http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html
Ενημερώστε το project σας ώστε να χρησιμοποιεί τον Tomcat 9.0.
Σε πρώτη φάση δουλεύετε με το localhost. Δοκιμάστε από άλλο σταθμό εργασίας.
12.3 Δημιουργία servlet
Δημιουργήστε το δικό σας servlet. Ακολουθήστε τις οδηγίες στο τμήμα «Create a
New Web App in Eclipse» της ιστοσελίδας http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html
Αναπτύξτε τις υπηρεσίες daytime, echo και reverse ως servlets και επιδείξτε την
χρήση τους.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 18/68 21/05/2019
Τις υπηρεσίες αυτές είχατε χρησιμοποιήσει στα πλαίσια της εργαστηριακής άσκησης
Networking in Java του μαθήματος Αντικειμενοστρεφής Τεχνολογία.
Για ένα πλήρες πιο προχωρημένο παράδειγμα μπορείτε να αξιοποιήσετε την
παρακάτω ιστοσελίδα http://met.guc.edu.eg/OnlineTutorials/JSP%20-%20Servlets/A%20servlet%20example.aspx
12.4 Tomcat ως service
Για την εγκατάσταση του Tomcat στο raspberry ανατρέξτε στην ιστοσελίδα
https://www.element14.com/community/community/raspberry-
pi/raspberrypi_projects/blog/2014/05/02/tomcat-on-my-pi
Για να τρέξετε τον Tomcat ως windows service ανατρέξτε στην ιστοσελίδα
https://tomcat.apache.org/tomcat-9.0-doc/windows-service-howto.html
Μπορείτε να τρέξετε τα servlets σας στον Tomcat ανεξάρτητα από το Εclipse.
Creating your first servlet & Executing it in Tomcat - YouTube
12.5 Άσκηση
Κάντε την πρόταση/σεις σας για αξιοποίηση του Tomcat στα πλαίσια ενός BPMS.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 19/68 21/05/2019
13. 5η έκδοση – Centralized using JSON
Επεκτείνατε την λειτουργικότητα της 3ης έκδοσης της εφαρμογής σας προσθέτοντας
λειτουργικότητα αποθήκευσης και φόρτωσης βιβλίου μετρήσεων με την χρήση του
lightweight data-interchange format JSON.
Βασική απαίτηση για την υλοποίηση της έκδοσης αυτής η κατανόηση του πως γίνεται
encode and decode JSON objects μέσα σε Java κώδικα.
JSON
“JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to
read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript
Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format
that is completely language independent but uses conventions that are familiar to programmers of the
C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These
properties make JSON an ideal data-interchange language.” Source: http://www.json.org/
“JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is
derived from the object literals of JavaScript, as defined in the ECMAScript Programming Language
Standard, Third Edition [ECMA].
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured
types (objects and arrays).
A string is a sequence of zero or more Unicode characters [UNICODE].
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a
value is a string, number, boolean, null, object, or array.
An array is an ordered sequence of zero or more values.
The terms "object" and "array" come from the conventions of JavaScript.
JSON's design goals were for it to be minimal, portable, textual, and a subset of JavaScript.”
Source: http://www.ietf.org/rfc/rfc4627.txt
JSON “was designed to represent structured data in the scripting language JavaScript, but today JSON
is language-independent and all major programming languages provide JSON parsers.
JSON is simpler and less verbose than XML (XML element has a name, and content is enclosed
between pairs of matching tags) [Bray et al. 1997]. However, XML has richer semantics, for
example, XML supports nodes of different kinds and different data types.
The majority of the Web service composition environments that support RESTful services support
JSON.” Source: A. Lemos, et al. “Web Service Composition: A Survey of Techniques and Tools”
14. 6η έκδοση – Distributed based on JSON
Τροποποιήστε την 4η έκδοση ώστε η επικοινωνία client/server να αξιοποιεί το
lightweight data-interchange format JSON
Επεκτείνατε την λειτουργικότητα της 4ης έκδοσης της εφαρμογής σας προσθέτοντας
λειτουργικότητα αποθήκευσης και φόρτωσης βιβλίου μετρήσεων με την χρήση του
lightweight data-interchange format JSON.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 20/68 21/05/2019
15. 7η έκδοση – A proxy-based implementation
To proxy είναι ένα αντικείμενο (shown as proxy in the figure below) που δίνει την
ψευδαίσθηση στον client που είναι χρήστης των υπηρεσιών ενός άλλου αντικειμένου,
του RealSubject, που τυχαίνει να βρίσκεται απομακρυσμένα, ότι αυτό βρίσκεται στο
local space. Το proxy ενθυλακώνει (encapsulates) το communication infrastructure
that is required for the remote access to the real subject. This makes the integration of
the Client with the RealSubject that exists in remote space similar to the case that it
resides on the local space.
Request()
....
Subject Client
Request()
.....
RealSubject
Request()
....
Proxy ..... realSubject->Request() ....
Client
Request()
.....
RealSubject
Request()
....
Proxy ..... realSubject->Request() ....
Request()
.....
RealSubject
Request()
....
Proxy ..... realSubject->Request() ....
Request()
.....
RealSubject
Request()
....
Proxy ..... realSubject->Request() ....
Request()
....
Proxy ..... realSubject->Request() ....
..... realSubject->Request() ....
..... realSubject->Request() ....
..... realSubject->Request() ....
..... realSubject->Request() ....
..... realSubject->Request() ....
Fig. 5. The construct of Proxy
Αξιοποιήστε την κατασκευή του proxy στο BPMS σύστημα σας.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 21/68 21/05/2019
16. Δράση 5η - JDBC API
“JDBC provides an abstraction layer between Java applications and database
servers, so that an application's code does not need to be altered in order for it to
communicate with multiple database formats. Rather than connecting to the database
directly, the applications send requests to the JDBC API, which in turn communicates
with the specified database through a driver that converts the API calls into the
proper dialect for the database to understand.”
To σχήμα 6 δείχνει τον BPM device client να επικοινωνεί με τον storage server μέσω
του JDBC driver.
Fig. 6. Using the JDBC driver to exploit a DBMS.
16.1 JDBC driver for MS SQL Server
Εγκαταστήστε στο σταθμό εργασίας σας τον JDBC driver για τον MS SQL Server.
Microsoft JDBC Driver 6.0 for SQL Server
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
Μια περιληπτική αναφορά στον Microsoft JDBC Driver μπορείτε να βρείτε στην
ιστοσελίδα
Overview of the JDBC Driver
https://docs.microsoft.com/en-us/sql/connect/jdbc/overview-of-the-jdbc-driver
Ανατρέξτε στην ιστοσελίδα
Microsoft JDBC Driver for SQL Server
https://docs.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server
16.2 JDBC driver and DBMS
Αν επιλέξετε να δουλέψετε με άλλο database management system (DBMS) θα πρέπει
να κατεβάσετε τον αντίστοιχο driver.
Για παράδειγμα μπορείτε να χρησιμοποιήσετε ως DBMS:
• JavaDB http://www.oracle.com/technetwork/java/javadb/overview/index.html
• MySQL: https://www.mysql.com/
Για την περίπτωση αυτή ανατρέξτε στην ιστοσελίδα
JDBC Basics: https://docs.oracle.com/javase/tutorial/jdbc/basics/
H MySQL αποτελεί μέρος του δημοφιλούς LAMP stack. A LAMP Stack is an acronym for the set of open-source software that can be used to create dynamic web sites and web applications. LAMP typically consist of the Linux operating system, the Apache HTTP Server, the MySQL relational database management system, and the PHP programming language.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 22/68 21/05/2019
17. 8η έκδοση – Distributed using a DBMS
Αξιοποιήστε τον JDBC driver της Java για την ανάπτυξη ενός Blood Pressure client ο
οποίος θα αποθηκεύει τις μετρήσεις σε ένα DBMS server, π.χ. SQLServer. H
διαμόρφωση του συστήματος στην τελική του έκδοση δίνεται στο σχήμα 7.
Fig. 7. Blood Pressure Monitoring System based on DBMS.
17.1 localhost
Σε πρώτη φάση σας συνιστώ να δουλέψετε σε έναν σταθμό εργασίας με localhost.
Μετά την επιτυχή εκτέλεση του βήματος αυτού μπορείτε να περάσετε στην επόμενη
φάση όπου η database σας θα είναι σε έναν server.
Οδηγίες για την υλοποίηση της localhost έκδοσης
Α. Microsoft SQL Server Express
Κατεβάσετε και εγκαταστήσετε τον Microsoft SQL Server Express Edition (SQL
Server Express).
Microsoft SQL Server 2012 Express is a light weight version of the MS SQL Server.
https://www.microsoft.com/en-us/download/details.aspx?id=29062
Προσέξτε ότι απαιτούνται διάφορες ρυθμίσεις από τον SQL Server Configuration
Manager για την επιτυχή χρήση του SQL Server. Για παράδειγμα θα πρέπει να
ενεργοποιήσετε (Start) τον SQL Server (SQLEXPRESS) που προσφέρετε ως SQL
Service στο σταθμό σας μετά την εγκατάσταση του SQL Server. To σχήμα 8 δείχνει
τις υπηρεσίες SQL server ενός σταθμού όπου φαίνεται πως ο SQLEXPRESS4APT
server είναι ενεργός ενώ ο SQLEXPRESS server δεν είναι.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 23/68 21/05/2019
Fig. 8. Sql server Configuration Manager.
Μπορείτε να αξιοποιήσετε το Microsoft SQL Server Management Studio για να δείτε
και διαχειριστείτε τις βάσεις σας.
To σχήμα 9 δείχνει μια εικόνα από το Microsoft SQL Server Management Studio
όπου διακρίνεται για την βάση SQLEXPRESS4APT o πίνακας
KTBloodPressureBook και τα πεδία από τα οποία αποτελείται μια εγγραφή του.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 24/68 21/05/2019
Fig. 9. Τυπικό Screenshot από το Microsoft SQL Server Management Studio
Β. JDBC Driver - Βασικές έννοιες
Για την επικοινωνία της Java εφαρμογής σας με τον SQL server θα αξιοποιήσετε τον
JDBC driver.
Μελετήστε τα παρακάτω για το JDBC API της Java και ανατρέξτε στις σχετικές
ιστοσελίδες.
JDBC API
The Java Database Connectivity (JDBC) API “is a Java API that can access any kind of tabular data,
especially data stored in a Relational Database.
JDBC helps you to write Java applications that manage these three programming activities:
1. Connect to a data source, like a database
2. Send queries and update statements to the database
3. Retrieve and process the results received from the database in answer to your query”
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 25/68 21/05/2019
Source: https://docs.oracle.com/javase/tutorial/jdbc/overview/
“The JDBC API provides universal data access from the Java programming language. Using the JDBC
API, you can access virtually any data source, from relational databases to spreadsheets and flat files.
JDBC technology also provides a common base on which tools and alternate interfaces can be built.
The JDBC API is comprised of two packages:
• java.sql
• javax.sql
You automatically get both packages when you download the Java Platform Standard Edition (Java
SE) 8.
To use the JDBC API with a particular database management system, you need a JDBC technology-
based driver to mediate between JDBC technology and the database. Depending on various factors, a
driver might be written purely in the Java programming language or in a mixture of the Java
programming language and Java Native Interface (JNI) native methods. To obtain a JDBC driver for a
particular database management system, see JDBC Data Access API.”
Source: https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/
C. BPMS device client
Ως πρώτο βήμα θα πρέπει να εγκαταστήσετε μια σύνδεση με την βάση δεδομένων
σας (DBMS), τον sqslserver στην δική μας περίπτωση.
Αξιοποιήστε την μέθοδο getConnection(String url)της κλάσης DriverManager
του πακέτου java.sql που περιλαμβάνεται στην εκδοση 8 της Java.
Πριν την κλήση της μεθόδου θα πρέπει να διαμορφώσετε το όρισμα της. Η σύνταξη
του URL που προσδιορίζει τον SQL Server είναι η ακόλουθη:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=v
alue[;property=value]]
Δες: https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
Για πληροφορίες σχετικά με την πιστοποίηση μπορείτε να ανατρέξετε στην επόμενη
ενότητα “remote database”.
Αν δουλεύετε με java 9 πιθανότατα θα έχετε πρόβλημα “Caused by: java.lang.ClassNotFoundException: javax.xml.bind.Datatype Converter …”
Θα το αντιμετωπίσετε κάνοντας μεταγλώττιση με το όρισμα
--add-modules java.xml.bind
Για να κάνετε έναν έλεγχο της σύνδεσης σας μπορείτε να αξιοποιήσετε την μέθοδο
getMetaData() πάνω στο στιγμιότυπο Connenction το οποίο σας επέστρεψε η
getConnection(). H getMetaData()σας επιστρέφει ένα στιγμιότυπο που υλοποιεί το
interface DatabaseMetaData το οποίο περιέχει “metadata about the database to which
this Connection object represents a connection”.
Αξιοποιήστε το για να τυπώσετε στην οθόνη σας πληροφορίες σχετικές με την βάση
με την οποία συνδεθήκατε. Η εικόνα 10 δείχνει μια τέτοια εκτύπωση.
Μετά την επιτυχή σύνδεση μπορείτε να στείλετε commands στην DBMS σας. Αυτό
μπορεί να γίνει δημιουργώντας ένα στιγμιότυπο της κλάσης Statement.
Αξιοποιήστε κατάλληλα την μέθοδο createStatement()του Connection interface.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 26/68 21/05/2019
Εχοντας δημιουργήσει ένα στιγμιότυπο που υλοποιεί το interface Statement το
χρησιμοποιείται για να στείλετε τις κατάλληλες commands προς το DBMS σας.
Fig. 10. Τυπική εκτύπωση των Metadata μιας DBMS.
Για τον σκοπό αυτό μπορείτε να αξιοποιήσετε την μέθοδο
executeUpdate(String sql)που ορίζεται από το Interface Statement. Η μέθοδος
εκτελεί το “SQL statement, which may be an INSERT, UPDATE, or DELETE statement or
an SQL statement that returns nothing, such as an SQL DDL statement.”
Αξιοποιήστε την μέθοδο για να δημιουργήσετε πρώτα έναν πίνακα και στην συνέχεια
κάντε εισαγωγή εγγραφών (records). Διαμορφώστε κατάλληλα το sql όρισμα της executeUpdate().
Κλείστε τη σύνδεση σας με την βάση.
conn.close();
Είστε έτοιμοι για να αναπτύξετε τον BPMS user client. Αναπτύξτε σε πρώτη φάση
έναν πολύ απλό BPMS user client ο οποίος απλά θα σας τυπώνει στην οθόνη σας τις
εγγραφές του BPMS πίνακα σας.
17.2 remote database (fog computing)
Εκτελέστε το βήμα αυτό μόνο μετά την εξοικείωση με τον JDBC και το DBMS στο
localhost.
To BPM σύστημα σας έχει τη διαμόρφωση που δίνεται στο σχήμα 7.
Μπορείτε να δημιουργήσετε ένα πίνακα (Table) στον MS SQL Server που είναι
εγκατεστημένος στο ΚΥΠΕΣ σε IP 150.140.189.34 και ακούει στο port 1433.
Στον πίνακα αυτόν ο μετρητής σας (BPM Device) θα καταχωρεί τις μετρήσεις.
O χρήστης μέσω της εφαρμογής Health Monitoring Application θα έχει πρόσβαση
στον πίνακα αυτό για αξιοποίηση των στοιχείων.
Τον πίνακα σας θα δημιουργήσετε στην database με όνομα APTStudents που έχει
δημιουργηθεί για τον σκοπό αυτό.
Εναλλακτικά μπορείτε να χρησιμοποιήσετε την database που έχει δημιουργηθεί για
το Raspberry σας με στοιχεία: Database name: 'RPi' + <RPiNo> + 'DB' π.χ. RPi1DB για το RPi1, και
Account name: 'RPi' + <RPiNo> + 'Account'
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 27/68 21/05/2019
Μπορείτε να αξιοποιήσετε μια από τις δύο ενναλακτικές μεθόδους πιστοποίησης
(authentication modes) χρήστη για την σύνδεση σας με τον MS SQL Server.
α) Windows authentication
Χρησιμοποιούμε την μέθοδο αυτή (τον λογαριασμό των Windows) για την
περίπτωση που ο server τρέχει στο ίδιο σταθμό με τον client. Για ον ορισμό αυτής της
επιλογής προσθέστε την παρακάτω ιδιότητα στο url της αίτησης σύνδεσης σας προς
τον JDBC driver.
integratedSecurity=true
β) SQL Server authentication
Στην περίπτωση αυτή χρησιμοποιούμε για πιστοποίηση τον λογαριασμό μας στον
SQL Server. Το username σας είναι ανάλογο του Raspeberry το οποίο χρησιμοποιείτε
(δες παραπάνω). Το password θα σας δοθεί την ώρα εκτέλεσης του εργαστηρίου.
Enterprise Applications
The Java EE platform is designed to help developers create large-scale, multi-tiered,
scalable, reliable, and secure network applications.
In a multi-tiered application, the functionality of the application is separated into
isolated functional areas, called tiers. Typically, multi-tiered applications have a client
tier, a middle tier, and a data tier (often called the enterprise information systems tier).
The client tier consists of a client program that makes requests to the middle tier.
The middle tier's business functions handle client requests and process application
data, storing it in a permanent datastore in the data tier. Source: http://docs.oracle.com/javaee/6/firstcup/doc/gcrky.html#gcrkr
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 28/68 21/05/2019
18. 6η Δράση – Raspberry: Interfacing με τον φυσικό
κόσμο
Εισαγωγή
To RPi υποστηρίζει την επικοινωνία με τον φυσικό κόσμο με ένα σύνολο από γενικού
σκοπού εισόδους/εξόδους GPIOs (general-purpose input/output). A 40-pin GPIO
header is found on all current Raspberry Pi boards.
Για την επικοινωνία με τον φυσικό κόσμο από το RPi χρησιμοποιώντας Java έχετε
δύο επιλογές
Α) Pi4J Project, και
B) Device I/O API 1.0.
18.1 Pi4J Project
The Pi4J project "is intended to provide a friendly
object-oriented I/O API and implementation libraries
for Java Programmers to access the full I/O
capabilities of the Raspberry Pi platform. This project
abstracts the low-level native integration and interrupt
monitoring to enable Java programmers to focus on
implementing their application business logic."
http://pi4j.com/index.html
Pi4J (by default) uses an abstract pin numbering
scheme to help insulate software from hardware
changes. (see Pin Numbering - Raspberry Pi 3 Model
B http://pi4j.com/pins/model-3b-rev1.html)
Μπορείτε να αξιοποιήσετε και την ιστοσελίδα με
τίτλο Programming the Pi with Eclipse and Java
http://eclipsesource.com/blogs/2014/05/01/programm
ing-the-pi-with-eclipse-and-java/
18.2 Device I/O API
The Device I/O API 1.0 (http://docs.oracle.com/javame/8.0/api/dio/api/index.html):
a) defines a generic peripheral device I/O API for Java applications running on
small embedded devices.
b) is targeted at embedded Java platforms. It is an optional package primarily
targeting the Java Platform, Micro Edition 8 Connected Limited Device
Configuration for small embedded devices. It is also compatible with Java
Platform Standard Edition 8.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 29/68 21/05/2019
Ανατρέξτε στις παρακάτω ιστοσελίδες
Getting Started with the Device I/O API and the Raspberry Pi
https://www.voxxed.com/2014/12/device-io-api/
Java ME 8 + Raspberry Pi + Sensors = IoT World (Part 1)
http://www.oracle.com/technetwork/articles/java/cruz-gpio-2295970.html
Oracle Java ME Embedded Enables M2M Technology in Small Embedded Devices
An open and cross-industry platform for the Internet of Things (IoT) that helps
customers decrease time to market, manage costs, and deliver new capabilities for
embedded devices. Source: https://www.oracle.com/java/java-me-embedded.html
Java ME Embedded Getting Started Guide for the Reference Platform (Raspberry Pi)
https://docs.oracle.com/javame/8.3/get-started-rpi/installing-oracle-java-me-
embedded-software-raspberry-pi-board.htm#MEEPG109
18.3 Παράδειγμα χρήσης του GPIO
Μελετήστε το παράδειγμα που σας δίνεται στην ιστοσελίδα
(http://pi4j.com/example/control.html) για να ελέγξετε ένα led.
Στη συνέχεια τοποθετήστε έναν διακόπτη και ένα led και γράψτε την εφαρμογή που
θα ανάβει το led όταν πατήσετε τον διακόπτη.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 30/68 21/05/2019
19. 9η έκδοση – Προσθέτοντας αισθητήρες στο
Raspberry
Προσθέστε αισθητήρες στο Raspberry σας. Κάντε μια προσπάθεια να ενσωματώσετε
επικοινωνία με τον φυσικό κόσμο στο Blood Pressure Monitoring σύστημα σας.
Στην πιο απλή μορφή διαβάστε έναν αισθητήρα και περιλάβετε την πληροφορία στο
σύστημα σας.
19.1 Temperature PCB
Μπορείτε να δουλέψετε με ένα PCB με Temperature sensor (tempSensor PCB -
σχήμα 11) το οποίο κατασκευάστηκε για την άσκηση αυτή από τον φοιτητή Φοράδη
Θοδωρή. Το σχηματικό διάγραμμα δίνεται στο σχήμα 12.
Βασίζεται πάνω στον Temperature sensor LM335Z
http://grobotronics.com/images/datasheets/lm335.pdf
και τον Analog to Digital Converter MCP3002 - 2.7V Dual Channel 0-Bit A/D
Converter with SPI Serial Interface
http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
Fig. 11. To tempSensor PCB με τονTemperature Sensor και τον A/D Converter.
H επικοινωνία με το RPi βασίζεται πάνω στο Serial Peripheral Interface (spi) Bus.
Δες σχετικό πλαίσιο παρακάτω
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 31/68 21/05/2019
The Serial Peripheral Interface bus is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. The interface was developed by Motorola in the mid 1980s and has become a de facto standard.
SPI devices communicate in full duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing. Multiple slave devices are supported through selection with individual slave select (SS) lines.
Source: Wikipedia
Fig. 12. Σχηματικό διάγραμμα με τον Temperature Sensro και τον A/D Converter.
Για την σύνδεση του RPi με το PCB χρησιμοποιήστε την παρακάτω αντιστοίχιση
RPi TempSensor PCB
P1-19 MOSI
P1-21 MISO
P1-23 CLK
P1-24 CS
Για να κατανοήσετε την σημασία της παραπάνω συνδεσμολογίας θα πρέπει να
ανατρέξετε στο data sheet του MCP3002.
http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
Για την πρόσβαση στο Temperature sensor σε πρώτη φάση μπορείτε να
χρησιμοποιήσετε την κλάση TempSensor, που αναπτύχθηκε από τον φοιτητή
Σωκράτη Ζούρα. H κλάση σας δίνεται με την μορφή αρχείου jar (tsensor.jar)
package apt.lab.rpi.tempsensor
class TempSensor implements AutoCloseable
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 32/68 21/05/2019
Class API TempSensor()
Note that the application needs to be running as root or this will fail. void close()
Call when done with the temperature sensor unless you use TempSensor with try-
with-resources block.
float getKelvin()
Get current temperature in Kelvin. float getCelcius()
Get current temperature in Celcius. float getFahrenheit()
Get current temperature in Fahrenheit.
Η πρόσβαση στα led και του διακόπτες είναι πιο απλή. Αν θέλετε να αποφύγετε την
άμεση χρήση του pi4j project μπορείτε να αξιοποιήσετε την κλάση GPIO
(αναπτύχθηκε και αυτή από τον φοιτητή Σωκράτη Ζούρα), που σας δίνεται με την
μορφή αρχείου jar (gpio.jar).
package apt.lab.rpi.gpio
class GPIO implements AutoCloseable
public enum GPIO.PIN {
GPIO_03,
GPIO_05,
GPIO_07,
GPIO_08,
GPIO_10,
GPIO_11,
GPIO_12,
GPIO_13,
GPIO_15,
GPIO_16,
GPIO_18,
GPIO_22,
GPIO_29,
GPIO_31,
GPIO_32,
GPIO_33,
GPIO_35,
GPIO_36,
GPIO_37,
GPIO_38,
GPIO_40
};
Class API GPIO(PIN pin)
void close()
Call when done with the temperature sensor PCB unless you use GPIO with try-with-
resources block.
void turn_on()
Turns on (+3.3V) the GPIO pin. void turn_off()
Turns off (0.0V) the GPIO pin.
19.2 ΑΣΚΗΣΕΙΣ
Άσκηση TemperaturePCB-1
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 33/68 21/05/2019
Δημιουργήστε μια Java εφαρμογή που θα έχει πρόσβαση στο temperature Sensor
PCB αξιοποιώντας τους sensors and actuators.
Άσκηση TemperaturePCB-2. Remote access using Sockets
Επεκτείνατε την εφαρμογή σας ώστε να σας επιτρέψει να έχετε απομακρυσμένη
πρόσβαση στο temperature Sensor PCB αξιοποιώντας το internet (TCP/IP).
Επιπλέον ασκήσεις πάνω στο temperature Sensor PCB θα συναντήσετε στις επόμενες
ενότητες.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 34/68 21/05/2019
20. 7η Δράση - HTTP server
“The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed,
collaborative, hypermedia information systems.
HTTP has been in use by the World-Wide Web global information initiative since 1990. This
specification defines the protocol referred to as "HTTP/1.1", and is an update to RFC 2068
[33].” Hypertext Transfer Protocol -- HTTP/1.1 RFC 2616
Αναπτύξτε τον δικό σας στοιχειώδη Http server.
Χρησιμοποιήστε ως βάση τον TCP server που αναπτύξατε στην δράση 1. Επεκτείνετε
την λειτουργικότητα του ώστε να λειτουργεί ως στοιχειώδης HTTP server.
Ως πρώτο βήμα μετατρέψτε τον TCP server ώστε να δέχεται ένα GET HTTP request
και να αποκρίνεται σε αυτό “by returning response messages that contain the
requested resource”. https://www.httpwatch.com/httpgallery/introduction/
20.1 http request line
Διαβάστε και τυπώστε την πρώτη γραμμή του http request. «The first line of the message, known as the request line, contains:
• The HTTP method. (See 6. HTTP Methods for more information)
• The relative URL of the resource or a full URL if you are using an HTTP proxy
• The version of HTTP that is being used. Most modern HTTP clients and servers will
use HTTP version 1.1 as defined in RFC 2616.»
Για να διαβάσετε το http request μπορείτε να χρησιμοποιήσετε έναν BufferedReader
όπως παρακάτω:
BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
20.2 A draft http response
Ανοίξτε το αρχείο που προσδιορίζεται στο http request για να διαβάσετε το
περιεχόμενο του και να το στείλετε ως content στο http response.
“The web server's response message has a similar structure, but is followed by the
contents of the HTML page”
Για την αποστολή του http response μπορείτε να χρησιμοποιήσετε έναν PrintWriter
όπως παρακάτω:
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
Για την περίπτωση που το αρχείο δεν υπάρχει στείλτε ως header στο response "HTTP 404"
Κάντε flush τον PrintWriter.
Για την αποστολή των headers στην περίπτωση που το αρχείο υπάρχει και είναι txt ή
html χρησιμοποιήστε τα παρακάτω:
out.println("HTTP/1.0 200"); out.println("Content-type: text/html");
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 35/68 21/05/2019
out.println("Content-length: " + <file_length>); out.println("");
Συνεχίστε με την αποστολή του περιεχομένου του αρχείου διαβάζοντας και
στέλνοντας γραμμή προς γραμμή το αρχείο σας.
Τερματίστε με ένα
out.println("");
Χρησιμοποιήστε ως client τον Microsoft edge.
Έχετε ολοκληρώσει ένα πολύ στοιχειώδη http server.
Αν θέλετε να βελτιώσετε τον http server σας, θα βρείτε αρκετό υλικό στο διαδίκτυο.
Για παράδειγμα η παρακάτω ιστοσελίδα
http://hc.apache.org/httpcomponents-core-ga/index.html
δίνει ένα σύνολο από χαμηλού επιπέδου «HTTP transport components that can be
used to build custom client and server side HTTP services with a minimal footprint»
(HttpCore).
20.3 Άσκηση
Κάντε την πρόταση/σεις σας για αξιοποίηση του http server στα πλαίσια ενός BPMS.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 36/68 21/05/2019
21. 8η Δράση - Jetty Server and Servlet Container
21.1 Eclipse Jetty servlet container
Ανταλλακτικά, αντί για τον tomcat και με βάση άλλη φιλοσοφία (δες ένθετο
παρακάτω), μπορείτε να χρησιμοποιήσετε τον jetty o οποίος είναι ένας http server και
servlet container που μπορεί να ενσωματωθεί στην εφαρμογή σας.
Jetty - Servlet Engine and Http Server
Eclipse Jetty provides a Web server and javax.servlet container, plus support for
HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These
components are open source and available for commercial use and distribution.
Source: https://www.eclipse.org/jetty/
Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your
application." What this means is that as an alternative to bundling your application as
a standard WAR to be deployed in Jetty, Jetty is designed to be a software component
that can be instantiated and used in a Java program just like any POJO. Put another
way, running Jetty in embedded mode means putting an HTTP module into your
application, rather than putting your application into an HTTP server.
Source: https://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
21.2 Ασκήσεις Jetty και TemperaturePCB
Άσκηση Jetty-1. Jetty as Standalone server
Τρέξτε τον jetty ως standalone server.
Στην επόμενη ενότητα δίνονται οδηγίες για την εκτέλεση των ασκήσεων. Ανατρέξτε
σε αυτές μόνο μετά από την δική σας προσπάθεια επίλυσης.
Άσκηση Jetty-2. Jetty embedded in the application
Αναπτύξτε μια Java εφαρμογή που θα δέχεται ένα GET request από έναν browser και
θα απαντά «Hello world». Αξιοποιήστε τον Jetty.
Προσθέστε τις υπηρεσίες daytime, echo και reverse και επιδείξτε την χρήση τους.
Άσκηση TemperaturePCB-3. Http based TemperatureSensorPCB using Jetty
and the Temperature Sensor PCB Emulator
Επεκτείνατε την εφαρμογή σας της άσκησης jetty-2 με στόχο να σας επιτρέπει να
έχετε πρόσβαση από έναν browser στο temperature Sensor PCB Emulator (δες
σχετικό πλαίσιο παρακάτω).
Ενναλακτικά μπορείτε να επεκτείνατε την εφαρμογή της άσκησης TemperaturePCB-
2 ώστε να σας επιτρέψει να έχετε απομακρυσμένη πρόσβαση στο temperature PCB
Emulator αξιοποιώντας το www και πιο συγκεκριμένα τον Jetty server. Μια επιλογή
είναι να χρησιμοποιήσετε τον Jetty σε συνδυασμό με τις ασκήσεις Jetty-1, Jetty-2,
Jetty-3 της 8ης Δράσης
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 37/68 21/05/2019
Αξιοποιήστε τις υπηρεσίες που προσφέρει ο server σας για να αναπτύξετε μια
εφαρμογή που θα αξιοποιεί τις υπηρεσίες του temperature Sensor PCB.
Temperature Sensor PCB Emulator
Μπορείτε να αναπτύξετε έναν emulator του temperature Sensor PCB που θα σας
επιτρέπει να δουλευετε και να ελέγχετε την εφαρμογή σας χωρίς να χρειάζεται να
έχετε συνεχώς το φυσικό temperature Sensor PCB. Η παρακάτω εικόνα δείχνει το gui
ενός τέτοιου απλού emulator στον οποίο έχει προστεθεί ένα display για να εμφανίζει
την τιμή του αισθητήρα θερμοκρασίας για διευκόλυνση της διαδικασίας ελέγχου της
εφαρμογής.
Άσκηση TemperaturePCB-4. Http based TemperatureSensorPCB using Jetty
and the physical Temperature Sensor PCB
Αναβαθμίστε την προηγούμενη άσκηση αντικαθιστώντας τον emulator με το φυσικό
Temperature Sensor PCB.
Άσκηση TemperaturePCB-5. Http based TemperatureSensorPCB application
Περιγράψτε μια εφαρμογή που θα αξιοποιεί τις υπηρεσίες του φυσικού αντικειμένου.
Αναπτύξτε την εφαρμογή.
21.3 Οδηγίες εκτέλεσης Ασκήσεων
Άσκηση Jetty-1
Κατεβάστε (https://www.eclipse.org/jetty/download.html) και εγκαταστήστε τον
Jetty. Η διαδικασία της εγκλατάστασης περιλαμβάνει μόνο την αποσυμπίεση του
αρχείου σε ένα κατάλογ.
Για την ενεργοποίηση του server τρέξτε το start.jar. O server θα ενεργοποιηθεί και θα
ακούει στο port 8080. Στην οθόνη θα δείτε μια εκτύπωση που θα έχει ως τελευταία
γραμμή κάτι ανάλογο με το παρακάτω
2018-10-13 00:11:50.691:INFO:oejs.Server:main: Started @7916ms
Δοκιμάστε με τον browser σας αν είναι ενεργός.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 38/68 21/05/2019
Θα δείτε κάτι σαν το παρακάτω καθώς δεν έχει γίνει deploy καμία εφαρμογή στο root
context.
Δοκιμάστε να κάνετε deploy μια web εφαρμογή στον server σας. Στο ευρετήριο
demo-base/webapps υπάρχουν ενδεικτικές web εφαρμογές σε μορφή war αρχείων.
Επιλέξτε μια π.χ. την test.war και αντιγράψτε την στον κατάλογο webapps.
Στον Browser σε απόκριση ενός Get request στο localhost:8080/ θα πάρετε την λίστα
των εφαρμογών του server όπως παρακάτω
Στείλτε ένα Get request στο localhost:8080/async-rest και θα πάρετε ως απάντηση
στον browser την παρακάτω οθόνη
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 39/68 21/05/2019
Μπορείτε να αλλάξετε το port του server στο αρχείο start.ini.
Άσκηση Jetty-2
Μπορείτε να ενσωματώσετε τον jetty σε μια java εφαρμογή σας. Στην συνέχεια
περιγράφουμε τα βήματα για την ανάπτυξη μιας java εφαρμογής HelloWorld η οποία
θα δέχεται GET request στο /helloworld και θα αποκρίνεται στέλνοντας HelloWorld.
Θα αναπτύξουμε την εφαρμογή ως Maven project (δες ένθετο παρακάτω).
Δημιουργήστε ένα Maven project και ορίστε groupId και artifactId και επιλέξτε
Finish. Μπορείτε να βάλετε org.upatras.sseg.iot και jetty-test.
To project σας θα δημιουργηθεί και μπορείτε να δείτε και να αλλάξετε τα παραπάνω
στοιχεία ανοίγοντας το αρχείο pom.xml (project object model).
Θα πρέπει να ορίσετε τα dependencies του project σας με τα jetty-server και jetty-
servlet.
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects each with their own Ant build files that were all slightly different and JARs were checked into CVS. We wanted a standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects.
The result is a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project. https://maven.apache.org/
Για να κάνετε ον ορισμό αυτό θα πρέπει πρώτα να κάνετε ένα update των central
dependencies του Maven όπως παρακάτω:
1. on the top menu bar, open Window -> Show View -> Other
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 40/68 21/05/2019
2. in the Show View window, open Maven -> Maven Repositories Θα δείτε στην οθόνη
3. right-click on Global Repositories and select Go Into 4. right-click on "central (http://repo.maven.apache.org/maven2)" and
select "Rebuild Index". it will take a while to complete the download 5. Once completed, Right-click on the project -> Maven -> Add
Dependency and start typing the name of the project you want to import (e.g. "jetty-server"). 1. The search results will auto-fill in the "Search Results" box below.
Επιλέξτε τον Jetty-server (org.eclipse.jetty).
Επαναλάβατε για το jetty-servlet.
Μπορείτε να δείτε τα dependencies του project σας στο αρχείο pom.xml όπως
φαίνεται παρακάτω.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 41/68 21/05/2019
Η εναλλακτικά σε xml μορφή επιλέγοντας το pom.xml
Δημιουργήστε ένα πακέτο στο ευρετήριο src/maim/java και δημιουργήστε μέσα την
κλάση με την main η οποία θα δημιουργεί ένα jetty server και θα τον εκκινεί.
public class EmbeddedJettyServerTest { public static void main(String[] args) { // TODO Auto-generated method stub Server server = new Server(8080);
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 42/68 21/05/2019
ServletContextHandler sch = new ServletContextHandler(server, "/servlets"); sch.addServlet(HelloWorldServlet.class, "/helloworld");
sch.addServlet(DayTimeServlet.class, "/"); try { server.start(); server.join(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Τρέξτε την εφαρμογή σας και δοκιμάστε να στείλετε ένα GET request στο servlet
helloworld. Θα δείτε στην οθόνη σας
με την προυπόθεση ότι έχετε ορίσει και την κλάση
public class HelloWorldServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpStatus.OK_200); resp.getWriter().println("Hello World"); } }
Προσθέστε και τα υπόλοιπα servlets. Αξιοποιήστε το υλικό στην ενότητα Apache
Tomcat της άσκησης.
Επιδείξτε την χρήση του ReverseServlet.
Άσκηση TemperaturePCB-3.
Http based TemperatureSensorPCB using Jetty and the Temperature Sensor
PCB Emulator
Ορίστε τα servlets που είναι απαραίτητα για την απομακρυσμένη πρόσβαση στο
φυσικό αντικείμενο.
Άσκηση TemperaturePCB-4.
Http based TemperatureSensorPCB using Jetty and the physical Temperature
Sensor PCB
Αναβαθμίστε την προηγούμενη άσκηση αντικαθιστώντας τον emulator με το φυσικό
Temperature Sensor PCB.
Η σημαντική δυσκολία στην άσκηση αυτή είναι να βρείτε τον τρόπο να αξιοποιήσετε
σε ένα maven project κλάσεις τρίτου κατασκευαστή που σας δίνονται με την μορφή
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 43/68 21/05/2019
jar. Στην συγκεκριμένη περίπτωση μπορείτε να αξιοποιήσετε τις κλάσεις που σας
δίνονται με τα tsensor.jar and gpio.jar.
Φυσικά την άσκηση μπορείτε να κάνετε (ανάλογα με τον στόχο σας) σε ένα δεύτερο
βήμα χωρίς την χρήση των παραπάνω βιβλιοθηκών όπως ήδη αναφέραμε στην
ενότητα “Raspberry – Interfacing με τον φυσικό κόσμο”. Προς την κατεύθυνση αυτή
έχετε δύο επιλογές
Α) Pi4J Project, και
B) Device I/O API 1.0.
Στη συνέχεια δίνονται οδηγίες για το πρόβλημα της αξιοποίησης jar σε maven
project.
Η πρώτη σας προσπάθεια θα πρέπει να βασιστεί στην ιστοσελίδα Guide to installing
3rd party JARs
Για την περίπτωση που δεν τα καταφέρετε και μόνο τότε προχωρήστε με τις πιο
αναλυτικές οδηγίες που δίνονται στη συνέχεια.
Πρέπει να εκτελέσετε τις παρακάτω ενέργειες:
1. Να ενημερώσετε το local repository του maven για το/τα jar αρχεία που
σκοπεύετε να χρησιμοποιήσετε στο project σας.
2. Να ενημερώσετε το pom αρχείο του project σας για το/τα jar αρχεία.
3. Να εισάγετε τα απαραίτητα import statements στον κώδικα σας
4. Να κάνετε built το project σας
1. Ενημέρωση του local repository του maven για το/τα jar αρχεία που σκοπεύετε
να χρησιμοποιήσετε στο project σας
Install Maven if not installed.
Run the following command on the cmd prompt in the project’s dir mvn install:install-file -Dfile=tsensor.jar -DgroupId=org.sseg.iot -DartifactId=tSensorLib -Dversion=1.0.0 -Dpackaging=jar Για επιτυχή εκτέλεση θα έχετε κάτι σαν το παρακάτω.
Μπορείτε να δώσετε δικές σας τιμές για τα groupId και artifactId.
Επισκεφθείτε το .m2 repository στον δίσκο σας για να ελέγξετε το αποτέλεσμα της
παραπάνω ενέργειας.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 44/68 21/05/2019
Θα δείτε κάτι σαν το παρακάτω.
Στη συνέχεια πρέπει να ενημερωθεί το Maven του Eclipse για την προσθήκη αυτή, με
άλλα λόγια να συγχρονιστεί με το περιεχόμενο του .m2 ευρετηρίου. Select Maven Repositories/Local Repositories/Local Repository and execute Rebuilt
Check the result of this command. Expand org and locate the tSensorLib
Check the project’s pom file. There is no dependency for tSensor. The project’s pom file is like the following
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 45/68 21/05/2019
2. Ενημέρωση του το pom αρχείου του project σας για το/τα jar αρχεία.
Select the project and then Maven / Add Dependency
The dependency for the tSensor has been added to the pom file as shown below.
3. Εισαγωγή των απαραίτητων import statements στον κώδικα σας
Αξιοποιήστε την σχετική υποστήριξη του Eclipse για το θέμα αυτό.
4. Δημιουργήστε το jar αρχείο (Κάντε build το project σας)
Select the project, right click, select run As, and select 6 Maven Install
If you get an error like the following Compilation failure: Compilation failure: [ERROR] Source option 1.5 is no longer supported. Use 1.6 or later.
add the following to the pom file <properties> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> </properties>
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 46/68 21/05/2019
After a successful built (shown below)
You may find the generated jar under the target directory
A similar process should be applied for using the gpio.jar
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 47/68 21/05/2019
22. 9η Δράση - OMA Lightweight M2M (LwM2M)
H δράση έχει ως στόχο την εξοικείωση με το OMA Lightweight M2M IoT protocol.
OMA Lightweight M2M is a protocol from the Open Mobile Alliance for M2M or
IoT device management. Lightweight M2M enabler defines the application
layer communication protocol between a LWM2M Server and a LWM2M Client, which is
located in a LWM2M Device. The OMA Lightweight M2M enabler includes device management and service enablement for LWM2M Devices. The target LWM2M Devices for this enabler are mainly resource-constrained devices. Therefore, this enabler makes use of a light and compact protocol as well as an efficient resource data model. It provides a choice for the M2M Service Provider to deploy a M2M system to provide
service to the M2M User. It is frequently used with CoAP. Πηγή: Wikipedia.
Δημιουργήστε ένα maven project στο οποίο θα αναπτύξετε ένα leshan server και έναν
leshan client.
22.1 Απλός leshan server
Αναπτύξτε ένα πολύ απλό leshan server. Σας δίνεται παρακάτω ενδεικτικός κώδικας.
Ορίστε τα dependencies στο project σας ώστε να είναι αποδεκτά τα import
statements.
Τρέξτε τον demo server. Θα δείτε στην οθόνη κάτι σαν το παρακάτω.
Κατεβάστε έναν leshan client (leshan-client-demo) σε μορφή jar από το URL
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 48/68 21/05/2019
https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-client-demo.jar
Τρέξτε τον leshan-client-demo και παρατηρήστε ότι κάνει register στον server σας. Trying to register to coap …
Registered with location ….
Μπορείτε να προσθέσετε λειτουργικότητα που να υποστηρίζει το registration
interface του LwM2M, δηλαδή να ορίσετε την λειτουργικότητα του server για τις
περιπτώσεις που ένας client κάνει register, update and unregister.
Για τον σκοπό αυτό πρέπει να ορίσετε έναν handler να υλοποιεί το
RegistrationListener interface
https://github.com/eclipse/leshan/blob/master/leshan-server-
core/src/main/java/org/eclipse/leshan/server/registration/RegistrationListener.java
Τρέξτε τον server σας (SimpleLeshanServerV2) και ελέγξτε την λειτουργικότητα που
μόλις υλοποιήσατε.
Τρέξτε τον SimpleLeshanClientV2 (δική σας 2η version του SimpleLeshanClient)
Θα δείτε κάτι σαν το παρακάτω
O lashan client εκανε register and unregister και ο server σας τύπωσε την σχετική
πληροφορία.
Προσθέστε στον server σας την αποστολή ενός READ request στον client που μόλις
έκανε register για να διαβάσετε την τιμή ενός resource του. Αξιοποιήστε την κλάση class ReadRequest https://github.com/eclipse/leshan/blob/master/leshan-
core/src/main/java/org/eclipse/leshan/core/request/ReadRequest.java
Κάθε leshan client έχει ένα instance του ΟΜΑ IPSO object type Device (see
http://openmobilealliance.org/wp/OMNA/LwM2M/LwM2MRegistry.html)
Στην εικονα παρακάτω φαίνονται τα resources που διαθέτει το instance της Device
στον leshan-client-demo που χρησιμοποιήσατε σε προηγούμενο βήμα.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 49/68 21/05/2019
Με βάση την πληροφορία αυτή μπορείτε να στείλετε ένα read requests στον leshan-
client-demo αμέσως μετά το registration του.
Διαβάστε current Time, Timezone και UTC Offset.
Θα χρησιμοποιήσετε την κλάση ReadResponse
https://github.com/eclipse/leshan/blob/master/leshan-
core/src/main/java/org/eclipse/leshan/core/response/ReadResponse.java
Ενδεικτικός κώδικας δίνεται στη συνέχεια
ReadResponse resp = server.send(registration, new ReadRequest(3,0,13)); if (resp.isSuccess()) { System.out.println("Device time:" + ((LwM2mResource)resp.getContent()).getValue()); }else { System.out.println("Failed to read:" + response.getCode() + " " + resp.getErrorMessage()); }
Θα πάρετε στην οθόνη
20:41:43.197 [CoapServer#1] DEBUG … Device time:Sun Oct 14 20:41:43 EEST 2018 20:41:43.213 [CoapServer#4] DEBUG … Device Timezone:Europe/Athens 20:41:43.213 [CoapServer#2] DEBUG … Device UTC Offset:+03
22.2 Απλός leshan Client
SimpleLeshanClient
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 50/68 21/05/2019
Για την ανάπτυξη ενός απλού leshan client θα χρησιμοποιήσετε την κλάση
LeshanClientBuilder που υλοποιεί για την Java το Builder design pattern.
https://github.com/eclipse/leshan/blob/master/leshan-client-
cf/src/main/java/org/eclipse/leshan/client/californium/LeshanClientBuilder.java
Θα χρειαστείτε και την κλάση LeshanClient
https://github.com/eclipse/leshan/blob/master/leshan-client-
cf/src/main/java/org/eclipse/leshan/client/californium/LeshanClient.java
Ενδεικτικός κώδικας
Εκτελέστε τον client θα δείτε κάτι σαν το παρακάτω
SimpleLeshanClientV2
Ένας leshan client πρέπει να έχει υποχρεωτικά instances των τύπων Security, Server
και Device.
Για την δημιουργία τους θα χρησιμοποιήσετε την κλάση ObjectsInitializer
https://github.com/eclipse/leshan/blob/master/leshan-client-
core/src/main/java/org/eclipse/leshan/client/resource/ObjectsInitializer.java
και τις κλάσεις Security, κλπ από το πακέτο org.eclipse.leshan.client.object public class Security extends BaseInstanceEnabler
https://github.com/eclipse/leshan/blob/master/leshan-client-
core/src/main/java/org/eclipse/leshan/client/object/Security.java
Αξιοποιήστε ως αναφορά την κλάση LeshanClientDemo από το leshan github για να
αναβαθμίσετε τον SimpleLeshanClient σας με τους τύπους αντικειμένων Security,
Server και Device. Μην χρησιμοποιήσετε ως βάση την κλάση LeshanClientDemo
αλλά δουλέψτε πάνω στην SimpleLeshanClient προσθέτοντας μόνο τον κώδικα που
είναι απαραίτητος για να οριστεί ο client ώστε να κάνει register σε localhost Lesan
Server.
Τρέξτε τον SimpleLeshanClientV2. Μεταξύ των άλλων θα δείτε στην οθόνη
Προσέξτε την δημιουργία του endpoint for server coap://localhost:5683 at coap://0.0.0.0:50272
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 51/68 21/05/2019
και παρακάτω
Όπου φαίνεται η προσπάθεια για registration στο localhost και η αποτυχία, καθώς δεν
υπάρχει ενεργός server.
Χρησιμοποιήστε ως Leshan server τον SimpleLeshanServerV3 ή τον
LeshanServerDemo για να δείτε επιτυχημένο registration.
Αν χρησιμοποιήσετε τον SimpleLeshanServerV3 στην οθόνη θα δείτε μεταξύ άλλων
το αποτέλεσμα του read statement που στέλνει στον client σας ο
SimpleLeshanServerV3.
23:03:15.910 [CoapServer#1] DEBUG org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder - Decoding value for path /3/0/13 and format ContentFormat [name=TLV, code=11542]: [-60, 13, 91, -52, -37, -125] Device time:Sun Oct 21 23:03:15 EEST 2018 23:03:15.917 [CoapServer#4] DEBUG org.eclipse.californium.core.network.stack.ReliabilityLayer - Exchange[L3] send request, failed transmissions: 0 …. 23:03:15.933 [CoapServer#1] DEBUG org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder - Decoding value for path /3/0/15 and format ContentFormat [name=TLV, code=11542]: [-56, 15, 13, 69, 117, 114, 111, 112, 101, 47, 65, 116, 104, 101, 110, 115] Device Timezone:Europe/Athens 23:03:15.933 [CoapServer#3] DEBUG org.eclipse.californium.core.network.stack.ReliabilityLayer - Exchange[L4] send request, failed transmissions: 0 …. 23:03:15.948 [CoapServer#1] DEBUG org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder - Decoding value for path /3/0/14 and format ContentFormat [name=TLV, code=11542]: [-61, 14, 43, 48, 51] Device UTC Offset:+03 23:03:16.681 [Deduplicator1] DEBUG org.eclipse.californium.core.network.deduplication.SweepDeduplicator - Sweep run took 0ms
Αν δοκιμάσετε το localhost:8080 θα δείτε στην οθόνη
καθώς ο SimpleLeshanServerV3 δεν έχει την απαιτούμενη λειτουργικότητα.
Δοκιμάστε τον SimpleLeshanClientV2 με τον LeshanServerDemo.
Αν δοκιμάσετε το localhost:8080 θα δείτε στην οθόνη
Και στη συνέχεια επιλέγοντας τον tempSensorPCB_LeshanClient
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 52/68 21/05/2019
SimpleLeshanClientV3
Αναβαθμίστε τον SimpleLeshanClientV2 προσθέτοντας ένα instance του τύπου OMA
LwM2M Temperature Sensor.
Αξιοποιήστε ως αναφορά τον LeshanClientDemo.
Μετά την επιτυχή ολοκλήρωση του θα δείτε στο localhost την γραφική διεπαφή μέσω
της οποίας έχετε πρόσβαση στο instance της Temperature Sensor.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 53/68 21/05/2019
22.3 Δουλεύοντας με τον demo leshan server
Κατεβάστε από το GitHub (https://github.com/eclipse/leshan) τον demo leshan server.
Θα δουλέψετε πάνω σε αυτόν. Πρώτος στόχος να τρέξει χωρίς καμία αλλαγή στην
λειτουργικότητα του.
Ορίστε τα dependencies στο project σας ώστε να είναι αποδεκτά τα import
statements.
Τρέξτε τον demo server Θα δείτε στην οθόνη κάτι σαν το παρακάτω.
Στείλτε ένα GET request στο http://localhost:8080/
Στην οθόνη θα δείτε το παρακάτω
1. Τι σημαίνει αυτό;
2. Προσπαθήστε να το εξηγήσετε.
3. Δώστε ένα σχήμα που δείχνει τα βασικά components που παράγουν το
αποτέλεσμα αυτό.
4. Επαναλάβατε την διαδικασία για να ενσωματώσετε στο project σας τον leshan
client.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 54/68 21/05/2019
5. Διορθώστε το package statement και τα import statements ρυθμίζοντας
κατάλληλα τα dependencies.
6. Διορθώστε τα λάθη που αναγνωρίζει το eclipse στον κώδικα σας.
7. Εκτελέστε τον leshan server και μετά τον leshan client.
8. Στείλτε ένα GET request στο http://localhost:8080/
Στην οθόνη θα δείτε το παρακάτω
Ακολουθήστε το link Client Endpoint. Στην οθόνη θα δείτε το παρακάτω
Επιλέξτε να δείτε αναλυτικά το LwM2M Server. Στην οθόνη θα δείτε το ένα
στιγμιότυπο (object instance) του LwM2M Server με τα χαρακτηριστικά του
(resources), τα οποία ανάλογα με τον τύπο τους έχετε ανάλογες δυνατότητες
πρόσβασης.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 55/68 21/05/2019
Έχετε την δυνατότητα να δημιουργήσετε και άλλο στιγμιότυπο του LwM2M Server.
Επιλέξτε το CREATE NEW INSTANCE. Στην οθόνη θα δείτε το νέο instance.
Επιλέξτε να δείτε αναλυτικά το Location.
Υπάρχει ένα instance τύπου Location με τα χαρακτηριστικά του (resources) τα οποία
μπορείτε να δείτε (Read). Στην οθόνη βλέπουμε τα Lattitude, Longtitude και
Timestamp τα οποία έκανε Read ο χρήστης.
Επιλέξτε να δείτε αναλυτικά το Temperature.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 56/68 21/05/2019
Η θερμοκρασία είναι 23.1 cel με ελάχιστη 19.8 και μέγιστη μετρηθείσα 25.9.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 57/68 21/05/2019
23. 10η Έκδοση - Leshan based Temperature PCB
Η έκδοση αυτή θα προκύψει μέσα από την εκτέλεση ενός συνόλου ασκήσεων που θα
σας οδηγήσουν βήμα προς βήμα στο ζητούμενο αποτέλεσμα.
23.1 Άσκηση TemperaturePCB-6 (TempSencorPCB_LeshanClientV1)
Με βάση την εκδοση SimpleLeshanClientV3 δημιουργήστε ένα TempSensorPcb
LeshanClient που να σας δίνει τη δυνατότητα να έχετε πρόσβαση στην θερμοκρασία
του φυσικού PCB με την αξιοποίηση του leshan demo server.
Σε πρώτη φάση μπορείτε να δουλέψετε όλες τις ασκήσεις αυτής της ενότητας με τον
emulator σας.
23.2 Άσκηση TemperaturePCB-7 (TempSencorPCB_LeshanClientV2)
Αναβαθμίστε την έκδοση TemperaturePCB-6 ώστε να έχετε πρόσβαση στα leds του
φυσικού PCB.
23.3 Άσκηση TemperaturePCB-8 (TempSencorPCB_LeshanClientV3)
Αναβαθμίστε την έκδοση TemperaturePCB-7 ώστε να έχετε πρόσβαση και στους
διακόπτες του φυσικού PCB.
23.4 Άσκηση TemperaturePCB-9.
Αναβαθμίστε την έκδοση TemperaturePCB-8 ώστε όταν ο χρήστης πατάει τον
διακόπτη 1 στο φυσικό PCB να ανάβουν τα led 1 και 3.
23.5 Άσκηση TemperaturePCB-10.
Αναβαθμίστε την έκδοση TemperaturePCB-9 ώστε να δώσετε την δυνατότητα
απομακρυσμένης πρόσβασης στο φυσικό PCB με Http και με την μεσολάβηση του
leshan server.
23.6 Άσκηση TemperaturePCB-11.
Με βάση την έκδοση TemperaturePCB-10 αναπτυξτε τον δικό σας controller για το
φυσικό PCB που θα έχει πρόσβαση στο φυσικό PCB με χρήση Http και με την
μεσολάβηση του leshan server.
23.7 Άσκηση TemperaturePCB-12
Κάντε την πρόταση σας για αξιοποίηση MQTT IoT protocol στα πλαισια του
TemperaturePCB συστήματος.
23.8 Java Application as Leshan server.
1. Περιγράψτε μια εφαρμογή που θα έχει τον ρόλο του leshan server με βάση το
TempSencorPCB_LeshanClientV3.
2. Αναπτύξτε την εφαρμογή.
23.9 Άσκηση Liqueur Plant Smart Silo
Δουλέψτε τις παραπάνω ασκήσεις χρησιμοποιώντας αντί για το temperature PCB τον
Liqueur plant silo simulator.
https://sites.google.com/site/uml4iot/liqueur-plant-case-study
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 58/68 21/05/2019
24. 11η Έκδοση – BPMS as a State Machine
Η έκδοση αυτή έχει ως στόχο την αξιοποίηση της διαδικασίας μοντελοποίησης μιας
εφαρμογής ως μηχανής πεπερασμένων καταστάσεων (finite state machine).
A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton,
or simply a state machine, is a mathematical model of computation. It is an abstract machine that can
be in exactly one of a finite number of states at any given time. The FSM can change from one state to
another in response to some external inputs; the change from one state to another is called a transition.
An FSM is defined by a list of its states, its initial state, and the conditions for each transition. Source:
Wikipedia.
Για παράδειγμα το πιεσόμετρο μπορεί να αναπαρασταθεί από την state machine του
σχήματος 13, που αναπαριστά τις 3 καταστάσεις στις οποίες μπορεί να βρίσκετε η
συσκευή. Η κατάσταση IDLE είναι η αρχική. Στην κατάσταση αυτή είναι η συσκευή
μόλις την ανοίξω. Αν της στείλω το event MANUAL αυτό προκαλεί (triggers) την
μετάβαση (transition) στην κατάσταση Manual. H συμπεριφορά της συσκευής
ορίζεται από την κατάσταση στην οποία αυτή βρίσκεται.
Fig. 13. Ενδεικτική state machine για το πιεσόμετρο.
Για την υλοποίηση μιας εφαρμογής ως μηχανής καταστάσεων μπορεί να
χρησιμοποιηθεί μια βιβλιοθήκη που υποστηρίζει την διαδικασία αυτή.
Στα πλαίσια αυτής της δράσης θα προχωρήσετε στην υλοποίηση της μηχανής
καταστάσεων αξιοποιώντας την generic state machine που μπορείτε να βρείτε στο
GitHub (https://github.com/sseg-APT/GenericStateMachine).
Μπορείτε να δείτε το SimpleSiloExample για να εξοικειωθείτε με τον τρόπο
αξιοποίησης της generic state machine ή να μελετήσετε τις παρακάτω οδηγίες
χρήσης. Την τεκμηρίωση της βιβλιοθήκης θα βρείτε στο https://sseg-
apt.github.io/GenericStateMachine/
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 59/68 21/05/2019
Στη συνέχεια δίνονται οδηγίες για την υλοποίηση μιας εφαρμογής ως μηχανής
καταστάσεων.
Η generic state machine μπορεί να χρησιμοποιηθεί με τους παρακάτω τρόπους:
1. Χρήση του αντίστοιχου jar αρχείου.
2. Download του generis state machine project στο workspace σας.
3. Απευθείας αξιοπόιηση από το GitHub.
Η χρήση της βιβλιοθήκης σε μορφή jar είναι η πιο απλή επιλογή και την υιοθετείτε αν
σας δίνεται το jar.
Σε κάθε μία από τις περιπτώσεις θα πρέπει να ορισθούν κατάλληλα τα dependencies
στο project σας.
24.1. Οδηγίες χρήσης της Generic state machine
Δηλώστε την κλάση που υλοποιεί τον simulator της συσκευής μέτρησης πίεσης να
κληρονομεί την StateMachine και να υλοποιεί το interface MessageQueueIf
π.χ. public class BPMDevice extends StateMachine implements
MessageQueueIf {
ArrayBlockingQueue<ObservableTuple> notificationQueue;
Για να αξιοποιήσετε τα πλεονεκτήματα του logging δημιουργήστε έναν logger στην
εφαρμογή σας όπως παρακάτω private Logger LOGGER = LoggerFactory.getLogger
(SimpleSiloCtrl.class);
Logging
Logging is the process of writing log messages during the execution of a program to a
central place. This logging allows you to report and persist error and warning
messages as well as info messages (e.g., runtime statistics) so that the messages can
later be retrieved and analyzed.
The object which performs the logging in applications is typically just called Logger. http://www.vogella.com/tutorials/Logging/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html
Δηλώστε τις αναφορές (object references) στις καταστάσεις και τις μεταβάσεις του
state chart σας.
π.χ. State idle, manual, auto; // for the states Idle, manual and
Auto
Transition i2mt; // for the transition from Idle to Manual
state
Δηλώστε την αρχική κατάσταση (initial state) του state machine σας. π.χ. setInitState(idle);
Διαμορφώστε τον δημιουργό για την δημιουργία του state machines σας, όπως
παρακάτω public BPMDevice () {
super(null); //null creates a new MessageQueue
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 60/68 21/05/2019
notificationQueue = new ArrayBlockingQueue<>(20);
idle = new Idle ();
…
i2mt = new Idle2ManualTrans(idle, manual);
….
this.setInitState(idle);
}
Για κάθε μια κατάσταση δηλώστε μια κλάση που κληρονομεί την State και κάνει
override τις απαραίτητες μεθόδους της
π.χ. private class Idle extends State {
@Override
protected void entry() { }
@Override
protected void doActivity() {
LOGGER.debug("BPMDevice state: EMPTY");
}
@Override
protected void exit() { }
}
Το entry action είναι ο κώδικας που τρέχει μόλις το state machine εισέλθει στην
κατάσταση (state). Στην κατάσταση Empty για την περίπτωση μας.
Το exit action είναι ο κώδικας που τρέχει αμέσως πριν το state machine εξέλθει από
την κατάσταση (state). Την κατάσταση Empty για την περίπτωση μας.
Το doActivity είναι ο κώδικας που τρέχει κατά την διάρκεια που το state machine
είναι στην κατάσταση (state). Στην κατάσταση Empty για την περίπτωση μας.
Για κάθε μια μετάβαση δηλώστε μια κλάση που κάνει extends την Transition
κάνοντας override τις απαραίτητες μεθόδους της
π.χ.
private class Idle2ManualTrans extends Transition {
public Idle2ManualTrans (State sState, State tState) {
super(sourceState,targetState,false, false, false);
}
@Override
protected boolean trigger(SMReception smr) {
return (smr == BPMDeviceSMEvent.MANUAL);
}
@Override protected void effect() {
try { ….. ; }
catch (Exception e) {
LOGGER.error("Exception in ….: " +
e.toString()); }
}
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 61/68 21/05/2019
}
Το trigger είναι η έκφραση που πυροδοτεί (fires) το transition.
To effect είναι κώδικας που εκτελείται κατά την διάρκεια του transition και έχει
ολοκληρωθεί πριν την είσοδο στο target state του transition.
Το παρακάτω σχήμα δείχνει συνοπτικά την σειρά εκτέλεσης των hooks του
StateMachine:
Δηλώστε τα events που δέχεται η state machines σας να κάνουν implements to
SMReception interface π.χ.
public enum BPMDeviceSMEvent implements SMReception {
MANUAL(0),
AUTO(1),
STOP(2),
;
private int val;
private BPMDeviceSMEvent (int v){
val = v;
}
}
24.2. Οδηγίες για ορισμό dependencies από το GitHub
Στην ενότητα αυτή δίνονται οδηγίες για τον ορισμό των dependencies για το state
machines σας από το GitHub. Οι οδηγίες περιλαμβάνουν και τα αρχικά βήματα
δημιουργίας του αντίστοιχου project. Παρακάτω χρησιμοποιείται ως παράδειγμα το
SimpleSiloExample που θα βρείτε στο GitHub.
Βήμα 1: Δημιουργία Project
Δημιουργήσετε ένα Maven Project και περάσετε τα αρχεία που απαιτούνται για το
παράδειγμα.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 62/68 21/05/2019
Επιλέγετε File > New > Other … και από την ενότητα Maven επιλέγετε Maven
Project.
Επιλέγετε Next και θα εμφανιστεί το παρακάτω παράθυρο:
Επιλέγετε το artifact maven-archetype-quickstart και Next > Next.
Θα εμφανιστεί το παρακάτω παράθυρο:
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 63/68 21/05/2019
Στο πεδίο Group Id δώστε: sseg.apt.GenericStateMachine
Στο πεδίο Artifact Id δώστε: GenericStateMachineExample
Πατήστε Finish για να δημιουργηθεί το project
Βήμα 2: Εισαγωγή του κώδικα
Στον φάκελο src/main/java δημιουργήθηκε ένα package με όνομα
sseg.apt.GenericStateMachine.GenericStateMachineExample στο οποίο
περιέχεται το αρχείο App.java. Διαγράψτε το αρχείο αυτό και περάστε τα αρχεία του
example application από το Github. Θα παρατηρήσετε ότι εμφανίζονται πολλά
σφάλματα στον κώδικα που σχετίζονται με τύπους που δεν μπορούν να βρεθούν. Το
πρόβλημα θα λυθεί αν ορίσετε ως maven dependency στο project σας το
GenericStateMachine το οποίο βρίσκεται στο GitHub.
Βήμα 3: Ρύθμιση του pom.xml
Για τον ορισμό των dependencies στο project σας από το Github θα χρησιμοποιήσετε
ένα package repository για το Maven που ονομάζεται JitPack.io. (https://jitpack.io/)
Εισάγετε στο pom.xml αρχείο του project σας τις παρακάτω γραμμές:
<repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories>
Το αρχείο pom.xml διαμορφώνεται όπως παρακάτω:
Βήμα 4: Εισαγωγή των dependencies
Για να εισάγετε ένα maven dependency στο project σας κάνετε δεξί κλικ στο project
στον Package Manager και στην ενότητα Maven επιλέγετε Add Dependency. Θα
εμφανιστεί το παρακάτω παράθυρο:
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 64/68 21/05/2019
Στο πεδίο Group Id δώστε: com.github.sseg-APT (στη γενικότερη μορφή είναι
com.github.Username)
Στο πεδίο Artifact Id δώστε: GenericStateMachine (στη γενικότερη μορφή
χρειάζεται το όνομα του Repository που θα χρησιμοποιήσετε)
Στο πεδίο Version δώστε: master (στη γενικότερη μορφή χρειάζεται το release tag ή
το commit hash ή το branch που θα χρησιμοποιήσετε)
Επιλέξτε OK.
Τα σφάλματα θα εξαφανιστούν μόλις φορτωθούν τα dependencies που περάσατε.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 65/68 21/05/2019
25. 10η Δράση - Using Azure
A) Εγκαταστήστε το Azure Toolkit for Eclipse. Στον παρακάτω σύνδεσμο θα βρείτε
κατάλληλες οδηγίες.
Installing the Azure Toolkit for Eclipse
https://docs.microsoft.com/en-us/azure/azure-toolkit-for-eclipse
B) Αναπτύξτε ένα απλό service και κάντε το deploy στο Cloud αξιοποιώντας το
Azure. Αυτό μπορεί να είναι το Hello World. Στον παρακάτω σύνδεσμο θα βρείτε
μια καλή περιγραφή για την εκτέλεση αυτής της δράσης.
Create a Hello World Cloud Service for Azure in Eclipse
Κατά την διαδικασία ανάπτυξης προσέξτε την δημιουργία στο Eclipse του Azure
Deployment project. “In order to deploy one or more Java applications to Azure, an Azure Deployment
Project is needed. It plays the role of the "package" that your applications need to be
wrapped into in order to be published on Azure.
Besides the information about your applications, an Azure deployment project also
contains information about other key components of your deployment, most importantly:
the application server container to run your web app in, and the Java runtime to run it
on.”
C) Εγκαταστήστε τον Azure storage emulator και κάντε deploy την υπηρεσία σας σε
αυτόν. Τον Azure storage emulator θα χρησιμοποιήσετε για ανάπτυξη και έλεγχο
της υπηρεσίας σας πριν το deployment στο Cloud.
Microsoft Azure Java SDK API http://azure.github.io/azure-sdk-for-java/
D) Αναπτύξτε και κάντε deploy στο Cloud τις υπηρεσίες daytime και echo που
είχατε αναπτύξει για τον Tomcat.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 66/68 21/05/2019
26. 12η έκδοση – Distributed based on Cloud
Αξιοποιήστε το Azure Storage Client SDK για την αποθήκευση των μετρήσεων στο
Cloud. Εναλλακτικά μπορούν να χρησιμοποιηθούν άλλες cloud platforms όπως,
Google Clooud platform (https://cloud.google.com/), Amazon web services
(https://aws.amazon.com/), Mbed Cloud (https://cloud.mbed.com/), κ.λ.π.
26.1 Using the Azure Storage Emulator
Σε πρώτη φάση χρησιμοποιήστε τον Azure storage emulator.
The Azure storage emulator uses a local Microsoft SQL Server instance and the local
file system to emulate the Azure storage services. By default, the storage emulator
uses a database in Microsoft SQL Server 2012 Express LocalDB
Use the Azure Storage Emulator for Development and Testing
https://docs.microsoft.com/en-us/azure/storage/storage-use-emulator
26.2 Using the Azure Cloud computing platform services.
To BPM σύστημα σας έχει την διαμόρφωση που δίνεται στο σχήμα 14.
Fig. 14. Blood Pressure Monitoring System based on Cloud.
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 67/68 21/05/2019
27. 13η έκδοση – Using Bluetooth
Δώστε wireless connectivity στο BPM device (RPi). Το BMP σύστημα
διαμορφώνεται όπως στο σχήμα 15.
Fig. 15. Blood Pressure Monitoring System using Bluetooth.
How to set up Bluetooth on your Raspberry Pi
https://www.raspberrypi.org/learning/robo-butler/bluetooth-setup/
How to setup Bluetooth on a Raspberry Pi 3
https://www.cnet.com/how-to/how-to-setup-bluetooth-on-a-raspberry-pi-3/
BBlloooodd PPrreessssuurree MMoonniittoorriinngg SSyysstteemm –– FFrroomm SSoocckkeettss ttoo WWooTT
Software and System Engineering Group 68/68 21/05/2019
28. 14η έκδοση – Using an Android device for the
Health Monitoring App
Μεταφέρατε την Health Monitoring Application σε Android OS. Το BMP σύστημα
διαμορφώνεται όπως στο σχήμα 16.
Fig. 16. Blood Pressure Monitoring System using Android devices for the Health
Monitoring App.
29. 15η έκδοση – Using the Azure IoT Hub
Αξιοποιήστε το Azure IoT hub στα πλαίσια του BPMS συστήματος σας.
Azure IoT Hub
https://azure.microsoft.com/en-us/services/iot-hub/
Αξιοποιήστε την ιστοσελίδα
Connect your simulated device to your IoT hub using Java
https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-java-java-getstarted