2

I have this simple Perl software to monitor the activity of a my home automation bus and "say" when a light changes its status. I would like to implemented it using the Swift language using the Xcode playground. Any solutions to try?

#!/usr/bin/perl -w
#
use strict;
use IO::Socket::INET;
my $mh200ip = "10.0.1.82";
my $ownport = "20000";
my $frames;
my $socket = IO::Socket::INET->new(
    PeerAddr => $mh200ip,
    PeerPort => $ownport,
    Proto    => "tcp",
    Type     => SOCK_STREAM
);
die "Could not create socket: $!\n" unless $socket;
$socket->send("*99*1##");    # Sending OPEN ACK

while (1) {
    $socket->recv( $frames, 128 );
    if ( $frames ne '' ) {
        print "$frames\n";
        my $data = ($frames);
        my @values = split( '##', $data );
        if ( $data eq "*1*0*53##" ) {
            `say "light off"`;
        }
        if ( $data eq "*1*1*53##" ) {
            `say "light on"`;
        }
    } else {
        print "server closed connection";
        exit 1;
    }
}
Miller
  • 34,962
  • 4
  • 39
  • 60
  • This is a “please translate my code”-request, which is not suitable for Stack Overflow – it's far too broad. Instead of asking for a translation, first attempt a translation and then ask about the specific parts you didn't manage to translate yourself. This demonstrates that you've undertaken some effort to help yourself instead of asking others to do all the work for you. – amon Oct 29 '14 at 23:17

1 Answers1

1

This is an almost-verbatim translation into Swift of your Perl snippet:

#!/usr/bin/swift
import Foundation

func say(whatToSay: String) {
    let task = NSTask()
    task.launchPath = "/usr/bin/say"
    task.arguments = [whatToSay]
    task.launch()
}

let addr = "127.0.0.1"
let port = 2000

var buffer = [UInt8](count: 255, repeatedValue: 0)

var inp : NSInputStream?
var out : NSOutputStream?

NSStream.getStreamsToHostWithName(addr, port: port, inputStream: &inp, outputStream: &out)

if inp != nil && out != nil {
    let inputStream : NSInputStream = inp!
    let outputStream : NSOutputStream = out!
    inputStream.open()
    outputStream.open()

    if outputStream.streamError == nil && inputStream.streamError == nil {
        let queryString: String = "*99*1##"
        let queryData: NSData = queryString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
        while true {
            outputStream.write(UnsafePointer(queryData.bytes),maxLength:queryData.length)
            var readChars: Int = inputStream.read(&buffer, maxLength: buffer.count)
            if (readChars > 0) {
                let readString: String = NSString(data: NSData(bytes:buffer, length:readChars), encoding: NSUTF8StringEncoding)!
                if readString == "*1*0*53##" {
                    say("Light off")
                } else if readString == "*1*1*53##" {
                    say("Light on")
                }
            } else {
                println ("server closed connection")
                inputStream.close()
                outputStream.close()
                break
            }
        }
    } else {
        println ("could not create socket")
    }
} else {
    println ("could not initialize stream")
}
  • I started Xcode again, and the file runs also in the playground. The println results are shown in the Value History window. – Andres Albanese Oct 31 '14 at 04:17
  • Thanks Francesco, your program was running OK before Swift 3. Now I have a problem with the Unsafe winter type that I am not able to fix. – Andres Albanese Oct 19 '16 at 08:05