OU blog

Personal Blogs

David Pennington

The software for TM470 is pretty much done - just 10,000 words to write!

Visible to anyone in the world
Edited by David Pennington, Friday, 25 Aug 2017, 18:29

It has been a long slog but finally, I have an Android app working, almost(!!), the way that I want. the app is all about the routing of freight cars (that's US parlance - goods wagons in the UK, of course). Here is how it goes:

Firstly, for the initial pass at the software, I had to make sure that everything was working correctly before I started work on the Android app. To do this, I created some simple HTML web pages. The process is as follows:

  1. Create some spreadsheet comma separated files for the standing data such as towns, routes, locos, cars, etc. 
  2. Import this into the back end database under a single user name.
  3. Create web pages to display this standing data to check that it is imported correctly.
  4. Create a few pages that can update data - car locations for instance, so that any initial errors can be corrected before running the trains.
  5. Create a decision engine that analyses the train requirements and the cars available to that train and create a train make up.
  6. Display each train in sequence so that the train can be run on the layout
  7. Provide an update function that is used once the train has completed its run. This then updates the locations of each constituent part - loco, caboose, freight cars, passenger cars.
  8. Once all the trains in the sequence have been run, it recyles to the start and begins again (with everything in its current location, of course).
  9. Put all of this functionality into an Android app using Cordova/HTML and JavaScript.
If all of this is gobbledegook then check out my 470Router web site, There is a detailed explanation of the Technical Details HERE.

The train makeup page of the app is designed to look like, what the Americans call, a Switch List. This is the piece of paper that the Conductor (Guard) has to tell him how the train is comprised and what has to be sent where as they go along. A typical app screen looks like this.



Now it is done, I have two and a bit weeks to get the EMA written. I have about 6,000 words so far but I think that some of this is rubbish and needs to be severely edited. I am puzzled over how I can discuss legal and professional concerns as no one is ever going to use this except me. I have also been unable to get anyone to test it - I have discussed it on some US model railroad forums but either my descriptions have been rubbish or people are being particularly obtuse in not understanding. 

I have had comments that imply that the software is a waste of time as you can do it all with hand written cards. Well, so what, I wouldn't get much of a mark in TM470 for that! Also, people have suggested that treating each train as a discrete event misses the facts that some railroad customers might not get a freight car when they want one and I wasn't keeping this in mind. I had to explain that, believe it or not, there weren't any REAL customers - it's a model railroad, for goodness sake!

My choice of database came out of TM351. I am using a Riak No-SQL Key/Value database.  Again, comments were along the lines of - how can you do efficient searches if you aren't using a SQL database. Again, I was at a loss to understand why I would be doing searches. Once a train has run, it can be forgotten so the huge overhead of a SQL database, with all of the coding involved was a waste of time and energy. With Riak, you create a user - give it a bucket, create a JSON for each set of information and dump each set into the bucket under a key. So there is a user - "demo" with data items called locos, cars, trains, towns and so on each of which has its own key. Recovering a town is as simple as sending an http REST request for the content behind that key. In my beloved Smalltalk, it looks like this:

add: anItem toBucket: aBucket usingKey: aKey

| parameterString |

parameterString := aBucket, '/', aKey.

^ self riakSstPost: parameterString content: anItem

where aBucket is "demo" and aKey is "towns". This creates a REST request as follows:

http://192.168.1.92:8098/riak/demo/towns

So what did I find out along the way regarding the technical environment?

On a Mac, use Curl to send the REST commands with Wireshark on the go. This way, when your own REST request fails, you can see exactly where you went wrong. You have to be quick with Wireshark, though as you get load of extra information regarding all of the internet traffic down the line.

JavaScript

  • Use a knowledgeable text editor, such as Sublime (and skip over the requests to spend $70 on a full license) as this will show you the matching brackets etc.
  • Use Chrome as your browser - although Cordova will default to Chrome.
  • Always open the developer tools as they will instantly warn you if you do have a "curly bracket" moment.
  • You can in-line edit JavaScript in Chrome - but I never found out where the file was saved so I always cut and pasted back into Sublime after any in-line changes.
  • You can't in-line edit HTML as this seems to be fixed at launch time - makes sense really.
  • Chrome doesn't seem to forget some errors (cannot -- as undefined, for instance) and will continue to show them even when the problem is fixed.
  • I created all my tables on the fly in JavaScript by concatenating strings of HTML but - you can't use CSS formatting on such dynamic tables - at least that's what I found - as, I guess, the CSS stuff is done at HTML run time and not when JS executes an event.
  • I could never get a JS function to work from HTML - I wanted to use an image where the URL was in a JS variable but I never could get the image to appear - again, probably me.

Cordova

I had great success in getting Cordova to work on my big iMac (left over from TM352 days, so I thought) and also to get it up on my new MacBookPro. That was for "Cordova run browser". However, when I tried to do a "cordova run android" it just didn't want to go anywhere. I had forgotten that I had rebuilt my Mac since TM352 and the MacBookPro was a new install. I got lots of different errors. When studying TM352 and having huge trouble with TMA03 and the EMA, I signed up for a Udemy course on PhoneGap (old name of Cordova). This cost $10 and was worth its weight in gold. I went back and watched the trainer do his installation and followed it to the letter. Cordova then said that I needed an AVD. By watching some Youtube videos, I found out that I had to create an Android Studio project and then set an AVD from a button on the project screen. Bingo, my Lenovo tablet came to life - well, actually it didn't because, for some stupid reason, I was expecting Cordova to turn the tablet on - guess what, I had to do that!

As you can see from the screen shot, it has all worked out well in the end. There were a few Smalltalk bugs but they went quickly. There were some sillies in the JS and they are "sort of" sorted but not vital for the EMA.

Next episode will be my reactions to finishing the EMA. It will then go quiet for nearly three months - yes, that's how long it takes for us to get our results! Once I have the result, I will surface again. Hopefully it will be as David BSc.(Hons)!


Permalink Add your comment
Share post