Monday, May 25, 2015

Setting and resetting Default Apps

Was playing with a map app. The map application offers to take pictures. When the option is clicked, it prompts to choose which camera app to use. Being curious, I select "always use" option and did not notice the prompt after that.

I tried to go through all the menus and settings but just could not find the setting to unset the default app. Going to the internet to search for "unset default app" for the map app does not help. It did not have anything that is related to resetting the default.

Going on a hunch, I tried looking for "Android reset default app". There are a lot of people talking about it. I hit jackpot.

Unlike windows programs where all the settings are within the program itself, Android have different ways of doing things. You can set the default app in another app but to reset it, you have to go back to "settings", "application management" and select the application to reset. Just click on the app and scroll down. You will find a "reset default" button. Choose it and the work is done.

You may ask "what about other apps which I want to keep the default camera setting". Well, you are out of luck.



Sunday, May 24, 2015

Simple Restful Web Services

I was trying to use PHP CURL library to access a web services. In the process I found a useful library that makes using CURL very easy. It is called Httpful (https://github.com/nategood/httpful). The library is useful if you don't mind the many duplicate functionality.

In the process of testing, I was wondering whether CURL actually posts the XML. Since I did not want to access any real time web services, I decided to use PHP to get the posting. Naturally it failed. The standard global variables $_POST[] does not contain any content neither does $_RESPONSE[]. I search the internet and discovered that the post is more like posting a file rather than posting a form. There is a PHP function to get this kind of content.

After successfully retrieving the content, I was wondering whether I can make this functionality work like a very simple restful web services. I began to write a proper PHP page to simulate the web service. The following are the scripts for posting and the restful web services page. The XML request and response are dummy values which is stored as a simple variable, It is not in the scope of this blog to show how to create XML in PHP.

The posting page :-

include "httpful.phar";
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$url="http://localhost/sampleresponse.php";
$authenticate = "jonathan@mail.commypassword";

$response = \Httpful\Request::Post($url)
    ->body($authenticate)
    ->mime("XML")
    ->autoParse(false)
    ->expectsType("XML")
    ->send();
$xml= new SimpleXMLElement($response);
$sessionid=$xml->xpath("//userSessionId")[0];
if ($sessionid==""){
    die("Error User does not exist or password is wrong.");
}

The restful response :-

include "httpful.phar";
$postText = file_get_contents('php://input');
$xml= new SimpleXMLElement($postText);
$auth=$xml->xpath("//AuthenticateRequest")[0];
if ($auth){
$user=$xml->xpath("//userId")[0];
.
.
.
if ($verified) {
    die("
             Sdt7tXp2XytTEVwHBeDx6lHTXI3w9s+M
       
");}};

Well, it is simple straightforward PHP turned into restful web services. Obviously CURL could post user credentials itself while posting the request. This is a simple example.

Not everyone have a web server. In the case of user just want to access the restful web services via the browser, they could simply write a Javascript AJAX function to do the posting. There are plenty of examples in the internet that shows you how to do posting in AJAX. It will not show it here.

For more security, it is best to use HTTPS for the web services.



Sunday, May 03, 2015

Traffic Lights at Junctions

Recently there is a discussion on Facebook Group on the above subject. The discussion is quite extensive. I will give an account on the facts discuss and add a few points not discussed.

In modern cities roads are primarily meant for cars. Pedestrian walks were added to facilitate pedestrian transports. On some old roads pedestrian still share with cars. Express ways are for the sole use by cars only.

LTA does not leave pedestrians without the means too. First, there are pedestrian crossings guided by traffic light. Many busy roads have on-request buttons so as to keep cars moving more smoothly. Minor roads does not have button control. It just switch on and off regularly.

Zebra crossings are actually pedestrian priority crossings. It is usually found near schools. Pedestrian bridges span across roads so that it will not disrupt car traffic. Similarly, underpass for pedestrians serve the same purpose.

There are a number of junctions near MRTs that are fitted with senior citizen card reader so that it will give senior citizens extra time to cross the road.

Some roads are closed at specific time just for pedestrians. It is not just during festive seasons. Some place like Holland Village have permanent time slots for this purpose.

As usual, every group are biased towards their own stand point. It is not wrong to behave this way as it will check the balance of the transportation facility based on their own interest. However, most people does not look at the standpoint of the other side.

During my study at a seminary, the Dean Dr. Roger Capps have a specific assignment for the Masters degree students. They are to take on the role of the opposition and argue from that stand point. His reason is that if the student can't even withstand his own argument against his own belief, how would he/she able to go out and face the real world. Obviously it is super tough to be in the shoe of the opposition to one's conviction. Sun Tze have this rule "知己知彼,百战百胜。" (Know yourself, know your enemy, you will always win). Its an analogy that pass the time test.

Back to our point on traffic light. It is this lesson that I learned that keeps me reminded that I should not look only on my stand point. The lesson applies to every one too. Obviously, we would want the traffic light to favor pedestrians. However, it means that cars will suffer. If you have to drive through the city, you will know how it feels to stop at every traffic light. There are lots of people earning a living driving vehicles. It is, therefore, imperative that a balance must be maintained. Although, the priority is given to cars, but there are a number of avenues where pedestrians were given due consideration. We should accept the fact that not all things will work in our favor. We complained against cars but cars also complained against us too.

LTA should not stop making life better for pedestrians too. There are other countries that favor pedestrians to cars.  LTA have to look at what is done and see if it can be implemented here too. However, just because other did it does not means that it is applicable here. Each country have their own unique setup. It works in one place does not means that it works everywhere.




Saturday, May 02, 2015

Silent Majority

In society there are a large number of people usually does not voice out or act openly. They are usually called the "Silent Majority". Their opinions are usually unknown since they do not act or voice out. It is, therefore, very difficult to find out what is their opinion on a specific issue unless a very large scale anonymous opinion poll is conducted.

Those people in the "silent majority" usually composed of the following

1. Those who just don't care.
2. Those who are indifferent to the issue.
3. Those who are against.
4. Those who are pro activity but does not want to take part.
5. Those who are afraid.
6. Those who are too timid to act.
7. Those who are too tied down/busy to act.

When one stands out and say he/she speaks for the "silent majority", The implication is that he/she is speaking on behalf of "all" of them. It is actually true only for those who are agreeable to the action but do not act. He/she cannot claim to speak on behalf of all "silent majority". Since they are "silent", how can you know whether you are truly speaking on behalf of "all".




Saturday, March 28, 2015

Printing to Label Printer

In commerce, shops used to print receipts on a small printer called Receipt Printer. These printers usually do not require a driver. Basically you just treat it as a device and send raw data to it like sending chat messages.

Here I am not talking about what to send to the printer. It is about how to send the raw text to the printer. These printers can generally be connected via Parallel, Serial, USB, WIFI, Bluetooth or Ethernet. The first two is getting rare as most computers and devices now don't have LPT and COM ports.

Modern technology also enable mobile devices like notepads to talk to the printer through wireless communication like Bluetooth or Wifi.

Most shop use a computer/server to manage the Point of Sales system. Therefore, there is always a computing device available. In addition, some may use mobile devices to communicate with the computing device ("server). It is usually the server that does the printing. In some circumstances, the mobile device actually does the printing directly.

In order for the device to talk to the printer, both must be in the same network regardless of whether it is one->one or one->many type of connection. Generally they use a intranet infrastructure if there are two or more devices.

The easiest way is to provide a network system using a standard network router. It need not have a connection to the internet. This method applies to Wifi and Ethernet printer devices.

Another method is to use Bluetooth. This method needs the device to be paired. It does not matter how many can pair with the printer. Some actually does not require pairing. I have little information on this technology.

Once the connection is available, the next task is to use a protocol to talk to the printer.I have not played with Bluetooth enabled printer thus unable to talk about it.

The more common ones usually use Wifi or Ethernet. The two are basically the same except that one is wireless the other one requires a LAN connection.

Since they are connected via network, the best protocol is of course Sockets. To use sockets, devices must be identified by a IP address and a port number. There are plenty of programs that could perform sockets. It will depend on what programming language you use. Just look for "Sockets" in your programming language and you should be able to get plenty of example on how to do it.

No matter what programming language you use, it generally follows the pattern of "opening a connection", "write to the connection" and "close the connection", Here I use PHP as an example.

The very first thing is to connect to the printer. Therefore we use "fsockopen" like ths

$mysock = fsockopen("192.168.0.100", "9100");

The IP address varies according to your network setting. The port number is generally set as 9100. The command allows us to open a connection to the printer.

Once the connection is open, it is treated like any media connection (e.g. file). It is therefore appropriate to use fwrite command (file write) to write to it. Since you generally do not expect a communication to be sent from the printer, you do not need to do fread (file read).

The fwrite command looks like this.

$written = fwrite($mysock, "my text", 7);

It is always good to include the length of the text you are writing. This method of writing to a device is generally referred to as "streaming". When you do streaming with fwrite, there is a peculiar thing that you must always remember. You may write 10000 characters to the printer. Not all the 10000 will be successfully sent. Some will be lost.

Luckily fwrite does returns a number indicating what was successfully sent. You just need to get the returned value and use a substr command to write the rest of the string till all are written. The following code is copied from internet.

function fwrite_stream($fp, $string) {
    for (
$written = 0; $written < strlen($string); $written += $fwrite) {
        
$fwrite = fwrite($fp, substr($string, $written));
        if (
$fwrite === false) {
            return 
$written;
        }
    }
    return 
$written;
}


I am always amazed as to why fwrite never ensure that all the characters are written. Anyhow, there must be a reason for it.

OK. You now have written the text over to the printer. Obviously you do not want to keep the connection open. The next natural step is to close the connection.

fclose($mysock);

Is it not simple? Actually the hard part is to figure what to sent to the printer. Generally you have to use "ESC" code to achieve this. Most printer actually use Ascii 27 (HEX 1B) to indicate that the text follows is a command instead of pure text. PHP have a nice way of allowing this by another "escape" code. It uses "\x" to indicate that the next two character after it is a HEX code. When PHP sends the text "\x1B" it becomes a single character that is equal to Ascii 27.

I shall not go too deep into this except that most printers expect a start code and a end code. It depends on what code the printer uses. This allow the printer to start interpreting the data and stops all activity accordingly.

If you are thinking of using an iOS device to do it. I suggest you do a deep research first before even trying. Apple does not allow user to even use their own programming codes on iOS devices. Every program that you want to run on iOS has to be code signed (from Apple). This means that you need to pay Apple ($99 a year for personal codes) to write and use your own code in iOS. No wonder people want to "jailbreak" iOS.

Android also requires you to pay but it is a one time payment only. Furthermore, if you do not want to sell your app in Google Play Store, you still can write your own and use it on Android without paying.



Thursday, March 26, 2015

Transparent background PNG with Google Map

I used to have this problem with using PNG icons on Google Map. It just refused to honor the PNG setting for transparency. This applies to GIF too.

Recently, I have accidentally deleted "New PCN on Bike" map so I had to create a new one from the SHP files which I exported. Again I faced with the issue of icon transparency. Some how I downloaded PNGs from the internet and they all have transparency background enabled. Why then my own creation cannot work?

I tried using graphic editors to see what is the difference between my own PNG and internet ones. Since the PNG are actually icons, I start to search for icon editors instead. No point using a graphic editor that could edit large bitmap files for such a small picture.

One of the icon editor is a shareware but its earlier version is free. This app is called IconFX 1.6. When I open my PNG and compare with other PNG, I noticed that my background is solid color whereas other PNG is "checkered". There is actually no color on the "checkered" area. It is then I realize that I don't even have to set transparency for the PNG files. Just delete the unwanted part and I have transparency. It is as simple as that.

With the recent Google change of heart on the Google Map, icon are now available and more than 5 layers are allowed. The map now looks nicer with many layers.



Wednesday, March 18, 2015

Printing on receipt printer using PHP

Printing in PHP is actually not a right solution. In windows PHP, it does have a dll to do printing. On Mac, and others, you don't have such luck. PHP is server side script which could be anywhere in the world. Why do you need to send print commands at the server? Actually it is usually used in business where the client sent a print copy to the company like for example placing an order. However, the use is very limited. Most of the time the company employee will print using local browser to the local printer.

PHP printing does have its use if it is used within a close vicinity of the user like for example a retail shop. It usually employs one printer for all the requests. The printouts are usually receipts printed in narrow stripes of paper. That's why they are also called "receipt printer". This is especially useful in Point of Sale (POS) system. It is easier to write a web based POS program than a custom application.

Now, it defeats the purpose of having a web based program when you need to install print drivers on the user's computer just to do printing. Why not send print commands directly to the printer? Yes. it is actually done in this way.

There exists a series of printers that does not require drivers. They accept text as input. The early printers usually coded the text using ASCII "ESC" to format the printout. Thus it is usual to refer to this kind of printing as ESC POS because both are closely associated with each other.  Epson is the first one that patented the codes.

With the advance of technology, using a tablet or iPad to do mobile POS is becoming a trend. Device like iPad generally do not have print drivers. It is therefore well suited to run POS systems. The printers that works with mobile devices are usually bluetooth,  WIFI or network printers.

The PHP program to do printing is quite simple. In a few lines of code the printing can be done.

The program starts with

$fs = fsockopen(192.168.0.1.111, 9100);

You can see that the address is actually an Local IP address. 9100 is the normal port used.

The printing starts with an escape code ESC @. This is a command to initiate the printer.

$bytes = fwrite($fs, "\x1B@");

The next series of "fwrite" usually contains a series of ESC codes followed by text to format the text according to the coding commands. There are simply too many such coding commands to be described here.

The last "fwrite"  will be the ESC i - a command to cut paper. Some printers use GS V n to do paper cutting.

It is well known that fwrite don't always send all the data when used on "streams". The term normally refer to sending a series of data (streaming) to remote connected devices. As fwrite does return a value representing the number of bytes sent, it is usually used within a loop where a total byte count is kept and the fwrite is continuous send with the remainder text as value using substr($string, total_byte_sent) as value. You can read about "substr" command in PHP documentation.

After sending all the data, the last step is of course to close the stream

fclose($fs);

It is as simple as that. Actually it took me a week just to write this code as it is the first time I tried printing to streams in PHP.

In real life, the text are usually submitted form data or database so you normally have to use $_POST["var"] or SQL to get the text and then add the ESC codes to format it.

Below is a sample code.

function fwrite_stream($fp, $string) {
    for ($written = 0; $written < strlen($string); $written += $fwrite) {
        $fwrite = fwrite($fp, substr($string, $written));
        if ($fwrite === false) {
            return $written;
        }
    }
    return $written;
}
$fp = fsockopen("192.168.1.3",9100);
if (!$fp){
    die("Cannot open sock");
}
$mytext="Hello this is a test print 13 ";
$string="\x1B@".$mytext."\x1Bd\x07\x1Bi";
$bytes=fwrite_stream($fp, $string);
fclose($fp);
printf('wrote %d bytes',$bytes);