Blog


P38 Part4 – ‘Time’ for a change

After fitting a new Android head unit, powerfold mirrors and rear seat entertainment my next project was decided by my partner’s plaintive question of ‘where’s the clock’ when we were driving at night.

The P38 as standard has this in the dash:

I think the face is supposed to light up at night, but mine never has, hence the complete lack of timekeeping once it’s dark.

I could have just fixed the light, but where would the fun be in that? I decided, instead, to design and build an arduino powered LCD Clock that can also (at some point in the future) display other things like…. temperature, car suspension height, anything else I can tap into.

I had a spare NodeMcu board lying around and debated whether to rely on my car’s internet connection to get the time from an NTP client.  In the end I decided to use an RTC module, this one in fact. 

For the screen, I needed something that would fit nicely into the space available (60mm by 55mm) and, after trying a 1.5″ OLED display eventually settled on a 1.8″ LCD. Getting that working was a bit of a pain initially, but after finding this incredibly helpful comment on amazon I got it going with no further problems.

The actual code was surprisingly straightforward. Using the Examples from the adafruit RTC library it was pretty straightforward to set / get the time from the RTC chip (see bottom of this post for the full code)

To display the time I played around with a few different formats but eventually settled on a simple 24 hour digital display and the current date, with an analog face just for added niceness. The analog code was blatantly ripped off from here and mangled to make something that works (that’s how I roll) and the final display looks something like this:

the circle on the bottom left is a light sensor, the display goes brighter in direct sunlight and dims when it’s dark, to avoid being dazzled at night.

On the left of the display are the outputs from a nifty 12v to usb board I found on ebay, by soldering some wires to the back of the USB ports I also pull 5v from this to power the board and LCD.

The enclosure went through several revisions, but now has an outer ‘shell’ and an interior panel that fits tightly into it, holding the LCD panel and light sensor in place. I’m quite pleased with this, no glue is required to assemble it and everything stays in place pretty well!

You can find the arduino code for this project here and the 3d printing files for the enclosure here




P38 Part 3 – Rear seat entertainment

One thing that always made me want a range rover when I was younger, was the screens in the rear headrests. The thought of having the option of watching videos (yes, actual VHS videos!) while stuck in the back of a car on a road trip was just mind blowing to my teenage self. On buying my very own V8 powered beast, rear screens were one of the first things I wanted to add (since mine originally didn’t have them fitted).




P38 part 2 – folding wing mirrors

After having my range rover for a couple of months, a few things have started to annoy me, notably having to push the wing mirrors in when I park up on my (narrow, typically British) front street. Because the car is so wide, if I park with the nearside next to the kerb I have to walk round the car to reach the mirror on that side. Clearly this is unacceptable, so I sought a solution.




P38 part 1 – adding android

I’ve had limited success doing multi-part posts in the past, but for this little ongoing project I thought I’d give it another go….

At the ripe old age of 38, in April 2018 I passed my driving test (4 minors, all to do with use of mirrors!) and decided to buy my first car. Rather than go for something boring, that just…worked, I thought I’d go for possibly the most troublesome, unreliable and badly engineered car I could find which led to me spending in the region of £2000 on this beautiful boxy vision of british engineering:




Docker – add host.docker.internal on linux

Just a quick note (for my own memory more than anything)…

If you need to connect from a docker container to something running on your local host, it can be a bit of a pain on Linux. On windows & Mac, there’s a special DNS entry ‘host.docker.internal’ which always points to the host machine. However that isn’t available on Linux (here’s a bug open for this as of May 2018: https://github.com/docker/for-linux/issues/264)

That bug report also contains a pretty good solution for this problem, so to save you wading through, simply add the following line to your docker file:

RUN ip -4 route list match 0/0 | awk '{print $3 "host.docker.internal"}' >> /etc/hosts

That adds an entry to the container’s Host file, so host.docker.internal will resolve to your host just like it does on Mac / Windows.




e-Commerce building blocks

I’ve been working in e-commerce for a good few years now, for very similar companies (early stage, fast fashion focussed) and there’s always the same sorts of problems to solve. Here’s a very basic, very generic guide to the things you need to get up and running. Each bit has a ‘too long / didn’t read’ summary at the end, so you can just look for those if you don’t like reading. Please note that this is entirely my opinion, I don’t get paid by any of the companies I mention (although I am totally open to bribery. Seriously guys, hit me up.)

I’m going to split this up into a few different sections:

  1. Your website
  2. Your logistics (warehouse etc)
  3. Your customer service
  4. Internal IT / infrastructure
  5. Data / Reporting

First up…..




Merchandise magento category by product age / availability

Just a quick snippet of code today, it grabs all the products in a certain category (given in category_id), sorts them by age and stock availability (you can adjust the relative weighting of these using $stock_weight) and then updates the category. The end result is a nice mix of newer products, and products with loads of stock towards the top of the category, older / lower stocked products further down.

This uses the external database script access script from: here

I should probably bundle this up into an extension, with a cron job and so on but…. I’ll leave that as an exercise for the reader!


$category_id = 8;
$stock_weight = 10;


$sql = "select ccp.*, DATEDIFF(Now(),cpe.created_at) as age,
(select sum(qty) from cataloginventory_stock_item as csi join catalog_product_super_link as cpsl on csi.product_id = cpsl.product_id where cpsl.parent_id = cpe.entity_id) as stock_qty,
((select DATEDIFF(Now(),MIN(created_at)) from catalog_product_entity) - (select age)) as age_factor,
(select stock_qty) * $stock_weight as stock_factor,
(select age_factor) + (select stock_factor) as sortby
from catalog_category_product as ccp
	join catalog_product_entity as cpe
		on cpe.entity_id = ccp.product_id
where ccp.category_id = $category_id
and cpe.type_id = 'configurable'
ORDER BY sortby DESC";


function build_query($results){
  $update_sql = "Insert into catalog_category_product (`category_id`,`product_id`,`position`) VALUES ";
  $pos = 0;
  foreach($results as $result){
    $pos++;
    $update_sql .= "('".$result['category_id']."','".$result['product_id']."','".$pos."'),";
  }
  $update_sql = rtrim($update_sql,',');
  return $update_sql;
}


require_once "include/db.php";

$db = new db();

$results = $db->query($sql);
$db->query("delete from catalog_category_product where category_id = $category_id");
$db->query(build_query($results));








Access magento database outside magento

Just a quick note, sometimes you need to access the magento database from a script, but don’t want the overhead of loading the whole magento framework….. Here’s a little snippet of code that  grabs the database details from the magento xml config, so you don’t have to store them in your script:

<?php
 class db {

 private $data = null;


 public function __construct(){
     $xml=simplexml_load_file(realpath(dirname(__FILE__))."/../../app/etc/local.xml");

     $host = (string) $xml->global->resources->default_setup->connection->host;
     $db = (string) $xml->global->resources->default_setup->connection->dbname;
     $user =(string) $xml->global->resources->default_setup->connection->username;
     $pass = (string) $xml->global->resources->default_setup->connection->password;
     $charset = 'utf8mb4';

     $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
     $opt = [
         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
         PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
         PDO::ATTR_EMULATE_PREPARES => false,
     ];
     $this->data = new PDO($dsn, $user, $pass, $opt);
 }

 public function query($sql){
    return $this->data->query($sql)->fetchAll(PDO::FETCH_ASSOC);
 }

 }

 ?>

Save it somewhere like /magento_root/scripts/include/db.php and use it like this:
 

<?php

require "include/db.php"
$db = new db();

$results = $db->query('select * from cataloginventory_stock_item');

?>

 

 

 




Mage Tools

Quick plug for some magento scripts I wrote for development / deployments of Magento 1.x

There’s some info in the readme, but I might write a couple of quick tutorials if anyone’s interested:

https://github.com/Tall-Paul/mage-tools




Dev tooling with docker

This is the first in what will become a series of articles on development processes and techniques in use at my current place of work, Public Desire.  To start off with I’m going to take a run through our docker based development environment, then in future articles I might dip into our deployment processes and testing.  In my usual fashion I’m going to stay away from delving too deeply into the concepts and technical gubbins underlaying Docker and concentrate on the pragmatic business of getting a dev environment up and running.  If you’ve wanted to give docker a try, but found it a little intimidating give this a shot….