#!/usr/bin/env python
# -*- coding: cp936 -*-

import os
import binascii
import struct
import BaseProc
from ES.DataStruct import *
import logging
import logger

import dataSerial

logp = logger.FinalLogger.getLogger()

class ver2To3(BaseProc.BaseProc):
    def __init__(self):
        BaseProc.BaseProc.__init__(self, 2, 3)

    def process(self, inFn, inTID, inDst, inDstTopDir):
        logp.debug ('%d to %d with test id %d', self.old, self.new, inTID)
        fd = open(inFn, 'rb')
        data = fd.read()
        oldFileSize = os.path.getsize(inFn)
        fd.close()
        
        if (self.old == struct.unpack("<I",data[0:4])[0]):
            os.rename(inFn, inFn + '.pre' + str(self.old))
            fd = open(inFn, 'wb')
            newVer = struct.pack("<I", self.new)
            fd.write(newVer)

            #ver2 到 ver3 ah wh
            stream = BaseProc.stream2struct(data[4 : sizeof(ver2TMsgSaveEngine) + 4], ver2TMsgSaveEngine)
            
            #首次转换时,写入相关内容到文件
            if (inDst == self.old):
                dataSer = dataSerial.DataSerial.getDataSerial(inDstTopDir)
                dataSer.debug("%d-%d-%d-%d-%d", stream.channelNoInfoRow, stream.channelNoInfoCol, stream.channelNoInfoIndex, stream.testId, stream.dfDataIndex - 1)
                
            if inTID:#测试ID不为0时,更新
                stream.testId = inTID
                
            #取globalVariable之前的数据并写入 c_float * 100
            npose = sizeof(ver2TMsgSaveEngine) - sizeof(c_float) * 100
            newdata = BaseProc.struct2stream(stream)
            fd.write(newdata[0 : npose])
            crc32 = binascii.crc32(newdata[0 : npose])
            
            #写入ah 数据
            fd.write(c_longlong(0))
            #写入ah 数据
            fd.write(c_longlong(0))
            crc32 = binascii.crc32(c_longlong(0), crc32)
            crc32 = binascii.crc32(c_longlong(0), crc32)
            
            #写入ver2其它数据
            #data中的数据需要npose需要加上4
            npose += 4
            fd.write(data[npose : npose + sizeof(c_float) * 100])
            crc32 = binascii.crc32(data[npose : npose + sizeof(c_float) * 100], crc32)
            npose += sizeof(c_float) * 100
            
            #ver2 到 ver3 该处未变化，直接写入该数据
            if (stream.cycleStackSize > 0):
                fd.write(data[npose : npose + stream.cycleStackSize * 12])
                crc32 = binascii.crc32(data[npose : npose + stream.cycleStackSize * 12], crc32)
            npose += stream.cycleStackSize * 12
            
            #ver2 到 ver3 该处未变化，直接写入该数据
            if (stream.auxChNum > 0):
                fd.write(data[npose : npose + stream.auxChNum * sizeof(ver123TAchInfoOfEngineState)])
                crc32 = binascii.crc32(data[npose : npose + stream.auxChNum * sizeof(ver123TAchInfoOfEngineState)], crc32)
            npose += stream.auxChNum * sizeof(ver123TAchInfoOfEngineState)
            
            #ver2从该处起的　6664字节　ver3 只有其中的最前面4字节和最后的8字节
            fd.write(data[npose : npose + 4])
            crc32 = binascii.crc32(data[npose : npose + 4], crc32)

            #其中一些版本不为6664长度........从后向前推算 crc 4 字节  mESWriteSer 4 字节  atlLatestCCCCap 8 字节 需要写入
            npose = oldFileSize - 4 - 4 - 8
            fd.write(data[npose : npose + 12])
            crc32 = binascii.crc32(data[npose : npose + 12], crc32)
            fd.write(struct.pack("<I", crc32))
            fd.close()
            return 0
        else:
            logp.error ('old ver in file not equal to' + self.old )
            return 1





