Tuesday, March 3, 2015

Using Spring boot and returning paged JSON DTO list

I currently work on a REST application, for collecting and providing agregated datas.

The REST way (relying on Spring JPA and hateoas technical stack), was chosen because it fit well for many data provider.
But for a few datas this approach does not fit.

In fact most of datas published in a REST fashion have their own dedicated @Repository and @Entity
  • the collected data 
  • the calculated data
But monitoring datas which are volatile data view, should be expose too and those one :
  • Does not have @Repository
  • Does not have @Entity
  • Comes from @Query using Join and returning DTO
  • Should be provided as paged resource.
For those one standard REST  @RepositoryRestResource does not fit.

To acheive this goal  :

first produce your DTO and build the query :

@Query("SELECT new dataKeeper.pojo.datadirectory.DTOKeeperData(art, data)"
  + " FROM KeeperDataDirectory AS data INNER JOIN  data.artifact AS art " )
 List<DTOKeeperData> findKeeperDataDirectoryArtifact();

Then we want paging for our data so :
  • add to Query the parameter "countQuery",
  • add Pageable parameter  to interface method
  • and return Page instead of List

@Query(value = "SELECT new dataKeeper.pojo.datadirectory.DTOKeeperData"
  + " (art, data) "
  + " FROM KeeperDataDirectory AS data INNER JOIN  data.artifact AS art ",
  countQuery="SELECT count(data) FROM KeeperDataDirectory AS data INNER JOIN"
  + "  data.artifact AS art ")
 Page<DTOKeeperData> findAllDataDir(Pageable pageable );



At this point, this method does not work from a REST client. The error is :
{
  "cause": null,
  "message": "Cannot create self link for class dataKeeper.pojo.datadirectory.DTOKeeperData!
     No persistent entity found!"
}
And it's true... the DTO has no Entity... As I define this method in a @RepositoryRestResource spring wait for an entity. Another point, this request is declared by Spring hateoas.... So I create a new Class extending Repository and put the query in it and then create (or use) a Controller (@Controller). This controller
  • define (in our case) the page size
  • handle our request for data on a specific url containing the page number we want to load.

@Controller
public class KeeperDataController {
    private static final int PAGE_SIZE = 30;
    static Logger logger = Logger.getLogger(KeeperDataController.class);

    @Autowired
    KeeperDataDirectoryRepository sddr;
  
    @RequestMapping(value = "keeperdatadir/dto/all/{pageNumber}", method = RequestMethod.GET)
    public @ResponseBody Page<DTOKeeperData> getDto(@PathVariable Integer pageNumber){
        Pageable pageable = new PageRequest (pageNumber - 1, PAGE_SIZE);
        Page<DTOKeeperData> dtos = sddr.findAllDataDir(pageable);
    return dtos;
  }
}

When returning Page you will have access to Total page, number of element and a list of DTO element.

Monday, February 16, 2015

Raspberry pi, standalone arduino, 433Mhz wireless communication and 20 voltage sensors



For one of my projects I want to use an arduino to measure voltage for from 20 sensors.

To do that I saw many shield that allow to multiplex voltage source connected to a few arduino. ADCs ... But It's for fun, so I decide to build my onwn including standalone AT328 (see this Arduino link).

Requirements

To begin this is my requirements list :
  • max 20 sensors inputs. Max voltage Vref /AA. 
  • work on battery so as low consumption as I could
  • wireless communication (as cheap as I could)
  • one raspberry pi as server (receive/ persist datas comming from my 328)
I view this project as many phase :
  • proof of concept/validation (is all is working, communication, sensors and so on)
  • optimization / validation (reduce consumption, resolve dysfunction)
  • optimization / validation 
  • end of projet  : build circuit board, solder, put in box , validation

Proof of concept

At this point I have to :
  • choose components
  • prototype this card
  • and when all will work I will thinl about circuit board.

Component list

  • atmel 328  with bootloader (It more expensive as ATmel without bootloader, but one thing at a time)
  • 16 Mhz quartz
  • few resistor and capacitor
  • L7805CV regulator. (I will try LM317 latter)
  • emitter / receiver 433MHz (Warning you should verify if the 433 Mhz use is allowed in your country...)
  • 2 x CD4051 8 channels multiplexer (for test it's enough)
  • raspberry pi (I have a B model with wifi)
  • battery connector

For prototyping 

  • breadboard (I have 2 : 840 points)
  • 1 usb / rs232 converter (to upload sketch in arduino)
  • I have one arduino (will allow me to make comparison)

Schematics

TODO/Work in progress

let's start building

first build an arduino like (see "Building an Arduino on a Breadboard"), at this point I could not test anything except Vcc.
 
Then add an USB to Serial arduino module to be able to push sketch in arduino.


To connect this module  you have to connect
  • +Vcc, 
  • Gnd, 
  • Tx (to atmel328 Rx)
  • Rx (to atmel328 Tx)
  • Reset (to atmel 328 pin 1 reset using a 100nF capacitor)

Note :  At the begining I do not connect the ext reset of my usb to serial card and I had and error during sketch uploading.




At this point I could test my arduino and usb connection.

I use the blink example and connect a LED to pin arduino pin 13. Then push this sketch to my arduino bread board. And... all is ok.

Now it's time to add wireless comunication. I choose a 433Mhz emitter end receiver (very cheap)



In my plan, in want an arduino send periodically information to my raspberry pi. In my mind, at the end I'd like to build a "server ask --> client response" and see if it decrease consumption.

So I had now to connect an emmiter to arduino and the receiver to my raspberry pi, and see if all work toghether.

First I'll start with receiver on raspberry pi.

As I found a lot of informations about internet I saw there difference between RPI revision and GPIO.

What is my RPI model ?

So first I have to identify which RPI model I have.

So I connect the RPI through SSH using moba xterm. And in a terminal I use the command line :

cat /proc/cpuinfo


at the and of the command output you should see something like "Revision : Number"

Go through http://www.magdiblog.fr/divers/connaitre-le-modele-exacte-dun-pi/
and look at your model.

mine is : 000e Model B Revision 2.0 512MB, (Sony)

Ok , let's connect RPI and arduino

see http://www.homautomation.org/2013/09/21/433mhtz-rf-communication-between-arduino-and-raspberry-pi/

Mainly :

Receiver on RPI
  • connect pin GPIO 21 (wiring pi 2; chip 13) on receiver data out
  • GND to GND
  • and 5v to vcc
On arduino side, almost same
  • connect pin 10-PWM (physical 16) on emitter data in
  • GND to GND
  • and 5v to vcc
Connect 20 cm antenna on each device.


Software

For software on both I use RCswith (VirtualWire does not work for me...) see (https://code.google.com/p/rc-switch/)

For RPI I use RFSniffer
on the arduino side the sketch is as in http://www.homautomation.org/2013/09/21/433mhtz-rf-communication-between-arduino-and-raspberry-pi/


And all is working...
Ok now,
  •  due to the nature itself of this protocol, message are received many times... Anyway, just modify the RFSniffer.cpp (as follow) relying on this source (see last program : http://faitmain.org/volume-1/dispositifs.html
  • I want to send more than activity form my arduino... I want to send many data, in fact one for each sensor connected... To do this I have to find or write some kind of protocol wich is allowing to :
    • to identify my arduino card (with an adress)
    • to identify on sensor (relying on multiplexer adress ??? 3 or 6 bit  ie 8 or 16 sensor)
    • to add for each sensor my digital measured value.
|Arduino adress | Sensor adress | value |

 first, what is the max size of a "data packet" ? To determine this I firstly search but could not find. So I use rule of thumb : send bit since reaching the limit... I found that I could send 31 bits (31 + 1 bit for sync I think)

  mySwitch.send(2147483647, 31);

so  I choose
  • 14 bit for base address (0 to 16383) identifier
  • 5 bit for sensor identifier (32 sensors max for each arduino)
  • 12 bit data (0 to 4095)
12 bits for data is enough and pheraps too bignow, because the ADC in atmega 328 is 10 bit. But if I decide to add a more precise ADC in the future those 2 extra bit would be welcome.

At this time on receiver side I could have :



while sending on arduino side :

  mySwitch.send("1111111111000000000000000000100"); // base adress 16368 and sensor value 0
          // and value 4
  mySwitch.send("1111111110010000000000000001000"); // base adress 16356 sensor module 0
          // and value 8

How to send data ?

Before adding multiplexer, I have decide how to send Sensor datas.
What i already know :
  • ADC conversion take 13ADC clock cycles
  • ADC clock is Atmel frequency (16MHz in my case)/ 128 (prescale set toobtain between 50kHz and 200Khz ADC clock for 10bit resolution) , that gives 125KHz. 
  • So the conversion time is 13 cycles / 125KHz = 0.104 * 10^-3 that gives 104 uS

As I could read the ADC frequency could be increase up to 1Mhz without much degradation by changing the prescale.

In my case 1 sensor value send  each minute is more than enough. it means

1 multiplexer adressing + 1 conversion time + 1 send (multiple send beacause of 433 Mhz protocole)
so it means

AAA us +  104 us + BBB us .....

I am missing data, let grasp them.

Multiplexing time

I could try to measure it when  i will have it...

Sending time duration

To have an idea I does :

void loop() {
 unsigned int i;
  for(i=0;i<10;i++) {
   start_times[i] = micros();
   mySwitch.send("1111111111111100000000000000111");
   delay(1000);
   stop_times[i] = micros();

  }
  Serial.println("\n\n--- Results ---");

  for(i=0;i<10;i++) {
   Serial.print(" elapse = ");
   Serial.print(stop_times[i] - start_times[i]);
   Serial.print(" us\n");
  }

That is giving me ~500ms for BBB.

Connecting multiplexer (CD 4051) and Mock sensor

Firstly, I don't want to already use sensor, so I will fake it :
  • Mock sensor : potentiometer gnd, +5V , middle pin to in multiplexer
Then I had a look at http://playground.arduino.cc/learning/4051 about CD4051 and it's enough to wire this CI to my breadboard arduino:
 connect multiplexer pin z (4051) to pin A0
connect the voltage sensor to pin Y0 (4051). As I fake only one entry I connect the  Y1-Y7 to gnd.

And after adding some lines to my sketch I obtain on my Raspberry  :
  Know I have my arduino sending  :
  • an adress (in the future based on DIP switch)
  • a sensor id
  • the sensor value.


What if wifi card is cheaper...

A the very beginning I'd like to use Wifi shield to does exchange with my RPI, but the cost was unaffordable.
Recently I found  an ESP8266 device from 5$ to 8$ ... So I decide to test it. I'm waiting for delivery. After a few test I will probably change the 433Mhz emitter. This evolution will allow to exchange more data in a request-response way with no receiver to install.


I finally received my ESP8266... But I was a little bit optimistic and I missed a couple of important things :
  • ESP8266 run  at 3.3V
  • ESP could required 300mA peak
Hum... So for RX and Vcc I will use voltage divider or zener.. and for the 300 mA it should be ok as I use my own power supply  with 7805 that could provide 1.5A max...
For the connection, I found many link for example :
here is the current schematic


So let's start and see if a zener regulation with BZXC55C3V3 500mW (with a serial resistor) is possible (see Zener Diode  introduction from M. H. Miller or http://electronics.stackexchange.com/questions/28944/selecting-correct-zener-diode).
With this kind of regulation, the serial resistor is calculated with the max current in the "load" (here 300mA for ESP8266) and the minimum for zener work (I took 10mA) so 310mA. When no more load is required, the execess should be absorb be zener... when nothing is required from the load the ESP 8266 require low current. The wort case is 0 A , the datasheet gives 0,9 mA in standby mode.
So the zener should absorb 309,1mA .... far more than the maximum current (I= P/U) 151mA (Izrm 115mA)
This solution is not suitable.

So as I have LM317, let's use one....
To build a regulation with a LM317 you could use this schematic

and this formula 
Vout=Vref(1+R1/R2)+IadjR2
 
$E=mc^2$

Vref = 1.25
Iadj = 50A



to be continued...


Next step : 
  • serve information  collected : exposing data through REST API on raspberry PI
  • Using arduino interrupt 
  • what if other 433Mhz peripherals exists  ?....  One : how to check that.... Two :change ardess with dip switch WIP


Saturday, February 14, 2015

Raspberry pi adding USB dongle Realtek RTL8188CUS


I received today my Realtek dongle for my Raspberry pi.

To have a reminder. I write this short post.

Recipe

  • Power off raspberry pi
  • Put add your wifi dongle
  • Use lsusb command to see if your peripheral is right detected. Your should see something like

Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp.
 RTL8188CUS 802.11n WLAN Adapter


Then in /etc/network edit in sudo mode the interfaces file

allow-hotplug
wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-conf etc/${path_to_your_wpa_supplicant.conf}


Then edit your wpa_supplicant.conf. see here for more informations or here. Be carreful when setting up network parameter, that depends of your wifi point authentification (WEP,WPA or WPA2)
network={
     ssid="your_ssid_box_for_example"
     psk="your_password"
     proto=RSN
     key_mgmt=WPA-PSK
     pairwise=CCMP TKIP
     group=CCMP TKIP
 }
At the end shutdown your raspberry, disconnect your previous ethernet conection (if exists). Restart your RPi.
After restart you should have an IP adress for your wlanYY interface (use ifconfig command).

Tuesday, February 10, 2015

Jenkins : duplicate jenkins enviromnent and apply SCM change on every job

Duplicate Continuous integration (Jenkins) enviromnent

Just a reminder to explain and speak about jenkins scripting console.

Our problem is :
  • we have many hundred maven projets
  • each project has its job in jenkins (project choice)
  • when preparing a new release we "branch" all sources in our SCM

question how can we duplicate jenkins environement ?

Cookbook

  • install a new jenkins instance 
  • install in this instance a plugin called Job import plugin
  • use this plugin and import all job from your initial instance
  • Then use the script console  ${host}/${Jenkins_inst_name}/script . This jenkins feature is very powerfull. It's allow to walk through all job and mofify them if needed. In our case we apply this groovy script (I start from this source wiki.jenkins-ci.org/display/JENKINS/Change+Version-Number+in+SVN-path)
import hudson.scm.*
hudsonInstance = hudson.model.Hudson.instance
overrideExistingValues = false
allItems = hudsonInstance.items
allItems.each { job-> println "Name : "+job.name;
if(!(job instanceof hudson.model.ExternalJob)) {
   if (job.scm instanceof SubversionSCM) {
     def newSvnPath = [][]
     println "SCM job : "+job.name;
     job.scm.locations.each{
       println "Scm location : "+it.remote;
       newRemote = it.remote
       newRemote = newRemote.replaceAll("franckys/svn/LOCAL/trunk",
        "franckys/svn/LOCAL/branches/preV2")
       newSvnPath.add(new hudson.scm.SubversionSCM.
        ModuleLocation(newRemote,it.local))
       println "Scm new location : "+newRemote;
     }
     newscm = new hudson.scm.SubversionSCM(newSvnPath,
        job.scm.workspaceUpdater, job.scm.browser,
     job.scm.excludedRegions, job.scm.excludedUsers, job.scm.excludedRevprop, 
        job.scm.excludedCommitMessages, job.scm.includedRegions)
     if (overrideExistingValues){
       job.scm = newscm;
     }
   }
 }
}
If you have interest in script you could have a look at : https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

Friday, September 19, 2014

Java Memory leak tips

Last couple of days I had to track an Out of mermory error on a production application. During this tracking I searched and use different tools for differents problems.
So this post is a reminder.

the error context environment is : a java application launch by maven (use exec:java ; benefits from transitive dependencies) launch from eclipse configuration for external tool (this tool is an eclipse one, benefits from log redirection through console)

Use java Debug Architecture

in this context the debugging could be done with java debug architecture :
So for JVM (TI) side : launch maven exec command with JVM args :
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009
(activate debug, load jdwp, transport mode = socket, target application listen for a debugger to attach, target vm will be suspendended until debugger apps connection)

Or the new one (not use in my case)
-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y

For JDI side one can use eclipse with "Remote Java Application"

Run the VM then the debugger.

Use java -verbose

this java option could takes args :
-verbose [class | gc | jni]

In our case the gc arg is usefull, it allows log of Garbage Collection event.
[GC 4416K->1055K(15872K), 0.0029002 secs]
[GC 5471K->1640K(15872K), 0.0030503 secs]
[GC 6056K->2100K(15872K), 0.0034942 secs]
[GC 6516K->2633K(15872K), 0.0027937 secs]
[GC 7049K->2846K(15872K), 0.0028010 secs]

You could also use to have more details or output to file:
-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails
-Xloggc:<file>

You could then have :
 0.319: [GC 0.319: [DefNew: 4927K->512K(4928K), 0.0024456 secs]
5922K->1924K(15872K), 0.0024653 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
 0.398: [GC 0.398: [DefNew: 4928K->512K(4928K), 0.0021861 secs] 
6340K->2307K(15872K), 0.0022057 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
 0.465: [GC 0.465: [DefNew: 4928K->512K(4928K), 0.0025962 secs] 
6723K->2509K(15872K), 0.0026180 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 0.545: [GC 0.545: [DefNew: 4928K->511K(4928K), 0.0022087 secs]
6925K->3017K(15872K), 0.0022284 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 0.620: [GC 0.620: [DefNew: 4927K->511K(4928K), 0.0019218 secs] 
7433K->3584K(15872K), 0.0019427 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
 0.684: [GC 0.684: [DefNew: 4927K->395K(4928K), 0.0021938 secs] 
8000K->3580K(15872K), 0.0022122 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
 def new generation   total 4928K, used 3683K [0x24c40000, 0x25190000, 0x2a190000)
  eden space 4416K,  74% used [0x24c40000, 0x24f760d0, 0x25090000)
  from space 512K,  77% used [0x25090000, 0x250f2d70, 0x25110000)
  to   space 512K,   0% used [0x25110000, 0x25110000, 0x25190000)
 tenured generation   total 10944K, used 3185K [0x2a190000, 0x2ac40000, 0x34c40000)
   the space 10944K,  29% used [0x2a190000, 0x2a4ac688, 0x2a4ac800, 0x2ac40000)
 compacting perm gen  total 12288K, used 4456K [0x34c40000, 0x35840000, 0x38c40000)
   the space 12288K,  36% used [0x34c40000, 0x3509a390, 0x3509a400, 0x35840000)
    ro space 10240K,  45% used [0x38c40000, 0x390c7988, 0x390c7a00, 0x39640000)
    rw space 12288K,  54% used [0x39640000, 0x39ccb5d8, 0x39ccb600, 0x3a240000)

Use the dump

The get a rapid diagnosis, you could use a JVM option that allow to output a dump when Out of memory error

-XX:+HeapDumpOnOutOfMemoryError
Or use with ctrl break
-XX:+HeapDumpOnCtrlBreak

You can then use the produce dump (hprof file) in Visual VM (See below).

Use Visual VM

This tool located in ${JAVA_INSTALL_DIR}/bin/jvisualvm.exe (on windows), allow you this view many things on the VM (mainly):
  • see threads, memory... behavior 
  • trigger dump 


Object size Measure (Reflexion)

Using this jar (as maven deps)
<dependency>
<groupid>net.sf.ehcache</groupid>
<artifactid>ehcache</artifactid>
<version>2.8.3</version>
</dependency>

You could now use a method relying on reflexion for object size calculation :

try {
    ReflectionSizeOf reflectionSizeOf = new ReflectionSizeOf();
    Size deepSizeOf = reflectionSizeOf.deepSizeOf(1000, false, model);
    logger.info("Memory in use Model deepSize | F3 |  {}", deepSizeOf.getCalculated());
    deepSizeOf = reflectionSizeOf.deepSizeOf(1000, false, context);
    logger.info("Memory in use Context deepSize | F4 |  {}", deepSizeOf.getCalculated());
} catch (Exception e) {
    e.printStackTrace();
}

Object size Measure (Using Java Agent)

Agent could instrument java through JVM addition (transformation) of byte-codes to method.
An instrument is a class. This class should provides methods :
public static void premain(String args, Instrumentation inst) throws Exception
public static void agentmain(String args, Instrumentation inst) throws Exception

declared in META-INF/MANIFEST.MF
Agent-Class: my.package.JavaAgent
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Premain-Class: my.package.JavaAgent
There is two ways that provides instrument access
  • launch JVM with instrumentation flag (instrument premain method is used)
  • use instrumentation dynamically (agentmain method use), implies tools.jar usage
An instrument provide access to a method :
  long     getObjectSize(Object objectToSize)

WARNING this method provide only object size without object instance references

If you encounter problem while loading instrument you could use this one
org.apache.openjpa.enhance.InstrumentationFactory

Comming from
<dependency>
 <groupId>org.apache.openjpa</groupId>
 <artifactId>openjpa-kernel</artifactId>
 <version>2.3.0</version>
</dependency>

Much more usefull information about agent here

Use TPTP

See here

Java memory map

To finish this post, I tried to build a graphic memory figure to remind me how it is approximately organized


Friday, June 27, 2014

Eclipse contribute to "Show view" menu for an existing perspective

How to contribute to "Show view" menu for an existing perspective

In this post I show how to add a shortcut to your own perspective to Window -> Show View menu in a particular perspective.

In fact you just have to define in your plugin.xml (see  Eclipse Doc. PerspectiveExtensions)
  • a perspectiveExtensions point
  • specify the perspective tagrget id
  • add a shortcut to your view.
It gives for a shortcut in Java perspective :

<extension point="org.eclipse.ui.perspectiveExtensions">
    <perspectiveExtension targetID = "org.eclipse.jdt.ui.JavaPerspective">
        <viewShortcut id="my.view.id"/>
    </perspectiveExtension>
</extension>

If you want to do this in your own perspective you can use this too or define in the createInitialLayout method of your perspective (implementing IPerspectiveFactory, see Eclipse doc IperspectiveFactory)

 public void createInitialLayout(IPageLayout pageLayout)
  { 
  pageLayout.addShowViewShortcut("my.view.id");
  }




Tuesday, June 3, 2014

Playing with kineticjs



Explanations

This post present a small Kineticjs experiment. And a JS integration in Google blog experiment. The small js above allow:
  • to zoom in and out the rectangle
  • to move the rectangle by selecting it

Encountered problems to be investigate and Todos (may be...)

  • scale function does not work
  • usage of JQuery $() function (to be removed)
  • refactor duplicated code
  • Use kineticJs drag start event

Ref

Notes using CDN here

  • The url for kineticjs could be found at https://github.com/ericdrowell/KineticJS/wiki/Version-Archive. Page extract : All of the versions below are hosted on the Amazon CloudFront CDN (Content Delivery Network), which means that you can hotlink to the URLs and enjoy fast response times no matter where you are in the world.
  • for JQuery the url could be found at http://jquery.com/download/