Month: December 2017

Air quality monitoring script for Argos (Linux) and BitBar (macOS)

2017-12-29 Bash, Linux, macOS No comments

From some time, I wanted to create my own app, which will display some data in top panel in macOS or Gnome environment on Linux. I collected some resources about that and I knew that for macOS I need to write an app in Obj-C and for Gnome I need to write a plugin in JavaScript. In both cases it requires some ceremony and preparation. Recently I’ve found a great app for macOS called BitBar (by the way it’s open-source). BitBar allows to put anything to macOS menu bar (top panel) in no time! With this project creating top panel apps is simplified to the limit. Moreover, there’s another project called Argos, which does the same thing, but for Linux with Gnome (it’s an open-source Gnome Extension).

In both cases, we just need to create a shell script, put it into appropriate directory (in case of Argos, it’s ~/.config/argos/ and in case of BitBar, we define it during the installation or first run) and then app displays our data automatically. We can also set refresh rate. E.g. if we want our script to be executed every 60 seconds, we can name it script.60s.sh. We can also create more advanced scripts and more details can be found in BitBar and Argos documentation.

In my case, I wanted to create a script, which reads CAQI (Common Air Quality Index) in my current location based on Airly sensors. Airly provides nice API, which we can use in our projects. Please remember that most of the sensors are located in Poland.

On my Ubuntu Linux with Gnome 3, I created a new script in the following path:

~/.config/argos/caqi.60s.sh

For a BitBar script location could be different. Both for BitBar and Argos follow the same naming convention.

My script looks as follows:

#!/usr/bin/env bash

CAQI=$(curl -s -X GET --header 'Accept: application/json' --header 'apikey: YOUR_API_KEY' \
    'https://airapi.airly.eu/v1/nearestSensor/measurements?latitude=YOUR_LATITUDE&longitude=YOUR_LONGITUDE&maxDistance=1000' \
    | jq .airQualityIndex | cut -f1 -d".")

MSG="Unknown"

case 1 in
  $(($CAQI <= 25)))  MSG="Great!";;
  $(($CAQI <= 50)))  MSG="Good!";;
  $(($CAQI <= 75)))  MSG="Medium";;
  $(($CAQI <= 100))) MSG="Bad";;
  $(($CAQI >= 101))) MSG="Very Bad";;
esac

echo "CAQI: $CAQI ($MSG)"

It works fine on Linux. The only requirement is to install jq. For macOS, I needed to change jq to /usr/local/bin/jq to make it work. I gathered information about air quality level from this website: https://www.airqualitynow.eu/pl/about_indices_definition.php [PL]. Of course, we need to replace API_KEY with our api key, which we can get from https://developer.airly.eu/ website as well as YOUR_LATITUDE and YOUR_LONGITUDE with coordinates of our location. It can be static location in our city. We can get them e.g. from Google Maps.

As a result, we have beautiful text in our top panel:

This screenshot was taken on Ubuntu Linux with Gnome 3. On macOS it works the same (I checked it).

Creating a Docker container with Alpine Linux including Java 8 and 9

2017-12-27 Docker, Java, Linux No comments

Recently, I’ve decided to refresh my knowledge regarding Docker and created an image with Alpine Linux and Java 9, which can be a useful base for the future projects. I used Alpine as a base image because it became quite popular in the Docker world due to its simplicity and the fact that it’s pretty lightweight when we compare it to containers based on other Linux distributions. Pure Alpine Docker container has about 4.144 MB, what is really impressing.

Container with Java 9

My Dockerfile looks pretty simple:

FROM alpine:latest
MAINTAINER pwittchen
USER root

RUN wget http://download.java.net/java/jdk9-alpine/archive/181/binaries/jdk-9-ea+181_linux-x64-musl_bin.tar.gz
RUN tar -xzvf *.tar.gz
RUN chmod +x jdk-9
RUN mv jdk-9 /usr/local/share
ENV JAVA_HOME=/usr/local/share/jdk-9
ENV PATH="$JAVA_HOME/bin:${PATH}"
RUN rm -rf *.tar.gz

We’re downloading JDK, unpacking it, moving to /usr/local/share directory, creating $JAVA_HOME environmental variable and adding $JAVA_HOME/bin to the $PATH. After that, we’re removing downloaded *.tar.gz file.

Repository with this project is available at: https://github.com/pwittchen/docker-alpine-java9
We can also find it on Docker Hub: https://hub.docker.com/r/pwittchen/alpine-java9/

To pull the image from Docker Hub, just type:

sudo docker pull pwittchen/alpine-java9

To run it with CLI, type:

sudo docker run -i -t pwittchen/alpine-java9

Then, we can play around with jshell inside the container:

/ # jshell
Dec 27, 2017 1:18:10 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> System.out.println("hello from docker!")
hello from docker!

This container is not so small and has about 919.2 MB. It contains whole JDK, so probably this size could be reduced.

Container with Java 8

I’ve also created another image with Java 8 (just in case):

FROM alpine:latest
MAINTAINER pwittchen
USER root

RUN apk update
RUN apk fetch openjdk8
RUN apk add openjdk8

We can also find it on the web:
GitHub: https://github.com/pwittchen/docker-alpine-java8
Docker Hub: https://hub.docker.com/r/pwittchen/alpine-java8/

and pull it from the Docker Hub:

sudo docker pull pwittchen/alpine-java8

and run it with CLI as follows:

sudo docker run -i -t pwittchen/alpine-java8

In this case, container has 118.5 MB, which is better result than for the previous container. In this case, we’re installing Java 8 for Alpine from official repository, so probably it’s already optimized.

I hope, you’ll find it useful while developing your projects in Java 8 or Java 9.

DroidCon Poland: Is your app really connected?

2017-12-02 Android, Conferences No comments

Yesterday, I gave a presentation about connectivity in the Android apps during the DroidCon Poland 2017 Conference in Kraków.

Below, you can see slides from this presentation.

There’s also tweet related to this presentation from DroidCon Kraków:

I hope, you enjoyed it. Any kind of feedback is welcome (in the comments below this article or via e-mail).

Don’t forget to check ReactiveNetwork library I mentioned during the presentation.