From 0333bb2a9d62ae0dc6a53dec76f7fbbf31975940 Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Mon, 12 Nov 2018 21:04:27 -0500
Subject: [PATCH] add link rep. to ui

---
 README.md                         |  4 ++-
 client/client.js                  | 14 ++++++++++
 client/divtools.js                | 42 ++++++++++++++++++++++++++++++
 main.js                           | 14 +++++-----
 modules/hardware/atkseriallink.js |  5 ++--
 programs.js                       | 43 +++++++++++++++++++++++++++++--
 reps.js                           | 11 ++++++++
 views.js                          |  6 +++++
 8 files changed, 127 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index 9673350..d5a3c69 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,9 @@ This project serves the developement environment / api we use to write and repre
  - how do users know to hookup, how to hookup ? 
  - then draw something, and how does UI hook up, and when route changes ? 
 
- - change /src to /modules or /units ?
+ - catch err where can't click-escape from settings ?
+
+ - 
 
  - walk program units and change 
   - rename inout to jsunit
diff --git a/client/client.js b/client/client.js
index 62b5160..eec0eee 100644
--- a/client/client.js
+++ b/client/client.js
@@ -309,6 +309,14 @@ function redrawLinks() {
     while (svg.firstChild) {
         svg.removeChild(svg.firstChild)
     }
+    // find that link
+    var lnkPt
+    var nLnk = 0 
+    for(mdlName in program.modules){
+        if(program.modules[mdlName].description.isLink){
+            lnkPt = getLeftWall(program.modules[mdlName].ui.domElem)
+        }
+    }
     // redraw thru all links, just look at reps
     for (mdlName in program.modules) {
         var mdlRep = program.modules[mdlName]
@@ -328,6 +336,12 @@ function redrawLinks() {
                 }
             }
         }
+        if(mdlRep.description.isHardware && !mdlRep.description.isLink){
+            nLnk ++ 
+            var hwPt = getRightWall(mdlRep.ui.domElem)
+            lnkPt.y += 10 * nLnk
+            var ln = newLine(hwPt.x, hwPt.y, lnkPt.x, lnkPt.y)
+        }
     }
 }
 
diff --git a/client/divtools.js b/client/divtools.js
index f6b9eb3..94f7df9 100644
--- a/client/divtools.js
+++ b/client/divtools.js
@@ -296,4 +296,46 @@ function getInputArrow(div) {
     }
 
     return pos
+}
+
+function newLine(x1, y1, x2, y2) {
+    var ln = {}
+    ln.elem = document.createElementNS(svgns, 'line')
+    ln.elem.style.stroke = '#fcd17b'
+    ln.elem.style.fill = 'none'
+    ln.elem.style.strokeWidth = '7px'
+    ln.x1 = x1
+    ln.y1 = y1
+    ln.x2 = x2
+    ln.y2 = y2
+    redrawLine(ln)
+    svg.appendChild(ln.elem)
+    return ln
+}
+
+function redrawLine(ln) {
+    ln.elem.setAttribute('x1', ln.x1)
+    ln.elem.setAttribute('y1', ln.y1)
+    ln.elem.setAttribute('x2', ln.x2)
+    ln.elem.setAttribute('y2', ln.y2)
+}
+
+function getLeftWall(div) {
+    var x = div.offsetLeft + 25
+    var y = div.offsetTop + 25
+    var pt = {
+        x: x,
+        y: y
+    }
+    return pt
+}
+
+function getRightWall(div) {
+    var x = div.offsetLeft + div.clientWidth - 25
+    var y = div.offsetTop + div.clientHeight - 25
+    var pt = {
+        x: x,
+        y: y
+    }
+    return pt
 }
\ No newline at end of file
diff --git a/main.js b/main.js
index 864b017..32e9012 100644
--- a/main.js
+++ b/main.js
@@ -29,7 +29,9 @@ const Programs = require('./programs.js')
 
 var program = Programs.new('hw unit test')
 
-var link = Programs.loadModuleFromSource(program, './modules/hardware/atkseriallink.js')
+//var link = Programs.loadModuleFromSource(program, './modules/hardware/atkseriallink.js')
+
+
 var atkbbs = Programs.loadModuleFromSource(program, './modules/hardware/atkbbs.js')
 
 atkbbs.description.position = {
@@ -37,12 +39,12 @@ atkbbs.description.position = {
 	top: 200
 }
 
-link.description.position = {
-	left:475,
-	top: 100
-}
+// link.description.position = {
+// 	left:475,
+// 	top: 100
+// }
 
-link.attach(atkbbs.route)
+//link.attach(atkbbs.route)
 
 //program = Programs.open('programs/default.json')
 
diff --git a/modules/hardware/atkseriallink.js b/modules/hardware/atkseriallink.js
index 281d0ee..ef40a5d 100644
--- a/modules/hardware/atkseriallink.js
+++ b/modules/hardware/atkseriallink.js
@@ -12,6 +12,7 @@ function ATKSerialLink() {
     var atkSerialLink = {
         description: {
             isHardware: true,
+            isLink: true,
             name: 'Serialport ATK Link',
             alt: 'window into hardware world'
         },
@@ -95,7 +96,7 @@ function ATKSerialLink() {
             pckt = literalRoute.concat(pckt) // add route 
             pckt.unshift(pckt.length + 1) // add length byte 
             if (writeToSerialPort(pckt)) {
-                console.log('PCKT OOT >>', pckt.toString())
+                console.log('PCKT OUT >>', pckt.toString(), '---------------')
             } else {
                 // try to open ? 
                 openSerialPort()
@@ -188,10 +189,8 @@ function ATKSerialLink() {
         var match = false
         for (key in atkSerialLink.routes) {
             if (returnRoute.toString() === atkSerialLink.routes[key].route.toString()) {
-                console.log('RETURN LINK AT', atkSerialLink.routes[key])
                 // strip header and return message 
                 var msg = pckt.slice(pckt.indexOf(255) + 1)
-                console.log("RETURN MSG", msg)
                 match = true 
                 atkSerialLink.routes[key].onMessage(msg)
             }
diff --git a/programs.js b/programs.js
index 1c5a080..35c238f 100644
--- a/programs.js
+++ b/programs.js
@@ -5,10 +5,10 @@ const Reps = require('./reps.js')
 const JSUnit = require('./lib/jsunit.js')
 let isStateKey = JSUnit.isStateKey
 
-function newProgram(name){
+function newProgram(name) {
     var program = {
         description: {
-            name: name, 
+            name: name,
             id: name
         },
         modules: {}
@@ -38,6 +38,19 @@ function loadModuleFromSource(program, path, id) {
         }
         mod.description.path = path
 
+        /* ---------------------------------- */
+        // WARN! Corner Case should Go Away or Improve at next spiral 
+        if(mod.description.isLink){
+            for (mdlName in program.modules) {
+                if (program.modules[mdlName].description.isLink) {
+                    console.log("PRGMEM ONLY BIG ENOUGH FOR ONE LINK")
+                    process.exit()
+                }
+            }
+        }
+        // end corner case code 
+        /* ---------------------------------- */
+
         // add to program object 
         program.modules[mod.description.id] = mod
 
@@ -69,6 +82,32 @@ function loadModuleFromSource(program, path, id) {
         }
 
         console.log('ADDING', mod.description.id, 'to', program.description.id)
+
+        /* ---------------------------------- */
+        // WARN! Corner Case should Go Away or Improve at next spiral 
+        // hardware corner case is hardware corner case
+        // here's what we'll do
+        // if it's hardware, and we're not loading from some saved program (no id)
+        // 
+        if (mod.description.isHardware && !mod.description.isLink && id == null) {
+            // make sure we haven't already done this, thx 
+            var lnk = null 
+            for (mdlName in program.modules) {
+                if (program.modules[mdlName].description.isLink) {
+                    lnk = mdlName
+                }
+            }
+            if (lnk) {
+                console.log('CORNER CASE: LOADING HW MODULE, LINKING TO LINK')
+                program.modules[lnk].attach(mod.route)
+            } else {
+                console.log('CORNER CASE: LOADING HW MODULE, ALSO LOADING LINK')
+                var link = loadModuleFromSource(program, './modules/hardware/atkseriallink.js')
+                // hook it up auto-like 
+                link.attach(mod.route)
+            }
+        }
+
         // also return it so that we can write programs without the UI 
         return mod
     } else {
diff --git a/reps.js b/reps.js
index 3f5883d..853de08 100644
--- a/reps.js
+++ b/reps.js
@@ -2,6 +2,8 @@ const JSUnit = require('./lib/jsunit.js')
 let isStateKey = JSUnit.isStateKey
 
 function makeRepFromModule(mdl) {
+    // rep != mdl 
+    // rep is rep of mdl 
     var rep = {
         description: {
             id: mdl.description.id,
@@ -11,6 +13,15 @@ function makeRepFromModule(mdl) {
         }
     }
 
+    // yikes tho, corner cases should be easier ... 
+    if(mdl.description.isHardware){
+        rep.description.isHardware = true
+    }
+
+    if(mdl.description.isLink){
+        rep.description.isLink = true 
+    }
+
     if(mdl.description.position){
         rep.description.position = mdl.description.position
     }
diff --git a/views.js b/views.js
index 487c8bf..9b419bf 100644
--- a/views.js
+++ b/views.js
@@ -224,6 +224,12 @@ function uiRequestNewModule(data) {
     Programs.loadModuleFromSource(program, data)
     // bit of a mess to pick out the last entered module 
     var keys = Object.keys(program.modules)
+    var latest = keys[keys.length - 1]
+    if(program.modules[latest].description.isLink && data != './modules/hardware/atkseriallink.js'){
+        // we just added hardware, so added a link, so we've added two 
+        // just burn it down 
+        socketSend('restart', '')
+    }
     socketSend('put module', Reps.makeFromModule(program.modules[keys[keys.length - 1]]))
 }
 
-- 
GitLab