2021.05.07

Comparing bytes differences between 2 binary files.

  1. Open a binary file
  2. M-x hexl-mode
  3. In another buffer, open another binary file
  4. M-x hexl-mode
  5. M-x ediff-buffers
  6. Press enter twice to automatically compare the last two buffers
  7. In the small ediff popup window, press n to go to the next difference
  8. Press * to show more detailed difference by byte
2021.02.03

I just recently started reading about Agile and Test Driven Development. Youtube video of Uncle Bob speaking in Netherland was really insightful (The way he describes customer don’t know what they want until they see what they don’t want is so true!).

Anyway, I want to start using test functions in my codes, and a good start is to implement it in a new project. Just at this time I’m developing another Firebase project. I need mocha for the test and typescript to be able to import the interface that I used in my App. I hate setting up node js projects because I don’t really understand how to make it work (and the versioning problem as well). But I managed to set it up quite easily.

Installed dependencies (dev):

  • “@firebase/rules-unit-testing”: “^1.1.10”
  • “@types/mocha”: “^8.2.0”

Installed dependencies:

  • “firebase”: “^7.21.1”
  • “firebase-admin”: “^9.4.2”
  • “firebase-tools”: “^8.18.1”
  • “mocha”: “^8.2.1”
  • “ts-node”: “^9.1.1”

Running script (package.json): mocha --exit --require ts-node/register test/test.ts

Importing mocha in test/test.ts:

import "mocha";
import * as fs from "fs";
import * as firebase from '@firebase/rules-unit-testing';

And the rest is as the example from here

Neat!

2021.01.10

Since the import lines in python can take several seconds (in Raspberry Pi), it’s better to just use a simple bash when trying to do simple stuff.

In bash scripts, I often see /dev/null but never really sure what it’s used for. Also, there’s a lot of different > like 2>/dev/null or &> /dev/null or > /dev/null 2>&1 etc.

/dev/null is basically a way to suppress output. 1 refers to stdout, and 2 refers to stderr. Basically &> /dev/null a new syntax for > /dev/null 2>&1.

Example:

  • ls by itself will show the result in terminal
  • ls 1> /dev/null will not show the result in terminal
  • ls 2> /dev/null will show the result in terminal
  • ls &> /dev/null will not show the result in terminal
  • ls -0 1> /dev/null will show the error in terminal
  • ls -0 2> /dev/null will not show the error in terminal
  • ls -0 &> /dev/null will not show the error in terminal

echo $? is a neat way to get the result of previously executed command, 0 when succeeded, and 1 when failed.

Example:

  • ping -c 1 google.com &> /dev/null; echo $? will print out 0 if the ping succeeded.

Another useful thing when debugging a script run with cron is to echo to journal. By echo 'Debug' | /usr/bin/systemd-cat we can see the echoed line in journalctl -f. And do not to forget to add #!/bin/bash at the beginning of the bash script file.

ref

2020.07.10

2 years ago when I started using cloud service, specifically google’s, I didn’t really know about the difference between services, blindly following tutorials. Heck, even now I don’t really have a clue as well (too many cloud services, especially AWS!).

Just today I found out that google cloud function for Node.js 8 will be deprecated and everyone is asked to update to version 10, which also requires Blaze plan, which means giving google a credit card number. That sucks. I didn’t know until today that I can actually make google function with python. I should’ve used this 2 months ago if I knew Blaze plan would be required…

Google Function with Firebase Admin in python

  1. In one folder, create requirements.txt and main.py
  2. Login with gcloud auth login
  3. Deploy function with gcloud functions deploy firebase_test --runtime python37 --trigger-http
  4. Add allUsers by adding members from google cloud console

requirements.txt

Flask==1.0.2
firebase-admin

main.py

import firebase_admin
from firebase_admin import db
import flask

firebase_admin.initialize_app(options={
    'databaseURL': 'https://<path>.firebaseio.com',
})

root_ref = db.reference('ref')

def firebase_test(request):
    name = root_ref.child("name").get()
    if not name:
        return 'Resource not found', 404
    return flask.jsonify({"name":name})

For Firebase Auth, I need to check this.

Google App Engine

Dialogflow’s inline fulfillment will also be required to use Node.js 10 I suppose (I think it only allows javascript). Also, without Blaze plan outbound request (request to server other than google) is not possible. I didn’t remember that I was actually making outbound request in Dialogflow’s fulfillment. I thought it wasn’t working and finally I just noticed that 2 years ago I actually used Google App Engine for the fulfillment, which apparently allows outbound request.

Sending Email

  1. Google App Engine: 100 email per day, or use Mailjet (free for 6000 email per month)
  2. Google Function: Using gmail only for 500 email per day, or use nodemailer with Blaze plan
2020.05.06

The app needs to read/write from serial. Build in macOS Catalina. Error due to version sucks.

Electron

  • Was able to read serial using nodejs serialport
  • Versioning error when trying to read the serial from electron
  • Fix the version to the older ones from here:
      "electron": "5.0.11",
      "electron-rebuild": "1.8.6"
      "serialport": "^7.1.5"
    
  • How to read and write to serial from the html

PyInstaller

  • pyserial is supported by PyInstaller
  • Tried with the python3 default from Catalina, does not work OSError: Python library not found: libpython3.7.dylib, libpython3.7m.dylib, Python, .Python
  • While searching, found that people uses pyenv to get the library, so
      # Install pyenv
      brew update
      brew install pyenv
    
      # Set up pyenv in bash
      git clone https://github.com/pyenv/pyenv.git ~/.pyenv
      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
      echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
      echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
    
      # Install python
      env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.3
      which python        # will return ~/.pyenv/shims/python not the default python anymore
      pyenv versions      # will list out different python installed in the system
      pyenv global 3.7.3  # uses python version 3.7.3
    
      # Install packages
      pip -V              # check if it comes from pyenv
      pip install pyinstaller pyserial
    
  • Make the executable by python -m PyInstaller test.py, running pyinstaller directly didn’t work. This will create a folder with a bunch of libraries inside. The first launch is slow, but consequent launch is fast.
  • To generate one executable, python -m PyInstaller -c --clean --onefile test.py. However, the launch is slow…