The point which the exercise was trying to make is the following:
A says: the two lights flash at the same time
B says: the light 2 flashes first, then light 1
This is precisely the relativity of simultaneity - one cannot judge which light flashed first because it depends on the observer
The point is not that A sees that B observes light 2 first, but B sees the two lights simultaneously, in fact this statement is wrong. Image that this was the case. Let's assume that something terrible happens if B gets hit by two light at the same time, let's say B dies. If the lights hit B at different times, however, nothing happens. But now A would observe that B is safe and sound whereas B in his rest frame seeing the two lights at the same time would die. Dead and not dead at the same time? This is not possible and Schrödinger giggles silently in his tomb. The two lights hit B at different times in any rest frame
How is it possible? As you said for B the light sources are at rest. The crucial point is the Lorentz contraction. In this point the exercise is somewhat imprecise. It states that the separation of the electric devices matches the length of the carriage, but the carriage is in move so it gets contracted. A carriage in rest is thus longer then the separation of the devices. Let's correct the exercise and say that the electric devices are situated at such a distance apart that the Lorentz contracted carriage matches their separation in the rest frame of A. Now we go into the rest frame of B. From his point of view the electric devices move towards him. Therefore B sees their separation even shorter, so as the front of the carriage hits the electric device #2, the rear of the carriage will not have reached device #1 yet. So for B light 2 flashes first, then light #1. Note that this is precisely what A would have predicted for B, but the reason that A gives is that B travels towards the light #2 and away from light #1 so it sees light #2 first. As we have seen for B in its rest frame the reason is Lorentz contraction.
If you are interested in special (and general) relativity I can highly recommend you the Book by Lewis C. Epstein - "Relativity visualized".