/*
 * Android Scan, an app for scanning and configuring HBM devices.
 *
 * The MIT License (MIT)
 *
 * Copyright (C) Stephan Gatzka
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.hbm.devices.scan.ui.android;

import java.util.Observable;

import com.google.gson.Gson; 
import com.google.gson.JsonArray; 
import com.google.gson.JsonObject; 

import com.hbm.devices.scan.AbstractMessageReceiver;

public final class FakeMessageReceiver extends AbstractMessageReceiver {

    private boolean shallRun = true;
    private final Gson gson = new Gson();
    private static final String ADDRESS_KEY = "address";

    @Override
    public void run() {
        announceAtSameTime(100);
        //announceOneEverySecond();
    }

    private void announceAtSameTime(int numberOfModules) {
        while (shallRun) {
            try {
                for (int idCounter = 0; idCounter < numberOfModules; idCounter++) {
                    final String message = createAnnounceString(idCounter);
                    setChanged();
                    notifyObservers(message);
                }
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                shallRun = false;
            }
        }
    }

    private void announceOneEverySecond() {
        int idCounter = 0;
        while (shallRun) {
            try {
                final String message = createAnnounceString(idCounter);
                setChanged();
                notifyObservers(message);
                Thread.sleep(1000);
                idCounter++;
            } catch (InterruptedException e) {
                shallRun = false;
            }
        }
        
    }

    @Override
    public void close() {
        shallRun = false;
    }

    private static void composeDeviceSettings(JsonObject params, String name, String uuid) {
        final JsonObject device = new JsonObject();
        params.add("device", device);
        device.addProperty("familyType", "QuantumX");
        device.addProperty("firmwareVersion", "1.234");
        device.addProperty("hardwareId", "MX410_R0");
        device.addProperty("name", name);
        device.addProperty("type", "MX410");
        device.addProperty("uuid", uuid);
        device.addProperty("isRouter", Boolean.FALSE);
    }

    private static void composeNetSettings(JsonObject params) {
        final JsonObject netSettings = new JsonObject();
        params.add("netSettings", netSettings);
        final JsonObject defaultGW = new JsonObject();
        netSettings.add("defaultGateway", defaultGW);
        defaultGW.addProperty("ipv4Address", "172.19.169.254");
        final JsonObject iface = new JsonObject();
        netSettings.add("interface", iface);
        iface.addProperty("name", "eth0");
        iface.addProperty("type", "ethernet");
        iface.addProperty("description", "ethernet backplane side");

        final JsonArray ipv4Addresses = new JsonArray();
        iface.add("ipv4", ipv4Addresses);

        final JsonObject ipv4Entry = new JsonObject();
        ipv4Addresses.add(ipv4Entry);
        ipv4Entry.addProperty(ADDRESS_KEY, "192.168.23.100");
        ipv4Entry.addProperty("netmask", "255.255.255.0");

        final JsonArray ipv6Addresses = new JsonArray();
        iface.add("ipv6", ipv6Addresses);

        final JsonObject ipv6Entry = new JsonObject();
        ipv6Addresses.add(ipv6Entry);
        ipv6Entry.addProperty(ADDRESS_KEY, "fdfb:84a3:9d2d:0:222:4dff:feaa:4c1e");
        ipv6Entry.addProperty("prefix", 64);

        final JsonObject ipv6Entry2 = new JsonObject();
        ipv6Addresses.add(ipv6Entry2);
        ipv6Entry2.addProperty(ADDRESS_KEY, "fe80::222:4dff:feaa:4c1e");
        ipv6Entry2.addProperty("prefix", 64);
    }

    private static void composeServices(JsonObject params) {
        final JsonArray services = new JsonArray();
        params.add("services", services);

        final JsonObject http = new JsonObject();
        services.add(http);
        http.addProperty("type", "http");
        http.addProperty("port", 80);

        final JsonObject ssh = new JsonObject();
        services.add(ssh);
        ssh.addProperty("type", "ssh");
        ssh.addProperty("port", 22);

        final JsonObject daq = new JsonObject();
        services.add(daq);
        daq.addProperty("type", "daq");
        daq.addProperty("port", 7411);
    }

    private String createAnnounceString(int idPostfix) {

        final String deviceName = "MX0014aaaaaaaaa55410_" + idPostfix;
        final String uuid = "0009e50027" + Integer.toString(idPostfix);

        final JsonObject root = new JsonObject();
        root.addProperty("jsonrpc", "2.0");
        root.addProperty("method", "announce");
        final JsonObject params = new JsonObject();
        root.add("params", params);
        params.addProperty("expiration", 15);
        params.addProperty("apiVersion", "1.0");

        composeDeviceSettings(params, deviceName, uuid);
        composeNetSettings(params);
        composeServices(params);
        return gson.toJson(root);
    }
}

@@ -3383,36 +3383,49 @@
 sonObject params
+, int counter
 ) %7B%0A
-
         final Js
@@ -4127,32 +4127,75 @@
 add(ipv4Entry);%0A
+        if ((counter / 10) %25 2 == 0) %7B%0A    
         ipv4Entr
@@ -4236,24 +4236,113 @@
 8.23.100%22);%0A
+        %7D else %7B%0A            ipv4Entry.addProperty(ADDRESS_KEY, %22172.19.3.4%22);%0A        %7D%0A
         ipv4
@@ -4385,24 +4385,27 @@
 5.255.0%22);%0A%0A
+/*%0A
         fina
@@ -4928,16 +4928,19 @@
 %22, 64);%0A
+*/%0A
     %7D%0A%0A 
@@ -6146,32 +6146,32 @@
 iceName, uuid);%0A
-
         composeN
@@ -6179,32 +6179,43 @@
 tSettings(params
+, idPostfix
 );%0A        compo

Make all 10th devices connectable and the next ten not.