<html5>
<head>
       <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>HAT Interface</title>
       <link rel="stylesheet" type="text/css" href="/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="/css/bootstrap-theme.css">
        <link rel="stylesheet" type="text/css" href="/css/style.css">
       <script type="text/javascript" src="/js/html5shiv.js"></script>
        <script type="text/javascript" src="/js/respond.min.js"></script>
        <![endif]-->
        <script type="text/javascript" src="/js/jquery-1.11.0.min.js"></script>
        <script type="text/javascript" src="/js/jquery-ui-1.10.4.min.js"></script>
        <script type="text/javascript" src="/js/bootstrap.js"></script>
        <script type="text/javascript" src="/js/fullscreen.js"></script>
        <script type="text/javascript" src="/js/jquery-cookie.js" charset="utf-8" ></script>
        <script src="/js/jquery-lang.js" charset="utf-8" type="text/javascript"></script>
        <script src="/js/utility.js" charset="utf-8" type="text/javascript"></script>


 	<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<script>
// Create a client instance
var d= new Date();
var clientid = "test"+d.getTime();
console.log(clientid);
var client = new Paho.MQTT.Client("192.168.6.2", 9001, clientid);

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});


// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("from/test/#");
  console.log("client subscribed");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "World";
  client.send(message);
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
  }
}

// called when a message arrives
function onMessageArrived(message) {
  var name_content = message.payloadString.split(":");
  var name = cleanItem(name_content[0]);
  var value = cleanItem(name_content[1]);
  update(name,value);
}

function cleanItem(item) {
  var citem = "";
  for(i=0; i<item.length; i++) if(item[i] != '"' && item[i] != '}' && item[i] != '{' && item[i] != '\n') citem += item[i];
  return citem;
}

function normalizeValue(value, digit) {
  var res = "";
  var n = 0;
  var dot = false;
  for(i=0; i<value.length && n <digit; i++) {
    res += value[i];
    if(dot == true) n++;
    if(value[i] == '.') dot = true;
  }
  return res;
}

function getNamefromTag(name) {
  var names = name.split("-");
  if(names.length >1) return names[1];
  else return "";
}

function update(name, value) {
   //document.getElementById(name).innerHtml=value;
   var pname = "#"+name;
   $(pname).text(value);
   return;
}


</script>

<style>
.all {
   padding:30px;
   width:100%;
   overflow:auto;
}

.header {
    color:black;
    text-align:left;
    font-size: 1em;
    padding:20px;
    background-color:WhiteSmoke;
    border:3px solid #d4d4d4;
    width:100%;
}


div.holder{
   display:block
   width:100%;
   float:left;
   text-align: left;
   font-size: 16px;
   color: black;
}

div.bholder{
   display:block;
   float:left;
   width:160px;
   padding:10px
   text-align: center;
   font-size: 16px;
   background-color:#CCCCCC;
   color: black;
   border:3px solid #808080;
}

img.button{
   float:right;
   width:25px;
   height:25px;
}

button.switch{
   width:100%;
   float:left;
   text-align: center;
   width:150px;
   height:40px;
   border:2px solid;
   background-color:#808080;
   font-size: 14px;
   color: white;
   moz-border-radius: 6px;
   webkit-border-radius: 6px;
   border-radius: 6px;
}

.meter{
   width:100%;
   float:left;
   width:160;
   height:40px;
   border:2px solid;
   background-color:#808080;
   font-size: 16px;
   color: white;
   moz-border-radius: 6px;
   webkit-border-radius: 6px;
   border-radius: 6px;
   padding:5px;
}

.meter_inner {
   width:120;
   height:30px;
   padding:1px;
   background-color:#808080;
   font-size: 16px;
   color: white;
   border:2px solid;
   border-radius: 6px;
   moz-border-radius: 6px;
   webkit-border-radius: 6px;
}

.measure{
   float:right;
   text-align: center;
   height:25px;
   width:50%;
   color:black;
   background-color:white;
}
</style>

</head>
<body>
<div id="allin" class="all">
<div id="header" class="header">
<b> Input </b>
</div>
<div class="holder">
<div class="meter"> IN0 <div id="S0" class="measure">000</div> </div>
</div>
<div class="holder">
<div class="meter"> IN1  <div id="S1" class="measure">000</div></div>
</div>
<div class="holder">
<div class="meter"> IN2  <div id="S2" class="measure">000</div></div>
</div>
<div class="holder">
<div class="meter"> IN3  <div id="S3" class="measure">000</div></div>
</div>
<div class="holder">
<div class="meter"> IN4  <div id="S4" class="measure">000</div></div>
</div>
<div class="holder">
<div class="meter"> IN5  <div id="S5" class="measure">000</div></div>
</div>
<div class="holder">
<div class="meter"> IN6  <div id="S6" class="measure">000</div></div>
</div>
<div class="holder">
<div class="meter"> IN7  <div id="S7" class="measure">000</div></div>
</div>
</div>
<div id="allout" class="all">
<div id="header1" class="header">
<b> Output </b>
</div>
<div class="bholder">
<button id="R0" class="switch" onclick='doClick("R0")'><img id="I_R0" src="/img/Button-Blank-Green-icon.png" class=button> OUT0 </button>
</div>
<div class="bholder">
<button id="R1" class="switch" onclick='doClick("R1")'><img id="I_R1" src="/img/Button-Blank-Green-icon.png" class=button> OUT1 </button>
</div>
<div class="bholder">
<button id="R2" class="switch" onclick='doClick("R2")'><img id="I_R2" src="/img/Button-Blank-Green-icon.png" class=button> OUT2 </button>
</div>
<div class="bholder">
<button id="R3" class="switch" onclick='doClick("R3")'><img id="I_R3" src="/img/Button-Blank-Green-icon.png" class=button> OUT3 </button>
</div>
<div class="bholder">
<button id="R4" class="switch" onclick='doClick("R4")'><img id="I_R4" src="/img/Button-Blank-Green-icon.png" class=button> OUT4 </button>
</div>
<div class="bholder">
<button id="R5" class="switch" onclick='doClick("R5")'><img id="I_R5" src="/img/Button-Blank-Green-icon.png" class=button> OUT5 </button>
</div>
<div class="bholder">
<button id="R6" class="switch" onclick='doClick("R6")'><img id="I_R6" src="/img/Button-Blank-Green-icon.png" class=button> OUT6 </button>
</div>
<div class="bholder">
<button id="R7" class="switch" onclick='doClick("R7")'><img id="I_R7" src="/img/Button-Blank-Green-icon.png" class=button> OUT7 </button>
</div>
</div>

</body>
</html5>