Skip to content

Commit

Permalink
Major changes to file saving. Not working yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkvt80 committed Jul 9, 2018
1 parent 9e9680d commit 1c1dbfc
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 246 deletions.
277 changes: 59 additions & 218 deletions keystroke.js
Expand Up @@ -9,14 +9,16 @@
* replayEvents() - Replays the events to newly connected clients
* saveEvents()
* matchPage(event) - Returns a matching event if there is one in the list. (page, subpage, service)
*
& @todo Move all the file stuff out of here!
*/

var readline = require('readline')
var fs = require('fs')

require('./page.js')
const page=new Page()
KeyStroke=function()
{
var that=this
var that=this // Make the parent available in the nested functions. Probably 100 reasons why you shouldn't do this.
this.sourceFile=""
this.destFile=""

Expand Down Expand Up @@ -84,6 +86,21 @@ KeyStroke=function()
return event // @todo
}

/*( Helper for saveEdits() */
this.savePage=function()
{
page.filename="/dev/shm/mypage.tti"
page.savePage(function()
{
console.log("Write completed")
},
function(err)
{
console.log("Write failed"+err)
}
)
}

/* Write the edits back to file */
this.saveEdits=function()
{
Expand All @@ -96,6 +113,12 @@ KeyStroke=function()
{
return
}

err=function(error)
{
console.log("Something went wrong: "+error)
}

// Sort the event list by S(service name) p(page 100..8ff) s(subpage 0..99) y(row 0..24)
this.eventList.sort(
function(a,b)
Expand Down Expand Up @@ -124,228 +147,46 @@ KeyStroke=function()
if (this.eventList.length>0)
{
//console.log(this.event)
this.event=this.eventList[0]
this.eventList.shift()
var event=this.eventList[0]
// Get the filename
service=this.event.S
if (service==undefined) service='onair'
var filename='/var/www/'+service+'/p'+this.event.p.toString(16)+'.tti' // The filename of the original page
var copyFilename=service+'.p'+this.event.p.toString(16)+'.tti' // The filename of the copied page
var copyPath='/run/shm/'

copyFile(filename, copyPath+copyFilename, function(err)
{
console.log("File copied, now trying to parse "+copyFilename)
const currentPath= filename
const newPath= filename+".backup"


// @todo Look at err and abandon if needed
// Open a stream and get ready to read the file
var instream
instream = fs.createReadStream(copyPath+copyFilename,{encoding: "ascii"}) // Ascii strips bit 7 without messing up the rest of the text. latin1 does not work :-(
instream.on('error',function()
{
console.log("error routine not written")
throw new Error("Something went badly wrong!")
})

var reader = readline.createInterface(
service=event.S
if (service==undefined)
{
service='onair'
}
var pageNumber=event.p
var subPage=event.s
var filename='/var/www/'+service+'/p'+pageNumber.toString(16)+'.tti' // The filename of the original page

console.log("Filename="+filename)
const that=this
page.loadPage(filename, function()
{
input: instream,
terminal: false
})

that.outfile=fs.createWriteStream(tempFile) // Stick the edited file here until we prove that it is working

// If not set, then set the service to default
if (that.event.S==undefined)
console.log("Loaded. Now edit...")
// apply all the edits that refer to this page
for ( ; (that.eventList.length>0) && (pageNumber==event.p) && (service==event.S) ;
event=that.eventList[0] )
{
page.keyMessage(event)
that.eventList.shift()
}
page.print()
// At this point we trigger off a timer
setTimeout(this.savePage(), 500);
}
,function(err)
{
that.event.S='onair'
console.log(err)
}

reader.on('line', function(line)
{

var SaveEvent=that.event // Save so we can check if we stay on the same row
var subCode=0
if (line.indexOf('SC')==0)
{
subCode=line.substring(3)
if (subCode>0) subCode--
console.log("Found subcode:"+subCode)
//that.dump()
}
if (that.event.s==subCode) // If the subcode matches, look for our line
{
// @todo: BUG. If the line doesn't exist, we can't add or edit it.
if (line.indexOf('OL,')==0) // teletext row?
{
var ix=3
var row=0
var ch
ch=line.charAt(ix)
if (ch!=',')
{
row=ch
}
ix++
ch=line.charAt(ix)
if (ch!=',')
{
row=row+ch // haha. Strange maths
ix++
}
row=parseInt(row)
ix++ // Should be pointing to the first character now
// console.log('row='+row)

// Pad strings shorter than 40 characters
if (line.length<40)
{
line+=" "
line=line.substring(0,40)
}

var str=DeEscapePrestel(line)

var changed=false

// This is if we have changes to apply to a line that doesn't exist
// We have to create the missing lines
// This is annoyingly almost the same code repeated in the next block, We can factorise
if (that.eventList.length>0 && that.event.y<row)
{
console.log("TODO: We are at row "+row+" but event.y="+that.event.y)
var moreLines=true
var moreEvents=true
// We don't actually have a line from the file. We need to add one.

while (moreLines)
{
var currentRow=that.event.y
var strPrefix="OL,"+that.event.y+","
str=" "
while (moreEvents)
{
str=setCharAt(str,that.event.x, that.event.k) // Set the key of the event
if (that.eventList.length>0) // Any more events?
{
that.event=that.eventList[0]
if (that.event.S==undefined) // default service is onair
{
that.event.S='onair'
}
if (that.event.y!=currentRow ||
that.event.s!=SaveEvent.s ||
that.event.S!=SaveEvent.S ||
that.event.p!=SaveEvent.p) // If the next event is not on the same row
{
moreEvents=false // Done with this line
// Don't eat the event, it belongs to the next line
}
else
{
that.eventList.shift() // Eat the event. It is on the same row
}
}
else
{
moreLines=false
that.event.y=0 // Signal that we ar
break; // Done! There are no more events to process
}
} // moreEvents
if (that.eventList.length>0 || that.event.y>=row) // Are we caught up with the actual rows yet?
{
moreLines=false;
}
line=strPrefix+EscapePrestel(str); // Escape and write the line
console.log("Writing out "+line)
that.outfile.write(line+'\n')
}
} // Create missing line(s)

var more=that.event.y==row // If the row matches, we have an entry to process
while (more) // Any more changes on this line?
{
changed=true
str=setCharAt(str,that.event.x+ix, that.event.k)
console.log("Setting key="+that.event.k)
if (that.eventList.length>0)
{
that.event=that.eventList[0]
if (that.event.S==undefined)
{
that.event.S='onair'
}
if (that.event.y!=row ||
that.event.s!=SaveEvent.s ||
that.event.S!=SaveEvent.S ||
that.event.p!=SaveEvent.p) // If the next event is not on the same row
{
more=false // Done with this line
console.log("S="+that.event.S)
console.log(that.event)
console.log(SaveEvent)
}
else
{
that.eventList.shift() // Eat the event. It is on the same row
}
// console.log(that.event)
}
else
{
console.log("Nothing left to process")
break // Nothing left to process
}
}
// The result of editing this row
if (changed)
{
console.log("ix="+ix)
console.log("[1. before]"+line+">")
console.log("[2. edited]"+str+">")

line=EscapePrestel(str); // Escape and write the line
console.log("[3. escaped]"+line+">")
}

} // OL
} // If subcode matches
// Write the line out to the file
that.outfile.write(line+'\n')
)
return


}) // reader.on
/*
reader.on('end', function()
{
reader.close()
that.sourceFile=tempFile
that.destFile=filename
setTimeout(that.copyback,5000)
})
*/
/* When the input stream ends */
reader.on('close', function(line)
{
// console.log("Copy "+tempFile+" to "+filename)
reader.close()
// Oh. This fails because we are already nested inside copyfile
// Instead we schedule the file write so that it doesn't interfere with the current thread.
that.sourceFile=tempFile
that.destFile=filename
setTimeout(that.copyback,500)
//This is when we will also close the output file and probably rename them.
// that.outfile.end()
console.log("[reader.on] closed file. byebye.")
}) // lambda/reader.close
} // lambda
)
} // if any events
} // saveEdits



this.copyback=function()
{
copyFile(that.sourceFile,that.destFile,function(err)
Expand Down

0 comments on commit 1c1dbfc

Please sign in to comment.